Skip to content

Commit

Permalink
Add OpenWeatherMap tool if API Key is set (#22)
Browse files Browse the repository at this point in the history
* Add OpenWeatherMap tool if API key is set

* Simplify
  • Loading branch information
JoshuaC215 authored Sep 2, 2024
1 parent 13d62f9 commit c69b5d1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ With that said, there are several other interesting projects in this space that
# Optional, to enable simple header-based auth on the service
AUTH_SECRET=any_string_you_choose
# Optional, to enable OpenWeatherMap
OPENWEATHERMAP_API_KEY=your_openweathermap_api_key
# Optional, to enable LangSmith tracing
LANGCHAIN_TRACING_V2=true
LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
Expand Down
2 changes: 1 addition & 1 deletion agent/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
## IT IS NOT INTENDED TO BE USED FOR LOCAL DEVELOPMENT

aiohttp~=3.10.0
arxiv~=2.1.3
duckduckgo-search~=6.2.6
langchain-community~=0.2.11
langchain-core~=0.2.26
langchain-openai~=0.1.20
langchain-groq~=0.1.9
numexpr~=2.10.1
pyowm~=3.3.0
python-dotenv~=1.0.1
12 changes: 10 additions & 2 deletions agent/research_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from langchain_openai import ChatOpenAI
from langchain_groq import ChatGroq
from langchain_community.tools import DuckDuckGoSearchResults, OpenWeatherMapQueryRun
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import AIMessage, SystemMessage, RemoveMessage
from langchain_core.runnables import RunnableConfig, RunnableLambda
Expand All @@ -10,7 +11,7 @@
from langgraph.managed import IsLastStep
from langgraph.prebuilt import ToolNode

from agent.tools import calculator, web_search
from agent.tools import calculator
from agent.llama_guard import LlamaGuard, LlamaGuardOutput


Expand All @@ -28,10 +29,17 @@ class AgentState(MessagesState):
if os.getenv("GROQ_API_KEY") is not None:
models["llama-3.1-70b"] = ChatGroq(model="llama-3.1-70b-versatile", temperature=0.5)

web_search = DuckDuckGoSearchResults(name="WebSearch")
tools = [web_search, calculator]

# Add weather tool if API key is set
# Register for an API key at https://openweathermap.org/api/
if os.getenv("OPENWEATHERMAP_API_KEY") is not None:
tools.append(OpenWeatherMapQueryRun(name="Weather"))

current_date = datetime.now().strftime("%B %d, %Y")
instructions = f"""
You are a helpful research assistant with the ability to search the web for information.
You are a helpful research assistant with the ability to search the web and use other tools.
Today's date is {current_date}.
NOTE: THE USER CAN'T SEE THE TOOL RESPONSE.
Expand Down
3 changes: 0 additions & 3 deletions agent/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import numexpr
import re
from langchain_core.tools import tool, BaseTool
from langchain_community.tools import DuckDuckGoSearchResults

web_search = DuckDuckGoSearchResults(name="WebSearch")


def calculator_func(expression: str) -> str:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies = [
"langsmith ~=0.1.96",
"numexpr ~=2.10.1",
"pydantic ~=1.10.17",
"pyowm ~=3.3.0",
"python-dotenv ~=1.0.1",
"streamlit ~=1.37.0",
"uvicorn ~=0.30.5",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ langgraph-checkpoint-sqlite~=1.0.0
langsmith~=0.1.96
numexpr~=2.10.1
pydantic~=1.10.17
pyowm~=3.3.0
python-dotenv~=1.0.1
streamlit~=1.37.0
uvicorn~=0.30.5

0 comments on commit c69b5d1

Please sign in to comment.