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

feat: Handle multiple user messages in a single request #26

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
77 changes: 73 additions & 4 deletions src/api/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ def _parse_messages(self, chat_request: ChatRequest) -> list[dict]:
if message.content:
# Text message
messages.append(
{"role": message.role, "content": [{"text": message.content}]}
{
"role": message.role,
"content": self._parse_content_parts(
message, chat_request.model
),
}
)
else:
# Tool use message
Expand Down Expand Up @@ -378,7 +383,69 @@ def _parse_messages(self, chat_request: ChatRequest) -> list[dict]:
else:
# ignore others, such as system messages
continue
return messages
return self._reframe_multi_payloard(messages)


def _reframe_multi_payloard(self, messages: list) -> list:
""" Receive messages and reformat them to comply with the Claude format

With OpenAI format requests, it's not a problem to repeatedly receive messages from the same role, but
with Claude format requests, you cannot repeatedly receive messages from the same role.

This method searches through the OpenAI format messages in order and reformats them to the Claude format.

```
openai_format_messages=[
{"role": "user", "content": "hogehoge"},
{"role": "user", "content": "fugafuga"},
]

bedrock_format_messages=[
{
"role": "user",
"content": [
{"text": "hogehoge"},
{"text": "fugafuga"}
]
},
]
```
"""
reformatted_messages = []
current_role = None
current_content = []

# Search through the list of messages and combine messages from the same role into one list
for message in messages:
next_role = message['role']
next_content = message['content']

# If the next role is different from the previous message, add the previous role's messages to the list
if next_role != current_role:
if current_content:
reformatted_messages.append({
"role": current_role,
"content": current_content
})
# Switch to the new role
current_role = next_role
current_content = []

# Add the message content to current_content
if isinstance(next_content, str):
current_content.append({"text": next_content})
elif isinstance(next_content, list):
current_content.extend(next_content)

# Add the last role's messages to the list
if current_content:
reformatted_messages.append({
"role": current_role,
"content": current_content
})

return reformatted_messages


def _parse_request(self, chat_request: ChatRequest) -> dict:
"""Create default converse request body.
Expand Down Expand Up @@ -458,8 +525,10 @@ def _create_response(
)
message.tool_calls = tool_calls
message.content = None
elif content:
message.content = content[0]["text"]
else:
message.content = ""
if content:
message.content = content[0]["text"]

response = ChatResponse(
id=message_id,
Expand Down
2 changes: 1 addition & 1 deletion src/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class UserMessage(BaseModel):
class AssistantMessage(BaseModel):
name: str | None = None
role: Literal["assistant"] = "assistant"
content: str | None
content: str | list[TextContent | ImageContent] | None
tool_calls: list[ToolCall] | None = None


Expand Down