Skip to content

Commit

Permalink
update openai function_call to tools
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshiyouyang committed Jun 14, 2024
1 parent 8dd3acc commit be1a183
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ai/agents/oai/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ def _get_response(cls, config: Dict, raise_on_ratelimit_or_timeout=False, use_ca
del config['agent_name']
if "api_type" in config:
del config['api_type']

tools = []
if "functions" in config:
functions = config.pop("functions")
for function in functions:
tools.append({
"type": "function",
"function": function
})
if len(tools) > 0:
config['tools'] = tools

openai_completion = (
openai.ChatCompletion
if config["model"].replace("gpt-35-turbo", "gpt-3.5-turbo") in cls.chat_models
Expand All @@ -313,6 +325,14 @@ def _get_response(cls, config: Dict, raise_on_ratelimit_or_timeout=False, use_ca
response = openai_completion.create(**config)
else:
response = openai_completion.create(request_timeout=request_timeout, **config)
if "tool_calls" in response.choices[0].message and len(response.choices[0].message.tool_calls) > 0:
tool_calls = response.choices[0].message.pop("tool_calls")
function_call = tool_calls[0]
response.choices[0].message['function_call'] = {
"name": function_call.function.name,
"arguments": function_call.function.arguments
}
response.choices[0]['finish_reason'] = "function_call"

except (
ServiceUnavailableError,
Expand Down

0 comments on commit be1a183

Please sign in to comment.