Re:New GPT3.5/4 Function Calling in OpenAI API
by ttul on Tuesday June 13, 2023 @08:51PM
(#36319795)
TLDR, you can now pass a list of functions into a chat completion and the model will respond appropriately to "call" your function.
[ Parent
| on HN ]
Re:New GPT3.5/4 Function Calling in OpenAI API
by ttul on Tuesday June 13, 2023 @08:53PM
(#36319821)
If GPT thinks a function call is warranted based on the input, then it fills in a "function_call" parameter in its response and provides the arguments. You then call the function using those arguments and in your next call to ChatCompletion, you pass in the response from your function.
message = response["choices"][0]["message"]
# Step 2, check if the model wants to call a function
if message.get("function_call"):
function_name = message["function_call"]["name"]
...
second_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{"role": "user", "content": "What is the weather like in boston?"},
message,
{
"role": "function",
"name": function_name,
"content": function_response,
},
],
)