# Knowledge Retrieval

知识检索功能通过引入模型之外的知识，例如专有产品信息或用户提供的文档，来增强Afarensis 助手的能力。通过上传文件并将其传递给助手，Afarensis 会自动对文档进行分块、索引和存储嵌入式内容，并利用向量搜索技术来检索与用户查询相关的内容。

启用知识检索 要在助手中启用知识检索功能，请在创建助手时在 tools 参数中指定 retrieval：

assistant = client.beta.assistants.create( instructions="您是一个客户支持聊天机器人。利用您的知识库以最佳方式回应客户查询。", model="gpt-4-turbo-preview", tools=\[{"type": "retrieval"}] ) 一旦为特定助手启用知识检索，所有附加的文件将被自动索引，您将按每个助手每天 0.20 美元/GB 的费率被收费。您可以通过修改助手端点来启用或禁用知识检索。

如何工作 Afarensis 助手API根据用户消息决定何时进行内容检索。API自动选择两种检索技术之一：对短文档直接在提示中传递文件内容，或对较长文档执行向量搜索。目前，检索通过将所有相关内容添加到模型调用的上下文中来优化质量。我们计划引入更多的检索策略，以便开发者可以在检索质量和模型使用成本之间做出不同的选择。

上传检索文件 文件可以在创建助手时或在单个消息级别上传和传递，类似于代码解释器功能。

## 上传一个目的为 "assistants" 的文件

file = client.files.create( file=open("knowledge.pdf", "rb"), purpose='assistants' )

## 将文件添加到助手

assistant = client.beta.assistants.create( instructions="您是一个客户支持聊天机器人。利用您的知识库以最佳方式回应客户查询。", model="gpt-4-turbo-preview", tools=\[{"type": "retrieval"}], file\_ids=\[file.id] ) 在消息级别附加的文件仅在特定线索中可访问。上传文件后，您可以在创建消息时传递此文件的ID。

message = client.beta.threads.messages.create( thread\_id=thread.id, role="user", content="我在PDF手册中找不到如何关闭这个设备的方法。", file\_ids=\[file.id] ) 检索定价 知识检索的费用为每个助手每天 0.20 美元/GB。将同一文件ID附加到多个启用检索工具的助手上将产生每个助手每天的费用。

删除文件 要从助手中移除文件，您可以分离文件：

file\_deletion\_status = client.beta.assistants.files.delete( assistant\_id=assistant.id, file\_id=file.id ) 分离文件将从检索索引中移除该文件，意味着您将不再为该索引文件的存储支付费用。

文件引用 当代码解释器在消息中输出文件路径时，您可以使用 annotations 字段将这些路径转换为相应的文件下载链接。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.afarensis.com/gong-ju/knowledge-retrieval.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
