langgraph basic use in tool inject

This commit is contained in:
lingyuzeng
2025-08-30 23:28:48 +08:00
parent 19487cb117
commit 273e6088fc
4 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
"""
Example using the sugar: create_qwen_react_agent(...)
Prereqs:
uv pip install -e '.[openai,custom]'
Env:
export QWEN_API_KEY=sk-...
export QWEN_BASE_URL=https://dashscope-intl.aliyuncs.com/compatible-mode/v1
export QWEN_MODEL=qwen3-coder-flash
"""
from langchain_core.messages import HumanMessage
try:
from langchain.tools import tool
except Exception:
from langchain_core.tools import tool # type: ignore
from langgraph_qwen import create_qwen_react_agent
@tool
def echo(text: str) -> str:
"""Echo back the given text."""
return text
def main():
agent = create_qwen_react_agent(
[echo],
prefer="custom", # use our custom BaseChatModel by default
model_kwargs={"temperature": 0},
)
res = agent.invoke({"messages": [HumanMessage(content="调用 echo 工具返回你好LangGraph")]})
print("\n=== Final ===\n")
print(res["messages"][-1].content)
if __name__ == "__main__":
main()