使用 Codex 来写入 PDF 文件并创建 AI Agent 对它进行问答

0 阅读3分钟

在之前的文章 “哈利波特问答 Agent Builder + Workflow 教程”,我们展示了如何使用 阿里云 Elasticssearch 实例来写入哈利波特章节5,并对它进行搜索的详细步骤。在今天的文章里,我讲使用 Codex 来完成同样的事情。

运行 Codex 并建立和 Elasticsearch 的连接

`

1.  我的 Elasticsearch 运行于 ​​
2.  https://localhost:9200
3.  ,它的 API key 是:b2FxWkJaMEJybXdnQjhrT1VNN1g6NE90SkdXTG9WMU5MS0JReUMwNlphdw== 请帮我建立和 Elasticsearch 的连接,并打出测试信息。这是一个自签名证书的 Elasticsearch 集群。请忽略证书。

`AI代码解释

`请把当前目录下的 harrypotter_sorcerers_stone_chapter_5.pdf 文件写入到 Elasticsearch 中去。我希望有一个字段叫做 content, 而另外一个字段的名字叫做 content_vector。它的内容是从 content 拷贝过来,并使用推理节点 .jina-embeddings-v5-omni-small 作为其 inference_id。它的类型是 semantic_text。`AI代码解释
`

1.  $ pwd
2.  /Users/liuxg/python/harrypotter
3.  $ ls
4.  harrypotter_sorcerers_stone_chapter_5.pdf

`AI代码解释

注意:上面我使用了 .jina-embeddings-v5-omni-small 这个 EIS 推理端点来完成。你可以根据自己的情况创建一个推理端点,就像在文章  “哈利波特问答 Agent Builder + Workflow 教程” 里说的那样。

我们到 Kibana 中查看一下:

`GET harrypotter_sorcerers_stone_chapter_5/_mapping`AI代码解释

从上面我们可以看到 content 字段被拷贝到 content_vector 字段,并对它进行向量化。

我们也可以使用如下的命令来查看已经写入的文档:

`GET harrypotter_sorcerers_stone_chapter_5/_search`AI代码解释

从上面我们可以看到我们已经成功地把一个 pdf 文档写入到 Elasticsearch 中去了。如果你的 pdf 文档里含有表格等信息,你也可以使用我的另外一篇文章 “基于 Jina OCR v1 与 Elasticsearch 的 PDF 智能混合检索与 RAG 系统实战指南” 来完成数据的写入。

创建 AI agent 来进行搜索

我们使用如下的步骤来完成。

注意:你必须版本在 9.4+

创建 tool

Target pattern:

`harrypotter_sorcerers_stone_chapter_5`AI代码解释

注:这个必须和索引名称一致。

Custom instructions:

`Search for harry potter chapter 5`AI代码解释

Tool ID:

`harrypotter_sorcerers_stone_chapter_5`AI代码解释

Description:

`Fetches information and data from Harry Potter and the Sorcerer's Stone, Chapter 5 (Diagon Alley) from vector embeddings. Returns relevant passages, quotes, and context based on search queries about the chapter's content, characters, events, and themes.`AI代码解释

点击 Save 按钮保存:

创建 agent

我们按照如下的步骤来创建 agent:

Agent ID:

`potter-answers`AI代码解释

Custom Instructions:

``

1.  You are an expert on Harry Potter Chapter 5 (Diagon Alley). Help users explore the chapter by retrieving relevant passages from vectors. Be accurate, cite sources, provide context, and maintain an enthusiastic but educational tone. Clarify when information is outside Chapter 5's scope. You will only answer questions based on context provided. Do not use external sources not explicitly provided.

3.  ## Tools

5.  ### `harrypotter_sorcerers_stone_chapter_5`
6.  Use this tool to fetch relevant passages and content from Chapter 5. Always use this tool when answering questions about the chapter.

8.  ### `potter.send.email`
9.  Use this tool when the user asks you to send an email. Accepts a recipient name or email address, a subject, and a body. If a name is provided, the tool resolves the email address automatically.

``AI代码解释![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

Display Name:

`Potter Answers`AI代码解释

Display Description:

`Your expert guide to Harry Potter and the Sorcerer's Stone, Chapter 5! I help you explore Diagon Alley, from Ollivander's wand shop to Gringotts Bank. Ask me about any scene, character, or magical detail from this iconic chapter.`AI代码解释

我们接下来添加上面创建的 tool:

点击 Save 保存自己的 agent:

测试 agent

我们接下来选择我们刚创建的 agent,并进行测试:

`What is Quidditch, and which Hogwarts House does Hagrid warn Harry against, linking it to dark wizards?`AI代码解释

Horray! 是不是非常容易地创建了自己的 agent。是不是很容易啊?