当前位置:   article > 正文

OpenAI ChatGPT-4开发笔记2024-03:Chat之Tool和Tool_Call(含前function call)_tool calling

tool calling

Updates on Function Calling were a major highlight at OpenAI DevDay.

In another world,原来的function call都不再正常工作了,必须全部重写。

function和function call全部由tool和tool_choice取代。2023年11月之前关于function call的代码都准备翘翘。

干嘛要整个tool出来取代function呢?原因有很多,不再赘述。作为程序员,我们真正关心的是:怎么改?

简单来说,就是整合chatgpt的能力和你个人的能力通过这个tools。怎么做呢?

第一步,定义function和parameters

import json
import openai

#step 1: Define 2 functions: self-defined function and text completions
def get_weather(location, unit="fahrenheit"):
    if "beijing" in location.lower():
        return json.dumps({"location": location, "temperature": "11", "unit": "celsius"})
    elif "tokyo" in location.lower():
        return json.dumps({"location": location, "temperature": "33", "unit": "celsius"})
    else:
        return json.dumps({"location": location, "temperature": "22", "unit": "celsius"})

def chat_completions(parameter_message):
    
    response = openai.chat.completions.create(
        model    = "gpt-3.5-turbo-1106",
        messages = parameter_message,
        tools    = ai_function,
        tool_choice="auto",  # auto is default, but we'll be explicit
    )

    return response.choices[0].message

#step 2: Define parameters for text completion, and functions for transferring to Tools

ai_prompt = [{"role"   : "user",
              "content": "What's the weather in tokyo?"}]

ai_function = [
    {
        "type"    : "function",
        "function": {
            "name": "get_weather",
            "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"],
            },
        },
    }
]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

第二步,第一轮chat completions

先做一个chat completions, 再由chat completion决定是否要调用function。

# step 3: first round call text completions, to get the response_message/tool_calls
first_response = chat_completions(ai_prompt)
tool_calls = first_response.tool_calls
  • 1
  • 2
  • 3

tool_choice参数让chatgpt模型自行决断是否需要function介入。
response是返回的object,message里包含一个tool_calls array.

tool_calls array The tool calls generated by the model, such as function calls.
id string The ID of the tool call.
type string The type of the tool. Currently, only function is supported.
function object:  The function that the model called.
	name: string The name of the function to call.
	arguments: string The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

第三步,第一轮chat completions的结果加入prompt,再把function参数加入prompt,然后一起喂给chatgpt

    if tool_calls:
        available_functions = {
            "get_weather": get_weather,
        }  # only one function in this example, but you can have multiple
        ai_prompt.append(first_response)  # step 4: extend conversation with assistant's reply
        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(
                location=function_args.get("location"),
                unit=function_args.get("unit"),
            )
            ai_prompt.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )  # step 5: extend conversation with function response
        print("\nfinal msg1 --> ", ai_prompt)
        second_response = chat_completions(ai_prompt)  # get a new response from the model where it can see the function response
        print("\n second_response --> ", second_response)  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

得出结果:

ChatCompletionMessage(
	content='The weather in Tokyo is 33°C. Enjoy the sunny day!', 
	role='assistant', 
	function_call=None, 
	tool_calls=None)
  • 1
  • 2
  • 3
  • 4
  • 5

tool和tool_choice,取代了过去的function和function calling。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/538562
推荐阅读
相关标签
  

闽ICP备14008679号