Passio 营养 AI
为了更好地理解NutritionAI如何为您的智能体赋予强大的食物营养能力,让我们构建一个能够通过Passio NutritionAI获取该信息的智能体。
定义工具
我们首先需要创建Passio NutritionAI工具。
Passio 营养 AI
我们已经在LangChain中内置了一个工具,可以轻松使用Passio NutritionAI来查找食物营养信息。 请注意,这需要一个API密钥——他们有一个免费套餐。
一旦您创建了 API 密钥,您需要将其导出为:
export NUTRITIONAI_SUBSCRIPTION_KEY="..."
... 或者通过其他方式将其提供给您的Python环境,例如使用dotenv包。您还可以通过构造函数调用显式控制键。
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]
创建智能体
现在我们已经定义了工具,接下来可以创建代理了。我们将使用一个OpenAI Functions代理 - 如需了解这种类型的代理以及其它选项,请参见这份指南
首先,我们选择想要引导代理的大型语言模型。
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
接下来,我们选择用于引导代理的提示。
from langchain import hub
# Get the prompt to use - you can modify this!
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、提示和工具来初始化代理。代理负责接收输入并决定采取什么行动。至关重要的是,代理不会执行这些操作——这由AgentExecutor(下一步)完成。要了解这些组件的思考方式,请参阅我们的概念指南。
from langchain.agents import create_openai_functions_agent
agent = create_openai_functions_agent(llm, tools, prompt)
最终,我们将代理(大脑)与AgentExecutor内的工具结合在一起(AgentExecutor将会重复调用代理并执行工具)。有关这些组件的思考方式的更多信息,请参阅我们的概念性指南。
from langchain.agents import AgentExecutor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
运行智能体
我们现在可以在一些查询上运行代理了!请注意,目前这些都是无状态查询(它不会记住之前的交互)。
agent_executor.invoke({"input": "hi!"})
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mHello! How can I assist you today?[0m
[1m> Finished chain.[0m
{'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?"
}
)
结论
那这就结束了!在本快速入门中,我们介绍了如何创建一个简单的代理,使其能够将食物营养信息纳入其回答。代理是一个复杂的主题,还有很多东西需要学习!