You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from openai import OpenAI
client = OpenAI
assistant = client.beta.assistants.create(
name="Math Tutor",
instructions="You are a personal math tutor. Write and run code to answer math questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-4o",
)
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)
from llama_index.core.tools import FunctionTool
from llama_index.llms.ollama import Ollama
from llama_index.core.agent import ReActAgent
from llama_index.core import Settings
from llm_service import llm
Settings.llm = llm
def multiply(a: int, b: int) -> int:
return a * b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
agent = ReActAgent.from_tools([multiply_tool], llm=llm, verbose=True)
接下来agent就可以与你对话了:
agent.chat("What is 2123 * 215123")
# 输出
> Running step 98361e05-06fa-4c53-9bb6-833c0f44a24b. Step input: what is 2123 * 215123
Thought: The user wants me to multiply two numbers, let's say A = 2123 and B = 215123. I will use the multiply tool from the previous step.
Action: multiply
Action Input: {'a': 2123, 'b': 215123}
Observation: 456706129
> Running step c457ef99-9d2e-44ef-95b3-bc199494277a. Step input: None
Thought: (Implicit) I can answer without any more tools!
Answer: Answer: The product of the numbers 2123 and 215123 is 456706129.
from langchain_core.messages import HumanMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.prebuilt import create_react_agent
from langchain_anthropic import ChatAnthropic
from langchain_community.tools.tavily_search import TavilySearchResults
# create the agent
memory = MemorySaver()
model = ChatAnthropic(model_name="claude-3-sonnet-20240229")
search = TavilySearchResults(max_results=2)
tools = [search]
agent_executor = create_react_agent(model, tools, checkpointer=memory)
# use the agent
config = {"configurable": {"thread_id": "abc123"}}
for chunk in agent_executor.stream(
{"messages": [HumanMessage(content="whats the weather where I live?")]}
):
print(chunk)
print("----")
什么是 AI Agent
AI Agent 是可以感知环境,并且使用 LLM 和 工具来执行各种任务和功能。其可通过外部信息源来克服 LLM 的一些限制,可以计划和执行需要多个步骤或子任务的复杂操作。 1
分类
2005 年,Russell and Norvig's 基于感知和执行能力的强弱将 Agents 分为 5 类:1 (由于时间较早,而当前的agents 进展较快,分类标准不一定适用,这里不做过多解释)
2013 年,Weiss 根据 decision-making 的实现方式定义了 4 类 Agents:
当前,LangGraph 根据 LLM 对流程控制程度不同,LangGraph 将 Agent 大致分为了三类:Router、Tool calling agent、Custom agent architectures。2
create_react_agent
可以低成本地实现一个这类agent:创建一个和LLM写作并利用tool calling能力的 graph 工作流。3ReAct Agent
ReAct 是 Reasoning and Acting,结合了推理和行动能力,模拟人类在解决问题和执行任务时的思维和行动过程。例如,当用户提出一个问题时,ReAct agent 不仅生成文本答案,还可能根据需求自动执行相关操作,如查询天气、预定餐厅等。
实现
在 ReAct 提出时,还没有 Function/Tool Calling 能力,当前,结合 tool-calling 可以很方便地实现 ReAct。
应用场景
协议或规范
大家都在讲多 agent 协作完成复杂任务,而协作就得有协议,目前还没有统一的标准,agent-protocol 算是比较多认可的一个协议。它定义了一组API,实现的Agent只需对外提供对应API,并符合规范定义的输入和输出即可。 4 主要的 API 有:
当前 agent-protocol 提供了一些 SDK,主要是 Python、JavaScript/TypeScript 的实现。
使用场景
LlamaIndex也认为Agents 使用场景广泛且使用场景在不断扩大,并且举了几个例子:
现有 Agent 实现
实现的要点
OpenAI Assistant API
OpenAI 的 Assistant API 可以利用 model、tool、file 来响应用户的 Query。当前(2024.12)Assistant API 支持三种类型的 tool:Code Interpreter、File Search、Function Calling. 7
Quick start code. 8
LlamaIndex Agents
LlamaIndex对Agents的定义:5
LlamaIndex 当前支持三类 agent:
接下来agent就可以与你对话了:
Lower-Level API
上面用到的 ReActAgent 是 AgentRunner 与 AgentWorker 交互的封装,
todo:Lower-Agent Guide 9
自定义Agent
自定义一个Agent最简单的方式是定义一个 stateful function,并用 FnAgentWorker 包装一下。
自定义Agent的例子
todo: 数据库查询Agent 10,可以模仿来创建各个场景的Agent,例如查询Log、Metric等。
Tool 封装
todo
agent 关键组件的 LlamaIndex 实现
If you want to leverage core agentic ingredients in your workflow, LlamaIndex has robust abstractions for every agent sub-ingredient.
routing
todo
Router Query Engine
Router Query Engine 12
LangChain Agents
todo: 如何使用本地或自行部署的LLM服务?
Footnotes
https://en.wikipedia.org/wiki/Intelligent_agent ↩ ↩2
https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/ ↩
https://langchain-ai.github.io/langgraph/reference/prebuilt/ ↩
https://github.com/Div99/agent-protocol ↩
https://docs.llamaindex.ai/en/stable/use_cases/agents/ ↩ ↩2
https://aws.amazon.com/cn/what-is/retrieval-augmented-generation/ ↩
https://platform.openai.com/docs/assistants/overview ↩
https://platform.openai.com/docs/assistants/quickstart ↩
https://docs.llamaindex.ai/en/stable/module_guides/deploying/agents/agent_runner/ ↩
https://docs.llamaindex.ai/en/stable/examples/agent/custom_agent/ ↩
https://docs.llamaindex.ai/en/stable/module_guides/querying/router/ ↩
https://docs.llamaindex.ai/en/stable/examples/query_engine/RouterQueryEngine/ ↩
The text was updated successfully, but these errors were encountered: