hanshan1988 commited on
Commit
963d8bd
·
1 Parent(s): a9dae1d

added duckduckgo

Browse files
Files changed (3) hide show
  1. app.py +11 -4
  2. requirements.txt +2 -0
  3. tools.py +15 -1
app.py CHANGED
@@ -24,7 +24,7 @@ from langgraph.graph.message import add_messages
24
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
25
  from langgraph.prebuilt import ToolNode, tools_condition
26
 
27
- from tools import fetch_website, get_wiki_full, youtube_transcript, python_repl_tool
28
 
29
  # Initialize the Hugging Face model
30
  hf_model_name = "openai/gpt-oss-120b" # "Qwen/Qwen2.5-72B-Instruct"
@@ -45,7 +45,8 @@ tools_list = [
45
  fetch_website,
46
  get_wiki_full,
47
  youtube_transcript,
48
- python_repl_tool
 
49
  ]
50
 
51
  llm_with_tools = chat_model.bind_tools(
@@ -62,6 +63,13 @@ def assistant(state: AgentState):
62
  # System message
63
  textual_description_of_tool = dedent(
64
  """
 
 
 
 
 
 
 
65
  fetch_website(url: str) -> str:
66
  Fetch the content of a website.
67
  Args:
@@ -143,12 +151,11 @@ class BasicAgent:
143
  content=question # + '/nothink'
144
  )
145
  ]
146
- response = await agent_graph.ainvoke({"messages": messages}, config={"recursion_limit": 20})
147
  response_text = response['messages'][-1].content
148
  # return response_text.split('</think>')[-1]
149
  return extract_answer(response_text)
150
 
151
-
152
  async def run_and_submit_all( profile: gr.OAuthProfile | None):
153
  """
154
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
24
  from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
25
  from langgraph.prebuilt import ToolNode, tools_condition
26
 
27
+ from tools import fetch_website, get_wiki_full, youtube_transcript, python_repl_tool, duckduckgo_search_results
28
 
29
  # Initialize the Hugging Face model
30
  hf_model_name = "openai/gpt-oss-120b" # "Qwen/Qwen2.5-72B-Instruct"
 
45
  fetch_website,
46
  get_wiki_full,
47
  youtube_transcript,
48
+ python_repl_tool,
49
+ duckduckgo_search_results
50
  ]
51
 
52
  llm_with_tools = chat_model.bind_tools(
 
63
  # System message
64
  textual_description_of_tool = dedent(
65
  """
66
+ duckduckgo_search_results(query: str) -> list[dict]:
67
+ Perform a web search using DuckDuckGo and return the results.
68
+ Args:
69
+ query: The search query string.
70
+ Returns:
71
+ A list of search results, where each result is a dictionary that includes the snippet, title, and link.
72
+
73
  fetch_website(url: str) -> str:
74
  Fetch the content of a website.
75
  Args:
 
151
  content=question # + '/nothink'
152
  )
153
  ]
154
+ response = await agent_graph.ainvoke({"messages": messages}, config={"recursion_limit": 10})
155
  response_text = response['messages'][-1].content
156
  # return response_text.split('</think>')[-1]
157
  return extract_answer(response_text)
158
 
 
159
  async def run_and_submit_all( profile: gr.OAuthProfile | None):
160
  """
161
  Fetches all questions, runs the BasicAgent on them, submits all answers,
requirements.txt CHANGED
@@ -3,6 +3,8 @@ requests
3
  wikipedia
4
  youtube-transcript-api
5
  pytube
 
 
6
 
7
  langchain
8
  langchain_experimental
 
3
  wikipedia
4
  youtube-transcript-api
5
  pytube
6
+ duckduckgo-search
7
+ ddgs
8
 
9
  langchain
10
  langchain_experimental
tools.py CHANGED
@@ -4,7 +4,7 @@ from bs4 import BeautifulSoup
4
 
5
  from langchain.tools import tool
6
  from langchain_community.utilities import WikipediaAPIWrapper
7
- from langchain_community.tools import WikipediaQueryRun
8
  from langchain_community.document_loaders import YoutubeLoader, WebBaseLoader
9
  from langchain_experimental.utilities import PythonREPL
10
 
@@ -12,6 +12,20 @@ from langchain_experimental.utilities import PythonREPL
12
  # Initialize Python REPL
13
  python_repl = PythonREPL()
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @tool
16
  def fetch_website(url:str) -> str:
17
  """Fetch the content of a website.
 
4
 
5
  from langchain.tools import tool
6
  from langchain_community.utilities import WikipediaAPIWrapper
7
+ from langchain_community.tools import WikipediaQueryRun, DuckDuckGoSearchRun, DuckDuckGoSearchResults
8
  from langchain_community.document_loaders import YoutubeLoader, WebBaseLoader
9
  from langchain_experimental.utilities import PythonREPL
10
 
 
12
  # Initialize Python REPL
13
  python_repl = PythonREPL()
14
 
15
+ @tool
16
+ def duckduckgo_search_results(query: str) -> list[dict]:
17
+ """Perform a DuckDuckGo search for the given query and return the results.
18
+ Args:
19
+ query: The search query string.
20
+ Returns:
21
+ A list of search results, where each result is a dictionary that includes the snippet, title, and link.
22
+ """
23
+ try:
24
+ search = DuckDuckGoSearchResults(output_format="list")
25
+ return search.invoke(query)
26
+ except Exception as e:
27
+ return f"Error performing search: {str(e)}"
28
+
29
  @tool
30
  def fetch_website(url:str) -> str:
31
  """Fetch the content of a website.