forked from fabiothiroki/langchain-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
30 lines (24 loc) · 847 Bytes
/
Copy pathmain.py
File metadata and controls
30 lines (24 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from langchain_ollama import ChatOllama
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
# 1. 定义工具
# @tool 装饰器会将函数注册为 Agent 可调用的工具
# docstring 会作为工具描述传给模型,写清楚很重要
@tool
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"Return the weather in {city} in a joke."
# 2. 初始化模型
# llama3.2 支持 tool call,llama3 不支持
model = ChatOllama(model="llama3.2")
# 3. 创建 Agent
agent = create_react_agent(
model=model,
tools=[get_weather],
prompt="You are a helpful assistant",
)
if __name__ == "__main__":
result = agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)
print(result["messages"][-1].content)