Commit
·
6140977
1
Parent(s):
260122f
fix: try simpler tools
Browse files
app.py
CHANGED
@@ -2,8 +2,10 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import inspect
|
5 |
-
import
|
6 |
import nest_asyncio
|
|
|
|
|
7 |
from llama_index.llms.ollama import Ollama
|
8 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
9 |
from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
|
@@ -59,21 +61,30 @@ class BasicAgent:
|
|
59 |
description="Fetches transcript of the given youtube_link and returns matching answers based on query. To be called when video link is given.",
|
60 |
)
|
61 |
|
62 |
-
def
|
63 |
try:
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
nodes = splitter.get_nodes_from_documents(documents)
|
70 |
-
|
71 |
-
retriever = BM25Retriever(nodes=nodes, similarity_top_k=10)
|
72 |
-
synthesizer = get_response_synthesizer(response_mode="tree_summarize", llm=self.llm)
|
73 |
-
query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
|
74 |
-
|
75 |
response = query_engine.query(query)
|
76 |
return response.response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
except Exception as e:
|
79 |
return f"An error occurred: {e}"
|
@@ -85,20 +96,14 @@ class BasicAgent:
|
|
85 |
description="Searches the web and refines the result into a high-quality answer. Use when other tools don't seem suitable"
|
86 |
)
|
87 |
|
88 |
-
def wikipedia_search(query: str) -> str:
|
89 |
try:
|
90 |
-
text = WikipediaToolSpec().
|
91 |
-
documents = [Document(text=text)]
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
synthesizer = get_response_synthesizer(response_mode="tree_summarize", llm=self.llm)
|
98 |
-
query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
|
99 |
-
|
100 |
-
response = query_engine.query(query)
|
101 |
-
return response.response
|
102 |
|
103 |
except Exception as e:
|
104 |
return f"An error occurred: {e}"
|
@@ -110,7 +115,7 @@ class BasicAgent:
|
|
110 |
description="Searches wikipedia and converts results into a high quality answer."
|
111 |
)
|
112 |
|
113 |
-
self.agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool, wikipedia_search_tool], llm=self.llm)
|
114 |
print("BasicAgent initialized.")
|
115 |
|
116 |
async def run_agent(self, question: str):
|
@@ -143,6 +148,7 @@ async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
143 |
api_url = DEFAULT_API_URL
|
144 |
questions_url = f"{api_url}/questions"
|
145 |
submit_url = f"{api_url}/submit"
|
|
|
146 |
|
147 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
148 |
try:
|
@@ -187,7 +193,19 @@ async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
187 |
print(f"Skipping item with missing task_id or question: {item}")
|
188 |
continue
|
189 |
try:
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
192 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
193 |
except Exception as e:
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import inspect
|
5 |
+
import base64
|
6 |
import nest_asyncio
|
7 |
+
from llama_index.core import SummaryIndex
|
8 |
+
from llama_index.readers.web import SimpleWebPageReader
|
9 |
from llama_index.llms.ollama import Ollama
|
10 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
11 |
from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
|
|
|
61 |
description="Fetches transcript of the given youtube_link and returns matching answers based on query. To be called when video link is given.",
|
62 |
)
|
63 |
|
64 |
+
def web_page_reader(url: str, query: str) -> str:
|
65 |
try:
|
66 |
+
documents = SimpleWebPageReader(html_to_text=True).load_data(
|
67 |
+
[url]
|
68 |
+
)
|
69 |
+
index = SummaryIndex.from_documents(documents)
|
70 |
+
query_engine = index.as_query_engine()
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
response = query_engine.query(query)
|
72 |
return response.response
|
73 |
+
except Exception as e:
|
74 |
+
print("error in webpage", e)
|
75 |
+
|
76 |
+
web_page_reader_tool = FunctionTool.from_defaults(
|
77 |
+
web_page_reader,
|
78 |
+
name="web_page_reader",
|
79 |
+
description="Visits the wepage on given url and returns response on the passed query"
|
80 |
+
)
|
81 |
+
|
82 |
+
def duck_duck_go_search_tool(query: str) -> str:
|
83 |
+
try:
|
84 |
+
raw_results = DuckDuckGoSearchToolSpec().duckduckgo_full_search(query, max_results=5)
|
85 |
+
texts = [res['body'] for res in raw_results]
|
86 |
+
full_text = "\n".join(texts)
|
87 |
+
return "Here's the responses from the search: " + full_text
|
88 |
|
89 |
except Exception as e:
|
90 |
return f"An error occurred: {e}"
|
|
|
96 |
description="Searches the web and refines the result into a high-quality answer. Use when other tools don't seem suitable"
|
97 |
)
|
98 |
|
99 |
+
def wikipedia_search(page_title: str, query: str) -> str:
|
100 |
try:
|
101 |
+
text = WikipediaToolSpec().load_data(page=page_title)
|
|
|
102 |
|
103 |
+
if text == "":
|
104 |
+
text = WikipediaToolSpec().search_data(query)
|
105 |
+
|
106 |
+
return "Here's the response from wikipedia search: " + text
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
except Exception as e:
|
109 |
return f"An error occurred: {e}"
|
|
|
115 |
description="Searches wikipedia and converts results into a high quality answer."
|
116 |
)
|
117 |
|
118 |
+
self.agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool, wikipedia_search_tool, web_page_reader_tool], llm=self.llm)
|
119 |
print("BasicAgent initialized.")
|
120 |
|
121 |
async def run_agent(self, question: str):
|
|
|
148 |
api_url = DEFAULT_API_URL
|
149 |
questions_url = f"{api_url}/questions"
|
150 |
submit_url = f"{api_url}/submit"
|
151 |
+
files_url = f"{api_url}/files/"
|
152 |
|
153 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
154 |
try:
|
|
|
193 |
print(f"Skipping item with missing task_id or question: {item}")
|
194 |
continue
|
195 |
try:
|
196 |
+
encoded = None
|
197 |
+
if item.get("file_name") != "":
|
198 |
+
response = requests.get(files_url + task_id)
|
199 |
+
response.raise_for_status()
|
200 |
+
|
201 |
+
data = response.content
|
202 |
+
|
203 |
+
encoded = base64.b64encode(data).decode('utf-8')
|
204 |
+
|
205 |
+
if encoded is not None:
|
206 |
+
submitted_answer = agent(question_text + "\nfile_data: " + encoded)
|
207 |
+
else:
|
208 |
+
submitted_answer = agent(question_text)
|
209 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
210 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
211 |
except Exception as e:
|