4 ms·
New GPT3.5/4 Function Calling in OpenAI API
- ttul 3y agoTLDR, you can now pass a list of functions into a chat completion and the model will respond appropriately to "call" your function. response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=[{"role": "user", "content": "What's the weather like in Boston?"}], functions=[ { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", }, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, }, "required": ["location"], }, } ], function_call="auto", )
- ttul 3y agoIf 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, }, ], )