跳转到主要内容

AI函数实用工具

项目描述

OpenAI GPT函数的Functools

OpenAI刚刚发布了GPT函数: https://openai.com/blog/function-calling-and-other-api-updates

但是创建实际函数签名真的很繁琐。手动解析结果和链式调用

curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "What is 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"]
      }
    }
  ]
}'

使用aifunctools代替

相反,aifunctools会自动解析可用的Python类型注解和文档字符串,您无需进行所有手动工作

from aifunctools.openai_funcs import complete_with_functions

def get_current_weather(location: str, unit: str) -> dict:
    """
    Get the current weather in a given location

    :param location: The city and state, e.g. San Francisco, CA
    :param unit: Either "celsius" or "fahrenheit"
    :return: the current temperature, unit, and a description
    """
    return {"temperature": 22, "unit": "celsius", "description": "Sunny"}


resp = complete_with_functions("What is the weather like in Boston?",
                               get_current_weather)
# The response should contain: "The weather in Boston is currently sunny with a temperature of 22 degrees Celsius."

下一步是什么

OpenAPI规范

Python函数很棒,但如果能从远程服务中解析OpenAPI规范那就更好了。想象一下每个服务提供商都提供他们的API规范,代理可以爬取这些API,并使用aifunctools将它们链接在一起。

更丰富的类型

在这个黑客攻击中,我们只支持基本字符串/数字/布尔类型。通过使其更灵活,我们可以得到更丰富的行为。

错误处理

我相信有很多情况,不完整的文档字符串和类型签名会导致错误。我们可以使这些更健壮(或使用一些合理的默认值)。

项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源分布

aifunctools-0.1.2.tar.gz (15.3 kB 查看散列值)

上传时间

支持