40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from langchain.agents import create_agent
|
||
from langchain_community.chat_models.tongyi import ChatTongyi
|
||
from langchain_core.tools import tool
|
||
|
||
|
||
@tool(description="获取体重,返回值整数,单位千克")
|
||
def get_weight() -> int:
|
||
return 93
|
||
|
||
|
||
@tool(description="获取身高,返回值整数,单位厘米")
|
||
def get_height() -> int:
|
||
return 185
|
||
|
||
|
||
agent = create_agent(
|
||
model=ChatTongyi(model="qwen3-max"),
|
||
tools=[get_height, get_weight],
|
||
system_prompt="""你是严格遵循ReAct框架的智能体,必须按[思考,行动,观察,再思考]的流程解决问题
|
||
每轮仅能思考并调用1个工具,禁止单词调用多个工具。并告知我你的思考过程,工具调用的原因,按思考、行动
|
||
、观察三个结构告知我"""
|
||
)
|
||
|
||
res = agent.stream(
|
||
{"messages": [{
|
||
"role": "user", "content": "计算我的BMI"
|
||
}]},
|
||
stream_mode="values"
|
||
)
|
||
|
||
for chunk in res:
|
||
last_msg = chunk["messages"][-1]
|
||
if last_msg.content:
|
||
print(type(last_msg).__name__, last_msg.content)
|
||
|
||
if type(last_msg).__name__ == "AIMessage":
|
||
if last_msg.tool_calls:
|
||
tool_list = [tc['name'] for tc in last_msg.tool_calls]
|
||
print(f"工具调佣 {tool_list}")
|