Files
agent/examples/qwen_langgraph_sugar.py
2025-08-30 23:28:48 +08:00

44 lines
978 B
Python
Raw Permalink 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.
"""
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()