-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathlangchainv1_tools.py
More file actions
101 lines (85 loc) · 3 KB
/
langchainv1_tools.py
File metadata and controls
101 lines (85 loc) · 3 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import logging
import os
import random
from datetime import datetime
import azure.identity
from dotenv import load_dotenv
from langchain.agents import create_agent
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from rich import print
from rich.logging import RichHandler
# Setup logging with rich
logging.basicConfig(level=logging.WARNING, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
logger = logging.getLogger("weekend_planner")
load_dotenv(override=True)
API_HOST = os.getenv("API_HOST", "github")
if API_HOST == "azure":
token_provider = azure.identity.get_bearer_token_provider(
azure.identity.DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default",
)
model = ChatOpenAI(
model=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
base_url=os.environ["AZURE_OPENAI_ENDPOINT"] + "/openai/v1",
api_key=token_provider,
)
elif API_HOST == "github":
model = ChatOpenAI(
model=os.getenv("GITHUB_MODEL", "gpt-4o"),
base_url="https://models.inference.ai.azure.com",
api_key=os.environ.get("GITHUB_TOKEN"),
)
elif API_HOST == "ollama":
model = ChatOpenAI(
model=os.environ.get("OLLAMA_MODEL", "llama3.1"),
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
api_key="none",
)
else:
model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini"))
@tool
def get_weather(city: str, date: str) -> dict:
"""Returns weather data for a given city and date."""
logger.info(f"Getting weather for {city} on {date}")
if random.random() < 0.05:
return {
"temperature": 72,
"description": "Sunny",
}
else:
return {
"temperature": 60,
"description": "Rainy",
}
@tool
def get_activities(city: str, date: str) -> list:
"""Returns a list of activities for a given city and date."""
logger.info(f"Getting activities for {city} on {date}")
return [
{"name": "Hiking", "location": city},
{"name": "Beach", "location": city},
{"name": "Museum", "location": city},
]
@tool
def get_current_date() -> str:
"""Gets the current date from the system and returns as a string in format YYYY-MM-DD."""
logger.info("Getting current date")
return datetime.now().strftime("%Y-%m-%d")
agent = create_agent(
model=model,
system_prompt=(
"You help users plan their weekends and choose the best activities for the given weather. "
"If an activity would be unpleasant in weather, don't suggest it. Include date of the weekend in response."
),
tools=[get_weather, get_activities, get_current_date],
)
def main():
response = agent.invoke(
{"messages": [{"role": "user", "content": "hii what can I do this weekend in San Francisco?"}]}
)
latest_message = response["messages"][-1]
print(latest_message.content)
if __name__ == "__main__":
logger.setLevel(logging.INFO)
main()