Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prompt caching (Sonnet, Haiku only) #3411

Merged
merged 39 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
260d7ea
Add prompt caching
Kaushikdkrikhanu Aug 16, 2024
c757ef3
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 16, 2024
ecb12ad
remove anthropic-version from extra_headers
Kaushikdkrikhanu Aug 16, 2024
0ea7e68
change supports_prompt_caching method to attribute
Kaushikdkrikhanu Aug 16, 2024
e02eae6
Merge branch 'main' of https://github.com/Kaushikdkrikhanu/OpenDevin …
Kaushikdkrikhanu Aug 16, 2024
6b9be67
Merge branch 'main' into add-prompt-caching
tobitege Aug 17, 2024
9fef189
Merge branch 'main' into add-prompt-caching
xingyaoww Aug 18, 2024
abcc8a1
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 18, 2024
ffb6035
change caching strat and log cache statistics
Kaushikdkrikhanu Aug 19, 2024
87eb4a6
add reminder as a new message to fix caching
Kaushikdkrikhanu Aug 19, 2024
ae4f99a
Merge branch 'main' of https://github.com/Kaushikdkrikhanu/OpenDevin …
Kaushikdkrikhanu Aug 19, 2024
5b101a0
'Merge branch 'main' of https://github.com/Kaushikdkrikhanu/OpenDevin…
Kaushikdkrikhanu Aug 19, 2024
417a69e
fix unit test
Kaushikdkrikhanu Aug 19, 2024
aa515c9
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 19, 2024
005602c
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 19, 2024
5034633
Merge branch 'main' into add-prompt-caching
tobitege Aug 20, 2024
5c1317a
append reminder to the end of the last message content
Kaushikdkrikhanu Aug 20, 2024
d08cf8a
move token logs to post completion function
Kaushikdkrikhanu Aug 20, 2024
4441455
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 20, 2024
1bee42f
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 20, 2024
14888f7
fix unit test failure
Kaushikdkrikhanu Aug 20, 2024
2a35687
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 21, 2024
a5d08fa
Merge branch 'main' into add-prompt-caching
tobitege Aug 21, 2024
ae66b5f
Merge branch 'main' into add-prompt-caching
enyst Aug 21, 2024
08004e4
Merge branch 'main' into add-prompt-caching
enyst Aug 21, 2024
749072d
fix reminder and prompt caching
enyst Aug 21, 2024
24d2e66
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 21, 2024
386d14f
unit tests for prompt caching
enyst Aug 22, 2024
2553ed2
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 22, 2024
732541c
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 23, 2024
cb2e4cc
add test
enyst Aug 23, 2024
90cf091
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 23, 2024
5e17027
clean up tests
enyst Aug 24, 2024
6d8e2a8
Merge branch 'add-prompt-caching' of github.com:Kaushikdkrikhanu/Open…
enyst Aug 24, 2024
8045111
separate reminder, use latest two messages
enyst Aug 24, 2024
fa516f7
Merge branch 'main' of github.com:All-Hands-AI/OpenHands into add-pro…
enyst Aug 24, 2024
1273d58
fix tests
enyst Aug 24, 2024
0f95bc8
Merge branch 'main' into add-prompt-caching
Kaushikdkrikhanu Aug 26, 2024
5667eca
Merge branch 'main' into add-prompt-caching
tobitege Aug 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions agenthub/codeact_agent/codeact_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,46 @@ def step(self, state: State) -> Action:
# prepare what we want to send to the LLM
messages = self._get_messages(state)

response = self.llm.completion(
messages=[message.model_dump() for message in messages],
stop=[
params = {
'messages': [message.model_dump() for message in messages],
'stop': [
'</execute_ipython>',
'</execute_bash>',
'</execute_browse>',
],
temperature=0.0,
)
'temperature': 0.0,
}

if self.llm.supports__prompt_caching():
params['extra_headers'] = {
'anthropic-version': '2023-06-01',
'anthropic-beta': 'prompt-caching-2024-07-31',
}

response = self.llm.completion(**params)

return self.action_parser.parse(response)

def _get_messages(self, state: State) -> list[Message]:
messages: list[Message] = [
Message(role='system', content=[TextContent(text=self.system_message)]),
Message(role='user', content=[TextContent(text=self.in_context_example)]),
Message(
role='system',
content=[
TextContent(
text=self.system_message,
cache_prompt=self.llm.supports__prompt_caching(),
)
],
),
Message(
role='user',
content=[
TextContent(
text=self.in_context_example,
cache_prompt=self.llm.supports__prompt_caching(),
)
],
),
]

for event in state.history.get_events():
Expand Down
9 changes: 8 additions & 1 deletion opendevin/core/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ def serialize_model(self):
class TextContent(Content):
type: ContentType = ContentType.TEXT
text: str
cache_prompt: bool = False

@model_serializer
def serialize_model(self):
return {'type': self.type.value, 'text': self.text}
data: dict[str, str | dict[str, str]] = {
'type': self.type.value,
'text': self.text,
}
if self.cache_prompt:
data['cache_control'] = {'type': 'ephemeral'}
return data


class ImageContent(Content):
Expand Down
10 changes: 10 additions & 0 deletions opendevin/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@ def async_streaming_completion(self):
def supports_vision(self):
return litellm.supports_vision(self.config.model)

def supports__prompt_caching(self):
cache_prompting_supported_models = [
'claude-3-5-sonnet-20240620',
'claude-3-haiku-20240307',
]
if self.config.model in cache_prompting_supported_models:
return True
else:
return False

def _post_completion(self, response: str) -> None:
"""Post-process the completion response."""
try:
Expand Down