OpenAI releases a new function call guide to help developers expand their model capabilities! This guide integrates user feedback, reduces 50% shorter, has clearer content, and contains complete examples of best practices, in-document function generation, and use of the weather API. OpenAI is committed to simplifying AI tools to make them easier for developers to use, thereby making it more efficient to utilize function calling capabilities.
OpenAI releases a brand new guide to function calling!
We have made important improvements based on your feedback:
-- 50% shorter, making it clearer and easier to understand – New best practices (see below for details?) – Supports in-document function generation! – Provides a complete feature example of using the weather API
View the guide and share your thoughts... pic.twitter.com/Id89E9PEff
— ilan bigio (@ilanbigio) January 13, 2025
Function calls allow the OpenAI model to interact with developer-defined tools, enabling it to perform more tasks beyond text or audio generation. The following is a simplified process:
This image shows the process of function calls between the developer and the AI model. Here are the step-by-step instructions:
Please read also: 6 top LLMs that support function calls
Let's look at a practical example using the get_weather function. This function retrieves the current temperature of the given coordinates.
<code>import requests def get_weather(latitude, longitude): response = requests.get(f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m") data = response.json() return data['current']['temperature_2m']</code>
<code>from openai import OpenAI import json client = OpenAI(api_key="sk-api_key”) tools = [{ "type": "function", "function": { "name": "get_weather", "description": "获取提供的坐标(摄氏度)的当前温度。", "parameters": { "type": "object", "properties": { "latitude": {"type": "number"}, "longitude": {"type": "number"} }, "required": ["latitude", "longitude"], "additionalProperties": False }, "strict": True } }] messages = [{"role": "user", "content": "今天巴黎的天气怎么样?"}] completion = client.chat.completions.create( model="gpt-4o", messages=messages, tools=tools, )</code>
<code>tool_call = completion.choices[0].message.tool_calls[0] args = json.loads(tool_call.function.arguments) result = get_weather(args["latitude"], args["longitude"])</code>
<code># 附加模型的工具调用消息 messages.append(completion.choices[0].message) # 将结果消息作为字符串附加 messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": json.dumps({"temperature": result}) # 将结果转换为JSON字符串 }) # 创建第二个聊天完成 completion_2 = client.chat.completions.create( model="gpt-4o", messages=messages, tools=tools, )</code>
<code>print(completion_2.choices[0].message.content)</code>
Output:
<code>巴黎目前的温度是-2.8°C。</code>
To help you make the most of your function calls, here are some professional tips:
For more information, please visit OpenAI.
OpenAI's improved function call guide enables developers to seamlessly integrate custom tools to make AI easier to access and use. By simplifying processes, providing clear examples, and prioritizing user feedback, OpenAI enables developers to innovate and build solutions that leverage the full potential of AI, thereby driving real-world applications and creativity.
The above is the detailed content of Checkout the OpenAI Function Calling Guide. For more information, please follow other related articles on the PHP Chinese website!