-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_agent.py
More file actions
59 lines (43 loc) · 1.74 KB
/
search_agent.py
File metadata and controls
59 lines (43 loc) · 1.74 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
from typing import Any, List
from dotenv import load_dotenv
from langchain_tavily import TavilySearch
from pydantic import BaseModel, Field
load_dotenv()
from langchain_ollama import ChatOllama
from langchain.messages import HumanMessage
from langchain.agents import create_agent
from langchain.tools import tool
from tavily import TavilyClient
tavily = TavilyClient()
class Source(BaseModel):
"""Schema for the url sources used by the agent this must be a url to the source."""
url: str = Field(description="the url of the source")
class AgentResponse(BaseModel):
"""Schema for the agent response with answer and sources."""
answer: str = Field(description="The agent answer to the query.")
source: List[Source] = Field(default_factory=list ,description="The list of the url sources used to generate the answer to the query.")
@tool
def search(query: str) -> dict[str, Any]:
"""
Tool to search over the internet and return the result
:param query: Query to search over
:return: string with the result
"""
return tavily.search(query=query)
def main():
llm = ChatOllama(model="qwen3:30b-a3b",
temperature=0.1,
reasoning=True)
tools = [TavilySearch()]
agent = create_agent(model=llm, tools=tools, response_format=AgentResponse)
result = agent.invoke(
{"messages": HumanMessage(content="What is the Bitcoin price now?")})
print(result)
structured_output = result['structured_response']
# You can now access fields directly on the structured object:
print(f"Agent Answer: {structured_output.answer}")
print("Sources Used:")
for source in structured_output.source:
print(f" - {source.url}")
if __name__ == "__main__":
main()