开源模型应用落地-LangChain高阶-Tools工具-集成agents(四)

发布于:2024-05-07 ⋅ 阅读:(54) ⋅ 点赞:(0)

一、前言

    LangChain 的 tools 是一系列关键组件,它们提供了与外部世界进行交互的能力。通过适当的使用这些组件,可以简单实现如执行网络搜索以获取最新信息、调用特定的 API 来获取数据或执行特定的操作、与数据库进行交互以获取存储的信息等需求。

    本章基于agents进一步串联工具(tools ),从而将大语言模型的能力和本地、云服务能力结合。


二、术语

2.1. agent

    是 LangChain 中的代理模块,它可以使用语言模型(LLM)动态地调用行为链(Chains),根据用户的输入调用不同的行为。代理可以访问单一工具,并根据用户输入确定要使用的工具,也可以使用多个工具,并使用一个工具的输出作为下一个工具的输入。


三、前提条件 

3.1. 基础环境

  1.  操作系统:不限

3.2. 安装虚拟环境

conda create --name langchain python=3.10
conda activate langchain
pip install langchain langchain-openai

3.3. 创建Wolfram账号

开源模型应用落地-LangChain高阶-Tools工具-WolframAlpha(二)

3.4. 创建serper账号

开源模型应用落地-LangChain高阶-Tools工具-GoogleSerperAPIWrapper(三)


四、技术实现

4.1.询问广州白云山位置

# -*-  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}')

调用结果:

4.2.求解数学表达式

# -*-  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}')

    

调用结果:


五、附带说明

5.1.AgentType取值

  • 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 框架,它可以根据对话的上下文和需求选择合适的工具来执行任务,并将工具执行的结果作为上下文反馈给代理,以便其继续进行推理和回答。

5.2.Agent的执行流程

  1. 接收用户输入:接收用户的输入,并将其作为执行的起点。
  2. 规划动作:根据用户输入和当前状态,agent 会规划下一步的动作。这可能包括选择使用哪个工具、确定工具的输入等。
  3. 执行动作:使用所选的工具执行动作,并记录动作的结果。
  4. 处理结果:处理动作的结果,并根据结果决定下一步的动作。
  5. 重复步骤:不断重复上述步骤,直到达到最终的目标或满足特定的条件。

    注意:具体的执行流程可能因 agent 的类型和配置而有所不同。

5.3.注意事项

  1. 工具选择和配置:要确保选择合适的工具,并正确配置它们。
  2. 输入处理:仔细处理用户输入,确保其清晰和准确。
  3. 工具依赖:注意工具之间的依赖关系,避免不必要的冲突。
  4. 性能和效率:关注执行过程中的性能和效率,优化可能的瓶颈。
  5. 错误处理:做好错误处理,应对可能出现的异常情况。
  6. 环境适应性:根据不同的应用场景,调整 Agent 的行为和策略。