ekabaruh commited on
Commit
4fa2cc2
·
verified ·
1 Parent(s): 67dd527

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +21 -16
agent.py CHANGED
@@ -17,33 +17,38 @@ from langchain_huggingface import (
17
  from langchain_core.messages import SystemMessage, HumanMessage
18
  from langchain_core.tools import tool
19
  from langchain_openai import ChatOpenAI
20
- from langchain_community.utilities import SerpAPIWrapper
21
 
22
  load_dotenv()
23
 
24
  ### =============== SEARCH TOOLS =============== ###
25
 
26
  @tool
27
- def serpapi_search(query: str) -> str:
28
- """Search the web using SerpAPI.
29
  Args:
30
  query: The search query."""
31
  try:
32
- # Get API key from environment variable
33
- api_key = os.getenv("SERPAPI_API_KEY")
34
- if not api_key:
35
- return {"search_results": "Error: SERPAPI_API_KEY not found in environment variables."}
36
 
37
- # Initialize SerpAPIWrapper with the API key
38
- search = SerpAPIWrapper(serpapi_api_key=api_key)
39
 
40
- # Perform the search
41
- results = search.run(query)
42
-
43
- if not results or results.strip() == "":
44
  return {"search_results": "No search results found."}
45
 
46
- return {"search_results": results}
 
 
 
 
 
 
 
 
 
 
47
  except Exception as e:
48
  return {"search_results": f"Error performing search: {str(e)}"}
49
 
@@ -80,7 +85,7 @@ print(system_prompt)
80
  sys_msg = SystemMessage(content=system_prompt)
81
 
82
  tools = [
83
- serpapi_search,
84
  save_and_read_file,
85
  ]
86
 
@@ -137,7 +142,7 @@ def build_graph(provider: str = "openai"):
137
  # test
138
  if __name__ == "__main__":
139
  question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
140
- graph = build_graph(provider="openai")
141
  messages = [HumanMessage(content=question)]
142
  messages = graph.invoke({"messages": messages})
143
  for m in messages["messages"]:
 
17
  from langchain_core.messages import SystemMessage, HumanMessage
18
  from langchain_core.tools import tool
19
  from langchain_openai import ChatOpenAI
20
+ from duckduckgo_search import DDGS
21
 
22
  load_dotenv()
23
 
24
  ### =============== SEARCH TOOLS =============== ###
25
 
26
  @tool
27
+ def duckduckgo_search(query: str) -> str:
28
+ """Search the web using DuckDuckGo.
29
  Args:
30
  query: The search query."""
31
  try:
32
+ # Initialize DuckDuckGo search
33
+ ddgs = DDGS()
 
 
34
 
35
+ # Perform the search and get the first 5 results
36
+ results = list(ddgs.text(query, max_results=5))
37
 
38
+ if not results:
 
 
 
39
  return {"search_results": "No search results found."}
40
 
41
+ # Format the results
42
+ formatted_results = []
43
+ for i, result in enumerate(results, 1):
44
+ formatted_result = f"{i}. {result.get('title', 'No title')}\n"
45
+ formatted_result += f" URL: {result.get('href', 'No URL')}\n"
46
+ formatted_result += f" {result.get('body', 'No description')}\n"
47
+ formatted_results.append(formatted_result)
48
+
49
+ search_output = "\n".join(formatted_results)
50
+
51
+ return {"search_results": search_output}
52
  except Exception as e:
53
  return {"search_results": f"Error performing search: {str(e)}"}
54
 
 
85
  sys_msg = SystemMessage(content=system_prompt)
86
 
87
  tools = [
88
+ duckduckgo_search,
89
  save_and_read_file,
90
  ]
91
 
 
142
  # test
143
  if __name__ == "__main__":
144
  question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
145
+ graph = build_graph(provider="huggingface")
146
  messages = [HumanMessage(content=question)]
147
  messages = graph.invoke({"messages": messages})
148
  for m in messages["messages"]: