赞
踩
本文翻译整理自:Build an Agent
https://python.langchain.com/v0.2/docs/tutorials/agents/
语言模型本身无法采取行动——它们只是输出文本。 LangChain 的一个重要用例是创建代理。
代理是使用 LLM 作为推理工程师 来确定 要采取哪些操作,以及这些操作的输入应该是什么的系统。
然后,这些操作的结果可以反馈给代理,并确定是否需要更多操作,或者是否可以完成。
在本教程中,我们将构建一个可以与多种不同工具交互的代理:一个是本地数据库,另一个是搜索引擎。
您将能够向该代理询问问题、观看它调用工具并与其进行对话。
我们将涵盖的概念是:
项目设置可参考:https://blog.csdn.net/lovechris00/article/details/139130091#_33
我们首先需要创建要使用的工具。我们将使用两个工具:Tavily(用于在线搜索),以及我们将创建的本地索引检索器
https://python.langchain.com/v0.2/docs/integrations/tools/tavily_search/
我们在 LangChain 中有一个内置工具,可以轻松使用 Tavily 搜索引擎作为工具。
请注意,这需要 API 密钥 - 他们有一个免费套餐,但如果您没有或不想创建一个,您可以随时忽略此步骤。
创建 API 密钥后,您需要将其导出为:
export TAVILY_API_KEY="..."
from langchain_community.tools.tavily_search import TavilySearchResults
search = TavilySearchResults(max_results=2)
search.invoke("what is the weather in SF")
[{'url': 'https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US',
'content': "Comfy & Cozy\nThat's Not What Was Expected\nOutside\n'No-Name Storms' In Florida\nGifts From On High\nWhat To Do For Wheezing\nSurviving The Season\nStay Safe\nAir Quality Index\nAir quality is considered satisfactory, and air pollution poses little or no risk.\n Health & Activities\nSeasonal Allergies and Pollen Count Forecast\nNo pollen detected in your area\nCold & Flu Forecast\nFlu risk is low in your area\nWe recognize our responsibility to use data and technology for good. recents\nSpecialty Forecasts\n10 Day Weather-San Francisco, CA\nToday\nMon 18 | Day\nConsiderable cloudiness. Tue 19\nTue 19 | Day\nLight rain early...then remaining cloudy with showers in the afternoon. Wed 27\nWed 27 | Day\nOvercast with rain showers at times."},
{'url': 'https://www.accuweather.com/en/us/san-francisco/94103/hourly-weather-forecast/347629',
'content': 'Hourly weather forecast in San Francisco, CA. Check current conditions in San Francisco, CA with radar, hourly, and more.'}]
我们还将针对我们自己的一些数据创建一个检索器。
有关此处每个步骤的更详细说明,请参阅本教程。
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
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, OpenAIEmbeddings())
retriever = vector.as_retriever()
retriever.invoke("how to upload a dataset")[0]
Document(page_content='import Clientfrom langsmith.evaluation import evaluateclient = Client()# Define dataset: these are your test casesdataset_name = "Sample Dataset"dataset = client.create_dataset(dataset_name, description="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":', metadata={'source': 'https://docs.smith.langchain.com/overview', 'title': 'Getting started with LangSmith | 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/687796
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。