Skip to content

Commit

Permalink
fix: tool call id use in a wrong way
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilJohnson0930 committed Nov 18, 2024
1 parent e447666 commit f50c94c
Show file tree
Hide file tree
Showing 3 changed files with 1,127 additions and 1,131 deletions.
29 changes: 25 additions & 4 deletions camel/models/cohere_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
self.tool_call_id = None
self._client = cohere.ClientV2(api_key=self._api_key)

def _to_openai_response(self, response: 'ChatResponse') -> ChatCompletion:
Expand Down Expand Up @@ -124,6 +125,10 @@ def _to_openai_response(self, response: 'ChatResponse') -> ChatCompletion:
def _to_cohere_chatmessage(
self, messages: List[OpenAIMessage]
) -> List["ChatMessageV2"]:
import ast
import json
import uuid

from cohere.types import ToolCallV2Function
from cohere.types.chat_message_v2 import (
AssistantChatMessageV2,
Expand All @@ -144,19 +149,30 @@ def _to_cohere_chatmessage(
elif role == "function":
new_message = ToolChatMessageV2(
role="tool",
tool_call_id="0",
tool_call_id=self.tool_call_id, # type: ignore[arg-type]
content=content, # type: ignore[assignment,arg-type]
)
elif role == "assistant":
if function_call is None:
new_message = AssistantChatMessageV2( # type: ignore[assignment]
role="assistant",
content=content, # type: ignore[arg-type]
)
continue
arguments = function_call.get("arguments") # type: ignore[attr-defined]
arguments_dict = ast.literal_eval(arguments)
arguments_json = json.dumps(arguments_dict)
assis_tool_call_id = str(uuid.uuid4())
self.tool_call_id = assis_tool_call_id # type: ignore[assignment]
new_message = AssistantChatMessageV2( # type: ignore[assignment]
role="assistant",
tool_calls=[
ToolCallV2(
id="0",
id=assis_tool_call_id,
type="function",
function=ToolCallV2Function(
name=function_call.get("name"), # type: ignore[attr-defined]
arguments=function_call.get("arguments"), # type: ignore[attr-defined]
arguments=arguments_json, # type: ignore[attr-defined]
),
)
]
Expand All @@ -173,7 +189,9 @@ def _to_cohere_chatmessage(
raise ValueError(f"Unsupported message role: {role}")

new_messages.append(new_message)

print("6666666666666666666666666666666666666666")
print(new_messages)
print("8888888888888888888888888888888888888888")
return new_messages # type: ignore[return-value]

@property
Expand Down Expand Up @@ -210,6 +228,9 @@ def run(self, messages: List[OpenAIMessage]) -> ChatCompletion:
model=self.model_type,
**self.model_config_dict,
)
print("22222222222222222222222222222222222222222")
print(response)
print("33333333333333333333333333333333333333333")
except ApiError as e:
logging.error(f"Cohere API Error: {e.status_code}")
logging.error(f"Error body: {e.body}")
Expand Down
2 changes: 1 addition & 1 deletion camel/toolkits/math_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sub(self, a: int, b: int) -> int:
Returns:
integer: The result of subtracting :obj:`b` from :obj:`a`.
"""
return a - b
return int(a) - int(b)

def mul(self, a: int, b: int) -> int:
r"""Multiplies two integers.
Expand Down
Loading

0 comments on commit f50c94c

Please sign in to comment.