Skip to content

Commit

Permalink
fix(anthropic): fixed vision and other issues with preparing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Sep 6, 2024
1 parent b3aaf2e commit b9b8455
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 10 additions & 6 deletions gptme/llm_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class MessagePart(TypedDict, total=False):
def chat(messages: list[Message], model: str) -> str:
assert anthropic, "LLM not initialized"
messages, system_messages = _transform_system_messages(messages)
messages_dicts = msgs2dicts(messages, anthropic=True)
response = anthropic.beta.prompt_caching.messages.create(
model=model,
messages=msgs2dicts(messages, anthropic=True), # type: ignore
messages=messages_dicts, # type: ignore
system=system_messages, # type: ignore
temperature=TEMPERATURE,
top_p=TOP_P,
Expand All @@ -51,11 +52,12 @@ def chat(messages: list[Message], model: str) -> str:


def stream(messages: list[Message], model: str) -> Generator[str, None, None]:
messages, system_messages = _transform_system_messages(messages)
assert anthropic, "LLM not initialized"
messages, system_messages = _transform_system_messages(messages)
messages_dicts = msgs2dicts(messages, anthropic=True)
with anthropic.beta.prompt_caching.messages.stream(
model=model,
messages=msgs2dicts(messages, anthropic=True), # type: ignore
messages=messages_dicts, # type: ignore
system=system_messages, # type: ignore
temperature=TEMPERATURE,
top_p=TOP_P,
Expand All @@ -79,16 +81,18 @@ def _transform_system_messages(
messages[i] = Message(
"user",
content=f"<system>{message.content}</system>",
files=message.files, # type: ignore
)

# find consecutive user role messages and merge them into a single <system> message
# find consecutive user role messages and merge them together
messages_new: list[Message] = []
while messages:
message = messages.pop(0)
if messages_new and messages_new[-1].role == "user":
if messages_new and messages_new[-1].role == "user" and message.role == "user":
messages_new[-1] = Message(
"user",
content=f"{messages_new[-1].content}\n{message.content}",
content=f"{messages_new[-1].content}\n\n{message.content}",
files=messages_new[-1].files + message.files, # type: ignore
)
else:
messages_new.append(message)
Expand Down
10 changes: 5 additions & 5 deletions gptme/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
# This is not persisted to the log file.
self.quiet = quiet
# Files attached to the message, could e.g. be images for vision.
self.files = (
self.files: list[Path] = (
[Path(f) if isinstance(f, str) else f for f in files] if files else []
)

Expand Down Expand Up @@ -123,12 +123,12 @@ def _content_files_list(
def to_dict(self, keys=None, openai=False, anthropic=False) -> dict:
"""Return a dict representation of the message, serializable to JSON."""
content: str | list[dict[str, Any]]
if not anthropic and not openai:
# storage/wire format should keep the content as a string
content = self.content
else:
if anthropic or openai:
# OpenAI format or Anthropic format should include files in the content
content = self._content_files_list(openai=openai, anthropic=anthropic)
else:
# storage/wire format should keep the content as a string
content = self.content

d = {
"role": self.role,
Expand Down

0 comments on commit b9b8455

Please sign in to comment.