Agentic RAG:实现添加自主代理功能

2024年03月18日 由 alex 发表 101 0

简介

如下图所示,加载了多个文档,每个文档都与一个代理工具相关联,或者可以称为子代理。


每个子代理都有一个描述,可以帮助元代理决定何时使用哪种工具。


8


关于 OpenAI 函数调用的更多信息

需要说明的是,聊天完成 API 不会调用任何函数,但模型会生成 JSON,可用于从你的代码中调用函数。


请看下面的工作 API 调用:


如下所示,向 https://api.openai.com/v1/chat/completions 提交了一个 API 调用,并在头中定义了 OPEN_API_KEY。


9


下面是发送到模型的 JSON 文件。该调用的目的是生成一个 JSON 文件,用于发送到发送电子邮件的 API。


你可以看到文件名是 send_email。其中定义了三个参数:to_address、subject 和 body,即邮件正文。聊天机器人的用户请求是 :Send Cobus from kore ai an email asking for the monthly report?


{
  "model": "gpt-3.5-turbo-0613","gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "Send Cobus from kore ai an email asking for the monthly report?"}
  ],
  "functions": [
    {
      "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"
          }
        }
      }
    }
  ]
}


下面是模型生成的 JSON,在本例中使用了 gpt-3.5-turbo-0613 模型来完成。


{
    "id": "chatcmpl-7R3k9pN6lLXCmNMiLNoaotNAg86Qg","id": "chatcmpl-7R3k9pN6lLXCmNMiLNoaotNAg86Qg",
    "object": "chat.completion",
    "created": 1686683601,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "function_call": {
                    "name": "send_email",
                    "arguments": "{\n  \"to_address\": \"cobus@kore.ai\",\n  \"subject\": \"Request for Monthly Report\",\n  \"body\": \"Dear Cobus,\\n\\nI hope this email finds you well. I would like to kindly request the monthly report for the current month. Could you please provide me with the report by the end of the week?\\n\\nThank you in advance for your assistance!\\n\\nBest regards,\\n[Your Name]\"\n}"
                }
            },
            "finish_reason": "function_call"
        }
    ],
    "usage": {
        "prompt_tokens": 86,
        "completion_tokens": 99,
        "total_tokens": 185
    }
}


这是朝着正确方向的一大飞跃,大语言模型不仅能将输出结构化为自然会话语言,还能将其结构化。


返回 LlamaIndex 的Agentic RAG

LlamaIndex 的这一实现结合了 OpenAI Agent 功能、函数调用和 RAG。我还喜欢 LlamaIndex 谈论上下文增强(contextual-augmentation)的方式,这实际上是利用上下文学习(in-context learning)的一个更准确的术语。


LlamaIndex ContextRetrieverOpenAIAgent 实现在 OpenAI 的函数 API 之上创建了一个代理,并存储/索引任意数量的工具。


LlamaIndex 的索引和检索模块有助于消除提示符中无法容纳过多功能的复杂性。


需要说明的是,这无论如何都不会利用 OpenAI 助手的功能。因此,代码运行时不会创建助手。


你需要添加 OpenAI API 密钥才能运行该代理。


%pip install llama-index-agent-openai-legacy
!pip install llama-index
import os
import openai
os.environ["OPENAI_API_KEY"] = "< Your OpenAI API Key Goes Here>"


除了添加 OpenAI API 密钥外,LlamaIndex 提供的笔记本也能完美运行。


工具,也称为子代理

请注意下面是如何定义子代理或代理工具的,包括名称和描述,这有助于元代理决定何时使用哪种工具。


query_engine_tools = [
    QueryEngineTool(
        query_engine=march_engine,
        metadata=ToolMetadata(
            name="uber_march_10q","uber_march_10q",
            description=(
                "Provides information about Uber 10Q filings for March 2022. "
                "Use a detailed plain text question as input to the tool."
            ),
        ),
    ),
    QueryEngineTool(
        query_engine=june_engine,
        metadata=ToolMetadata(
            name="uber_june_10q",
            description=(
                "Provides information about Uber financials for June 2021. "
                "Use a detailed plain text question as input to the tool."
            ),
        ),
    ),
    QueryEngineTool(
        query_engine=sept_engine,
        metadata=ToolMetadata(
            name="uber_sept_10q",
            description=(
                "Provides information about Uber financials for Sept 2021. "
                "Use a detailed plain text question as input to the tool."
            ),
        ),
    ),
]


复杂查询

在代理代码中,定义了以下缩写......你可以将这些缩写视为速记符号,用于提高查询效率。


 "Abbreviation: X = Revenue","Abbreviation: X = Revenue",
    "Abbreviation: YZ = Risk Factors",
    "Abbreviation: Z = Costs",


下面请看如何在使用速记法的同时向代理人提出问题:


response = context_agent.chat("What is the YZ of March 2022?")


但是,我相信你已经明白了这里的难点所在,查询只跨越了一个月,因此也只查询了一份文件......如果我们想计算所有三个月/三份文件的收入减去成本呢?


response = context_agent.chat(
    "Can you run MAGIC_FORMULA on Uber's revenue and cost?""Can you run MAGIC_FORMULA on Uber's revenue and cost?"
)


在下图中,你可以看到代理如何计算总收入减去成本。

上下文信息有助于代理的输出可解释和可检查。


10


下面是代理的最终答复......虽然答复是可以解释和检查的,但如果能以人工方式检查上传的 PDF 文件,看看得出的答案是否与代理的答复一致,那将是一项有趣的工作。


Calling function: 
magic_formula with args: 
{"revenue":23400,"cost":24960} 
Got output: -1560


我利用代理的输出结果,让 ChatGPT 围绕代理的输出结果撰写一篇叙述,结果如下...


在财务分析领域,有一种被称为 "神奇公式 "的有效工具,旨在根据特定的财务指标评估公司的业绩。考虑到 Uber 提供的收入和成本数据,让我们来执行 "神奇公式 "并解读其含义。
对 Uber 234 亿美元的收入和 249.6 亿美元的成本运行 "神奇公式",得出了一个耐人寻味的结果。结果显示负值为 15.60 亿美元。这个负值表明,根据神奇公式的评估,Uber 的财务业绩低于预期。
然而,深入研究导致这一结果的根本因素至关重要。对 Uber 的收入和成本结构进行更详细的分析,可以揭示公司运营中的优势和劣势。此外,探索一段时间内的趋势并与行业基准进行比较,也能为解读 "神奇公式 "的评估提供有价值的背景。
当我们在复杂的财务分析中遨游时,神奇公式就像一盏指路明灯,照亮了我们进一步调查和做出战略决策的潜在领域。~ 聊天 GPT


本文将自主代理、工具、RAG 和 OpenAI 功能调用结合在一起,成为一个 LLM 实现。

文章来源:https://medium.com/@cobusgreyling/agentic-rag-context-augmented-openai-agents-578e96212bc0
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消