Commit
·
26e5222
1
Parent(s):
536eabf
fix: try stricter tools
Browse files
app.py
CHANGED
@@ -13,12 +13,6 @@ from llama_index.core.tools import FunctionTool
|
|
13 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
14 |
from llama_index.core.agent.workflow import AgentWorkflow
|
15 |
from llama_index.llms.gemini import Gemini
|
16 |
-
from llama_index.tools.google import GoogleSearchToolSpec
|
17 |
-
from llama_index.retrievers.bm25 import BM25Retriever
|
18 |
-
from llama_index.core.query_engine import RetrieverQueryEngine
|
19 |
-
from llama_index.core.tools import QueryEngineTool
|
20 |
-
from llama_index.core.node_parser import SentenceSplitter
|
21 |
-
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
22 |
from llama_index.core.schema import Document
|
23 |
from llama_index.core import get_response_synthesizer
|
24 |
import pandas as pd
|
@@ -38,12 +32,8 @@ class BasicAgent:
|
|
38 |
|
39 |
# self.llm = Ollama(model="qwen2.5:7b", request_timeout=500)
|
40 |
self.llm = Gemini(model_name="models/gemini-2.0-flash")
|
|
|
41 |
def get_answers_from_youtube_transcript(youtube_link: str) -> str:
|
42 |
-
"""Fetches transcript of the given youtube_link and returns matching answers based on query.
|
43 |
-
Args:
|
44 |
-
youtube_link (str): youtube video link for which we need to answer questions on.
|
45 |
-
query (str): question to answer from video transcript.
|
46 |
-
"""
|
47 |
try:
|
48 |
loader = YoutubeTranscriptReader()
|
49 |
documents = loader.load_data(
|
@@ -52,7 +42,7 @@ class BasicAgent:
|
|
52 |
|
53 |
text = documents[0].text_resource.text
|
54 |
|
55 |
-
return "
|
56 |
except Exception as e:
|
57 |
print("error", e)
|
58 |
|
@@ -62,29 +52,30 @@ class BasicAgent:
|
|
62 |
description="Fetches transcript of the given youtube_link and returns matching answers based on query. To be called when video link is given.",
|
63 |
)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
def duck_duck_go_search_tool(query: str) -> str:
|
84 |
try:
|
85 |
-
raw_results =
|
86 |
-
|
87 |
-
|
|
|
88 |
|
89 |
except Exception as e:
|
90 |
return f"An error occurred: {e}"
|
@@ -103,8 +94,7 @@ class BasicAgent:
|
|
103 |
if text == "":
|
104 |
text = WikipediaToolSpec().search_data(query)
|
105 |
|
106 |
-
return "
|
107 |
-
|
108 |
except Exception as e:
|
109 |
return f"An error occurred: {e}"
|
110 |
|
@@ -115,7 +105,7 @@ class BasicAgent:
|
|
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], llm=self.llm)
|
119 |
print("BasicAgent initialized.")
|
120 |
|
121 |
async def run_agent(self, question: str):
|
|
|
13 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
14 |
from llama_index.core.agent.workflow import AgentWorkflow
|
15 |
from llama_index.llms.gemini import Gemini
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
from llama_index.core.schema import Document
|
17 |
from llama_index.core import get_response_synthesizer
|
18 |
import pandas as pd
|
|
|
32 |
|
33 |
# self.llm = Ollama(model="qwen2.5:7b", request_timeout=500)
|
34 |
self.llm = Gemini(model_name="models/gemini-2.0-flash")
|
35 |
+
|
36 |
def get_answers_from_youtube_transcript(youtube_link: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
37 |
try:
|
38 |
loader = YoutubeTranscriptReader()
|
39 |
documents = loader.load_data(
|
|
|
42 |
|
43 |
text = documents[0].text_resource.text
|
44 |
|
45 |
+
return { "video_transcript": text }
|
46 |
except Exception as e:
|
47 |
print("error", e)
|
48 |
|
|
|
52 |
description="Fetches transcript of the given youtube_link and returns matching answers based on query. To be called when video link is given.",
|
53 |
)
|
54 |
|
55 |
+
def web_page_reader(url: str, query: str) -> str:
|
56 |
+
try:
|
57 |
+
documents = SimpleWebPageReader(html_to_text=True).load_data(
|
58 |
+
[url]
|
59 |
+
)
|
60 |
+
index = SummaryIndex.from_documents(documents)
|
61 |
+
query_engine = index.as_query_engine()
|
62 |
+
response = query_engine.query(query)
|
63 |
+
return response.response
|
64 |
+
except Exception as e:
|
65 |
+
print("error in webpage", e)
|
66 |
+
|
67 |
+
web_page_reader_tool = FunctionTool.from_defaults(
|
68 |
+
web_page_reader,
|
69 |
+
name="web_page_reader",
|
70 |
+
description="Visits the wepage on given url and returns response on the passed query"
|
71 |
+
)
|
72 |
|
73 |
def duck_duck_go_search_tool(query: str) -> str:
|
74 |
try:
|
75 |
+
raw_results = DuckDuckGoSearchToolSpec().duckduckgo_full_search(query, max_results=5)
|
76 |
+
texts = [res['body'] for res in raw_results]
|
77 |
+
full_text = "\n".join(texts)
|
78 |
+
return { "web_search_results": full_text }
|
79 |
|
80 |
except Exception as e:
|
81 |
return f"An error occurred: {e}"
|
|
|
94 |
if text == "":
|
95 |
text = WikipediaToolSpec().search_data(query)
|
96 |
|
97 |
+
return { "wiki_search_results": text }
|
|
|
98 |
except Exception as e:
|
99 |
return f"An error occurred: {e}"
|
100 |
|
|
|
105 |
description="Searches wikipedia and converts results into a high quality answer."
|
106 |
)
|
107 |
|
108 |
+
self.agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool, wikipedia_search_tool, web_page_reader_tool], llm=self.llm, system_prompt="You're an ai agent designed for question answering. Keep your answers concise or even one word when possible. You have access to a bunch of tools, utilise them well to reach answers.")
|
109 |
print("BasicAgent initialized.")
|
110 |
|
111 |
async def run_agent(self, question: str):
|