Skip to main content
为了更好地了解 NutritionAI 如何赋予你的 Agent 强大的食物营养查询能力,让我们构建一个能够通过 Passio NutritionAI 获取营养信息的 Agent。

定义工具

首先,我们需要创建 Passio NutritionAI 工具

Passio nutrition AI

LangChain 内置了 Passio NutritionAI 工具,可以方便地查询食物营养信息。 注意,使用此工具需要 API 密钥——他们提供免费套餐。 创建 API 密钥后,需要将其导出为:
export NUTRITIONAI_SUBSCRIPTION_KEY="..."
……或通过其他方式(如 dotenv 包)将其提供给 Python 环境。也可以通过构造函数调用显式控制密钥。
from dotenv import load_dotenv
from langchain_core.utils import get_from_env

load_dotenv()

nutritionai_subscription_key = get_from_env(
    "nutritionai_subscription_key", "NUTRITIONAI_SUBSCRIPTION_KEY"
)
from langchain_community.tools.passio_nutrition_ai import NutritionAI
from langchain_community.utilities.passio_nutrition_ai import NutritionAIAPI
nutritionai_search = NutritionAI(api_wrapper=NutritionAIAPI())
nutritionai_search.invoke("chicken tikka masala")
nutritionai_search.invoke("Schnuck Markets sliced pepper jack cheese")

工具列表

现在我们已经有了工具,可以创建一个工具列表供后续使用。
tools = [nutritionai_search]

创建 Agent

定义好工具后,可以创建 Agent 了。这里使用 OpenAI Functions Agent——关于此类 Agent 及其他选项,请参阅本指南 首先,选择用于指导 Agent 的 LLM。
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
接下来,选择用于指导 Agent 的提示词。
from langchain_classic import hub

# 获取要使用的提示词 - 你可以修改它!
prompt = hub.pull("hwchase17/openai-functions-agent")
prompt.messages
[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='You are a helpful assistant')),
 MessagesPlaceholder(variable_name='chat_history', optional=True),
 HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], template='{input}')),
 MessagesPlaceholder(variable_name='agent_scratchpad')]
现在,可以使用 LLM、提示词和工具初始化 Agent。Agent 负责接收输入并决定采取哪些行动。需要注意的是,Agent 本身不执行这些行动——这由 AgentExecutor 完成(下一步)。更多信息请参阅我们的概念指南
from langchain.agents import create_openai_functions_agent

agent = create_openai_functions_agent(llm, tools, prompt)
最后,将 Agent(大脑)与工具组合到 AgentExecutor 中(它会反复调用 Agent 并执行工具)。更多信息请参阅我们的概念指南
from langchain.agents import AgentExecutor

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

运行 Agent

现在可以对 Agent 执行几个查询了!注意,目前这些都是无状态查询(不会记住之前的交互)。
agent_executor.invoke({"input": "hi!"})
> Entering new AgentExecutor chain...
Hello! How can I assist you today?

> Finished chain.
{'input': 'hi!', 'output': 'Hello! How can I assist you today?'}
agent_executor.invoke({"input": "how many calories are in a slice pepperoni pizza?"})
如果想自动跟踪这些消息,可以将其封装在 RunnableWithMessageHistory 中。
agent_executor.invoke(
    {"input": "I had bacon and eggs for breakfast.  How many calories is that?"}
)
agent_executor.invoke(
    {
        "input": "I had sliced pepper jack cheese for a snack.  How much protein did I have?"
    }
)
agent_executor.invoke(
    {
        "input": "I had sliced colby cheese for a snack. Give me calories for this Schnuck Markets product."
    }
)
agent_executor.invoke(
    {
        "input": "I had chicken tikka masala for dinner.  how much calories, protein, and fat did I have with default quantity?"
    }
)

总结

本快速入门介绍了如何创建一个简单的 Agent,使其能够在回答中融入食物营养信息。Agent 是一个复杂的话题,还有很多内容值得深入学习!