lovepreetsingh1996 commited on
Commit
a4240ad
·
1 Parent(s): c8febd3

fix: try #2

Browse files
Files changed (1) hide show
  1. app.py +31 -2
app.py CHANGED
@@ -2,8 +2,9 @@ import os
2
  import gradio as gr
3
  import requests
4
  import inspect
 
5
  from llama_index.llms.ollama import Ollama
6
- from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
7
  from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
8
  from llama_index.core.tools import FunctionTool
9
  from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
@@ -19,6 +20,8 @@ from llama_index.core import get_response_synthesizer
19
  import pandas as pd
20
  import asyncio
21
 
 
 
22
  # (Keep Constants as is)
23
  # --- Constants ---
24
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -77,13 +80,39 @@ class BasicAgent:
77
  name="duck_duck_go_search_tool",
78
  description="Searches the web and refines the result into a high-quality answer."
79
  )
 
 
 
 
 
80
 
81
- agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool], llm=self.llm)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  async def run_agent():
84
  return await agent.run(question)
85
 
86
  response = asyncio.run(run_agent())
 
87
  final_answer = response.response.blocks[0].text
88
  print(f"Agent returning fixed answer: {final_answer}")
89
  return final_answer
 
2
  import gradio as gr
3
  import requests
4
  import inspect
5
+ import nest_asyncio
6
  from llama_index.llms.ollama import Ollama
7
+ from llama_index.tools.wikipedia import WikipediaToolSpec
8
  from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
9
  from llama_index.core.tools import FunctionTool
10
  from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
 
20
  import pandas as pd
21
  import asyncio
22
 
23
+ nest_asyncio.apply()
24
+
25
  # (Keep Constants as is)
26
  # --- Constants ---
27
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
80
  name="duck_duck_go_search_tool",
81
  description="Searches the web and refines the result into a high-quality answer."
82
  )
83
+
84
+ def wikipedia_search(query: str) -> str:
85
+ try:
86
+ text = WikipediaToolSpec().search_data(query)
87
+ documents = [Document(text)]
88
 
89
+ splitter = SentenceSplitter(chunk_size=256)
90
+ nodes = splitter.get_nodes_from_documents(documents)
91
+
92
+ retriever = BM25Retriever(nodes=nodes, similarity_top_k=10)
93
+ synthesizer = get_response_synthesizer(response_mode="refine", llm=self.llm)
94
+ query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
95
+
96
+ response = query_engine.query(query)
97
+ return response.response
98
+
99
+ except Exception as e:
100
+ return f"An error occurred: {e}"
101
+
102
+
103
+ wikipedia_search_tool = FunctionTool.from_defaults(
104
+ wikipedia_search,
105
+ name="wikipedia_search",
106
+ description="Searches anything on wikipedia and converts results into a high quality answer"
107
+ )
108
+
109
+ agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool, wikipedia_search_tool], llm=self.llm)
110
 
111
  async def run_agent():
112
  return await agent.run(question)
113
 
114
  response = asyncio.run(run_agent())
115
+
116
  final_answer = response.response.blocks[0].text
117
  print(f"Agent returning fixed answer: {final_answer}")
118
  return final_answer