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

Update Wikipedia search tool

Browse files
Files changed (2) hide show
  1. basic_agent.py +3 -2
  2. wiki_tool.py +57 -0
basic_agent.py CHANGED
@@ -19,6 +19,7 @@ from llama_index.core.agent.workflow import (
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. "
@@ -62,8 +63,8 @@ class BasicAgent:
62
  )
63
 
64
  # TODO Wikipedia tool does not return the tables from the page...
65
- wiki_spec = WikipediaToolSpec()
66
- wiki_search_tool = wiki_spec.to_tool_list()[1]
67
 
68
  wiki_agent = FunctionAgent(
69
  name="WikiAgent",
 
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
+ from wiki_tool import CustomWikipediaToolSpec
23
 
24
  answer_specifics = ("When answering, provide ONLY the precise answer requested. "
25
  "Do not include explanations, steps, reasoning, or additional text. Be direct and specific. "
 
63
  )
64
 
65
  # TODO Wikipedia tool does not return the tables from the page...
66
+ wiki_spec = CustomWikipediaToolSpec()
67
+ wiki_search_tool = wiki_spec.to_tool_list()[0]
68
 
69
  wiki_agent = FunctionAgent(
70
  name="WikiAgent",
wiki_tool.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Wikipedia tool based on LlamaIndex's WikipediaToolSpec
3
+ https://docs.llamaindex.ai/en/stable/api_reference/tools/wikipedia/
4
+ """
5
+ from typing import Dict, Any
6
+
7
+ import requests
8
+ from llama_index.core.tools import FunctionTool
9
+ from llama_index.core.tools.tool_spec.base import BaseToolSpec
10
+
11
+ class CustomWikipediaToolSpec(BaseToolSpec):
12
+ """
13
+ Specifies two tools for querying information from Wikipedia.
14
+ """
15
+
16
+ # Define the functions that we export to the LLM
17
+ spec_functions = ["search_data"]
18
+
19
+ def load_data(
20
+ self, page: str, lang: str = "en", **load_kwargs: Dict[str, Any]
21
+ ) -> str:
22
+ """
23
+ Retrieve a Wikipedia page. Useful for learning about a particular concept that isn't private information.
24
+
25
+ Args:
26
+ page (str): Title of the page to read.
27
+ lang (str): Language of Wikipedia to read. (default: English)
28
+ """
29
+ import wikipedia
30
+
31
+ wikipedia.set_lang(lang)
32
+ try:
33
+ wikipedia_page = wikipedia.page(page, **load_kwargs, auto_suggest=False)
34
+ except wikipedia.PageError:
35
+ return "Unable to load page. Try searching instead."
36
+
37
+ # Return wikitext because the result does not contain tables
38
+ raw_url = wikipedia_page.url + "?action=raw"
39
+ try:
40
+ raw_content = requests.get(raw_url).content
41
+ except:
42
+ return "Unable to load page."
43
+ return raw_content
44
+
45
+ def search_data(self, query: str, lang: str = "en") -> str:
46
+ """
47
+ Search Wikipedia for a page related to the given query.
48
+
49
+ Args:
50
+ query (str): the string to search for
51
+ """
52
+ import wikipedia
53
+
54
+ pages = wikipedia.search(query)
55
+ if len(pages) == 0:
56
+ return "No search results. Try changing your query and use keywords instead of full sentences."
57
+ return self.load_data(pages[0], lang)