Update agent.py
Browse files
agent.py
CHANGED
@@ -64,7 +64,7 @@ logging.basicConfig(level=logging.INFO)
|
|
64 |
logging.getLogger("llama_index.core.agent").setLevel(logging.DEBUG)
|
65 |
logging.getLogger("llama_index.llms").setLevel(logging.DEBUG)
|
66 |
|
67 |
-
model_id = "
|
68 |
proj_llm = HuggingFaceLLM(
|
69 |
model_name=model_id,
|
70 |
tokenizer_name=model_id,
|
@@ -110,7 +110,7 @@ def read_and_parse_content(input_path: str) -> List[Document]:
|
|
110 |
'.jpg': ImageReader(),
|
111 |
'.jpeg': ImageReader(),
|
112 |
'.png': ImageReader(),
|
113 |
-
'.mp3': AssemblyAIAudioTranscriptReader(),
|
114 |
}
|
115 |
|
116 |
# --- URL Handling ---
|
@@ -159,8 +159,7 @@ read_and_parse_tool = FunctionTool.from_defaults(
|
|
159 |
)
|
160 |
)
|
161 |
|
162 |
-
|
163 |
-
def create_rag_tool(documents: List[Document]) -> QueryEngineTool:
|
164 |
"""
|
165 |
Creates a RAG query engine tool from a list of documents using advanced components.
|
166 |
Inspired by 'create_advanced_index' and 'create_context_aware_query_engine' methods.
|
@@ -219,6 +218,17 @@ def create_rag_tool(documents: List[Document]) -> QueryEngineTool:
|
|
219 |
|
220 |
return rag_engine_tool
|
221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
# 1. Create the base DuckDuckGo search tool from the official spec.
|
223 |
# This tool returns text summaries of search results, not just URLs.
|
224 |
base_duckduckgo_tool = DuckDuckGoSearchToolSpec().to_tool_list()[1]
|
@@ -535,11 +545,9 @@ class EnhancedGAIAAgent:
|
|
535 |
extract_url_tool,
|
536 |
code_execution_tool,
|
537 |
generate_code_tool,
|
|
|
538 |
]
|
539 |
-
|
540 |
-
# RAG tool will be created dynamically when documents are loaded
|
541 |
-
self.current_rag_tool = None
|
542 |
-
|
543 |
# Create main coordinator using only defined tools
|
544 |
self.coordinator = ReActAgent(
|
545 |
name="GAIACoordinator",
|
@@ -551,7 +559,17 @@ Available tools:
|
|
551 |
2. **extract_url_tool** - Search and extract relevant URLs when no specific source is provided
|
552 |
3. **generate_code_tool** - Generate Python code for complex computations
|
553 |
4. **code_execution_tool** - Execute Python code safely
|
554 |
-
5. **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
555 |
""",
|
556 |
llm=proj_llm,
|
557 |
tools=self.available_tools,
|
@@ -560,16 +578,6 @@ Available tools:
|
|
560 |
callback_manager=callback_manager,
|
561 |
)
|
562 |
|
563 |
-
def create_dynamic_rag_tool(self, documents: List) -> None:
|
564 |
-
"""Create RAG tool from loaded documents and add to coordinator"""
|
565 |
-
if documents:
|
566 |
-
rag_tool = create_rag_tool(documents)
|
567 |
-
if rag_tool:
|
568 |
-
self.current_rag_tool = rag_tool
|
569 |
-
# Update coordinator tools
|
570 |
-
updated_tools = self.available_tools + [rag_tool]
|
571 |
-
self.coordinator.tools = updated_tools
|
572 |
-
print("RAG tool created and added to coordinator")
|
573 |
|
574 |
def download_gaia_file(self, task_id: str, api_url: str = "https://agents-course-unit4-scoring.hf.space") -> str:
|
575 |
"""Download file associated with task_id"""
|
@@ -598,9 +606,7 @@ Available tools:
|
|
598 |
try:
|
599 |
file_path = self.download_gaia_file(task_id)
|
600 |
if file_path:
|
601 |
-
# Load documents and create RAG tool
|
602 |
documents = read_and_parse_content(file_path)
|
603 |
-
self.create_dynamic_rag_tool(documents)
|
604 |
except Exception as e:
|
605 |
print(f"Failed to download/process file for task {task_id}: {e}")
|
606 |
|
|
|
64 |
logging.getLogger("llama_index.core.agent").setLevel(logging.DEBUG)
|
65 |
logging.getLogger("llama_index.llms").setLevel(logging.DEBUG)
|
66 |
|
67 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
68 |
proj_llm = HuggingFaceLLM(
|
69 |
model_name=model_id,
|
70 |
tokenizer_name=model_id,
|
|
|
110 |
'.jpg': ImageReader(),
|
111 |
'.jpeg': ImageReader(),
|
112 |
'.png': ImageReader(),
|
113 |
+
'.mp3': AssemblyAIAudioTranscriptReader(input_path),
|
114 |
}
|
115 |
|
116 |
# --- URL Handling ---
|
|
|
159 |
)
|
160 |
)
|
161 |
|
162 |
+
def create_rag_tool_fn(documents: List[Document]) -> QueryEngineTool:
|
|
|
163 |
"""
|
164 |
Creates a RAG query engine tool from a list of documents using advanced components.
|
165 |
Inspired by 'create_advanced_index' and 'create_context_aware_query_engine' methods.
|
|
|
218 |
|
219 |
return rag_engine_tool
|
220 |
|
221 |
+
create_rag_tool = FunctionTool.from_defaults(
|
222 |
+
fn=create_rag_tool_fn,
|
223 |
+
name="create_rag_tool",
|
224 |
+
description=(
|
225 |
+
"Use this tool to create a Retrieval Augmented Generation (RAG) engine from a set of documents. "
|
226 |
+
"Input should be a list of documents or document paths. The tool processes these documents to build a vector index "
|
227 |
+
"and a query engine that enables natural language querying over the document content. "
|
228 |
+
"This tool is essential for enabling efficient and context-aware information retrieval in complex document collections."
|
229 |
+
)
|
230 |
+
)
|
231 |
+
|
232 |
# 1. Create the base DuckDuckGo search tool from the official spec.
|
233 |
# This tool returns text summaries of search results, not just URLs.
|
234 |
base_duckduckgo_tool = DuckDuckGoSearchToolSpec().to_tool_list()[1]
|
|
|
545 |
extract_url_tool,
|
546 |
code_execution_tool,
|
547 |
generate_code_tool,
|
548 |
+
create_rag_tool
|
549 |
]
|
550 |
+
|
|
|
|
|
|
|
551 |
# Create main coordinator using only defined tools
|
552 |
self.coordinator = ReActAgent(
|
553 |
name="GAIACoordinator",
|
|
|
559 |
2. **extract_url_tool** - Search and extract relevant URLs when no specific source is provided
|
560 |
3. **generate_code_tool** - Generate Python code for complex computations
|
561 |
4. **code_execution_tool** - Execute Python code safely
|
562 |
+
5. **create_rag_tool** - Create RAG tool from parsed files to improve the information retrieval.
|
563 |
+
|
564 |
+
WORKFLOW for questions that needs external knowledge :
|
565 |
+
a. Use extract_url_tool to extract a relevant URL from the query.
|
566 |
+
b. Use read_and_parse_tool to read and parse the content of the extracted URL.
|
567 |
+
c. Use create_rag_tool to create a query engine based on the parsed document.
|
568 |
+
d. Use the created query engine to retrieve the answer to the question.
|
569 |
+
WORKFLOW for questions where File available :
|
570 |
+
a. Use read_and_parse_tool to read and parse the content of the file.
|
571 |
+
b. Use create_rag_tool to create a query engine based on the parsed file.
|
572 |
+
c. Use the created query engine to retrieve the answer to the question.
|
573 |
""",
|
574 |
llm=proj_llm,
|
575 |
tools=self.available_tools,
|
|
|
578 |
callback_manager=callback_manager,
|
579 |
)
|
580 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
581 |
|
582 |
def download_gaia_file(self, task_id: str, api_url: str = "https://agents-course-unit4-scoring.hf.space") -> str:
|
583 |
"""Download file associated with task_id"""
|
|
|
606 |
try:
|
607 |
file_path = self.download_gaia_file(task_id)
|
608 |
if file_path:
|
|
|
609 |
documents = read_and_parse_content(file_path)
|
|
|
610 |
except Exception as e:
|
611 |
print(f"Failed to download/process file for task {task_id}: {e}")
|
612 |
|