Files
hz3-agent/step1_basic_fc.py
黄庄三号 451e9a12ed feat: 黄庄三号四能力Agent初始版本
能力1: Function Call - LangGraph ToolNode
能力2: MCP - langchain-mcp-adapters + 确定性路由
能力3: 思考模式 - think_node + CoT推理链
能力4: Skill - 自建SkillRegistry注册机制

模型: GLM-4.5-air (智谱)
2026-04-23 19:18:53 +08:00

87 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Step 1: 最简单的 LangGraph Agent + GLM-4.5-air + 工具调用
只验证核心能力Function Call
"""
import os
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
# ── 模型配置 ──
llm = ChatOpenAI(
base_url="https://open.bigmodel.cn/api/paas/v4",
api_key="2259e33a1357460abe17919aaf81e73d.K44a8LPQTmFM5PKm",
model="glm-4.5-air",
)
# ── 定义工具 ──
@tool
def get_weather(city: str) -> str:
"""查询指定城市的天气信息"""
# 模拟天气数据
weather_data = {
"北京": "晴天气温22°C北风3级",
"上海": "多云气温25°C东风2级",
"深圳": "阵雨气温28°C南风4级",
"黄庄": "晴转多云气温23°C微风",
}
return weather_data.get(city, f"暂无{city}的天气数据")
@tool
def calculate(expression: str) -> str:
"""计算数学表达式,输入如 '2+3*4'"""
try:
result = eval(expression, {"__builtins__": {}}, {})
return f"计算结果: {expression} = {result}"
except Exception as e:
return f"计算错误: {e}"
@tool
def search_knowledge(query: str) -> str:
"""搜索知识库(模拟)"""
kb = {
"黄庄三号": "黄庄三号是AI助手定位为严肃、认真、听话、聪明的AI助手",
"LangGraph": "LangGraph是LangChain团队推出的Agent框架支持状态图、循环、持久化",
"MCP": "MCP是Model Context ProtocolAI工具互操作标准协议",
}
for key, val in kb.items():
if key in query:
return val
return f"知识库中未找到关于'{query}'的信息"
# ── 创建 Agent ──
tools = [get_weather, calculate, search_knowledge]
agent = create_react_agent(llm, tools)
# ── 运行测试 ──
if __name__ == "__main__":
import asyncio
async def test():
print("=" * 50)
print("Step 1: LangGraph + GLM-4.5-air + FC 工具调用")
print("=" * 50)
# 测试1: 天气查询
print("\n[测试1] 天气查询")
result = await agent.ainvoke({"messages": [("user", "黄庄今天天气怎么样?")]})
last_msg = result["messages"][-1]
print(f"回复: {last_msg.content}")
# 测试2: 数学计算
print("\n[测试2] 数学计算")
result = await agent.ainvoke({"messages": [("user", "帮我算一下 123 * 456 + 789")]})
last_msg = result["messages"][-1]
print(f"回复: {last_msg.content}")
# 测试3: 知识搜索
print("\n[测试3] 知识搜索")
result = await agent.ainvoke({"messages": [("user", "LangGraph是什么")]})
last_msg = result["messages"][-1]
print(f"回复: {last_msg.content}")
print("\n" + "=" * 50)
print("Step 1 完成FC 工具调用正常工作")
asyncio.run(test())