@@ -33,18 +33,51 @@ def __init__(self, config: Optional[BaseLlmConfig] = None):
33
33
)
34
34
self .client = OpenAI (api_key = api_key , base_url = base_url )
35
35
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
+
36
66
def generate_response (
37
67
self ,
38
68
messages : List [Dict [str , str ]],
39
69
response_format : Optional [str ] = None ,
70
+ tools : Optional [List [Dict ]] = None ,
71
+ tool_choice : str = "auto" ,
40
72
) -> str :
41
73
"""
42
74
Generates a response using OpenAI based on the provided messages.
43
75
44
76
Args:
45
77
messages (List[Dict[str, str]]): A list of dictionaries, each containing a 'role' and 'content' key.
46
78
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".
48
81
49
82
Returns:
50
83
str: The generated response from the model.
@@ -57,6 +90,9 @@ def generate_response(
57
90
58
91
if response_format :
59
92
params ["response_format" ] = response_format
93
+ if tools :
94
+ params ["tools" ] = tools
95
+ params ["tool_choice" ] = tool_choice
60
96
61
97
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