侧边栏壁纸
博主头像
资深人工智能从业者博主等级

行动起来,活在当下

  • 累计撰写 193 篇文章
  • 累计创建 82 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

OpenAI的函数调用:大模型技术突破与应用实例

MobotStone
2023-11-12 / 0 评论 / 0 点赞 / 106 阅读 / 3983 字

大型语言模型(LLM)主要是以非结构化、对话式数据作为输入,输出的也是类似的非结构化且对话式的内容。但我们现在有了一种新方法,称为“函数工具”(Functions Tools),这种方法可以帮助我们将LLM的输出变得结构化并格式化,这通常是为了准备提交给外部API的格式。

这个名字可能有点误导,有人可能会误以为LLM在运行代码或是执行某些集成操作以调用函数。但实际上,LLM的工作方式是通过识别用户输入中的关键实体和短语,并将这些与预定义的JSON架构进行匹配,从而填充预定义JSON文档中的参数。

这种结构化的JSON输出不仅可以用于数据的结构化处理,还可以用于数据存储等多种用途。

那么,关于函数的更多信息是什么呢?首先,函数调用允许您向助手描述自定义函数,这样可以访问外部API。这意味着您可以通过生成API预期的JSON格式输出来调用外部函数,而这个JSON已经预先填充了相关的输入参数。

函数调用实际上是一种结构化LLM输出的方式,用户可以为所谓的“函数”定义一个架构,LLM会选择一个架构并填充该架构的条目。也就是说,函数工具的作用仅仅是尝试以API期望的格式准备数据。

值得一提的是,函数已经成为Chat Completion API的一部分,并且现在助手也支持此功能。

下面是一个简单的应用实例:您可以复制以下Python代码并粘贴到笔记本中运行。您需要做的只是添加自己的OpenAI API密钥。

举个例子,助手接收到的用户输入是:“给Mobot AI的stone发送一封电子邮件,索要月度报告?”生成的JSON文档如下所示,邮件正文中包含换行。

send_email({
   "to_address":"stone@mobot.ai",
   "subject":"Request for Monthly Report",
   "body":"Dear Stone,\n\n
          I hope this message finds you well. I am reaching out to kindly 
          request the monthly report. Could you please provide the latest 
          update at your earliest convenience?\n\nThank you in advance for 
          your assistance.\n\n
          Best regards,"
})

在代码部分,我们定义了一个类型为function的工具,它包含to_addresssubjectbody三个属性。

pip install --upgrade openai
########################################
import os
import openai
import requests
import json
from openai import OpenAI
########################################
api_key = "your openai api key goes here"
########################################
client = OpenAI(api_key=api_key)
########################################
assistant = client.beta.assistants.create(
  instructions="You are a HR bot, answering HR questions.",
  model="gpt-4-1106-preview",
  tools=[{
        "type": "function",
        "function": {
              "name": "send_email",
              "description": "Please send an email.",
              "parameters": {
                "type": "object",
                "properties": {
                  "to_address": {
                    "type": "string",
                    "description": "To address for email"
                  },          
                  "subject": {
                    "type": "string",
                    "description": "subject of the email"
                  },
                  "body": {
                    "type": "string",
                    "description": "Body of the email"
                  }
          }
                            }
        }
    }]
  )
########################################
thread = client.beta.threads.create()
########################################
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Send Stone from Mobot AI an email asking for the monthly report?"
)
########################################
run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  instructions="Use the function tool for this query."
)
########################################
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)
########################################
messages = client.beta.threads.messages.list(
  thread_id=thread.id
)
########################################

运行这段代码后,您可以在OpenAI仪表板中看到您的助手已被创建,并且在函数下创建了工具send_email。现在您可以在这里设置助手的名称和指令。

当您在仪表板中与助手交互时,JSON文档会在响应中生成,您还可以在此处检查日志。

总结一下,函数是LLM向正确方向迈出的一步,通过它,我们可以定义数据输出的格式,并为LLM提供了一种结构或框架,使其能够执行某种类型的数据转换。

0