赞
踩
LangChain 的 tools 是一系列关键组件,它们提供了与外部世界进行交互的能力。通过适当的使用这些组件,可以简单实现如执行网络搜索以获取最新信息、调用特定的 API 来获取数据或执行特定的操作、与数据库进行交互以获取存储的信息等需求。
本章基于agents进一步串联工具(tools ),从而将大语言模型的能力和本地、云服务能力结合。
是 LangChain 中的代理模块,它可以使用语言模型(LLM)动态地调用行为链(Chains),根据用户的输入调用不同的行为。代理可以访问单一工具,并根据用户输入确定要使用的工具,也可以使用多个工具,并使用一个工具的输出作为下一个工具的输入。
- conda create --name langchain python=3.10
- conda activate langchain
- pip install langchain langchain-openai
开源模型应用落地-LangChain高阶-Tools工具-WolframAlpha(二)
开源模型应用落地-LangChain高阶-Tools工具-GoogleSerperAPIWrapper(三)
- # -*- coding = utf-8 -*-
- import json
- import os
- import warnings
- import traceback
- from langchain.agents import initialize_agent, Tool, AgentType
- from langchain_community.utilities.wolfram_alpha import WolframAlphaAPIWrapper
- from langchain_openai import ChatOpenAI
- from langchain_community.utilities import GoogleSerperAPIWrapper
-
- warnings.filterwarnings("ignore")
-
- os.environ["SERPER_API_KEY"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- os.environ["WOLFRAM_ALPHA_APPID"] = "xxxxxx-xxxxxx"
-
- API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- os.environ["OPENAI_API_KEY"] = API_KEY
-
- def query_location(region):
- # print(f'region: {region}')
- search = GoogleSerperAPIWrapper(type="places")
- results = search.results(region)
- # print(f'results: {results}')
-
- try:
- places = results['places']
- # places_object = json.loads(places)
- if len(places) > 0:
- place = places[0]
- address = place['address']
- latitude = place['latitude']
- longitude = place['longitude']
- print(f'address: {address}, latitude: {latitude}, longitude: {longitude}')
-
- return address
- else:
- return 'unknown'
-
- except Exception as e:
- traceback.print_exc()
- return 'unknown'
-
- def mathematical_calculations(info):
- wolfram = WolframAlphaAPIWrapper()
- result = wolfram.run(info)
- return result
-
- tools = [
- Tool(name = "query_location",func=query_location,description="This function is used to query the location of a specified region, with the input parameter being the region"),
- Tool(name = "mathematical_calculations",func=mathematical_calculations,description="This function is used for mathematical calculations, and the input parameters are mathematical expressions")
- ]
-
- if __name__ == '__main__':
- llm = ChatOpenAI(model_name='gpt-3.5-turbo-1106', temperature=0.9, max_tokens=1024)
- agent = initialize_agent(tools,llm,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,max_iterations=3,verbose=True)
- result = agent.run('广州白云山在哪里?')
- print(f'result: {result}')
-
调用结果:
- # -*- coding = utf-8 -*-
- import json
- import os
- import warnings
- import traceback
- from langchain.agents import initialize_agent, Tool, AgentType
- from langchain_community.utilities.wolfram_alpha import WolframAlphaAPIWrapper
- from langchain_openai import ChatOpenAI
- from langchain_community.utilities import GoogleSerperAPIWrapper
-
- warnings.filterwarnings("ignore")
-
- os.environ["SERPER_API_KEY"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- os.environ["WOLFRAM_ALPHA_APPID"] = "xxxxxx-xxxxxx"
-
- API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- os.environ["OPENAI_API_KEY"] = API_KEY
-
- def query_location(region):
- # print(f'region: {region}')
- search = GoogleSerperAPIWrapper(type="places")
- results = search.results(region)
- # print(f'results: {results}')
-
- try:
- places = results['places']
- # places_object = json.loads(places)
- if len(places) > 0:
- place = places[0]
- address = place['address']
- latitude = place['latitude']
- longitude = place['longitude']
- print(f'address: {address}, latitude: {latitude}, longitude: {longitude}')
-
- return address
- else:
- return 'unknown'
-
- except Exception as e:
- traceback.print_exc()
- return 'unknown'
-
- def mathematical_calculations(info):
- wolfram = WolframAlphaAPIWrapper()
- result = wolfram.run(info)
- return result
-
- tools = [
- Tool(name = "query_location",func=query_location,description="This function is used to query the location of a specified region, with the input parameter being the region"),
- Tool(name = "mathematical_calculations",func=mathematical_calculations,description="This function is used for mathematical calculations, and the input parameters are mathematical expressions")
- ]
-
- if __name__ == '__main__':
- llm = ChatOpenAI(model_name='gpt-3.5-turbo-1106', temperature=0.9, max_tokens=1024)
- agent = initialize_agent(tools,llm,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,max_iterations=3,verbose=True)
-
- result = agent.run('求解:2x + 5 = -3x + 7')
- print(f'result: {result}')
-
调用结果:
AgentType.ZERO_SHOT_REACT_DESCRIPTION
表示零样本反应式描述代理,它利用 ReAct 框架根据工具的描述来决定使用哪个工具。这种代理可以使用多个工具,但需要为每个工具提供描述信息。工具的选择单纯依靠工具的描述信息。AgentType.SELF_ASK_WITH_SEARCH
表示 Self-Ask with Search 代理类型。这种代理使用一个名为“中间应答”的工具,该工具能够查找问题的真实答案。它的工作原理是利用网络搜索 API 进行搜索,并将搜索结果作为中间答案,然后继续进行提问和搜索,直到找到最终的答案。AgentType.REACT_DOCSTORE
使用 ReAct 框架与文档存储进行交互。适用于需要从文档存储中获取信息并进行处理的任务。通过使用“Search”和“Lookup”工具,它可以实现对文档的搜索和查找功能,帮助用户快速找到所需的信息。AgentType.CONVERSATIONAL_REACT_DESCRIPTION
主要用于对话场景。它使用 ReAct 框架来决定使用哪个工具,并使用内存来记忆先前的对话交互。这种代理类型的设计旨在使代理能够进行对话并提供帮助。通过使用 ReAct 框架,它可以根据对话的上下文和需求选择合适的工具来执行任务,并将工具执行的结果作为上下文反馈给代理,以便其继续进行推理和回答。
注意:具体的执行流程可能因 agent 的类型和配置而有所不同。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。