agentic-ai / agent_langchain.py
bstraehle's picture
Update agent_langchain.py
05f62c8
raw
history blame
1.02 kB
import os
from datetime import date
from langchain.agents import AgentType, initialize_agent, load_tools, tool
from langchain.chat_models import ChatOpenAI
@tool
def date_tool(text: str) -> str:
"""Returns today's date. Use this for any questions related to knowing today's date.
The input should always be an empty string, and this function will always return today's date.
Any date mathematics should occur outside this function."""
return str(date.today())
def agent_langchain(model, temperature, prompt):
llm = ChatOpenAI(
model_name = model,
temperature = temperature)
OPENWEATHERMAP_API_KEY = os.environ["OPENWEATHERMAP_API_KEY"]
tools = load_tools(["openweathermap-api"])
agent = initialize_agent(
tools + # built-in tools
[date_tool], # custom tools
llm,
agent = AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
handle_parsing_errors = True,
verbose = True)
return agent(prompt)