chatgpt喂数据以及知识库检索

openai 文章 2023-11-30 15:34 361 0 全屏看文

AI助手支持GPT4.0

知识检索

检索可以利用模型外部的知识来增强助手,例如专有产品信息或用户提供的文档。文件上传并传递给助手后,OpenAI 将自动对文档进行分块、索引和存储嵌入,并实施矢量搜索以检索相关内容来回答用户查询。


启用检索

传入Assistant 的参数以启用 Retrieval retrieval:tools

curl https://api.openai.com/v1/assistants \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "instructions": "You are a customer support chatbot. Use your knowledge base to best respond to customer queries.",
    "tools": [{"type": "retrieval"}],
    "model": "gpt-4-1106-preview"
  }'

如果您为特定助手启用检索,则所有附加文件都将自动索引,并且您将向每个助手每天支付 0.20 美元/GB 的费用。您可以使用修改助手端点启用/禁用检索。

怎么运行的

然后,模型根据用户消息决定何时检索内容。Assistants API 会自动在两种检索技术之间进行选择:


它要么在短文档的提示中传递文件内容,要么

对较长的文档执行矢量搜索

目前,检索通过将所有相关内容添加到模型调用的上下文中来优化质量。我们计划引入其他检索策略,使开发人员能够在检索质量和模型使用成本之间选择不同的权衡。


上传文件以供检索

与代码解释器类似,文件可以在助手级别或单独的消息级别传递。

# Upload a file with an "assistants" purpose
curl https://api.openai.com/v1/files \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F purpose="assistants" \
  -F file="@/path/to/knowledge.pdf"

# Add the file to the assistant
curl "https://api.openai.com/v1/assistants" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "instructions": "You are a customer support chatbot. Use your knowledge base to best respond to customer queries.",
    "name": "Math Tutor",
    "tools": [{"type": "retrieval"}],
    "model": "gpt-4-1106-preview"
    "file_ids": ["file_123abc456"]
  }'

当在消息级别附加文件时,只能在消息附加到的特定线程内访问该文件。上传文件后,您可以在创建消息时传递该文件的 ID。请注意,您不需要根据通过文件 API 上传的文件大小付费,而是根据您附加到特定助理或消息并建立索引的文件来付费。

curl https://api.openai.com/v1/threads/thread_abc123/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "role": "user",
    "content": "I can not find in the PDF manual how to turn off this device.",
    "file_ids": ["file_abc123"]
  }'

最大文件大小为 512 MB,且不超过 2,000,000 个令牌(附加文件时自动计算)。检索支持多种文件格式,包括.pdf、.md等.docx。有关支持的文件扩展名(及其相应的 MIME 类型)的更多详细信息,请参阅下面的支持的文件部分。


检索定价

检索价格为每个助手每天 0.20 美元/GB。启用检索工具后,将单个文件 ID 附加到多个助手将产生每个助手每天的费用。例如,如果您将同一个 1 GB 文件附加到启用了检索工具的两个不同助理(例如,面向客户的助理 #1 和内部员工助理 #2),您将需要支付两次存储费 (2 *每天 0.20 美元)。该费用不随从给定助手检索知识的最终用户和线程的数量而变化。


此外,如果消息是启用检索工具的运行的一部分,则附加到消息的文件将按每个助理收费。例如,在包含 10 条消息的线程上运行启用检索功能的助手(每条消息有 1 个唯一文件(总共 10 个唯一文件)),所有 10 个文件(除了附加到助手的任何文件之外)将产生每天每 GB 的费用本身)。


删除文件

要从助手中删除文件,您可以将该文件与助手分离:

curl https://api.openai.com/v1/assistants/asst_abc123/files/FILE_ID \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1" \
  -X DELETE

从助手中分离文件会从检索索引中删除该文件,这意味着您将不再需要为索引文件的存储付费。


当代码解释器在消息中输出文件路径时,您可以使用该annotations字段将它们转换为相应的文件下载。有关如何执行此操作的示例,请参阅注释部分。

{
    "id": "msg_abc123",
    "object": "thread.message",
    "created_at": 1699073585,
    "thread_id": "thread_abc123",
    "role": "assistant",
    "content": [
      {
        "type": "text",
        "text": {
          "value": "The rows of the CSV file have been shuffled and saved to a new CSV file. You can download the shuffled CSV file from the following link:\n\n[Download Shuffled CSV File](sandbox:/mnt/data/shuffled_file.csv)",
          "annotations": [
            {
              "type": "file_path",
              "text": "sandbox:/mnt/data/shuffled_file.csv",
              "start_index": 167,
              "end_index": 202,
              "file_path": {
                "file_id": "file-abc123"
              }
            }
          ]
        }
      }
    ],
    "file_ids": [
      "file-abc456"
    ],
        ...
  },

原文:https://platform.openai.com/docs/assistants/tools/knowledge-retrieval

-EOF-

AI助手支持GPT4.0