AI-创建一个Agent

语言模型本身无法采取行动——它们只是输出文本。 LangChain 的一个重要用例是创建Agent。代理是使用 LLM 作为reasoning engine来确定要采取哪些操作以及这些操作的输入应该是什么的系统。然后,这些操作的结果可以反馈给Agent,并确定是否需要更多操作,或者是否可以完成。

step1 定义工具

首先需要创建要使用的工具。我们将使用两个工具:Tavily(用于在线搜索)以及我们将创建的本地索引检索器
创建在线搜索工具

from langchain_community.tools.tavily_search import TavilySearchResults

search = TavilySearchResults(max_results=2)
print(search.invoke("what is the weather in SF"))
print(search.name,search.description)

输出结果为

[{‘url’: ‘http://www.sfchronicle.com/weather/article/bay-area-wind-rain-19423254.php’, ‘content’: “Sunday’s highs will be in the middle to upper 70s in the Sacramento and San Joaquin Valley and cooler near the coast with highs in the near 60 degrees. Family of East Bay teen desperately searching for answers Tech workers leaving San Francisco have a favorite destination — and it isn’t Austin Michelin Guide’s debut hotel rankings include these Bay Area gems What to eat in Marin’s quaint towns near Mount Tamalpais Yosemite just finished several big visitor projects. Strong gusts to sail through the Bay Area — chance of light rain in some regions Weather models predict strong winds throughout the Bay Area on Friday night, with gusts potentially over 30 mph. Email: weatherwonks@sfchronicle.com Advertisement Article continues below this ad Most Popular Tech workers leaving San Francisco have a favorite destination — and it isn’t Austin Thieves snatch Rep. Adam Schiff’s luggage in S.F. South Bay and Santa Cruz:\xa0Partly to mostly cloudy in Boulder Creek, San Jose, Santa Clara Valley and Milpitas, with highs in the middle 60s.”}, {‘url’: ‘http://www.sfchronicle.com/weather-forecast/article/heat-warm-northern-california-19440071.php’, ‘content’: 'She’s part of an epidemic at elite colleges California tourism recovers from pandemic and S.F. is almost there, new report finds S.F. crime is plummeting, with the exception of one affluent neighborhood One of California’s fastest-growing cities is in the Bay Area Big Sur Bakery, community icon and tourism hot spot, destroyed in fire About Contact Services Account Lows will be in the upper 40s to near 50. Pacific Coast and Peninsula: More sun than clouds is expected along the Peninsula, but some fog may attempt to spread toward the coast in the late afternoon and evening. She’s part of an epidemic at elite colleges One of California’s fastest-growing cities is in the Bay Area California doesn’t have an ‘exit tax’ — but it can still tax some people who move away Fatal shooting near Oakland’s Lake Merritt Top of the News A white UC Berkeley prof built her career after saying she was Native. He joins the Chronicle from the University of Washington where he was previously the president of the campus weather forecasting team and an editor at the student newspaper, The Daily UW. Advertisement Article continues below this ad High-pressure systems in May often result in 70-degree temperatures near the bay shoreline and 80s inland, but there is a fly in the ointment this week. '}]
tavily_search_results_json A search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events. Input should be a search query.

创建本地本地索引检索器工具

from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI
from langchain_text_splitters import RecursiveCharacterTextSplitter

#加载一篇blog作为本地知识库
loader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()

#对知识库进行拆分
documents = RecursiveCharacterTextSplitter(
    chunk_size=1000,chunk_overlap=200
).split_documents(docs)

#加载检索引擎
vector = FAISS.from_documents(documents,embed_model)
retriver = vector.as_retriever()

from langchain.tools.retriever import create_retriever_tool

#创建检索工具
retriever_tool = create_retriever_tool(
    retriver,
    "langsmith_search",
    "Search for information about LangSmith. For any questions about LangSmith, you must use this tool!",
    )

tools = [search,retriever_tool]

step2 创建语言

创建聊天模型

from langchain_openai import ChatOpenAI

chat = ChatOpenAI()

from langchain_core.messages import HumanMessage

chat.invoke([HumanMessage(content="hi")])

上面输出结果为:

AIMessage(content=‘Hello! How can I assist you today?’, response_metadata={‘token_usage’: {‘completion_tokens’: 9, ‘prompt_tokens’: 8, ‘total_tokens’: 17}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: None, ‘finish_reason’: ‘stop’, ‘logprobs’: None}, id=‘run-f4f7e659-a9a9-4291-baad-c6046d41731a-0’)

模型绑定工具

chat_with_tools = chat.bind_tools(tools)

response = chat_with_tools.invoke([HumanMessage(content="Hi!")])
print(response.content)
print(response.tool_calls)

结果输出:

Hello! How can I assist you today?
[]

发现上面模型并没有调用任何工具

response = chat_with_tools.invoke([HumanMessage(content="What's the weather in SF?")])

print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")

结果输出

ContentString:
ToolCalls: [{‘name’: ‘tavily_search_results_json’, ‘args’: {‘query’: ‘weather in San Francisco’}, ‘id’: ‘call_GeMFc9TMqDAAwBN1S22dz75b’}]

我们可以看到现在没有内容,但是有一个工具调用!它希望我们调用 Tavily Search 工具。这还不是调用该工具 - 它只是告诉我们要调用它。为了真正调用它,我们需要创建我们的代理。

step3创建agent

们已经定义了工具和 LLM,我们可以创建代理了。我们将使用LangGraph来构建代理。

from langgraph.prebuilt import chat_agent_executor
agent_executor = chat_agent_executor.create_function_calling_executor(chat,tools)

现在我们可以对几个查询运行agent了!请注意,目前这些都是无状态查询(它不会记住以前的交互)。请注意,代理将在交互结束时返回最终状态
没有调用任何工具

response = agent_executor.invoke({"messages": [HumanMessage(content="hi!")]})

response

输出结果;

{‘messages’: [HumanMessage(content=‘hi!’, id=‘588a7a56-a6f1-46dc-ad52-4984e7c5ed9b’),
AIMessage(content=‘Hello! How can I assist you today?’, response_metadata={‘token_usage’: {‘completion_tokens’: 10, ‘prompt_tokens’: 129, ‘total_tokens’: 139}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: ‘fp_2f57f81c11’, ‘finish_reason’: ‘stop’, ‘logprobs’: None}, id=‘run-fa5a1b2a-48de-4fe0-b5ee-b04f4056edc0-0’)]}

调用本地搜索引擎工具

response = agent_executor.invoke({
    "messages":[HumanMessage(content="how can langsmith help with testing")]
})
response['messages']

[HumanMessage(content=‘how can langsmith help with testing’, id=‘77e7875b-5f82-4db6-bd55-5750821b1936’),
AIMessage(content=‘’, additional_kwargs={‘function_call’: {‘arguments’: ‘{“query”:“how can LangSmith help with testing”}’, ‘name’: ‘langsmith_search’}}, response_metadata={‘token_usage’: {‘completion_tokens’: 21, ‘prompt_tokens’: 134, ‘total_tokens’: 155}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: None, ‘finish_reason’: ‘function_call’, ‘logprobs’: None}, id=‘run-d1400df3-a74c-4126-a9b0-b814c94bc988-0’),
FunctionMessage(content=‘Skip to main contentLangSmith API DocsSearchGo to AppQuick startTutorialsHow-to guidesConceptsReferencePricingSelf-hostingQuick startOn this pageGet started with LangSmithLangSmith is a platform for building production-grade LLM applications. It allows you to closely monitor and evaluate your application, so you can ship quickly and with confidence. Use of LangChain is not necessary - LangSmith works on its own!1. Install LangSmith\u200bPythonTypeScriptpip install -U langsmithyarn add langchain langsmith2. Create an API key\u200bTo create an API key head to the Settings page. Then click Create API Key.3. Set up your environment\u200bShellexport LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=# The below examples use the OpenAI API, though it’s not necessary in generalexport OPENAI_API_KEY=4. Log your first trace\u200bWe provide multiple ways to log traces to LangSmith. Below, we’ll highlight\n\nGet started with LangSmith | 🦜️🛠️ LangSmith\n\ndescription=“A sample dataset in LangSmith.”)client.create_examples( inputs=[ {“postfix”: “to LangSmith”}, {“postfix”: “to Evaluations in LangSmith”}, ], outputs=[ {“output”: “Welcome to LangSmith”}, {“output”: “Welcome to Evaluations in LangSmith”}, ], dataset_id=dataset.id,)# Define your evaluatordef exact_match(run, example): return {“score”: run.outputs[“output”] == example.outputs[“output”]}experiment_results = evaluate( lambda input: “Welcome " + input[‘postfix’], # Your AI system goes here data=dataset_name, # The data to predict and grade over evaluators=[exact_match], # The evaluators to score the results experiment_prefix=“sample-experiment”, # The name of the experiment metadata={ “version”: “1.0.0”, “revision_id”: “beta” },)import { Client, Run, Example } from “langsmith”;import { evaluate } from “langsmith/evaluation”;import { EvaluationResult } from “langsmith/evaluation”;const client = new\n\n"revision_id”: “beta” },)import { Client, Run, Example } from “langsmith”;import { evaluate } from “langsmith/evaluation”;import { EvaluationResult } from “langsmith/evaluation”;const client = new Client();// Define dataset: these are your test casesconst datasetName = “Sample Dataset”;const dataset = await client.createDataset(datasetName, { description: “A sample dataset in LangSmith.”,});await client.createExamples({ inputs: [ { postfix: “to LangSmith” }, { postfix: “to Evaluations in LangSmith” }, ], outputs: [ { output: “Welcome to LangSmith” }, { output: “Welcome to Evaluations in LangSmith” }, ], datasetId: dataset.id,});// Define your evaluatorconst exactMatch = async ( run: Run, example: Example): Promise => { return { key: “exact_match”, score: run.outputs?.output === example?.outputs?.output, };};await evaluate( (input: { postfix: string }) => ({ output: Welcome ${input.postfix} }), { data: datasetName, evaluators:’, name=‘langsmith_search’, id=‘718fa3a8-7cc2-49ca-b588-312b4956e019’),
AIMessage(content=“Sorry, I couldn’t find a specific answer to your question on LangSmith’s website. However, LangSmith is a platform for building production-grade LLM (Language Model Monitoring) applications and allows you to closely monitor and evaluate your application, so you can ship quickly and with confidence. It provides tools for tracing and logging model behavior, evaluating model performance, and automating model deployment. So, it can help with testing by providing insights into model behavior and performance.”, response_metadata={‘token_usage’: {‘completion_tokens’: 94, ‘prompt_tokens’: 883, ‘total_tokens’: 977}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: None, ‘finish_reason’: ‘stop’, ‘logprobs’: None}, id=‘run-bb1a25f6-10e4-42b7-b23c-dff58f31cb9f-0’)]

调用在线搜索工具

response = agent_executor.invoke(
    {
        "messages":[HumanMessage(content="what's ht weather in sf")]
    }
)

response['messages']

[HumanMessage(content=“what’s ht weather in sf”, id=‘9f821690-0b32-4073-ac5b-0e2f90c9e042’),
AIMessage(content=‘’, additional_kwargs={‘function_call’: {‘arguments’: ‘{\n “query”: “weather in San Francisco”\n}’, ‘name’: ‘tavily_search_results_json’}}, response_metadata={‘token_usage’: {‘completion_tokens’: 22, ‘prompt_tokens’: 133, ‘total_tokens’: 155}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: None, ‘finish_reason’: ‘function_call’, ‘logprobs’: None}, id=‘run-3fb71b64-af37-4937-9d67-b78075840bf3-0’),
FunctionMessage(content='[{‘url’: ‘http://www.sfchronicle.com/weather-forecast/article/sf-bay-area-heat-19468160.php’, ‘content’: 'Advertisement Article continues below this ad Highs will reach the mid-60s in the Richmond and Sunset districts, upper 60s in the Presidio and the low 70s in Pacific Heights, Glen Park, the Marina District and the Embarcadero. He joins the Chronicle from the University of Washington where he was previously the president of the campus weather forecasting team and an editor at the student newspaper, The Daily UW. It will be warmer away from the beaches, in the low 70s in San Bruno and South San Francisco, the mid-70s in San Mateo and near 80 in Redwood City and Menlo Park. Tuesday will likely be the city’s warmest day since May 10, when the high was 78. Advertisement Article continues below this ad Inland portions of the North Bay, East Bay and South Bay will be even hotter. The sea breeze should become stronger as the week progresses, and Thursday afternoon may be particularly windy through the Golden Gate, along the Peninsula, near the delta and over Altamont Pass. ‘}, {‘url’: ‘http://www.sfchronicle.com/weather/article/bay-area-wind-rain-19423254.php’, ‘content’: “Sunday’s highs will be in the middle to upper 70s in the Sacramento and San Joaquin Valley and cooler near the coast with highs in the near 60 degrees. Family of East Bay teen desperately searching for answers Tech workers leaving San Francisco have a favorite destination — and it isn’t Austin Michelin Guide’s debut hotel rankings include these Bay Area gems What to eat in Marin’s quaint towns near Mount Tamalpais Yosemite just finished several big visitor projects. Strong gusts to sail through the Bay Area — chance of light rain in some regions Weather models predict strong winds throughout the Bay Area on Friday night, with gusts potentially over 30 mph. Email: weatherwonks@sfchronicle.com Advertisement Article continues below this ad Most Popular Tech workers leaving San Francisco have a favorite destination — and it isn’t Austin Thieves snatch Rep. Adam Schiff’s luggage in S.F. South Bay and Santa Cruz:\xa0Partly to mostly cloudy in Boulder Creek, San Jose, Santa Clara Valley and Milpitas, with highs in the middle 60s.”}]’, name=‘tavily_search_results_json’, id=‘1d5845b3-0512-4cf9-b8f3-75105e0707a0’),
AIMessage(content=‘The weather in San Francisco is currently in the mid-60s in the Richmond and Sunset districts, upper 60s in the Presidio, and low 70s in Pacific Heights, Glen Park, the Marina District, and the Embarcadero. It will be warmer away from the beaches, with temperatures in the low 70s in San Bruno and South San Francisco, mid-70s in San Mateo, and near 80 in Redwood City and Menlo Park. Tuesday is expected to be the warmest day since May 10, with temperatures reaching the mid-70s in some areas. Inland portions of the North Bay, East Bay, and South Bay will be even hotter. The sea breeze is expected to become stronger as the week progresses.’, response_metadata={‘token_usage’: {‘completion_tokens’: 159, ‘prompt_tokens’: 665, ‘total_tokens’: 824}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: None, ‘finish_reason’: ‘stop’, ‘logprobs’: None}, id=‘run-02565051-f367-4e4a-a70d-5f2fdc856511-0’)]

如果代理正在执行多个步骤,则可能需要一段时间。为了显示中间进度,我们可以在消息发生时流回消息。

for chunk in agent_executor.stream({"messages": [HumanMessage(content="whats the weather in sf?")]}):
    print(chunk)
    print("##"*20)

输出结果为

{‘agent’: {‘messages’: [AIMessage(content=‘’, additional_kwargs={‘function_call’: {‘arguments’: ‘{“query”:“weather in San Francisco”}’, ‘name’: ‘tavily_search_results_json’}}, response_metadata={‘token_usage’: {‘completion_tokens’: 21, ‘prompt_tokens’: 134, ‘total_tokens’: 155}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: ‘fp_2f57f81c11’, ‘finish_reason’: ‘function_call’, ‘logprobs’: None}, id=‘run-dcee4a2a-623b-49d5-8302-901ce9330cc5-0’)]}}
########################################
{‘tools’: {‘messages’: [FunctionMessage(content='[{‘url’: ‘http://www.sfchronicle.com/weather-forecast/article/sf-bay-area-heat-19468160.php’, ‘content’: 'Advertisement Article continues below this ad Highs will reach the mid-60s in the Richmond and Sunset districts, upper 60s in the Presidio and the low 70s in Pacific Heights, Glen Park, the Marina District and the Embarcadero. He joins the Chronicle from the University of Washington where he was previously the president of the campus weather forecasting team and an editor at the student newspaper, The Daily UW. It will be warmer away from the beaches, in the low 70s in San Bruno and South San Francisco, the mid-70s in San Mateo and near 80 in Redwood City and Menlo Park. Tuesday will likely be the city’s warmest day since May 10, when the high was 78. Advertisement Article continues below this ad Inland portions of the North Bay, East Bay and South Bay will be even hotter. The sea breeze should become stronger as the week progresses, and Thursday afternoon may be particularly windy through the Golden Gate, along the Peninsula, near the delta and over Altamont Pass. ‘}, {‘url’: ‘http://www.sfchronicle.com/weather/article/bay-area-wind-rain-19423254.php’, ‘content’: “Sunday’s highs will be in the middle to upper 70s in the Sacramento and San Joaquin Valley and cooler near the coast with highs in the near 60 degrees. Family of East Bay teen desperately searching for answers Tech workers leaving San Francisco have a favorite destination — and it isn’t Austin Michelin Guide’s debut hotel rankings include these Bay Area gems What to eat in Marin’s quaint towns near Mount Tamalpais Yosemite just finished several big visitor projects. Strong gusts to sail through the Bay Area — chance of light rain in some regions Weather models predict strong winds throughout the Bay Area on Friday night, with gusts potentially over 30 mph. Email: weatherwonks@sfchronicle.com Advertisement Article continues below this ad Most Popular Tech workers leaving San Francisco have a favorite destination — and it isn’t Austin Thieves snatch Rep. Adam Schiff’s luggage in S.F. South Bay and Santa Cruz:\xa0Partly to mostly cloudy in Boulder Creek, San Jose, Santa Clara Valley and Milpitas, with highs in the middle 60s.”}]’, name=‘tavily_search_results_json’, id=‘addc1822-35a3-4a1f-909e-baa493ff291c’)]}}
########################################
{‘agent’: {‘messages’: [AIMessage(content=“The weather in San Francisco is currently in the mid-60s in the Richmond and Sunset districts, upper 60s in the Presidio, and low 70s in Pacific Heights, Glen Park, the Marina District, and the Embarcadero. It will be warmer away from the beaches, with temperatures in the low 70s in San Bruno and South San Francisco, mid-70s in San Mateo, and near 80 in Redwood City and Menlo Park. Tuesday is expected to be the city’s warmest day since May 10. Inland portions of the North Bay, East Bay, and South Bay will be even hotter. The sea breeze is expected to become stronger as the week progresses.”, response_metadata={‘token_usage’: {‘completion_tokens’: 149, ‘prompt_tokens’: 666, ‘total_tokens’: 815}, ‘model_name’: ‘gpt-3.5-turbo’, ‘system_fingerprint’: None, ‘finish_reason’: ‘stop’, ‘logprobs’: None}, id=‘run-d06c06fd-52ea-4a63-b459-ce4a380f3563-0’)]}}
########################################

  • 12
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值