Martin Bär commited on
Commit
4022a3e
·
1 Parent(s): 31f8487

Update web search tool

Browse files
Files changed (2) hide show
  1. basic_agent.py +9 -10
  2. web_tools.py +28 -0
basic_agent.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import re
3
  import asyncio
4
 
5
- from tavily import AsyncTavilyClient
6
  from llama_index.core.tools import FunctionTool
7
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
8
  from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
@@ -19,6 +18,11 @@ from llama_index.core.agent.workflow import (
19
 
20
  from multimodality_tools import get_image_qa_tool, get_transcription_tool, \
21
  get_excel_analysis_tool, get_excel_tool, get_csv_analysis_tool, get_csv_tool, _get_file, get_read_file_tool
 
 
 
 
 
22
 
23
  class BasicAgent:
24
  def __init__(self, ollama=False, langfuse=False):
@@ -42,8 +46,8 @@ class BasicAgent:
42
  system_prompt=(
43
  "You are a general AI assistant. I will ask you a question. "
44
  "Report your thoughts, delegate work to other agents if necessary, and"
45
- "finish your answer with the following template: "
46
- "FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number "
47
  "OR as few words as possible OR a comma separated list of numbers and/or "
48
  "strings. If you are asked for a number, don't use comma to write your "
49
  "number neither use units such as $ or percent sign unless specified otherwise. "
@@ -75,12 +79,7 @@ class BasicAgent:
75
  )
76
 
77
  tool_spec = DuckDuckGoSearchToolSpec()
78
- search_tool = FunctionTool.from_defaults(tool_spec.duckduckgo_full_search)
79
- # In case DuckDuckGo is not good enough
80
- async def search_web(query: str) -> str:
81
- """Searches the web to answer questions."""
82
- client = AsyncTavilyClient(api_key=os.getenv("TAVILY"))
83
- return str(await client.search(query))
84
 
85
  web_search_agent = FunctionAgent(
86
  name="WebAgent",
@@ -91,7 +90,7 @@ class BasicAgent:
91
  "you communicate this clearly. Always hand off your answer to MainAgent."
92
  ),
93
  llm=llm,
94
- tools=[search_web],
95
  can_handoff_to=["MainAgent"],
96
  )
97
 
 
2
  import re
3
  import asyncio
4
 
 
5
  from llama_index.core.tools import FunctionTool
6
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
7
  from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
 
18
 
19
  from multimodality_tools import get_image_qa_tool, get_transcription_tool, \
20
  get_excel_analysis_tool, get_excel_tool, get_csv_analysis_tool, get_csv_tool, _get_file, get_read_file_tool
21
+ from web_tools import get_search_web_tool
22
+
23
+ answer_specifics = ("When answering, provide ONLY the precise answer requested. "
24
+ "Do not include explanations, steps, reasoning, or additional text. Be direct and specific. "
25
+ 'For example, if asked "What is the capital of France?", respond simply with "Paris".')
26
 
27
  class BasicAgent:
28
  def __init__(self, ollama=False, langfuse=False):
 
46
  system_prompt=(
47
  "You are a general AI assistant. I will ask you a question. "
48
  "Report your thoughts, delegate work to other agents if necessary, and"
49
+ "finish your answer with the following template:\n"
50
+ "FINAL ANSWER: [YOUR FINAL ANSWER]. \nYOUR FINAL ANSWER should be a number "
51
  "OR as few words as possible OR a comma separated list of numbers and/or "
52
  "strings. If you are asked for a number, don't use comma to write your "
53
  "number neither use units such as $ or percent sign unless specified otherwise. "
 
79
  )
80
 
81
  tool_spec = DuckDuckGoSearchToolSpec()
82
+ search_tool = FunctionTool.from_defaults(tool_spec.duckduckgo_full_search)
 
 
 
 
 
83
 
84
  web_search_agent = FunctionAgent(
85
  name="WebAgent",
 
90
  "you communicate this clearly. Always hand off your answer to MainAgent."
91
  ),
92
  llm=llm,
93
+ tools=[get_search_web_tool()],
94
  can_handoff_to=["MainAgent"],
95
  )
96
 
web_tools.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tools to search the web and extract information."""
2
+ import os
3
+
4
+ from tavily import AsyncTavilyClient
5
+ from llama_index.core.tools import FunctionTool
6
+
7
+ async def tavily_web_tool(query: str) -> str:
8
+ """
9
+ Search the web with a given query and return the first two results.
10
+ If results do not return what you are looking for, rephrasing the query can help.
11
+ """
12
+ client = AsyncTavilyClient(api_key=os.getenv("TAVILY"))
13
+ res = await client.search(query)
14
+ urls = [r.get("url") for r in res.get("results", []) if "url" in r][:2]
15
+ page_contents = {
16
+ url: await client.extract(url)
17
+ for url in urls
18
+ }
19
+ return "\n\n".join(
20
+ f"RESULT {i}:\n{content}"
21
+ for i, content in enumerate(page_contents.values())
22
+ )
23
+
24
+ def get_search_web_tool():
25
+ return FunctionTool.from_defaults(
26
+ fn=tavily_web_tool,
27
+ description="Search the web with a given query and return the first two results."
28
+ )