AI全栈大模型工程师(十六)智能体架构:Agent

发布时间:2023年12月18日

五、智能体架构:Agent

5.1 什么是智能体(Agent)

将大语言模型作为一个推理引擎。给定一个任务,智能体自动生成完成任务所需的步骤,执行相应动作(例如选择并调用工具),直到任务完成。

5.2 先定义一些工具:Tools

  • 可以是一个函数或三方 API
  • 也可以把一个 Chain 或者 Agent 的 run()作为一个 Tool
from langchain import SerpAPIWrapper

search = SerpAPIWrapper()
tools = [
    Tool.from_function(
        func=search.run,
        name="Search",
        description="useful for when you need to answer questions about current events"
    ),
]


from langchain.tools import Tool, tool
import calendar
import dateutil.parser as parser
from datetime import date

@tool("weekday")
def weekday(date_str: str) -> str:
    """Convert date to weekday name"""
    d = parser.parse(date_str)
    return calendar.day_name[d.weekday()]


from langchain.agents import load_tools

tools = load_tools(["serpapi"])
tools += [weekday]


5.3 智能体类型:ReAct

!pip install google-search-results

from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
from langchain.agents import AgentType
from langchain.agents import initialize_agent

llm = ChatOpenAI(model_name=‘gpt-4’, temperature=0)

agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run(“周杰伦生日那天是星期几”)

5.4 通过 OpenAI Function Calling 实现智能体

from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
from langchain.agents import AgentType
from langchain.agents import initialize_agent

llm = ChatOpenAI(model_name=‘gpt-4-0613’, temperature=0)

agent = initialize_agent(
tools,
llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
max_iterations=2,
early_stopping_method=“generate”,
)
agent.run(“周杰伦生日那天是星期几”)

5.5 智能体类型:SelfAskWithSearch

from langchain import OpenAI, SerpAPIWrapper
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
tools = [
Tool(
name=“Intermediate Answer”,
func=search.run,
description=“useful for when you need to ask with search”,
)
]

self_ask_with_search = initialize_agent(
tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True
)
self_ask_with_search.run(
“冯小刚的老婆演过什么电影”
)

5.6 智能体类型:Plan-and-Execute

!pip install langchain-experimental

from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper
from langchain.agents import load_tools
from langchain import SerpAPIWrapper
from langchain.agents.tools import Tool
from langchain.llms import OpenAI
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationSummaryMemory

llm = ChatOpenAI(model_name=‘gpt-4’, temperature=0)

search = SerpAPIWrapper(params={
‘engine’: ‘google’,
‘gl’: ‘cn’,
‘google_domain’: ‘google.com.hk’,
‘hl’: ‘zh-cn’
})

tools = [
Tool(
name=“Search”,
func=search.run,
description=“useful for when you need to answer questions about current events”
)
]

planner = load_chat_planner(llm)
executor = load_agent_executor(llm, tools, verbose=True)
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)

agent.run(“分析北京明天天气,与上海明天天气对比,用中文写一遍报告”)

后记

📢博客主页:https://manor.blog.csdn.net

📢欢迎点赞 👍 收藏 ?留言 📝 如有错误敬请指正!
📢本文由 Maynor 原创,首发于 CSDN博客🙉
📢不能老盯着手机屏幕,要不时地抬起头,看看老板的位置?
📢专栏持续更新,欢迎订阅:https://blog.csdn.net/xianyu120/category_12471942.html

文章来源:https://blog.csdn.net/xianyu120/article/details/134161523
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。