Skip to content

Commit 65cffa0

Browse files
authored
Fix: made tools support for graph (#2400)
1 parent 9f93794 commit 65cffa0

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

mem0/llms/azure_openai_structured.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,49 @@ def __init__(self, config: Optional[BaseLlmConfig] = None):
5050
default_headers=default_headers,
5151
)
5252

53+
def _parse_response(self, response, tools):
54+
"""
55+
Process the response based on whether tools are used or not.
56+
Args:
57+
response: The raw response from API.
58+
tools: The list of tools provided in the request.
59+
Returns:
60+
str or dict: The processed response.
61+
"""
62+
if tools:
63+
processed_response = {
64+
"content": response.choices[0].message.content,
65+
"tool_calls": [],
66+
}
67+
68+
if response.choices[0].message.tool_calls:
69+
for tool_call in response.choices[0].message.tool_calls:
70+
processed_response["tool_calls"].append(
71+
{
72+
"name": tool_call.function.name,
73+
"arguments": json.loads(tool_call.function.arguments),
74+
}
75+
)
76+
77+
return processed_response
78+
else:
79+
return response.choices[0].message.content
80+
5381
def generate_response(
5482
self,
5583
messages: List[Dict[str, str]],
5684
response_format: Optional[str] = None,
85+
tools: Optional[List[Dict]] = None,
86+
tool_choice: str = "auto",
5787
) -> str:
5888
"""
5989
Generates a response using Azure OpenAI based on the provided messages.
6090
6191
Args:
6292
messages (List[Dict[str, str]]): A list of dictionaries, each containing a 'role' and 'content' key.
6393
response_format (Optional[str]): The desired format of the response. Defaults to None.
94+
tools (Optional[List[Dict]]): A list of dictionaries, each containing a 'name' and 'arguments' key.
95+
tool_choice (str): The choice of tool to use. Defaults to "auto".
6496
6597
Returns:
6698
str: The generated response from the model.
@@ -76,5 +108,9 @@ def generate_response(
76108
if response_format:
77109
params["response_format"] = response_format
78110

111+
if tools:
112+
params["tools"] = tools
113+
params["tool_choice"] = tool_choice
114+
79115
response = self.client.chat.completions.create(**params)
80-
return response.choices[0].message.content
116+
return self._parse_response(response, tools)

mem0/llms/openai_structured.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,51 @@ def __init__(self, config: Optional[BaseLlmConfig] = None):
3333
)
3434
self.client = OpenAI(api_key=api_key, base_url=base_url)
3535

36+
def _parse_response(self, response, tools):
37+
"""
38+
Process the response based on whether tools are used or not.
39+
Args:
40+
response: The raw response from API.
41+
tools (list, optional): List of tools that the model can call.
42+
Returns:
43+
str or dict: The processed response.
44+
"""
45+
46+
if tools:
47+
processed_response = {
48+
"content": response.choices[0].message.content,
49+
"tool_calls": [],
50+
}
51+
52+
if response.choices[0].message.tool_calls:
53+
for tool_call in response.choices[0].message.tool_calls:
54+
processed_response["tool_calls"].append(
55+
{
56+
"name": tool_call.function.name,
57+
"arguments": json.loads(tool_call.function.arguments),
58+
}
59+
)
60+
61+
return processed_response
62+
63+
else:
64+
return response.choices[0].message.content
65+
3666
def generate_response(
3767
self,
3868
messages: List[Dict[str, str]],
3969
response_format: Optional[str] = None,
70+
tools: Optional[List[Dict]] = None,
71+
tool_choice: str = "auto",
4072
) -> str:
4173
"""
4274
Generates a response using OpenAI based on the provided messages.
4375
4476
Args:
4577
messages (List[Dict[str, str]]): A list of dictionaries, each containing a 'role' and 'content' key.
4678
response_format (Optional[str]): The desired format of the response. Defaults to None.
47-
79+
tools (Optional[List[Dict]]): A list of dictionaries, each containing a 'name' and 'arguments' key.
80+
tool_choice (str): The choice of tool to use. Defaults to "auto".
4881
4982
Returns:
5083
str: The generated response from the model.
@@ -57,6 +90,9 @@ def generate_response(
5790

5891
if response_format:
5992
params["response_format"] = response_format
93+
if tools:
94+
params["tools"] = tools
95+
params["tool_choice"] = tool_choice
6096

6197
response = self.client.beta.chat.completions.parse(**params)
62-
return response.choices[0].message.content
98+
return self._parse_response(response, tools)

0 commit comments

Comments
 (0)