Large model development (thirteen): Function calling calls external tool API to realize real-time weather query

The full text has a total of more than 1.2w words, and the expected reading time is about 34~50 minutes | Full of dry goods (with code cases), it is recommended to collect!

The goal of this article: Completely construct a development process for calling external tool APIs with the help of Function calling function to realize real-time query of weather information

The code switching in this article uses the gpt3.5 and gpt4 interfaces, please pay attention to the screening

image-20230726102748156

Code download click here

1. Introduction

From the name, the Function calling function is to call external functions, but in fact, the function of "external functions" is not only to perform large-scale model development similar to the previous article (12): Function calling process optimization and multiple rounds For numerical operations such as user-defined functions in dialogue tasks , it is more common to increase function functions by adding APIs of external tools within the function .

For example, you can encapsulate and call the external real-time query weather tool API in the function, so as to realize the function of one-click real-time query weather; encapsulate some external database APIs, so as to realize the function of one-click query database data; you can even "encapsulate" Google search in a function Within, some information can be searched in real time by calling the Google search API. Once these functions are "connected" to the Chat model as external functions, it is equivalent to adding some additional capabilities to the Chat model , such as the ability to query weather data in real time, the ability to directly operate on the database, and the ability to obtain the latest information in real time with Google search, etc. .

From another perspective, the large model (LLMs) that can access weather data in real time is essentially an intelligent dialogue robot for weather query, and the large model (LLMs) that can directly operate the database is essentially a customized SQL code interpretation A large model (LLMs) that accesses Google search and can obtain real-time information is a simple version of the New bing chatbot. This is the nuclear weapon-level application scenario of Function calling-the realization of AI applications around the large language model. Agile development.

This article takes the API tool for real-time weather query as an example to introduce the method of connecting the Chat model to the weather query API.

2. OpenWeather registration and API key acquisition method

2.1 What is the external tools API

The full name of API is application programming interface . For example, the ChatCompletion.create function used in the Python environment is actually an interface of the GPT series model application program provided by OpenAI. Through this API (create function or openai library), you can successfully Call the GPT model and output the model results in real time according to the input information. Many other applications also have corresponding APIs. For example, Google search can also realize the search function by calling the API

2.2 Introduction to OpenWeatherAPI

OpenWeather is a company that provides meteorological data services around the world. The company's services include real-time weather information, weather forecasts, historical weather data, and various weather-related reports, and OpenWeather has opened a completely free API within a certain usage limit . , you can call the OpenWeather API in the code environment to perform real-time weather query, weather forecast and other functions . Developers can add the weather forecast function of OpenWeather to their own applications or websites.

An open weather query API means that it can be encapsulated as a local function, that is, by calling the OpenWeather API to obtain real-time weather information, and then use the Function calling function to call the function. This operation is equivalent to adding In addition to the ability to obtain weather information in real time, you can query the weather in real time through the Chat model or ask for clothing suggestions based on the weather conditions, but this is not complicated to implement.

2.3 OpenWeather registration and get API key

In order to be able to call the OpenWeather service, similar to the API usage process of OpenAI, you first need to register an OpenWeather account and obtain an OpenWeather API Key.

The process of obtaining the OpenWeather API key is as follows: (official website address: https://openweathermap.org/, no magic access required):

  • Step 1: Register

Log in to the OpenWeather official website and click Sign—>create account, and follow the prompts to complete the filling.

image-20230725144332355

image-20230725144425459

  • Step 2: Get the API-key

After registration and other logins are completed, you can view the API key of the current account on the API keys page

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-3guZfJu8-1690338934424)(https://snowball100.oss-cn-beijing.aliyuncs.com/images/202307261019025 .png)]

image-20230725144906692

  • Step 3: Set as environment variable

Similar to the OpenAI API key, for the convenience of subsequent calls, you can also directly set the OpenWeather API key as an environment variable, and the variable name is OPENWEATHER_API_KEY.

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-a392u3Vc-1690338934425) (https://snowball100.oss-cn-beijing.aliyuncs.com/images/202307261019027 .png)]

If you don't know how to configure local environment variables, refer to this article: Large Model Development (4): OpenAI API Call Method

Generally speaking, for a first-time registered user, the first API key needs to wait for 2-5 hours before it will be activated. During this period, using this API key will return a 401 error.

3. How to use OpenWeather API

3.1 API Function Description and Billing Rules

For the functions, calling methods and free quota of OpenWeather API, you can view it on the OpenWeather API introduction page: https://openweathermap.org/api

image-20230726083515355

The latest version 3.0 API can perform real-time weather query, 48-hour forecast, 8-day weather forecast, etc., and can query the weather information of 40 years of history. At the same time, for all registered users, each user has 1,000 free query quotas per day, exceeding 1,000 Time is charged at USD 0.0015 per time.

3.2 Real-time weather query API call method

First check the API Doc of real-time weather information on the official website. This page contains the method of querying real-time weather information with the help of API: https://openweathermap.org/current

image-20230726084136085

Unlike OpenAI, which provides a Python library as an API access method, the OpenWeather API communicates through the https protocol . It is a RESTful API that encrypts communication through the SSL or TLS protocol and uses the native semantics of the HTTP protocol for operations. Use different HTTP methods (GET, POST, PUT, DELETE, etc.) to add, delete, modify, and query resources.

The basic steps of using Python to call RESTful style API are as follows:

  • Step 1 build request

That is, enter a specific endpoint, which is also the target endpoint for subsequent requests. For OpenWeather's real-time weather query API, this endpoint is https://api.openweathermap.org/data/2.5/weather

  • Step 2 Set query parameters

That is, create request-related parameter objects. For example, if you are calling the OpenWeather API for weather query, you need to set the following parameters according to the official website instructions:

image-20230726084811916

These parameters are city name or location coordinates, API key, format of returned results, weather-related unit standards, and language for outputting weather information. When actually calling the OpenWeather API, you can flexibly adjust the values ​​of these parameters to obtain the desired result. result;

  • Step 3 Send the request

Make requests with the help of the request library in python

The request library is a library that provides support for sending HTTP requests. With the help of the request library, it is very convenient to perform get or post requests, and it is very convenient to analyze the returned results;

  • Step 4 Parse the request

After waiting for the API service to return the result, the result can be parsed and converted into a form readable by the current environment. The format of the returned result can be set through the mode parameter, and the default is JSON format.

According to the above process, the connectivity test of obtaining real-time weather information through the OpenWeather API is performed. The code is as follows:

import requests

open_weather_key = os.getenv("OPENWEATHER_API_KEY")

# Step 1.构建请求
url = "https://api.openweathermap.org/data/2.5/weather"

# Step 2.设置查询参数
params = {
    
    
    "q": "Beijing",               # 查询北京实时天气
    "appid": open_weather_key,    # 输入API key
    "units": "metric",            # 使用摄氏度而不是华氏度
    "lang":"zh_cn"                # 输出语言为简体中文
}

# Step 3.发送GET请求
response = requests.get(url, params=params)

# Step 4.解析响应
data = response.json()

# 打印response对象
print("Response: ", response)

# 打印response对象的类型
print("Type of response: ", type(response))

# Step 4.解析响应
data = response.json()

# 打印解析后的json数据
print("Data: ", data)

Look at the output:

image-20230726085715526

If the API key is not activated, the returned response will display 401, indicating that the request is Unauthorized (unauthorized). In addition, if the response shows 404, it means that the URL path is invalid, and the URL path needs to be checked again according to the information provided by the official website.

This is more intuitive:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-3SQySoBn-1690338934426)(https://snowball100.oss-cn-beijing.aliyuncs.com/images/202307261019033 .png)]

The returned result contains information such as weather conditions, temperature, humidity, wind speed, and weather description.

4. Use Function calling to realize real-time weather query of Chat model

  • Step 1: Write a real-time weather information acquisition function

In order to ensure smooth communication with the large language model, the input and output of the function are required to be in string format . The specific function is written as follows:

def get_weather(loc):
    """
    查询即时天气函数
    :param loc: 必要参数,字符串类型,用于表示查询天气的具体城市名称,\
    注意,中国的城市需要用对应城市的英文名称代替,例如如果需要查询北京市天气,则loc参数需要输入'Beijing';
    :return:OpenWeather API查询即时天气的结果,具体URL请求地址为:https://api.openweathermap.org/data/2.5/weather\
    返回结果对象类型为解析之后的JSON格式对象,并用字符串形式进行表示,其中包含了全部重要的天气信息
    """
    # Step 1.构建请求
    url = "https://api.openweathermap.org/data/2.5/weather"

    # Step 2.设置查询参数
    params = {
    
    
        "q": loc,               
        "appid": open_weather_key,    # 输入API key
        "units": "metric",            # 使用摄氏度而不是华氏度
        "lang":"zh_cn"                # 输出语言为简体中文
    }

    # Step 3.发送GET请求
    response = requests.get(url, params=params)
    
    # Step 4.解析响应
    data = response.json()
    return json.dumps(data)
  • Step 2: Test whether the real-time weather information acquisition function can work normally
get_weather("shanghai")

The output is as follows:
image-20230726090949516

  • Step 3: Test whether the Chat model understands OpenWeather related content

It is necessary to verify whether the Chat model itself knows OpenWeather, the code is as follows:

response = openai.ChatCompletion.create(
  model="gpt-4-0613",
  messages=[
    {
    
    "role": "user", "content": "你知道OpenWeather是什么吗?"}
  ]
)
response.choices[0].message['content']

Look at the results:

image-20230726091402672

It is necessary to verify whether the Chat model can interpret the function results, that is, whether it can give accurate answers to some weather query questions based on the understanding of data information. The code is as follows:

hangzhou_data = get_weather("hangzhou")
response = openai.ChatCompletion.create(
  model="gpt-4-0613",
  messages=[
    {
    
    "role": "system", "content": "天气信息来源于OpenWeather API:https://api.openweathermap.org/data/2.5/weather"},
    {
    
    "role": "system", "content": "这是今日杭州市的天气:%s" % hangzhou_data},
    {
    
    "role": "user", "content": "请问今日杭州天气如何?请用简体中文回答"}
  ]
)
response.choices[0].message['content']

Look at the results:

image-20230726092023679

The most critical verification: It is necessary to check whether the model itself knows the English names corresponding to these city names entered in Chinese.

Because the city name in the question is in Chinese when asking a question, but the OpenAIWeather API requires the input of an English name. Although it is indicated in the function parameter description that the loc function needs to be converted into English, the parameter arrangement of the external function is spontaneously carried out by the Chat model Sorting, the outside cannot intervene, whether the model can extract parameters according to the requirements and organize them into a specified format during the dialogue process depends entirely on the reasoning ability of the model itself. The verification code is as follows:

response = openai.ChatCompletion.create(
  model="gpt-4-0613",
  messages=[
    {
    
    "role": "user", "content": "请问,杭州市的英文名称是?"}
  ]
)
response.choices[0].message['content']

Look at the results:

image-20230726092347583

  • Step 4: Create the functions parameter of the get_weather function

Import the functions parameter of the Chat model to write a function

If you are not clear about this function, read this article: Large Model Development (12): Function calling process optimization and realization of multi-round dialogue tasks

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 = '以下是某函数说明:%s' % function_description
            user_prompt = '根据这个函数的函数说明,请帮我创建一个JSON格式的字典,这个字典有如下5点要求:\
                           1.字典总共有三个键值对;\
                           2.第一个键值对的Key是字符串name,value是该函数的名字:%s,也是字符串;\
                           3.第二个键值对的Key是字符串description,value是该函数的函数的功能说明,也是字符串;\
                           4.第三个键值对的Key是字符串parameters,value是一个JSON Schema对象,用于说明该函数的参数输入规范。\
                           5.输出结果必须是一个JSON格式的字典,只输出这个字典即可,前后不需要任何前后修饰或说明的语句' % function_name

            response = openai.ChatCompletion.create(
                              model="gpt-4-0613",
                              messages=[
                                {
    
    "role": "system", "content": system_prompt},
                                {
    
    "role": "user", "content": user_prompt}
                              ]
                            )
            functions.append(json.loads(response.choices[0].message['content']))
        return functions
    
    max_attempts = 3
    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

Then call this function to get its JSON Schema format, the code is as follows:

functions_list = [get_weather]
functions = auto_functions(functions_list)
functions

Look at the results:

image-20230726094104533

It can be found that the key information of the function extracted by the model is in place.

  • Step 5: Realize the Function calling of the real-time weather query function

Import two functions written before: Chat dialogue model run_conversation and multi-round dialogue that automatically execute external function calls

If you are not clear about these two functions, read this article: Large Model Development (12): Function calling process optimization and realization of multi-round dialogue tasks

def run_conversation(messages, functions_list=None, model="gpt-4-0613"):
    """
    能够自动执行外部函数调用的Chat对话模型
    :param messages: 必要参数,字典类型,输入到Chat模型的messages参数对象
    :param functions_list: 可选参数,默认为None,可以设置为包含全部外部函数的列表对象
    :param model: Chat模型,可选参数,默认模型为gpt-4
    :return:Chat模型输出结果
    """
    # 如果没有外部函数库,则执行普通的对话任务
    if functions_list == None:
        response = openai.ChatCompletion.create(
                        model=model,
                        messages=messages,
                        )
        response_message = response["choices"][0]["message"]
        final_response = response_message["content"]
        
    # 若存在外部函数库,则需要灵活选取外部函数并进行回答
    else:
        # 创建functions对象
        functions = auto_functions(functions_list)
        # 创建外部函数库字典
        available_functions = {
    
    func.__name__: func for func in functions_list}

        # first response
        response = openai.ChatCompletion.create(
                        model=model,
                        messages=messages,
                        functions=functions,
                        function_call="auto")
        response_message = response["choices"][0]["message"]

        # 判断返回结果是否存在function_call,即判断是否需要调用外部函数来回答问题
        if response_message.get("function_call"):
            # 需要调用外部函数
            # 获取函数名
            function_name = response_message["function_call"]["name"]
            # 获取函数对象
            fuction_to_call = available_functions[function_name]
            # 获取函数参数
            function_args = json.loads(response_message["function_call"]["arguments"])
            # 将函数参数输入到函数中,获取函数计算结果
            function_response = fuction_to_call(**function_args)

            # messages中拼接first response消息
            messages.append(response_message)  
            # messages中拼接函数输出结果
            messages.append(
                {
    
    
                    "role": "function",
                    "name": function_name,
                    "content": function_response,
                }
            )  
            # 第二次调用模型
            second_response = openai.ChatCompletion.create(
                model=model,
                messages=messages,
            )  
            # 获取最终结果
            final_response = second_response["choices"][0]["message"]["content"]
        else:
            final_response = response_message["content"]
    
    return final_response
def chat_with_model(functions_list=None, 
                    prompt="你好呀", 
                    model="gpt-4-0613", 
                    system_message=[{
    
    "role": "system", "content": "你是以为乐于助人的助手。"}]):
    
    messages = system_message
    messages.append({
    
    "role": "user", "content": prompt})
    
    while True:           
        answer = run_conversation(messages=messages, 
                                    functions_list=functions_list, 
                                    model=model)
        
        
        print(f"模型回答: {
      
      answer}")

        # 询问用户是否还有其他问题
        user_input = input("您还有其他问题吗?(输入退出以结束对话): ")
        if user_input == "退出":
            break

        # 记录用户回答
        messages.append({
    
    "role": "user", "content": user_input})

Directly start the multi-round dialogue robot chat_with_model after the external chain function library, check the dialogue effect, the code is as follows:

  • First test whether the Chat model can accurately answer the current weather conditions when no external functions are brought in;

image-20230726095007218

Look at the results:

image-20230726095105728

In the original state, the large model does not have the ability to query weather information in real time.

Allow the model to mount the external function library functions_list in the dialogue, and ask the current weather conditions again, and the results are as follows:

image-20230726100237194

It can be found that at this time, with the support of the external function library, the Chat model has the ability to query the weather in real time and can make accurate answers to weather questions.

So far, a development process of using the function calling function to call the external tool API has been completely constructed . With the help of function calling, as long as the external tool API can be obtained and used flexibly, the large language model can be quickly "empowered". The technology behind it There is a lot of room for imagination. As far as the OpenWeather API is concerned, we can further explore adding functions such as future weather forecasts and historical weather statistics to the Chat model to meet the application needs of different scenarios. The addition of each function is equivalent to completing a Simple AI application development, and this is the fundamental reason why the Function calling function claims to be able to greatly speed up the development of AI applications.

5. Use Few-sho Tips to Optimize Functions

This happens during the operation of the functions parameter writing function auto_functions of the Chat model:

image-20230726101227647

This is because auto_functions will judge that the parameter type of the get_weather function is an array type instead of an object type. This wrong reasoning will not cause an error to be reported when the auto_functions function is executed, but will cause an error to be reported when the subsequent run_conversation function runs.

A few-shot method can be considered to greatly improve the accuracy of auto_functions when performing external function parameter format reasoning: that is, by writing some JSON Schema writing examples for the model to improve the reasoning accuracy. The specific auto_functions modification method is as follows:

def auto_functions(functions_list):
    """
    Chat模型的functions参数编写函数
    :param functions_list: 包含一个或者多个函数对象的列表;
    :return:满足Chat模型functions参数要求的functions对象
    """
    def functions_generate(functions_list):
        # 创建空列表,用于保存每个函数的描述字典
        functions = []
        
        def chen_ming_algorithm(data):
            """
            陈明算法函数,该函数定义了一种特殊的数据集计算过程
            :param data: 必要参数,表示带入计算的数据表,用字符串进行表示
            :return:陈明函数计算后的结果,返回结果为表示为JSON格式的Dataframe类型对象
            """
            df_new = pd.read_json(data)
            res = np.sum(df_new, axis=1) - 1
            return res.to_json(orient='records')
        
        chen_ming_function_description = inspect.getdoc(chen_ming_algorithm)
        
        chen_ming_function_name = chen_ming_algorithm.__name__
        
        chen_ming_function = {
    
    "name": "chen_ming_algorithm",
                              "description": "用于执行陈明算法的函数,定义了一种特殊的数据集计算过程",
                              "parameters": {
    
    "type": "object",
                                             "properties": {
    
    "data": {
    
    "type": "string",
                                                                     "description": "执行陈明算法的数据集"},
                                                           },
                                             "required": ["data"],
                                            },
                             }

        
        # 对每个外部函数进行循环
        for function in functions_list:
            # 读取函数对象的函数说明
            function_description = inspect.getdoc(function)
            # 读取函数的函数名字符串
            function_name = function.__name__

            user_message1 = '以下是某函数说明:%s。' % chen_ming_function_description +\
                            '根据这个函数的函数说明,请帮我创建一个function对象,用于描述这个函数的基本情况。这个function对象是一个JSON格式的字典,\
                            这个字典有如下5点要求:\
                            1.字典总共有三个键值对;\
                            2.第一个键值对的Key是字符串name,value是该函数的名字:%s,也是字符串;\
                            3.第二个键值对的Key是字符串description,value是该函数的函数的功能说明,也是字符串;\
                            4.第三个键值对的Key是字符串parameters,value是一个JSON Schema对象,用于说明该函数的参数输入规范。\
                            5.输出结果必须是一个JSON格式的字典,只输出这个字典即可,前后不需要任何前后修饰或说明的语句' % chen_ming_function_name
            
            
            assistant_message1 = json.dumps(chen_ming_function)
            
            user_prompt = '现在有另一个函数,函数名为:%s;函数说明为:%s;\
                          请帮我仿造类似的格式为当前函数创建一个function对象。' % (function_name, function_description)

            response = openai.ChatCompletion.create(
                              model="gpt-4-0613",
                              messages=[
                                {
    
    "role": "user", "name":"example_user", "content": user_message1},
                                {
    
    "role": "assistant", "name":"example_assistant", "content": assistant_message1},
                                {
    
    "role": "user", "name":"example_user", "content": user_prompt}]
                            )
            functions.append(json.loads(response.choices[0].message['content']))
        return functions
    
    max_attempts = 3
    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

Few-shot's improvement of model reasoning ability can be applied to improve the stability of AI application development, and you can test it yourself.

6. Summary

This article describes how to register with OpenWeather and obtain an API key, and explains what is an external tool API and a basic introduction to OpenWeatherAPI. Afterwards, the method of using the OpenWeather API is explained in detail, including API function description, billing rules, and the calling method of the real-time weather query API. On this basis, it shows how to use Function calling to realize the real-time weather query of the Chat model, and finally introduces how to use Few-shot tips to optimize the function. This content is to implement Function calling to call external tool API, which has reference and inspiration value.

Finally, thank you for reading this article! If you feel that you have gained something, don't forget to like, bookmark and follow me, this is the motivation for my continuous creation. If you have any questions or suggestions, you can leave a message in the comment area, I will try my best to answer and accept your feedback. If there's a particular topic you'd like to know about, please let me know and I'd be happy to write an article about it. Thank you for your support and look forward to growing up with you!

Guess you like

Origin blog.csdn.net/Lvbaby_/article/details/131933871
Recommended