当前位置:   article > 正文

LangChain学习之Agent的相关操作

LangChain学习之Agent的相关操作

1.学习背景

在LangChain for LLM应用程序开发中课程中,学习了LangChain框架扩展应用程序开发中语言模型的用例和功能的基本技能,遂做整理为后面的应用做准备。视频地址:基于LangChain的大语言模型应用开发+构建和评估

2.Agent的学习和使用

有时LLM可能会使用它从互联网上学习的这些背景知识,但是也会利用你提供的新信息来帮助你回答问题,或者推理内容,甚至决定接下来要做什么。因此,如何创建自己的工具,可以让代理与任何数据存储、API、或者功能进行互动等等,如此有趣的功能,用起来才是强大!开始入门学习,以下代码基于jupyternotebook运行。

2.1 使用Langchain内置工具,先环境准备

import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file

import warnings
warnings.filterwarnings("ignore")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
from langchain.agents.agent_toolkits import create_python_agent
from langchain.agents import load_tools, initialize_agent
from langchain.agents import AgentType
from langchain.tools.python.tool import PythonREPLTool
from langchain.python import PythonREPL
from langchain.chat_models import ChatOpenAI
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

接下来如果你的环境版本较高,会出现如下报错:

ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 from langchain.agents.agent_toolkits import create_python_agent
      2 from langchain.agents import load_tools, initialize_agent
      3 from langchain.agents import AgentType

File <frozen importlib._bootstrap>:1075, in _handle_fromlist(module, fromlist, import_, recursive)
...

File ~/.python/current/lib/python3.10/site-packages/langchain/agents/agent_toolkits/__init__.py:120, in __getattr__(name)
    118 """Get attr name."""
    119 if name in DEPRECATED_AGENTS:
--> 120     relative_path = as_import_path(Path(__file__).parent, suffix=name)
    121     old_path = "langchain." + relative_path
    122     new_path = "langchain_experimental." + relative_path
ValueError: '/home/codespace/.python/current/lib/python3.10/site-packages/langchain/agents/agent_toolkits' is not in the subpath of '/home/codespace/.python/current/lib/python3.10/site-packages/langchain_core' OR one path is relative and the other is absolute.
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这是因为最新版的Agent模块已调整至experimental部分,因此导入包需要调整如下:

 !pip install langchain_experimental
  • 1
# 注意前两个包导入的位置
from langchain_experimental.agents.agent_toolkits import create_python_agent
from langchain_experimental.tools.python.tool import PythonREPLTool
from langchain.python import PythonREPL
from langchain.agents import load_tools, initialize_agent
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.2 尝试使用agent

llm = ChatOpenAI(temperature=0)
# 工具类里使用math工具和wikipedia工具
tools = load_tools(["llm-math","wikipedia"], llm=llm)
# 调用initialize_agent初始化
agent= initialize_agent(
    tools, # 使用预置的tools
    llm, 
    agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, # 使用chatreactAgent
    handle_parsing_errors=True, # 处理出错设置为True可以有效规避莫名bug
    verbose = True) # 开启日志打印
agent("What is the 25% of 300?")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

输出如下:

> Entering new AgentExecutor chain...
Thought: We can use the calculator tool to find 25% of 300.
Action:
\```
{
  "action": "Calculator",
  "action_input": "25% of 300"
}
\```
Observation: Answer: 75.0
Thought:I now know the final answer
Final Answer: 75.0

> Finished chain.
{'input': 'What is the 25% of 300?', 'output': '75.0'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在尝试问个问题,让chain自行调用wikipedia

question = "Tom M. Mitchell is an American computer scientist \
and the Founders University Professor at Carnegie Mellon University (CMU)\
what book did he write?"
result = agent(question) 
  • 1
  • 2
  • 3
  • 4

输出如下:

> Entering new AgentExecutor chain...
Thought: I should use Wikipedia to find out which book Tom M. Mitchell wrote.
Action:
\```
{
  "action": "wikipedia",
  "action_input": "Tom M. Mitchell"
}
\```
Observation: Page: Tom M. Mitchell
Summary: Tom Michael Mitchell (born August 9, 1951) is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU). He is a founder and former Chair of the Machine Learning Department at CMU. Mitchell is known for his contributions to the advancement of machine learning, artificial intelligence, and cognitive neuroscience and is the author of the textbook Machine Learning. He is a member of the United States National Academy of Engineering since 2010. He is also a Fellow of the American Academy of Arts and Sciences, the American Association for the Advancement of Science and a Fellow and past President of the Association for the Advancement of Artificial Intelligence. In October 2018, Mitchell was appointed as the Interim Dean of the School of Computer Science at Carnegie Mellon.

Page: Tom Mitchell (Australian footballer)
Summary: Thomas Mitchell (born 31 May 1993) is a professional Australian rules footballer playing for the Collingwood Football Club in the Australian Football League (AFL). He previously played for the Sydney Swans from 2012 to 2016, and the Hawthorn Football Club between 2017 and 2022. Mitchell won the Brownlow Medal as the league's best and fairest player in 2018 and set the record for the most disposals in a VFL/AFL match, accruing 54 in a game against Collingwood during that season. He would later join them in 2023, en route to winning the 2023 AFL Grand Final and his first AFL premiership.
Thought:I have found out that Tom M. Mitchell wrote the textbook "Machine Learning."

Final Answer: Machine Learning

> Finished chain.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

可以看到模型的action使用了 “wikipedia”。

2.3 尝试Python Agent

agent = create_python_agent(
    llm,
    tool=PythonREPLTool(), # 该工具可以理解为外置可运行代码的jupyternotebook
    verbose=True
)
# 给人名的List,对其进行排序
customer_list = [["Harrison", "Chase"], 
                 ["Lang", "Chain"],
                 ["Dolly", "Too"],
                 ["Elle", "Elem"], 
                 ["Geoff","Fusion"], 
                 ["Trance","Former"],
                 ["Jen","Ayai"]
                ]
agent.run(f"""Sort these customers by \
last name and then first name \
and print the output: {customer_list}""") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

输出如下:

> Entering new AgentExecutor chain...
Python REPL can execute arbitrary code. Use with caution.
We can use the `sorted()` function in Python to sort the list of customers based on their last name and then first name.
Action: Python_REPL
Action Input: sorted([['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']], key=lambda x: (x[1], x[0]))
Observation: 
Thought:I now know the final answer
Final Answer: [['Jen', 'Ayai'], ['Harrison', 'Chase'], ['Lang', 'Chain'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Dolly', 'Too']]

> Finished chain.
"[['Jen', 'Ayai'], ['Harrison', 'Chase'], ['Lang', 'Chain'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Dolly', 'Too']]"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

可以看到Action使用了Python_REPL工具,在解决任务过程中,使用了sorted()函数。

2.4 打开debug查看细节

import langchain
langchain.debug=True
agent.run(f"""Sort these customers by \
last name and then first name \
and print the output: {customer_list}""") 
langchain.debug=False
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出如下:

[chain/start] [chain:AgentExecutor] Entering Chain run with input:
{
  "input": "Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]"
}
[chain/start] [chain:AgentExecutor > chain:LLMChain] Entering Chain run with input:
{
  "input": "Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]",
  "agent_scratchpad": "",
  "stop": [
    "\nObservation:",
    "\n\tObservation:"
  ]
}
[llm/start] [chain:AgentExecutor > chain:LLMChain > llm:ChatOpenAI] Entering LLM run with input:
{
  "prompts": [
    "Human: You are an agent designed to write and execute python code to answer questions.\nYou have access to a python REPL, which you can use to execute python code.\nIf you get an error, debug your code and try again.\nOnly use the output of your code to answer the question. \nYou might know the answer without running any code, but you should still run the code to get the answer.\nIf it does not seem like you can write code to answer the question, just return \"I don't know\" as the answer.\n\n\nPython_REPL - A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Python_REPL]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]\nThought:"
  ]
}
[llm/end] [chain:AgentExecutor > chain:LLMChain > llm:ChatOpenAI] [4.28s] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "We can use the `sorted()` function in Python to sort the list of customers based on last name first and then first name.\nAction: Python_REPL\nAction Input: sorted([['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']], key=lambda x: (x[1], x[0]))",
...
[chain/end] [chain:AgentExecutor] [9.23s] Exiting Chain run with output:
{
  "output": "[['Jen', 'Ayai'], ['Harrison', 'Chase'], ['Lang', 'Chain'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Dolly', 'Too']]"
}
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

3. 尝试使用自己写的tools

from langchain.agents import tool
from datetime import date


# 定义tool工具,使用@tool装饰器完成,注意一定要写函数的功能细节描述,因为LLM在执行过程中要根据注释选择使用的工具
@tool
def time(text: str) -> str:
    """Returns todays date, use this for any \
    questions related to knowing todays date. \
    The input should always be an empty string, \
    and this function will always return todays \
    date - any date mathmatics should occur \
    outside this function."""
    return str(date.today())

agent= initialize_agent(
    tools + [time], # 往工具类列表中增加新增的time工具
    llm, 
    agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    handle_parsing_errors=True,
    verbose = True)

# 为了避免奇怪的输出或者错误,使用try,except运行
try:
    result = agent("whats the date today?") 
except: 
    print("exception on external access")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

输出如下:

> Entering new AgentExecutor chain...
Thought: I should use the `time` tool to get today's date.
Action:
\```
{
  "action": "time",
  "action_input": ""
}
\```

Observation: 2024-06-05
Thought:Final Answer: 2024-06-05

> Finished chain.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

可以看到,在已有的工具类中,Agent调用time工具,输出了最终的结果。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/687732
推荐阅读
相关标签
  

闽ICP备14008679号