Skip to content

Commit fe40dff

Browse files
author
Naresh Mehta
committed
Add tavily search agent to the LLM
Part of creating a react_agent. The langChainTavily_search_agent uses the Langchain wrapper from Tavily and something amazingly weird has happened. Follow my blog post for more details. But in general, the tavily_search_agent is functioning but the langchain wrapper for the same is not.
1 parent 6f43e0d commit fe40dff

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

langChaintavily_search_agent.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from dotenv import load_dotenv
3+
4+
load_dotenv()
5+
6+
from langchain.agents import create_agent
7+
from langchain.tools import tool
8+
from langchain_core.messages import HumanMessage
9+
from langchain_openai import ChatOpenAI
10+
from langchain_tavily import TavilySearch
11+
12+
llm = ChatOpenAI(openai_api_base=os.environ.get("OPENAI_API_BASE"),openai_api_key=os.environ.get("OPENAI_API_KEY"),
13+
model_name="openai/gpt-oss-20b", temperature=0)
14+
tools = [TavilySearch()]
15+
agent = create_agent(model=llm, tools=tools) #, agent_type="react-search")
16+
17+
def main():
18+
print("This is a placeholder for react_search_agent.py")
19+
"""
20+
result = agent.invoke(
21+
{"messages":HumanMessage(content="Who is the current president of the United States?")}
22+
)
23+
"""
24+
result = agent.invoke(
25+
{"messages":HumanMessage(content="Search for 3 job postings for LLM expert in the Malmö area on linkedin and list their details.")}
26+
)
27+
print(result)
28+
29+
30+
if __name__ == "__main__":
31+
main()
32+
33+
34+
"""
35+
https://eu.smith.langchain.com/public/6a390515-5af7-4f62-a314-77a3d134ad16/r
36+
"""

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ dependencies = [
1010
"langchain>=1.1.0",
1111
"langchain-nvidia-ai-endpoints>=1.0.0",
1212
"langchain-openai>=1.1.0",
13+
"langchain-tavily>=0.2.13",
1314
"python-dotenv>=1.2.1",
15+
"tavily-python>=0.7.13",
1416
]

tavily_search_agent.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
from dotenv import load_dotenv
3+
4+
load_dotenv()
5+
6+
from langchain.agents import create_agent
7+
from langchain.tools import tool
8+
from langchain_core.messages import HumanMessage
9+
from langchain_openai import ChatOpenAI
10+
from tavily import TavilyClient
11+
12+
tavily = TavilyClient(api_key=os.environ.get("TAVILY_API_KEY"))
13+
14+
@tool
15+
def search(query: str) -> str:
16+
"""
17+
Tool that searches for information based on a query from the internet
18+
19+
Args:
20+
query: The query to search for
21+
Returns:
22+
The search results
23+
"""
24+
# Placeholder for search functionality
25+
return tavily.search(query=query, max_results=3)
26+
27+
llm = ChatOpenAI(openai_api_base=os.environ.get("OPENAI_API_BASE"),openai_api_key=os.environ.get("OPENAI_API_KEY"),
28+
model_name="openai/gpt-oss-20b", temperature=0)
29+
tools = [search]
30+
agent = create_agent(model=llm, tools=tools) #, agent_type="react-search")
31+
32+
def main():
33+
print("This is a placeholder for react_search_agent.py")
34+
"""
35+
result = agent.invoke(
36+
{"messages":HumanMessage(content="Who is the current president of the United States?")}
37+
)
38+
"""
39+
result = agent.invoke(
40+
{"messages":HumanMessage(content="Search for 3 job postings for LLM expert in the Malmö area on linkedin and list their details.")}
41+
)
42+
print(result)
43+
44+
45+
if __name__ == "__main__":
46+
main()

uv.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)