Skip to content

Commit

Permalink
fix: Gemini tool calling support (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendong-Fan authored Jan 19, 2025
1 parent 3824bd7 commit a62b8a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions camel/messages/func_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def to_openai_assistant_message(self) -> OpenAIAssistantMessage:
"content": self.content or "",
"tool_calls": [
{
"id": self.tool_call_id or "",
"id": self.tool_call_id or "null",
"type": "function",
"function": {
"name": self.func_name,
Expand Down Expand Up @@ -159,5 +159,5 @@ def to_openai_tool_message(self) -> OpenAIToolMessageParam:
return {
"role": "tool",
"content": result_content,
"tool_call_id": self.tool_call_id or "",
"tool_call_id": self.tool_call_id or "null",
}
11 changes: 10 additions & 1 deletion camel/models/gemini_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,17 @@ def run(
`ChatCompletion` in the non-stream mode, or
`Stream[ChatCompletionChunk]` in the stream mode.
"""
# Process messages to ensure no empty content, it's not accepeted by
# Gemini
processed_messages = []
for msg in messages:
msg_copy = msg.copy()
if 'content' in msg_copy and msg_copy['content'] == '':
msg_copy['content'] = 'null'
processed_messages.append(msg_copy)

response = self._client.chat.completions.create(
messages=messages,
messages=processed_messages,
model=self.model_type,
**self.model_config_dict,
)
Expand Down
4 changes: 2 additions & 2 deletions test/messages/test_func_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_function_func_message(
msg_dict: Dict[str, str] = {
"role": "tool",
"content": json.dumps(3),
"tool_call_id": "",
"tool_call_id": "null",
}
assert function_result_message.to_openai_tool_message() == msg_dict

Expand All @@ -103,7 +103,7 @@ def test_assistant_func_message_to_openai_tool_message(
expected_msg_dict: Dict[str, str] = {
"role": "tool",
"content": json.dumps(None),
"tool_call_id": "",
"tool_call_id": "null",
}

assert (
Expand Down

0 comments on commit a62b8a8

Please sign in to comment.