|
@@ -201,6 +201,12 @@ class CodeActAgent(Agent):
|
|
|
],
|
|
],
|
|
|
'temperature': 0.0,
|
|
'temperature': 0.0,
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ if self.llm.is_caching_prompt_active():
|
|
|
|
|
+ params['extra_headers'] = {
|
|
|
|
|
+ 'anthropic-beta': 'prompt-caching-2024-07-31',
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
try:
|
|
try:
|
|
|
response = self.llm.completion(**params)
|
|
response = self.llm.completion(**params)
|
|
|
except Exception:
|
|
except Exception:
|
|
@@ -217,7 +223,7 @@ class CodeActAgent(Agent):
|
|
|
content=[
|
|
content=[
|
|
|
TextContent(
|
|
TextContent(
|
|
|
text=self.prompt_manager.system_message,
|
|
text=self.prompt_manager.system_message,
|
|
|
- cache_prompt=self.llm.supports_prompt_caching,
|
|
|
|
|
|
|
+ cache_prompt=self.llm.is_caching_prompt_active(), # Cache system prompt
|
|
|
)
|
|
)
|
|
|
],
|
|
],
|
|
|
),
|
|
),
|
|
@@ -226,7 +232,7 @@ class CodeActAgent(Agent):
|
|
|
content=[
|
|
content=[
|
|
|
TextContent(
|
|
TextContent(
|
|
|
text=self.prompt_manager.initial_user_message,
|
|
text=self.prompt_manager.initial_user_message,
|
|
|
- cache_prompt=self.llm.supports_prompt_caching,
|
|
|
|
|
|
|
+ cache_prompt=self.llm.is_caching_prompt_active(), # if the user asks the same query,
|
|
|
)
|
|
)
|
|
|
],
|
|
],
|
|
|
),
|
|
),
|
|
@@ -252,14 +258,14 @@ class CodeActAgent(Agent):
|
|
|
messages.append(message)
|
|
messages.append(message)
|
|
|
|
|
|
|
|
# Add caching to the last 2 user messages
|
|
# Add caching to the last 2 user messages
|
|
|
- if self.llm.supports_prompt_caching:
|
|
|
|
|
- user_messages = list(
|
|
|
|
|
- islice((m for m in reversed(messages) if m.role == 'user'), 2)
|
|
|
|
|
- )
|
|
|
|
|
- for message in user_messages:
|
|
|
|
|
- message.content[
|
|
|
|
|
- -1
|
|
|
|
|
- ].cache_prompt = True # Last item inside the message content
|
|
|
|
|
|
|
+ if self.llm.is_caching_prompt_active():
|
|
|
|
|
+ user_turns_processed = 0
|
|
|
|
|
+ for message in reversed(messages):
|
|
|
|
|
+ if message.role == 'user' and user_turns_processed < 2:
|
|
|
|
|
+ message.content[
|
|
|
|
|
+ -1
|
|
|
|
|
+ ].cache_prompt = True # Last item inside the message content
|
|
|
|
|
+ user_turns_processed += 1
|
|
|
|
|
|
|
|
# The latest user message is important:
|
|
# The latest user message is important:
|
|
|
# we want to remind the agent of the environment constraints
|
|
# we want to remind the agent of the environment constraints
|