基于大模型的在线搜索平台——整合function calling流程
- 一、function calling调用
- 总结
上篇文章已经实现了信息抓取能力,并封装成了函数。现在最后一步将能力转换为大模型可以调用的能力,实现搜索功能就可以了。这篇主要实现大模型的function calling能力,对于function calling在前面文章我们有主要讲述过。
一、function calling调用
整合function calling整个流程 ,这里用到之前封装好的两个函数 auto_functions 和 run_conversation
import inspect
def auto_functions(functions_list):
"""
Chat模型的functions自动生成函数
:param functions_list: 包含一个或者多个函数对象的列表;
:return:满足Chat模型functions参数要求的functions对象
"""
def functions_generate(functions_list):
# 创建空列表,用于保存每个函数的描述字典
functions = []
# 对每个外部函数进行循环
for function in functions_list:
# 读取函数对象的函数说明
function_description = inspect.getdoc(function)
# 读取函数的函数名字符串
function_name = function.__name__
system_prompt = f'以下是某的函数说明:{function_description}'
user_prompt = f'根据这个函数的函数说明,请帮我创建一个JSON格式的字典,这个字典有如下5点要求:\
1.字典总共有三个键值对;\
2.第一个键值对的Key是字符串name,value是该函数的名字:{function_name},也是字符串;\
3.第二个键值对的Key是字符串description,value是该函数的函数的功能说明,也是字符串;\
4.第三个键值对的Key是字符串parameters,value是一个JSON Schema对象,用于说明该函数的参数输入规范。\
5.输出结果必须是一个JSON格式的字典,不需要添加任何修饰语句,不需要解释'
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
json_function_description = json.loads(response.choices[0].message.content.replace("```","").replace("json",""))
json_str={"type": "function","function":json_function_description}
functions.append(json_str)
return functions
## 最大可以尝试4次
max_attempts = 4
attempts = 0
while attempts < max_attempts:
try:
functions = functions_generate(functions_list)
break # 如果代码成功执行,跳出循环
except Exception as e:
attempts += 1 # 增加尝试次数
print("发生错误:", e)
if attempts == max_attempts:
print("已达到最大尝试次数,程序终止。")
raise # 重新引发最后一个异常
else:
print("正在重新运行...")
return functions
tools = auto_functions([get_answer])
tools
运行结果
def run_conversation(messages, model="gpt-4o-mini", functions_list=None):
"""
能够自动执行外部函数调用的对话模型
:param messages: 必要参数,字典类型,输入到Chat模型的messages参数对象
:param functions_list: 可选参数,默认为None,可以设置为包含全部外部函数的列表对象
:param model: Chat模型,可选参数,默认模型为gpt-3.5-turbo
:return:Chat模型输出结果
"""
# 如果没有外部函数库,则执行普通的对话任务
if functions_list == None:
response = client.chat.completions.create(
model=model,
messages=messages,
)
response_message = response.choices[0].message
final_response = response_message.content
# 若存在外部函数库,则需要灵活选取外部函数并进行回答
else:
# 创建functions对象
tools = auto_functions(functions_list)
# 创建外部函数库字典
available_functions = {func.__name__: func for func in functions_list}
# 第一次调用大模型
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
tool_choice="auto", )
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
if tool_calls:
messages.append(response_message)
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
## 真正执行外部函数的就是这儿的代码
function_response = function_to_call(**function_args)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
)
## 第二次调用模型
second_response = client.chat.completions.create(
model=model,
messages=messages,
)
# 获取最终结果
final_response = second_response.choices[0].message.content
else:
final_response = response_message.content
return final_response
先测试一个大模型可以回答的问题
messages = [{"role": "user", "content": '介绍一下你自己?'}]
run_conversation(messages=messages, functions_list=[get_answer])
再测试大模型可能回答不了问题,看是否会调用tool
messages=[
{"role": "system", "content": "根据用户输入的问题进行回答,如果知道问题的答案,请回答问题答案,如果不知道问题答案,调用智能助手函数回答’"},
{"role": "user", "content": '如何学习pytorch'}
]
run_conversation(messages=messages, functions_list=[get_answer])
总结
到此,我们就实现了基于大模型的在线搜索平台,当然,除了知乎之外,大家可以使用任何可以搜索的站点,只要爬虫获取的信息准确就好。最后可以封装成多个tool给大模型调用。