大模型的缺陷:
- 没有最新消息:训练周期长且昂贵,GPT3.5/4的知识截至2021-9
- 没有真逻辑:表现出的逻辑和推理,是训练文本的统计规律,不是真正的逻辑
Plugins
订机票、数学计算、日程提醒...插件选择&使用
插件的原理
通过prompt判断是否应该调用插件失败
- 使用门槛高:用户需要知道每个插件是做什么的,学习成本大;插件数量多,用户不知道哪个插件最合适,此外只能手动选3个plugin
- Sam Altman:比起应用程序位于gpt中,gpt更应该在应用程序中(不应该是干什么事都打开gpt,而是gpt无所不在)
- 我认为插件不够灵活和精准,你无法保证大模型生成的参数就是规范的,人工介入一些会更好
Function Calling
目的
当gpt能力不足时,通过自定义的function补足(调哪个function、function的输入输出格式可以由gpt完成)原理
示例
实例1
实例2
prompt = "6 * 3 / (4+2) = ?" # prompt = "桌上有 2 个苹果,四个桃子和 3 本书,水果比书多多少?" # prompt = """ # 让我们一步步计算:小明在一家水果店买水果。他买了X斤苹果,每斤10元;4斤香蕉,每斤5元; # 和3斤橙子,每斤8元。他手头有100元。请问小明买完这些水果后,他还剩下多少钱? #""" messages = [ {"role": "system", "content": "你是一个小学数学老师,你要教学生四则混合运算"}, {"role": "user", "content": prompt} ] response = get_completion(messages) messages.append(response) # 把大模型的回复加入到对话中。非常重要! print(response) while (response.get("function_call")): # 是否要调用 sum args = json.loads(response["function_call"]["arguments"]) function_name = response["function_call"]["name"] if (function_name == "sum"): result = sum(args["numbers"]) elif (function_name == "subtract"): result = args["a"] - args["b"] elif (function_name == "multiply"): result = 1 for number in args["numbers"]: result *= number elif (function_name == "divide"): result = args["a"] / args["b"] else: result = "Unknown function" print(result) messages.append( {"role": "function", "name": function_name, "content": str(result)}) response = get_completion(messages) messages.append(response) # 把大模型的回复加入到对话中 print(response) print(response.content)View Code
def get_completion(messages, model="gpt-4"): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, # 模型输出的随机性,0 表示随机性最小 functions=[ # 用 JSON 描述函数。可以定义多个,但是只有一个会被调用,也可能都不会被调用 { "name": "sum", "description": "计算数组中所有数字的和", "parameters": { "type": "object", "properties": { "numbers": { "type": "array", "items": { "type": "number", "description": "必须是数值类型" } } } }, }, { "name": "subtract", "description": "计算 a - b 的值", "parameters": { "type": "object", "properties": { "a": { "type": "number", "description": "被减数,必须是数值类型" }, "b": { "type": "number", "description": "减数,必须是数值类型" } } }, }, { "name": "multiply", "description": "计算数组中所有数字的积", "parameters": { "type": "object", "properties": { "numbers": { "type": "array", "items": { "type": "number", "description": "必须是数值类型" } } } }, }, { "name": "divide", "description": "计算 a/b 的值", "parameters": { "type": "object", "properties": { "a": { "type": "number", "description": "被除数,必须是数值类型" }, "b": { "type": "number", "description": "除数,必须是数值类型" } } }, } ], ) return response.choices[0].messageView Code
{ "role": "assistant", "content": null, "function_call": { "name": "multiply", "arguments": "{\n \"numbers\": [6, 3]\n}" } } 18 { "role": "assistant", "content": null, "function_call": { "name": "sum", "arguments": "{\n \"numbers\": [4, 2]\n}" } } 6 { "role": "assistant", "content": null, "function_call": { "name": "divide", "arguments": "{\n \"a\": 18,\n \"b\": 6\n}" } ... "role": "assistant", "content": "6 * 3 / (4+2) = 3" } 6 * 3 / (4+2) = 3
标签:Function,function,name,messages,Calling,Plugins,type,response,description From: https://www.cnblogs.com/ssyfj/p/18306361