目录
OpenAI Assistant API支持function calling
LangChain Assistant API支持function calling
函数调用(Function Calling)是OpenAI
在今年6月13日对外发布的新能力。根据OpenAI官方博客描述,函数调用能力可以让大模型输出一个请求调用函数的消息,其中包含所需调用的函数信息、以及调用函数时所携带的参数信息。这是一种将大模型
(LLM)能力与外部工具/API
连接起来的新方式。
比如用户输入:
What’s the weather like in Tokyo?
使用function calling,可实现函数执行get_current_weather(location: string)
,从而获取函数输出,即得到对应地理位置的天气情况。这其中,location
这个参数及其取值是借助大模型能力从用户输入中抽取出来的,同时,大模型判断得到调用的函数为get_current_weather
。
开发人员可以使用大模型的function calling能力实现:
-
• 在进行自然语言交流时,通过调用外部工具回答问题(类似于ChatGPT插件);
-
• 将自然语言转换为调用API调用,或数据库查询语句;
-
• 从文本中抽取结构化数据
-
• 其它
那么,在OpenAI发布的模型中,是如何实现function calling的呢?
本文中,使用的第三方模块信息如下:
openai==1.3.2
langchain==0.0.339
入门例子
我们以函数get_weather_info
为例,其实现逻辑(模拟实现世界中的API调用,获取对应城市的天气状况)如下:
def get_weather_info(city: str):
weather_info = {"Shanghai": "Rainy", "Beijing": "Snow"}
return weather_info.get(city, "Sunny")
该函数只有一个参数:字符串变量city,即城市名称。为了实现function calling功能,需配置函数描述(类似JSON化的API描述),代码如下:
functions = [
{
"name": "get_weather_info",
"description": "Get the weather information of a city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The name of the city, e.g. Shanghai",
},
},
"required": ["city"],
}
}
]
对于一般的用户输入(query),大模型回复结果如下:
import json
from openai import OpenAI
client = OpenAI(api_key="sk-xxx")
query = "What is the capital of france?"
response = client.chat.completions.create(
model="gpt-3.5-turbo-0613",
messages=[{"role": "user", "content": query}],
functions=functions
)
message = response.dict()["choices"][0]["message"]
print(message)
>>> {'content': 'The capital of France is Paris.', 'role': 'assistant', 'function_call': None, 'tool_calls': None}
此时function_call
为None,即大模型判断不需要function calling.
对于查询天气的query,大模型输出结果如下:
import json
from openai import OpenAI
client = OpenAI(api_key="sk-xxx")
query = "What is the weather like in Beijing?"
response = client.chat.completions.create(
model="gpt-3.5-turbo-0613",
messages=[{"role": "user", "content": query}],
functions=functions
)
message = response.dict()["choices"][0]["message"]
print(message)
>>> {'content': None, 'role': 'assistant', 'function_call': {'arguments': '{\n "city": "Beijing"\n}', 'name': 'get_weather_info'}, 'tool_calls': None}
此时我们看到了令人吃惊的输出,大模型的输出内容为空,而判断需要function calling, 函数名称为get_weather_info
,参数为{'arguments': '{\n "city": "Beijing"\n}
。
下一步,我们可以调用该函数,传入参数,得到函数输出,并再次调用大模型得到答案回复。
func_name = message["function_call"]["name"]
func_args = json.loads(message["function_call"]["arguments"])
print("func name and args: ", func_name, func_args)
func_response = get_weather_info(**func_args)
final_response = client.chat.completions.create(
model="gpt-3.5-turbo-0613",
messages=[
{"role": "user", "content": query},
{"role": "assistant", "content": None, "function_call": message["function_call"]},
{
"role": "function",
"name": func_name,
"content": func_response
},
],
)
print("answer: ", final_response.dict()["choices"][0]["message"]["content"])
输出结果如下:
func name and args: get_weather_info {'city': 'Beijing'}
answer: The weather in Bei