测试gpt的function函数功能

2023-11-03

 官网API (科学上网查看)

1、我对该功能的理解

    利用gpt的上下文理解能力,在执行方法run_conversation(xx)时,目标锁定在--提取出functions里每个function下required属性对应的值。
    而真正的function函数(get_current_weather(xx))只是对提取出的信息进行了一个json封装,并没有实际功能。 

 2、举例

 1)如本例中functions里只有一个function,该function的"required": ["location"], 即要提取出文本中的location。
 2)而 "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                    },
    properties中关于"location"的描述"description",我理解就是喂入模型的prompt的一部分,即用户user表达了想让模型替自己干什么。
3)为什么说只是一部分?

        因为prompt除了要表述想要AI干什么,还要告诉AI他要从哪里提取,即告诉待处理的信息 messages=[{"role": "user", "content": "What's the weather like in Boston?"}]。
4)这样就给gpt了完整的prompt信息。

 3、至于为什么会发送两次gpt调用?

    第1次调用gpt目的:即上面第2条-例子中的说明;
    第2次调用gpt目的:把第1次提取到信息和function返回的json, 重新组合成对话式的语言,而不只是返回function规定死的json。这样不只是开发人员能看懂,而是任何一个人类都可以看懂。
    本例中:
    1) function返回的json 如下:

这里location和unit的信息是从message里取的,但message里并没有保存这2个信息,所以这里都是null。数据的真实信息在 message["function_call"]["arguments"]中。
        {
            weather_info: {
                    "location": null,
                    "temperature": "72",
                    "unit": null,
                    "forecast": ["sunny", "windy"],
                }
        }
    2) gpt第2次调用重新组装的对话语言:见choices--message--content
        {
          "choices": [
            {
              "finish_reason": "stop",
              "index": 0,
              "message": {
                "content": "The current weather in Boston, MA is 72\u00b0F (22\u00b0C). It is sunny and windy.",   # 这里的回答把function返回的json值重新组装成了对话语言。而且每次运行回答都会变,但意思都是一样的,也就是同一句话会换个说法,这样就更加符合人类语言的灵活性了。
                "role": "assistant"
              }
            }
          ],
          "created": 1687312724,
          "id": "chatcmpl-7ThPI7pl75aEQMV2NvcfNZK051YtH",
          "model": "gpt-3.5-turbo-0613",
          "object": "chat.completion",
          "usage": {
            "completion_tokens": 21,
            "prompt_tokens": 70,
            "total_tokens": 91
          }
        }

4、代码 
(1) 官网api测试代码详解
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import openai
import json


openai.api_base = '代理地址'  
openai.api_key = 'your own apikey'


# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    }
    return json.dumps(weather_info)

# Step 1, send model the user query and what functions it has access to
def run_conversation():
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=[{"role": "user", "content": "What's the weather like in Boston?"}],
        functions=[
            {
                "name": "get_current_weather",
                "description": "Get the current weather in a given location",
                "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"],
                },
            }
        ],
        function_call="auto",
    )

    message = response["choices"][0]["message"]  

    print("*"*60)
    print(message)
    print("*" * 60)
    # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
    # {
    #     "content": null,
    #     "function_call": {
    #         "arguments": "{\n  \"location\": \"Boston, MA\"\n}",
    #         "name": "get_current_weather"
    #     },
    #     "role": "assistant"
    # }
    # ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

    # Step 2, check if the model wants to call a function
    if message.get("function_call"):
        function_name = message["function_call"]["name"]

        # Step 3, call the function
        # Note: the JSON response from the model may not be valid JSON
        function_response = get_current_weather(
            location=message.get("location"),  # 这里message里并没有保存location和unit的信息,真实信息在 message["function_call"]["arguments"]中
            unit=message.get("unit"),
        )

        # Step 4, send model the info on the function call and function response
        second_response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=[
                {"role": "user", "content": "What is the weather like in boston?"},
                message,
                {
                    "role": "function",
                    "name": function_name,
                    "content": function_response,
                },
            ],
        )
        return second_response

print(run_conversation())
(2)自定义函数功能
  • 测试信息抽取 
#!/usr/bin/env python
# -*- coding: UTF-8 -*-


import openai
import json


####################################################1、测试function功能
openai.api_base = '代理地址'  # Your Azure OpenAI resource's endpoint value.
openai.api_key = 'your own apikey'


import json

def get_tax_info(location):
    tax_info = {
        "location": location,
        "forecast": "缴税地"
    }
    return json.dumps(tax_info)

# Step 1, send model the user query and what functions it has access to
def run_conversation(content):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=[{"role": "user", "content": content}],
        functions=[
            {
                "name": "get_tax_info",
                "description": "提取交税地点",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            # TODO 这里的描述是prompt的一部分,会影响模型的提取结果。
                            #  实测,content = "我在安吉开的有共工作室,同时在乐清也经营的有小店,我在这两个地方都要交税。"下,
                            #  设置"description": "交税地点,比如浙江省-湖州市-安吉县。如果有多个缴税地,则以数组的形式返回,如[浙江省-温州市-乐清市,浙江省-杭州市],模型提取结果:{'location': '浙江省-湖州市-安吉县,浙江省-温州市-乐清市'}
                            #  设置"description": "交税地点,比如浙江省-湖州市-安吉县。如果有多个缴税地,则以数组的形式返回,如[浙江省-湖州市-安吉县,浙江省-杭州市],模型提取结果:{'location': '浙江省-湖州市-安吉县'}
                            #  设置"description": "交税地点,比如浙江省-湖州市-安吉县。如果有多个缴税地,则以数组的形式返回,如[浙江省-杭州市],模型提取结果:{'location': '浙江省-湖州市-安吉县'}
                            #  设置"description": "交税地点,比如浙江省-湖州市-安吉县。如果有多个缴税地,则以数组的形式返回,如[浙江省-温州市-乐清市,浙江省-湖州市-安吉县],模型提取结果:{'location': '浙江省-湖州市-安吉县,浙江省-温州市-乐清市'}
                            "description": "交税地点,比如浙江省-湖州市-安吉县。如果有多个缴税地,则以数组的形式返回,如[浙江省-温州市-乐清市,浙江省-杭州市]",
                            "description": "交税地点,比如浙江省湖州市安吉县",
                        }
                    },
                    "required": ["location"],
                },
            }
        ],
        function_call="auto",
    )

    message = response["choices"][0]["message"]

    print("*" * 60)
    print(message)
    print(type(message))  # <class 'openai.openai_object.OpenAIObject'>
    print("*" * 60)
    # mess_dict = message.to_dict()
    # print(mess_dict)
    # print(type(mess_dict))  # <class 'dict'>
    # print("*" * 60)
    # json_str = json.dumps(mess_dict)
    # print(json_str)
    # print(type(json_str))  # <class 'str'>
    print("*" * 60)
    arguments_ = message["function_call"]["arguments"]
    print(type(arguments_))  # <class 'str'>
    print(arguments_)  # 这里已经是正常显示的中文了,所以不需要对字符串再次unicode解码
    # print(arguments_.encode('unicode-escape').decode('unicode-escape'))
    print("*" * 60)
    arg_json = json.loads(arguments_)  # <class 'dict'>
    print(type(arg_json))
    print(arg_json)

    return message
    # # Step 2, check if the model wants to call a function
    # if message.get("function_call"):
    #     function_name = message["function_call"]["name"]
    #
    #     # Step 3, call the function
    #     # Note: the JSON response from the model may not be valid JSON
    #     function_response = get_tax_info(
    #         location=message.get("location")
    #     )
    #
    #     # Step 4, send model the info on the function call and function response
    #     second_response = openai.ChatCompletion.create(
    #         model="gpt-3.5-turbo-0613",
    #         messages=[
    #             {"role": "user", "content": content},
    #             message,
    #             {
    #                 "role": "function",
    #                 "name": function_name,
    #                 "content": function_response,
    #             },
    #         ],
    #     )
    #     return second_response


# content = "我在安吉开的有共工作室,同时在乐清也经营的有小店,我在这两个地方都要交税。提取下我的交税地点,并组装成省-市-区的格式,如提取到杭州,则组装返回浙江省-杭州市。"
content = "我在安吉开的有共工作室,同时在乐清也经营的有小店,我在这两个地方都要交税。"
# content = "我在杭州上班,也在这里缴税"
result = run_conversation(content)

####################################################2、测试unicode解码
# str = '''
# {
#   "choices": [
#     {
#       "finish_reason": "stop",
#       "index": 0,
#       "message": {
#         "content": "\u5f88\u62b1\u6b49\uff0c\u6211\u65e0\u6cd5\u63d0\u4f9b\u5173\u4e8e\u5728\u676d\u5dde\u7f34\u7a0e\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u8bf7\u60a8\u54a8\u8be2\u5f53\u5730\u7a0e\u52a1\u5c40\u6216\u8005\u76f8\u5173\u7a0e\u52a1\u90e8\u95e8\uff0c\u4ed6\u4eec\u5c06\u80fd\u591f\u4e3a\u60a8\u63d0\u4f9b\u6b63\u786e\u7684\u4fe1\u606f\u548c\u6307\u5bfc\u3002",
#         "role": "assistant"
#       }
#     }
#   ],
#   "created": 1686797348,
#   "id": "chatcmpl-7RXKm2DOIKxDtyCkfHF1Gz2xEW7Yi",
#   "model": "gpt-3.5-turbo-0613",
#   "object": "chat.completion",
#   "usage": {
#     "completion_tokens": 63,
#     "prompt_tokens": 60,
#     "total_tokens": 123
#   }
# }
# '''
# print('*'*50)
#
# p = "\u65e0\u6cd5\u8bc6\u522b\u5c5e\u6027\u201cphysical_network, network_type\u201d"
# p2 = "\u8d26\u6237\u8bbe\u5907"
# content= "\u5f88\u62b1\u6b49\uff0c\u6211\u65e0\u6cd5\u63d0\u4f9b\u5173\u4e8e\u5728\u676d\u5dde\u7f34\u7a0e\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u8bf7\u60a8\u54a8\u8be2\u5f53\u5730\u7a0e\u52a1\u5c40\u6216\u8005\u76f8\u5173\u7a0e\u52a1\u90e8\u95e8\uff0c\u4ed6\u4eec\u5c06\u80fd\u591f\u4e3a\u60a8\u63d0\u4f9b\u6b63\u786e\u7684\u4fe1\u606f\u548c\u6307\u5bfc\u3002"
#
# # print(p2.encode().decode("unicode_escape"))
# # print(p2.encode('utf-8').decode('unicode_escape'))
# print(str.encode('unicode-escape').decode('unicode-escape'))
  • 测试信息总结
    import openai
    import json
    
    '''
    尝试function函数对input进行总结,方便后续用总结后的内容,发起其他操作。
    
    -- 结果:
     可以联系上下文,但是仍然给出的是最后一个问题的回复,而不是内容总结!!!
    '''
    
    
    def get_summary(summary):
        tax_info = {
            "summary": summary
        }
        return json.dumps(tax_info)
    
    # Step 1, send model the user query and what functions it has access to
    def run_conversation(messages):
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            # messages=[{"role": "user", "content": content}],
            messages=messages,
            functions=[
                {
                    "name": "get_summary",
                    "description": "提炼输入内容的主要信息,总结成句子的中心思想并输出",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "summary": {
                                "type": "string",
                                "description": "对输入内容的一段总结性描述,里面包含了输入内容的重要信息,不重要或非相关的信息被丢弃",
                            }
                        },
                        "required": ["summary"],
                    },
                }
            ],
            function_call="auto",
        )
    
        message = response["choices"][0]["message"]
    
        print("*" * 60)
        print(message)
        # print(type(message))  # <class 'openai.openai_object.OpenAIObject'>
        print("*" * 60)
        arguments_ = message["content"]
        # print(type(arguments_))  # <class 'str'>
        # print(arguments_)  # 这里已经是正常显示的中文了,所以不需要对字符串再次unicode解码
        return arguments_
    
    
    old_q = "我想邀请员工线上签合同,要怎么在你们app上操作"
    old_a = "您好,您先在【首页】点击【人员管理】-【员工】,选择线上签署签约,对合同模板内容进行确认并输入验证码获取签章,然后填写合同相关内容信息(被邀请的人员姓名、身份证号、税前工资、发薪日),最后对合同内容确认无误后即可通过链接或二维码方式发起邀请"
    new_q = "那如果是线下呢"
    # content = "我在杭州上班,也在这里缴税"
    
    messages = [
                {"role": "user", "content": old_q},
                {"role": "assistant", "content": old_a},
                {"role": "user", "content": new_q},
               ]
    result = run_conversation(messages)
    print(result)
    
    
     

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试gpt的function函数功能 的相关文章

随机推荐

  • 1、在不配置CATALINA_HOME环境变量的情况下启动tomcat

    场景 tomcat执行startup bat的时候 会寻找全局环境变量CATALINA HOME 如果CATALINA HOME对应的tomcat目录不是当前tomcat的目录 则就会启动报错 解决方法如下 1 打开bin目录下的start
  • 2020-04-27

    jsp中方法为找到异常 java lang NoSuchMethodException com bjsxt servlet UserServlet at java lang Class getConstructor0 Class java
  • MySQL高级篇-第02章_MySQL的数据目录

    1 MySQL的主要目录 root atguigu01 find name mysql 安装好MySQL 8之后 我们查看如下的目录结构 1 1 数据库文件的存放路径 MySQL数据库文件的存放路径 var lib mysql mysql
  • OpenWRT UCI API的使用

    OpenWRT UCI API的使用 UCI 是OpenWRT为实现配置集中化而引入的一个软件包 通过修改UCI 可以实现对OpenWRT的绝对部分配置的修改 LUCI OpenWRT 的WEB配置界面 也是通过读UCI配置文件的操作来实现
  • 《TCP IP网络编程》第十八章

    第 18 章 多线程服务器端的实现 18 1 理解线程的概念 线程背景 第 10 章介绍了多进程服务端的实现方法 多进程模型与 select 和 epoll 相比的确有自身的优点 但同时也有问题 如前所述 创建 复制 进程的工作本身会给操作
  • CH341A及XTW 2两种SPI FLASH烧录器的硬件使用说明_20220920【可用于升级主板BIOS】

    目录 一 XTW 2 烧录速度快一些 二 CH341A 烧录速度慢 但貌似兼容性更高 同时价格较便宜 三 烧录器驱动及软件的下载链接 一 XTW 2 烧录速度快一些 3 3V SPI FLASH芯片接法示意图 例如W25Q128FV 1 8
  • 安卓期末作业-仿外卖app-简单app(附下载资源)

    安卓期末作业 仿外卖app 2022学年上学期期末移动开发课设 资源下载链接 比较简单的一个app 适合初学者学习使用 app截图如下 点我下载
  • 学会不被洗脑 很重要!

    大家好 我是北妈 今天发一个铁粉在桃花岛向我提问的问题 0 虚x 提问 最近网暴频发 网友甚至好友互相攻击 在越来越复杂的社会事件中 怎么提升自己的思考深度 怎么避免被洗脑 北妈怎么看 这个话题我不止一次说过了 就是其中一个原则 兼听则明
  • 复杂数据类型 枚举类型

    枚举 如果一个变量只有几种可能的值 可以把它定义成枚举类型 所谓 枚举 顾名思义 就是把这种类型数据可取的值一一列举出来 一个枚举型变量取值仅限于列出值的范围 例如 定义一个枚举类型weekday enum weekday Mon Tue
  • 最全的软件测试面试题(含答案)

    软件的生命周期 prdctrm 计划阶段 planning 需求分析 requirement 设计阶段 design 编码 coding gt 测试 testing gt 运行与维护 running maintrnacne 测试用例 用例编
  • ChatGPT的奇思妙想

    ChatGPT的奇思妙想 最近 一个名叫 ChatGPT 通用人工智能 的人工智能系统引起了大家的关注 该系统可以通过对话生成文本和回答问题 在谷歌公司公布的一份新研究报告中 该系统已经具备了 与人类进行自然语言交流 的能力 虽然 Chat
  • Hungarian method 匈牙利算法 解决指派问题

    这个也讲得不错 https blog csdn net Wonz5130 article details 80678410 from scipy optimize import linear sum assignment import nu
  • javascript XMLHttpRequest 对象的open() 方法参数说明

    下文是从w3c上摘录下来的 其中参数 method 说明的很简短 不是很理解 所以又找了些资料作为补充 文中带括号部分 XMLHttpRequest open 初始化 HTTP 请求参数 语法 open method url async u
  • vscode设置第三方库路径和自动代码补全

    1 打开VSCode gt 文件 gt 首选项 gt 设置 gt 用户 gt 扩展 gt Python gt Auto Complete Extra Paths gt 在settings json中编辑 在settings json中添加代
  • vue工程可视化大屏 自适应问题

    目录 三大常用方式 vw vh方案 scale方案 rem vw vh方案 最新方式 调用autofit js包 vue工程可视化大屏 自适应问题 可视化大屏的适配是一个老生常谈的话题了 现在其实不乏一些大佬开源的自适应插件 工具但是我为什
  • Java 的使用习惯

    定义配置文件信息 有时候我们为了统一管理会把一些变量放到 yml 配置文件中 例如 用 ConfigurationProperties 代替 Value 使用方法 定义对应字段的实体 Data 指定前缀 ConfigurationPrope
  • Windows两台服务器之间实现文件共享

    1 检查工作 1 win r 输入services msc检查TCP IP NetBIOS Helper服务是否已经开启 2 两台服务器之间需要开放139 445端口 或者关闭防火墙 2 共享文件夹配置 1 共享设置 新建share文件夹
  • Java基础——参数传参(基本类型,引用类型传参)

    文章目录 一 方法 参数 返回值 二 参数传递过程中的底层原理 1 创建对象过程中发生的事 1 对象创建并存储 2 基本类型作为形参传递 值传递 3 引用类型作为形参传递 本质也是值传递 4 Java中的引用与C 中引用 指针的区别 三 特
  • scala Stack可变栈

    import scala collection mutable 栈 先进后出 后进先出 top 获取栈顶元素 但是不会把这个元素从栈顶移除 push 表示入栈操作 相当于把元素压入栈顶 pop 移除栈顶元素 并返回此元素 clear 清除集
  • 测试gpt的function函数功能

    官网API 科学上网查看 1 我对该功能的理解 利用gpt的上下文理解能力 在执行方法run conversation xx 时 目标锁定在 提取出functions里每个function下required属性对应的值 而真正的functi