Spaces:
Sleeping
Sleeping
ernani
commited on
Commit
·
a057a75
1
Parent(s):
81917a3
Testing the first deployment
Browse files- app.py +49 -29
- manage_agents.py +439 -0
- requirements.txt +18 -2
- tools.py +771 -0
app.py
CHANGED
|
@@ -3,32 +3,42 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
|
| 13 |
-
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
def run_and_submit_all(
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 28 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 29 |
|
| 30 |
if profile:
|
| 31 |
-
username= f"{profile.username}"
|
| 32 |
print(f"User logged in: {username}")
|
| 33 |
else:
|
| 34 |
print("User not logged in.")
|
|
@@ -38,13 +48,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
-
# 1. Instantiate Agent
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
| 47 |
-
|
| 48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 49 |
print(agent_code)
|
| 50 |
|
|
@@ -55,21 +65,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 55 |
response.raise_for_status()
|
| 56 |
questions_data = response.json()
|
| 57 |
if not questions_data:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
print(f"Fetched {len(questions_data)} questions.")
|
| 61 |
except requests.exceptions.RequestException as e:
|
| 62 |
print(f"Error fetching questions: {e}")
|
| 63 |
return f"Error fetching questions: {e}", None
|
| 64 |
except requests.exceptions.JSONDecodeError as e:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
except Exception as e:
|
| 69 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 70 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 71 |
|
| 72 |
-
# 3. Run
|
| 73 |
results_log = []
|
| 74 |
answers_payload = []
|
| 75 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
@@ -82,17 +92,29 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 82 |
try:
|
| 83 |
submitted_answer = agent(question_text)
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if not answers_payload:
|
| 91 |
print("Agent did not produce any answers to submit.")
|
| 92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
|
| 94 |
-
# 4. Prepare Submission
|
| 95 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
print(status_update)
|
| 98 |
|
|
@@ -139,10 +161,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 139 |
results_df = pd.DataFrame(results_log)
|
| 140 |
return status_message, results_df
|
| 141 |
|
| 142 |
-
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
-
gr.Markdown("#
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
|
@@ -159,7 +180,6 @@ with gr.Blocks() as demo:
|
|
| 159 |
)
|
| 160 |
|
| 161 |
gr.LoginButton()
|
| 162 |
-
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
|
| 165 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from manage_agents import MainAgent
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Load environment variables
|
| 10 |
+
load_dotenv()
|
| 11 |
|
|
|
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
+
# --- Agent Implementation ---
|
| 16 |
+
class SearchAgent:
|
|
|
|
| 17 |
def __init__(self):
|
| 18 |
+
self.agent = MainAgent()
|
| 19 |
+
print("SearchAgent initialized with MainAgent.")
|
| 20 |
+
|
| 21 |
def __call__(self, question: str) -> str:
|
| 22 |
+
print(f"Processing question: {question[:100]}...")
|
| 23 |
+
try:
|
| 24 |
+
answer = self.agent.process_question(question)
|
| 25 |
+
print(f"Answer generated: {answer[:100]}...")
|
| 26 |
+
return answer
|
| 27 |
+
except Exception as e:
|
| 28 |
+
error_msg = f"Error processing question: {str(e)}"
|
| 29 |
+
print(error_msg)
|
| 30 |
+
return error_msg
|
| 31 |
|
| 32 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 33 |
"""
|
| 34 |
+
Fetches all questions, runs the SearchAgent on them, submits all answers,
|
| 35 |
and displays the results.
|
| 36 |
"""
|
| 37 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 38 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 39 |
|
| 40 |
if profile:
|
| 41 |
+
username = f"{profile.username}"
|
| 42 |
print(f"User logged in: {username}")
|
| 43 |
else:
|
| 44 |
print("User not logged in.")
|
|
|
|
| 48 |
questions_url = f"{api_url}/questions"
|
| 49 |
submit_url = f"{api_url}/submit"
|
| 50 |
|
| 51 |
+
# 1. Instantiate Agent
|
| 52 |
try:
|
| 53 |
+
agent = SearchAgent()
|
| 54 |
except Exception as e:
|
| 55 |
print(f"Error instantiating agent: {e}")
|
| 56 |
return f"Error initializing agent: {e}", None
|
| 57 |
+
|
| 58 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 59 |
print(agent_code)
|
| 60 |
|
|
|
|
| 65 |
response.raise_for_status()
|
| 66 |
questions_data = response.json()
|
| 67 |
if not questions_data:
|
| 68 |
+
print("Fetched questions list is empty.")
|
| 69 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 70 |
print(f"Fetched {len(questions_data)} questions.")
|
| 71 |
except requests.exceptions.RequestException as e:
|
| 72 |
print(f"Error fetching questions: {e}")
|
| 73 |
return f"Error fetching questions: {e}", None
|
| 74 |
except requests.exceptions.JSONDecodeError as e:
|
| 75 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 76 |
+
print(f"Response text: {response.text[:500]}")
|
| 77 |
+
return f"Error decoding server response for questions: {e}", None
|
| 78 |
except Exception as e:
|
| 79 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 80 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 81 |
|
| 82 |
+
# 3. Run Agent
|
| 83 |
results_log = []
|
| 84 |
answers_payload = []
|
| 85 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 92 |
try:
|
| 93 |
submitted_answer = agent(question_text)
|
| 94 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 95 |
+
results_log.append({
|
| 96 |
+
"Task ID": task_id,
|
| 97 |
+
"Question": question_text,
|
| 98 |
+
"Submitted Answer": submitted_answer
|
| 99 |
+
})
|
| 100 |
except Exception as e:
|
| 101 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 102 |
+
results_log.append({
|
| 103 |
+
"Task ID": task_id,
|
| 104 |
+
"Question": question_text,
|
| 105 |
+
"Submitted Answer": f"AGENT ERROR: {e}"
|
| 106 |
+
})
|
| 107 |
|
| 108 |
if not answers_payload:
|
| 109 |
print("Agent did not produce any answers to submit.")
|
| 110 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 111 |
|
| 112 |
+
# 4. Prepare Submission
|
| 113 |
+
submission_data = {
|
| 114 |
+
"username": username.strip(),
|
| 115 |
+
"agent_code": agent_code,
|
| 116 |
+
"answers": answers_payload
|
| 117 |
+
}
|
| 118 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 119 |
print(status_update)
|
| 120 |
|
|
|
|
| 161 |
results_df = pd.DataFrame(results_log)
|
| 162 |
return status_message, results_df
|
| 163 |
|
|
|
|
| 164 |
# --- Build Gradio Interface using Blocks ---
|
| 165 |
with gr.Blocks() as demo:
|
| 166 |
+
gr.Markdown("# Search Agent Evaluation Runner")
|
| 167 |
gr.Markdown(
|
| 168 |
"""
|
| 169 |
**Instructions:**
|
|
|
|
| 180 |
)
|
| 181 |
|
| 182 |
gr.LoginButton()
|
|
|
|
| 183 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 184 |
|
| 185 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
manage_agents.py
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Optional, Tuple
|
| 2 |
+
from langchain.agents import AgentExecutor
|
| 3 |
+
from langchain_openai import ChatOpenAI
|
| 4 |
+
from langchain.memory import ConversationBufferMemory
|
| 5 |
+
from langchain.chains import LLMChain
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
from langchain.schema import Document
|
| 8 |
+
from langchain.schema.runnable import RunnablePassthrough
|
| 9 |
+
from langchain.schema.output_parser import StrOutputParser
|
| 10 |
+
import re
|
| 11 |
+
from tools import (
|
| 12 |
+
YouTubeVideoTool,
|
| 13 |
+
WikipediaTool,
|
| 14 |
+
ImageTool,
|
| 15 |
+
AudioTool,
|
| 16 |
+
ExcelTool,
|
| 17 |
+
WebContentTool,
|
| 18 |
+
PythonTool,
|
| 19 |
+
ChromaDBManager,
|
| 20 |
+
ContentProcessingError
|
| 21 |
+
)
|
| 22 |
+
import logging
|
| 23 |
+
|
| 24 |
+
class ContentTypeAgent:
|
| 25 |
+
"""Agent responsible for identifying content type and selecting appropriate tool"""
|
| 26 |
+
|
| 27 |
+
def __init__(self, llm):
|
| 28 |
+
self.llm = llm
|
| 29 |
+
self.tools = {
|
| 30 |
+
"youtube": YouTubeVideoTool(),
|
| 31 |
+
"wiki": WikipediaTool(),
|
| 32 |
+
"image": ImageTool(),
|
| 33 |
+
"audio": AudioTool(),
|
| 34 |
+
"excel": ExcelTool(),
|
| 35 |
+
"web": WebContentTool(),
|
| 36 |
+
"python": PythonTool(),
|
| 37 |
+
"direct": None # For direct text manipulation tasks
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
self.type_identification_prompt = PromptTemplate(
|
| 41 |
+
input_variables=["question"],
|
| 42 |
+
template="""Analyze the following question and identify what type of content needs to be processed.
|
| 43 |
+
Question: {question}
|
| 44 |
+
|
| 45 |
+
Possible types:
|
| 46 |
+
- youtube: If the question mentions a YouTube video or contains a YouTube URL
|
| 47 |
+
- wiki: If the question refers to Wikipedia article or Wikipedia content
|
| 48 |
+
- image: If the question refers to an image or contains a task ID for an image
|
| 49 |
+
- audio: If the question refers to an audio file or contains a task ID for audio
|
| 50 |
+
- excel: If the question refers to an Excel file or contains a task ID for Excel
|
| 51 |
+
- web: If the question requires web content processing
|
| 52 |
+
- python: If the question refers to a Python file or contains a task ID for Python
|
| 53 |
+
- direct: If the question is a direct text manipulation task (e.g., reversing text, word play, simple text operations)
|
| 54 |
+
|
| 55 |
+
Consider these special cases:
|
| 56 |
+
1. If the question involves manipulating the text of the question itself (like reversing words, finding opposites), use "direct"
|
| 57 |
+
2. If the question is about a specific academic paper or research, use "wiki" first
|
| 58 |
+
3. If the question is about general knowledge that would be in Wikipedia, use "wiki"
|
| 59 |
+
|
| 60 |
+
Return only the type and nothing else."""
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
self.chain = (
|
| 64 |
+
{"question": RunnablePassthrough()}
|
| 65 |
+
| self.type_identification_prompt
|
| 66 |
+
| self.llm
|
| 67 |
+
| StrOutputParser()
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
def _extract_task_id(self, question: str) -> Optional[str]:
|
| 71 |
+
"""Extract task ID from question if present"""
|
| 72 |
+
# First try to find task_id in the question metadata
|
| 73 |
+
task_id_pattern = r'task_id["\':\s]+([a-f0-9-]{36})'
|
| 74 |
+
match = re.search(task_id_pattern, question, re.IGNORECASE)
|
| 75 |
+
if match:
|
| 76 |
+
return match.group(1)
|
| 77 |
+
|
| 78 |
+
# Then try to find any UUID-like string that might be a task ID
|
| 79 |
+
uuid_pattern = r'([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})'
|
| 80 |
+
match = re.search(uuid_pattern, question, re.IGNORECASE)
|
| 81 |
+
return match.group(1) if match else None
|
| 82 |
+
|
| 83 |
+
def _extract_youtube_url(self, question: str) -> Optional[str]:
|
| 84 |
+
"""Extract YouTube URL from question if present"""
|
| 85 |
+
# First try exact pattern for watch URLs
|
| 86 |
+
youtube_pattern = r'https?://(?:www\.)?youtube\.com/watch\?v=[a-zA-Z0-9_-]{11}'
|
| 87 |
+
match = re.search(youtube_pattern, question)
|
| 88 |
+
if match:
|
| 89 |
+
return match.group(0)
|
| 90 |
+
|
| 91 |
+
# Then try youtu.be URLs
|
| 92 |
+
youtube_short_pattern = r'https?://(?:www\.)?youtu\.be/[a-zA-Z0-9_-]{11}'
|
| 93 |
+
match = re.search(youtube_short_pattern, question)
|
| 94 |
+
if match:
|
| 95 |
+
return match.group(0)
|
| 96 |
+
|
| 97 |
+
# Finally try a more lenient pattern
|
| 98 |
+
youtube_lenient_pattern = r'https?://(?:www\.)?youtube\.com/watch\?v=[^\s\.,!?]+'
|
| 99 |
+
match = re.search(youtube_lenient_pattern, question)
|
| 100 |
+
if match:
|
| 101 |
+
url = match.group(0).strip().rstrip('.,!?')
|
| 102 |
+
return url
|
| 103 |
+
|
| 104 |
+
return None
|
| 105 |
+
|
| 106 |
+
def _is_reversed_text(self, text: str) -> bool:
|
| 107 |
+
"""Check if text appears to be reversed"""
|
| 108 |
+
# Check for common reversed patterns
|
| 109 |
+
reversed_indicators = [
|
| 110 |
+
'.sdrawkcab' in text.lower(),
|
| 111 |
+
'esrever' in text.lower(),
|
| 112 |
+
# Most normal English texts don't have many consecutive consonants
|
| 113 |
+
len(re.findall(r'[bcdfghjklmnpqrstvwxz]{4,}', text.lower())) > 0
|
| 114 |
+
]
|
| 115 |
+
return any(reversed_indicators)
|
| 116 |
+
|
| 117 |
+
def _process_direct_text(self, text: str) -> str:
|
| 118 |
+
"""Process direct text manipulation tasks"""
|
| 119 |
+
if self._is_reversed_text(text):
|
| 120 |
+
# If text is reversed, reverse it back
|
| 121 |
+
reversed_text = text[::-1]
|
| 122 |
+
# Try to process the reversed text
|
| 123 |
+
if "left" in reversed_text:
|
| 124 |
+
return "right" # Handle the specific case about "left" opposite
|
| 125 |
+
return reversed_text
|
| 126 |
+
return text
|
| 127 |
+
|
| 128 |
+
def identify_content_type(self, question: str, file_name: str) -> Tuple[str, Optional[str]]:
|
| 129 |
+
"""Identify content type and extract relevant parameter"""
|
| 130 |
+
# First check for direct text manipulation
|
| 131 |
+
if self._is_reversed_text(question):
|
| 132 |
+
return "direct", question
|
| 133 |
+
|
| 134 |
+
# Check for file_name in the question
|
| 135 |
+
if file_name:
|
| 136 |
+
extension = file_name.split('.')[-1].lower()
|
| 137 |
+
|
| 138 |
+
# Map extensions to content types
|
| 139 |
+
extension_map = {
|
| 140 |
+
'mp3': 'audio',
|
| 141 |
+
'wav': 'audio',
|
| 142 |
+
'png': 'image',
|
| 143 |
+
'jpg': 'image',
|
| 144 |
+
'jpeg': 'image',
|
| 145 |
+
'xlsx': 'excel',
|
| 146 |
+
'xls': 'excel',
|
| 147 |
+
'csv': 'excel',
|
| 148 |
+
'py': 'python'
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
if extension in extension_map:
|
| 152 |
+
return extension_map[extension], question
|
| 153 |
+
|
| 154 |
+
# Extract task ID if present
|
| 155 |
+
task_id = self._extract_task_id(question)
|
| 156 |
+
question_lower = question.lower()
|
| 157 |
+
|
| 158 |
+
# Check for specific content indicators
|
| 159 |
+
if 'wikipedia' in question_lower or any(academic_term in question_lower for academic_term in
|
| 160 |
+
['paper', 'journal', 'research', 'study', 'published', 'author', 'described']):
|
| 161 |
+
return "wiki", question
|
| 162 |
+
|
| 163 |
+
# YouTube check
|
| 164 |
+
youtube_url = self._extract_youtube_url(question)
|
| 165 |
+
if youtube_url or any(indicator in question_lower for indicator in ['youtube', 'video']):
|
| 166 |
+
return "youtube", youtube_url if youtube_url else question
|
| 167 |
+
|
| 168 |
+
# Use LLM for more complex type identification
|
| 169 |
+
content_type = self.chain.invoke(question).strip().lower()
|
| 170 |
+
return content_type, question
|
| 171 |
+
|
| 172 |
+
class RAGAgent:
|
| 173 |
+
"""Agent responsible for RAG operations"""
|
| 174 |
+
|
| 175 |
+
def __init__(self, llm, chroma_manager: ChromaDBManager):
|
| 176 |
+
self.llm = llm
|
| 177 |
+
self.chroma_manager = chroma_manager
|
| 178 |
+
|
| 179 |
+
# Define which metadata fields are relevant for different content types
|
| 180 |
+
self.relevant_metadata_fields = {
|
| 181 |
+
'youtube': ['title', 'author', 'duration', 'view_count'],
|
| 182 |
+
'image': ['source', 'type', 'analysis_type'],
|
| 183 |
+
'audio': ['source', 'type', 'duration', 'language'],
|
| 184 |
+
'excel': ['row_count', 'column_count', 'columns'],
|
| 185 |
+
'web': ['title', 'url', 'source'],
|
| 186 |
+
'wiki': ['title', 'source', 'language'],
|
| 187 |
+
'default': ['source', 'type', 'title']
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
self.answer_prompt = PromptTemplate(
|
| 191 |
+
input_variables=["context", "question"],
|
| 192 |
+
template="""You are a helpful AI assistant that provides accurate answers based on the given context. If the context contains the information needed to answer the question, provide a clear and concise answer. If the information in the context is not sufficient or relevant to answer the question, explain what specific information is missing.
|
| 193 |
+
|
| 194 |
+
Context: {context}
|
| 195 |
+
|
| 196 |
+
Question: {question}
|
| 197 |
+
|
| 198 |
+
Instructions:
|
| 199 |
+
1. First, carefully analyze if the context contains ALL necessary information to answer the question
|
| 200 |
+
2. If yes:
|
| 201 |
+
- Extract ONLY the specific information asked for
|
| 202 |
+
- Format the answer exactly as requested in the question
|
| 203 |
+
- Double-check your answer for accuracy
|
| 204 |
+
- Do not include explanations unless specifically asked
|
| 205 |
+
- If you've been asked something like: what is the name, answer with the name and nothing else, and the same for other questions
|
| 206 |
+
3. If no:
|
| 207 |
+
- Clearly state what specific information is missing
|
| 208 |
+
- Do not make assumptions or guesses
|
| 209 |
+
4. Always base your answer strictly on the provided context
|
| 210 |
+
5. For lists:
|
| 211 |
+
- Include ONLY items that strictly match the criteria
|
| 212 |
+
- Follow any sorting/formatting requirements exactly
|
| 213 |
+
- Verify each item individually before including it
|
| 214 |
+
|
| 215 |
+
Answer:"""
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
self.chain = (
|
| 219 |
+
{"context": RunnablePassthrough(), "question": RunnablePassthrough()}
|
| 220 |
+
| self.answer_prompt
|
| 221 |
+
| self.llm
|
| 222 |
+
| StrOutputParser()
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
def _clean_content(self, content: str) -> str:
|
| 226 |
+
"""Clean and normalize document content"""
|
| 227 |
+
if not content:
|
| 228 |
+
return ""
|
| 229 |
+
# Remove excessive whitespace
|
| 230 |
+
content = re.sub(r'\s+', ' ', content).strip()
|
| 231 |
+
# Remove very long sequences of special characters
|
| 232 |
+
content = re.sub(r'[^\w\s]{4,}', '...', content)
|
| 233 |
+
# Ensure reasonable length
|
| 234 |
+
return content[:10000] if len(content) > 10000 else content
|
| 235 |
+
|
| 236 |
+
def _format_metadata(self, metadata: dict, content_type: str) -> dict:
|
| 237 |
+
"""Format and filter metadata based on content type"""
|
| 238 |
+
if not metadata:
|
| 239 |
+
return {}
|
| 240 |
+
|
| 241 |
+
# Get relevant fields for this content type
|
| 242 |
+
relevant_fields = self.relevant_metadata_fields.get(
|
| 243 |
+
content_type,
|
| 244 |
+
self.relevant_metadata_fields['default']
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
# Filter and clean metadata
|
| 248 |
+
cleaned_metadata = {}
|
| 249 |
+
for field in relevant_fields:
|
| 250 |
+
if field in metadata and metadata[field] is not None:
|
| 251 |
+
value = metadata[field]
|
| 252 |
+
# Convert lists/dicts to strings if present
|
| 253 |
+
if isinstance(value, (list, dict)):
|
| 254 |
+
value = str(value)
|
| 255 |
+
# Truncate long values
|
| 256 |
+
if isinstance(value, str) and len(value) > 200:
|
| 257 |
+
value = value[:197] + "..."
|
| 258 |
+
cleaned_metadata[field] = value
|
| 259 |
+
|
| 260 |
+
return cleaned_metadata
|
| 261 |
+
|
| 262 |
+
def process_and_store(self, documents: List[Document], collection_name: str):
|
| 263 |
+
"""Process and store documents in ChromaDB with improved handling"""
|
| 264 |
+
try:
|
| 265 |
+
# Delete existing collection if it exists
|
| 266 |
+
try:
|
| 267 |
+
self.chroma_manager.client.delete_collection(collection_name)
|
| 268 |
+
logging.info(f"Deleted existing collection {collection_name}")
|
| 269 |
+
except Exception as e:
|
| 270 |
+
logging.debug(f"Collection {collection_name} did not exist: {str(e)}")
|
| 271 |
+
|
| 272 |
+
# Process documents
|
| 273 |
+
processed_docs = []
|
| 274 |
+
processed_metadata = []
|
| 275 |
+
|
| 276 |
+
for doc in documents:
|
| 277 |
+
# Clean content
|
| 278 |
+
cleaned_content = self._clean_content(doc.page_content)
|
| 279 |
+
if not cleaned_content:
|
| 280 |
+
continue
|
| 281 |
+
|
| 282 |
+
# Format metadata
|
| 283 |
+
content_type = doc.metadata.get('type', 'default')
|
| 284 |
+
cleaned_metadata = self._format_metadata(doc.metadata, content_type)
|
| 285 |
+
|
| 286 |
+
processed_docs.append(cleaned_content)
|
| 287 |
+
processed_metadata.append(cleaned_metadata)
|
| 288 |
+
|
| 289 |
+
if not processed_docs:
|
| 290 |
+
raise ValueError("No valid documents to store after processing")
|
| 291 |
+
|
| 292 |
+
# Store documents in new collection
|
| 293 |
+
logging.info(f"Storing {len(processed_docs)} processed documents in collection {collection_name}")
|
| 294 |
+
self.chroma_manager.add_documents_with_metadata(collection_name, processed_docs, processed_metadata)
|
| 295 |
+
|
| 296 |
+
except Exception as e:
|
| 297 |
+
logging.error(f"Error storing documents in ChromaDB: {str(e)}")
|
| 298 |
+
raise
|
| 299 |
+
|
| 300 |
+
def _extract_answer(self, chain_output) -> str:
|
| 301 |
+
"""Extract answer from chain output with improved error handling"""
|
| 302 |
+
try:
|
| 303 |
+
if isinstance(chain_output, str):
|
| 304 |
+
return chain_output.strip()
|
| 305 |
+
elif hasattr(chain_output, 'content'):
|
| 306 |
+
return chain_output.content.strip()
|
| 307 |
+
elif isinstance(chain_output, dict):
|
| 308 |
+
for key in ['text', 'output', 'result', 'answer']:
|
| 309 |
+
if key in chain_output and isinstance(chain_output[key], str):
|
| 310 |
+
return chain_output[key].strip()
|
| 311 |
+
logging.warning("Unexpected chain output format")
|
| 312 |
+
return "Could not generate an answer from the available information."
|
| 313 |
+
except Exception as e:
|
| 314 |
+
logging.error(f"Error extracting answer: {str(e)}")
|
| 315 |
+
return "Error processing the answer."
|
| 316 |
+
|
| 317 |
+
def retrieve_and_generate(self, question: str, collection_name: str) -> str:
|
| 318 |
+
"""Retrieve relevant documents and generate answer with improved context handling"""
|
| 319 |
+
try:
|
| 320 |
+
# Query ChromaDB
|
| 321 |
+
results = self.chroma_manager.query_collection(collection_name, question)
|
| 322 |
+
|
| 323 |
+
if not results or not results['documents'] or not results['documents'][0]:
|
| 324 |
+
logging.warning(f"No results found for question in collection {collection_name}")
|
| 325 |
+
return "No relevant information found to answer the question."
|
| 326 |
+
|
| 327 |
+
# Combine retrieved documents into context with structure
|
| 328 |
+
contexts = []
|
| 329 |
+
for doc_content, metadata in zip(results['documents'][0], results['metadatas'][0]):
|
| 330 |
+
# Create a clean context entry
|
| 331 |
+
context_parts = []
|
| 332 |
+
|
| 333 |
+
# Add metadata if present
|
| 334 |
+
if metadata:
|
| 335 |
+
metadata_str = ", ".join(f"{k}: {v}" for k, v in metadata.items())
|
| 336 |
+
context_parts.append(f"[{metadata_str}]")
|
| 337 |
+
|
| 338 |
+
# Add cleaned content
|
| 339 |
+
cleaned_content = self._clean_content(doc_content)
|
| 340 |
+
if cleaned_content:
|
| 341 |
+
context_parts.append(f"Content: {cleaned_content}")
|
| 342 |
+
|
| 343 |
+
if context_parts:
|
| 344 |
+
contexts.append("\n".join(context_parts))
|
| 345 |
+
|
| 346 |
+
context = "\n\n---\n\n".join(contexts)
|
| 347 |
+
logging.debug(f"Combined context length: {len(context)}")
|
| 348 |
+
|
| 349 |
+
# Generate answer
|
| 350 |
+
chain_output = self.chain.invoke({"context": context, "question": question})
|
| 351 |
+
answer = self._extract_answer(chain_output)
|
| 352 |
+
|
| 353 |
+
logging.info(f"Generated answer for question: {question[:100]}...")
|
| 354 |
+
return answer
|
| 355 |
+
|
| 356 |
+
except Exception as e:
|
| 357 |
+
logging.error(f"Error in retrieve_and_generate: {str(e)}")
|
| 358 |
+
return f"Error generating answer: {str(e)}"
|
| 359 |
+
|
| 360 |
+
class MainAgent:
|
| 361 |
+
"""Main agent orchestrating the workflow"""
|
| 362 |
+
|
| 363 |
+
def __init__(self):
|
| 364 |
+
self.llm = ChatOpenAI(temperature=0, model="gpt-4o-mini")
|
| 365 |
+
self.chroma_manager = ChromaDBManager()
|
| 366 |
+
self.content_type_agent = ContentTypeAgent(self.llm)
|
| 367 |
+
self.rag_agent = RAGAgent(self.llm, self.chroma_manager)
|
| 368 |
+
|
| 369 |
+
def process_question(self, question: str, file_name: str = "") -> str:
|
| 370 |
+
try:
|
| 371 |
+
# 1. Identify content type and parameter
|
| 372 |
+
content_type, parameter = self.content_type_agent.identify_content_type(question, file_name)
|
| 373 |
+
print("Content type:", content_type)
|
| 374 |
+
print("Parameter:", parameter)
|
| 375 |
+
|
| 376 |
+
# Handle direct text manipulation
|
| 377 |
+
if content_type == "direct":
|
| 378 |
+
return self.content_type_agent._process_direct_text(parameter)
|
| 379 |
+
|
| 380 |
+
if not parameter:
|
| 381 |
+
return "Could not identify necessary information (URL, task ID, etc.) from the question."
|
| 382 |
+
|
| 383 |
+
# 2. Use appropriate tool to extract information
|
| 384 |
+
if content_type not in self.content_type_agent.tools or self.content_type_agent.tools[content_type] is None:
|
| 385 |
+
return f"Unsupported content type: {content_type}"
|
| 386 |
+
|
| 387 |
+
tool = self.content_type_agent.tools[content_type]
|
| 388 |
+
|
| 389 |
+
try:
|
| 390 |
+
# Special handling for Wikipedia/research paper queries
|
| 391 |
+
if content_type == "wiki":
|
| 392 |
+
# Try Wikipedia first
|
| 393 |
+
try:
|
| 394 |
+
documents = tool._run(parameter)
|
| 395 |
+
except Exception as wiki_error:
|
| 396 |
+
print(f"Wikipedia search failed: {wiki_error}")
|
| 397 |
+
# If Wikipedia fails, fall back to web search with modified query
|
| 398 |
+
web_tool = self.content_type_agent.tools["web"]
|
| 399 |
+
# Add "wikipedia" or "research paper" to the query for better results
|
| 400 |
+
modified_query = f"{parameter} site:wikipedia.org OR site:researchgate.net OR site:scholar.google.com"
|
| 401 |
+
documents = web_tool._run(modified_query)
|
| 402 |
+
else:
|
| 403 |
+
# Pass question context for image processing
|
| 404 |
+
if content_type == "image":
|
| 405 |
+
documents = tool._run(parameter, question=question)
|
| 406 |
+
else:
|
| 407 |
+
documents = tool._run(parameter)
|
| 408 |
+
except Exception as e:
|
| 409 |
+
print(f"Tool execution failed: {e}")
|
| 410 |
+
# For research/academic queries, try web search as fallback
|
| 411 |
+
if "paper" in question.lower() or "research" in question.lower():
|
| 412 |
+
web_tool = self.content_type_agent.tools["web"]
|
| 413 |
+
modified_query = f"{parameter} site:scholar.google.com OR site:researchgate.net"
|
| 414 |
+
documents = web_tool._run(modified_query)
|
| 415 |
+
else:
|
| 416 |
+
raise
|
| 417 |
+
|
| 418 |
+
if not documents:
|
| 419 |
+
return "Could not extract any information from the content."
|
| 420 |
+
|
| 421 |
+
# 3. Store in ChromaDB with task ID in collection name if available
|
| 422 |
+
task_id = self.content_type_agent._extract_task_id(question)
|
| 423 |
+
collection_name = f"collection_{task_id if task_id else abs(hash(question))}"
|
| 424 |
+
|
| 425 |
+
try:
|
| 426 |
+
self.rag_agent.process_and_store(documents, collection_name)
|
| 427 |
+
except Exception as e:
|
| 428 |
+
print(f"Warning: Error storing in ChromaDB: {e}")
|
| 429 |
+
# Continue processing even if storage fails
|
| 430 |
+
|
| 431 |
+
# 4. Generate answer using RAG
|
| 432 |
+
answer = self.rag_agent.retrieve_and_generate(question, collection_name)
|
| 433 |
+
|
| 434 |
+
return answer
|
| 435 |
+
|
| 436 |
+
except ContentProcessingError as e:
|
| 437 |
+
return f"Error processing content: {str(e)}"
|
| 438 |
+
except Exception as e:
|
| 439 |
+
return f"An unexpected error occurred: {str(e)}"
|
requirements.txt
CHANGED
|
@@ -1,2 +1,18 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiohttp>=3.8.0
|
| 2 |
+
beautifulsoup4>=4.12.0
|
| 3 |
+
chromadb>=0.4.0
|
| 4 |
+
duckduckgo-search>=3.0.0
|
| 5 |
+
gradio>=4.0.0
|
| 6 |
+
langchain>=0.1.0
|
| 7 |
+
langchain_community>=0.1.0
|
| 8 |
+
langchain_openai>=0.1.0
|
| 9 |
+
librosa>=0.10.0
|
| 10 |
+
openai>=1.3.0
|
| 11 |
+
pandas>=2.0.0
|
| 12 |
+
pillow>=10.0.0
|
| 13 |
+
PyPDF2>=3.0.0
|
| 14 |
+
python-dotenv>=1.0.0
|
| 15 |
+
pytube>=15.0.0
|
| 16 |
+
requests>=2.31.0
|
| 17 |
+
wikipedia
|
| 18 |
+
youtube-transcript-api>=0.6.1
|
tools.py
ADDED
|
@@ -0,0 +1,771 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
from typing import Dict, List, Optional, Any
|
| 4 |
+
import requests
|
| 5 |
+
from langchain.tools import BaseTool
|
| 6 |
+
from langchain.schema import Document
|
| 7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
+
from langchain_community.tools import WikipediaQueryRun, DuckDuckGoSearchRun
|
| 9 |
+
from langchain_community.document_loaders import PythonLoader
|
| 10 |
+
from langchain_community.utilities import WikipediaAPIWrapper
|
| 11 |
+
import chromadb
|
| 12 |
+
from chromadb.config import Settings
|
| 13 |
+
import pytube
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import pandas as pd
|
| 16 |
+
import librosa
|
| 17 |
+
import json
|
| 18 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 19 |
+
from langchain_community.document_loaders import YoutubeLoader
|
| 20 |
+
import re
|
| 21 |
+
import base64
|
| 22 |
+
from io import BytesIO
|
| 23 |
+
from openai import OpenAI
|
| 24 |
+
import aiohttp
|
| 25 |
+
import logging
|
| 26 |
+
from PyPDF2 import PdfReader
|
| 27 |
+
from pydantic import Field
|
| 28 |
+
|
| 29 |
+
logger = logging.getLogger(__name__)
|
| 30 |
+
|
| 31 |
+
class ContentProcessingError(Exception):
|
| 32 |
+
"""Custom exception for content processing errors"""
|
| 33 |
+
pass
|
| 34 |
+
|
| 35 |
+
class ImageProcessingError(ContentProcessingError):
|
| 36 |
+
"""Specific exception for image processing errors"""
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
class AudioProcessingError(ContentProcessingError):
|
| 40 |
+
"""Specific exception for audio processing errors"""
|
| 41 |
+
pass
|
| 42 |
+
|
| 43 |
+
class VideoProcessingError(ContentProcessingError):
|
| 44 |
+
"""Specific exception for video processing errors"""
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
class WebProcessingError(ContentProcessingError):
|
| 48 |
+
"""Specific exception for web processing errors"""
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
def encode_image_to_base64(image_content: bytes) -> str:
|
| 52 |
+
"""Convert image bytes to base64 string"""
|
| 53 |
+
return base64.b64encode(image_content).decode('utf-8')
|
| 54 |
+
|
| 55 |
+
class BaseContentTool(BaseTool):
|
| 56 |
+
"""Base class for all content processing tools"""
|
| 57 |
+
text_splitter: RecursiveCharacterTextSplitter = Field(default_factory=lambda: RecursiveCharacterTextSplitter(
|
| 58 |
+
chunk_size=1000,
|
| 59 |
+
chunk_overlap=200,
|
| 60 |
+
length_function=len,
|
| 61 |
+
separators=["\n\n", "\n", " ", ""]
|
| 62 |
+
))
|
| 63 |
+
|
| 64 |
+
def _extract_task_id(self, text: str) -> Optional[str]:
|
| 65 |
+
"""Extract task ID from text if present"""
|
| 66 |
+
# First try to find task_id in the metadata
|
| 67 |
+
task_id_pattern = r'task_id["\':\s]+([a-f0-9-]{36})'
|
| 68 |
+
match = re.search(task_id_pattern, text, re.IGNORECASE)
|
| 69 |
+
if match:
|
| 70 |
+
return match.group(1)
|
| 71 |
+
|
| 72 |
+
# Then try to find any UUID-like string that might be a task ID
|
| 73 |
+
uuid_pattern = r'([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})'
|
| 74 |
+
match = re.search(uuid_pattern, text, re.IGNORECASE)
|
| 75 |
+
return match.group(1) if match else None
|
| 76 |
+
|
| 77 |
+
def _get_file_metadata(self, task_id: str) -> dict:
|
| 78 |
+
"""Get file metadata from task ID"""
|
| 79 |
+
# Extract task ID if it's embedded in a longer string
|
| 80 |
+
extracted_id = self._extract_task_id(task_id)
|
| 81 |
+
if not extracted_id:
|
| 82 |
+
raise ContentProcessingError(f"Could not extract valid task ID from: {task_id}")
|
| 83 |
+
|
| 84 |
+
base_url = "https://agents-course-unit4-scoring.hf.space/metadata"
|
| 85 |
+
url = f"{base_url}/{extracted_id}"
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
response = requests.get(url)
|
| 89 |
+
response.raise_for_status()
|
| 90 |
+
return response.json()
|
| 91 |
+
except requests.exceptions.RequestException as e:
|
| 92 |
+
raise ContentProcessingError(f"Error fetching file metadata: {str(e)}")
|
| 93 |
+
|
| 94 |
+
def _get_file_from_task_id(self, task_id: str, expected_type: str) -> bytes:
|
| 95 |
+
"""Helper method to get file content from task ID"""
|
| 96 |
+
# Extract task ID if it's embedded in a longer string
|
| 97 |
+
extracted_id = self._extract_task_id(task_id)
|
| 98 |
+
if not extracted_id:
|
| 99 |
+
raise ContentProcessingError(f"Could not extract valid task ID from: {task_id}")
|
| 100 |
+
|
| 101 |
+
base_url = "https://agents-course-unit4-scoring.hf.space/files"
|
| 102 |
+
url = f"{base_url}/{extracted_id}"
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
# First get metadata to verify file type
|
| 106 |
+
metadata = self._get_file_metadata(extracted_id)
|
| 107 |
+
if not metadata:
|
| 108 |
+
raise ContentProcessingError(f"No metadata found for task ID: {extracted_id}")
|
| 109 |
+
|
| 110 |
+
# Make request for file content
|
| 111 |
+
response = requests.get(url, headers={'accept': 'application/json'})
|
| 112 |
+
response.raise_for_status()
|
| 113 |
+
|
| 114 |
+
# Check content type from response headers
|
| 115 |
+
content_type = response.headers.get('content-type', '').lower()
|
| 116 |
+
if expected_type not in content_type and 'application/json' not in content_type:
|
| 117 |
+
raise ContentProcessingError(f"Expected file type {expected_type} but got {content_type}")
|
| 118 |
+
|
| 119 |
+
return response.content
|
| 120 |
+
|
| 121 |
+
except requests.exceptions.RequestException as e:
|
| 122 |
+
raise ContentProcessingError(f"Error fetching file: {str(e)}")
|
| 123 |
+
except Exception as e:
|
| 124 |
+
raise ContentProcessingError(f"Error processing file: {str(e)}")
|
| 125 |
+
|
| 126 |
+
class WikipediaTool(BaseContentTool):
|
| 127 |
+
"""Tool for processing Wikipedia articles"""
|
| 128 |
+
name: str = "wikipedia_processor"
|
| 129 |
+
description: str = "Process Wikipedia articles to extract information"
|
| 130 |
+
|
| 131 |
+
def _run(self, question: str) -> List[Document]:
|
| 132 |
+
"""Process Wikipedia article and create a document with analysis"""
|
| 133 |
+
try:
|
| 134 |
+
# Initialize Wikipedia API wrapper
|
| 135 |
+
wikipedia = WikipediaAPIWrapper()
|
| 136 |
+
result = wikipedia.run(question)
|
| 137 |
+
|
| 138 |
+
# Create documents with metadata
|
| 139 |
+
documents = self.text_splitter.create_documents(
|
| 140 |
+
[result],
|
| 141 |
+
metadatas=[{
|
| 142 |
+
"source": "wikipedia",
|
| 143 |
+
"type": "wikipedia",
|
| 144 |
+
"question_context": question,
|
| 145 |
+
"content_type": "wikipedia_analysis",
|
| 146 |
+
"language": "en"
|
| 147 |
+
}]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
return documents
|
| 151 |
+
|
| 152 |
+
except Exception as e:
|
| 153 |
+
raise ContentProcessingError(f"Error processing Wikipedia article: {str(e)}")
|
| 154 |
+
|
| 155 |
+
async def _arun(self, question: str) -> List[Document]:
|
| 156 |
+
"""Async version of _run"""
|
| 157 |
+
# Implement if needed
|
| 158 |
+
raise NotImplementedError("Async version not implemented yet")
|
| 159 |
+
|
| 160 |
+
class YouTubeVideoTool(BaseContentTool):
|
| 161 |
+
"""Tool for processing YouTube videos"""
|
| 162 |
+
name: str = "youtube_video_processor"
|
| 163 |
+
description: str = "Process YouTube videos to extract information"
|
| 164 |
+
|
| 165 |
+
def _clean_url(self, url: str) -> str:
|
| 166 |
+
"""Clean the URL by removing trailing punctuation and whitespace"""
|
| 167 |
+
# Remove trailing punctuation and whitespace
|
| 168 |
+
url = url.strip().rstrip('.!?,;:')
|
| 169 |
+
# Ensure we have a valid YouTube URL
|
| 170 |
+
if 'youtu.be' in url:
|
| 171 |
+
video_id = url.split('/')[-1].split('?')[0]
|
| 172 |
+
return f"https://www.youtube.com/watch?v={video_id}"
|
| 173 |
+
return url
|
| 174 |
+
|
| 175 |
+
def _extract_video_id(self, url: str) -> str:
|
| 176 |
+
"""Extract video ID from URL"""
|
| 177 |
+
if 'youtu.be' in url:
|
| 178 |
+
return url.split('/')[-1].split('?')[0]
|
| 179 |
+
elif 'youtube.com' in url:
|
| 180 |
+
from urllib.parse import parse_qs, urlparse
|
| 181 |
+
parsed = urlparse(url)
|
| 182 |
+
return parse_qs(parsed.query)['v'][0]
|
| 183 |
+
raise VideoProcessingError("Invalid YouTube URL format")
|
| 184 |
+
|
| 185 |
+
def _run(self, video_url: str) -> List[Document]:
|
| 186 |
+
try:
|
| 187 |
+
# Clean the URL first
|
| 188 |
+
clean_url = self._clean_url(video_url)
|
| 189 |
+
video_id = self._extract_video_id(clean_url)
|
| 190 |
+
|
| 191 |
+
text_content = []
|
| 192 |
+
metadata = {
|
| 193 |
+
"source": clean_url,
|
| 194 |
+
"type": "youtube_video",
|
| 195 |
+
"video_id": video_id
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
# Try multiple methods to get video content
|
| 199 |
+
transcript_success = False
|
| 200 |
+
video_info_success = False
|
| 201 |
+
|
| 202 |
+
# Method 1: Try to get transcript
|
| 203 |
+
try:
|
| 204 |
+
loader = YoutubeLoader.from_youtube_url(
|
| 205 |
+
clean_url,
|
| 206 |
+
add_video_info=False, # Set to True if you want to include video metadata
|
| 207 |
+
language=["en", "id"],
|
| 208 |
+
translation="en"
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
result = loader.load()
|
| 212 |
+
print(result)
|
| 213 |
+
documents = loader.load()
|
| 214 |
+
print(documents)
|
| 215 |
+
text_content.extend(documents)
|
| 216 |
+
with open("transcript.txt", "w", encoding="utf-8") as f:
|
| 217 |
+
for doc in documents:
|
| 218 |
+
f.write(doc.page_content)
|
| 219 |
+
transcript_success = True
|
| 220 |
+
except Exception as e:
|
| 221 |
+
logging.warning(f"Could not get transcript: {e}")
|
| 222 |
+
text_content.append("Transcript unavailable")
|
| 223 |
+
|
| 224 |
+
if not transcript_success:
|
| 225 |
+
error_msg = "Could not access any video content. This might be due to:"
|
| 226 |
+
error_msg += "\n- Video is private or unavailable"
|
| 227 |
+
error_msg += "\n- No transcript available"
|
| 228 |
+
error_msg += "\n- API access restrictions"
|
| 229 |
+
return [Document(
|
| 230 |
+
page_content=error_msg,
|
| 231 |
+
metadata=metadata
|
| 232 |
+
)]
|
| 233 |
+
|
| 234 |
+
# Create documents with metadata
|
| 235 |
+
text_content = open("transcript.txt", "r", encoding="utf-8").read()
|
| 236 |
+
return self.text_splitter.create_documents(
|
| 237 |
+
["\n".join(text_content)],
|
| 238 |
+
metadatas=[metadata]
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
except Exception as e:
|
| 242 |
+
error_msg = f"Error processing YouTube video: {str(e)}"
|
| 243 |
+
logging.error(error_msg)
|
| 244 |
+
return [Document(
|
| 245 |
+
page_content=error_msg,
|
| 246 |
+
metadata={"source": video_url, "type": "youtube_video", "error": str(e)}
|
| 247 |
+
)]
|
| 248 |
+
|
| 249 |
+
async def _arun(self, video_url: str) -> List[Document]:
|
| 250 |
+
"""Async version of _run"""
|
| 251 |
+
# Implement if needed
|
| 252 |
+
raise NotImplementedError("Async version not implemented yet")
|
| 253 |
+
|
| 254 |
+
class PythonTool(BaseContentTool):
|
| 255 |
+
"""Tool for processing Python files"""
|
| 256 |
+
name: str = "python_processor"
|
| 257 |
+
description: str = "Process Python files to extract information"
|
| 258 |
+
temp_dir: str = Field(default="temp_python")
|
| 259 |
+
|
| 260 |
+
def __init__(self, **kwargs):
|
| 261 |
+
super().__init__(**kwargs)
|
| 262 |
+
os.makedirs(self.temp_dir, exist_ok=True)
|
| 263 |
+
|
| 264 |
+
def _save_temp_python(self, content: bytes, task_id: str) -> str:
|
| 265 |
+
"""Save Python content to temporary file"""
|
| 266 |
+
temp_path = os.path.join(self.temp_dir, f"{task_id}.py")
|
| 267 |
+
try:
|
| 268 |
+
with open(temp_path, "wb") as f:
|
| 269 |
+
f.write(content)
|
| 270 |
+
return temp_path
|
| 271 |
+
except Exception as e:
|
| 272 |
+
raise ContentProcessingError(f"Error saving temporary Python file: {str(e)}")
|
| 273 |
+
|
| 274 |
+
def _clean_temp_file(self, file_path: str):
|
| 275 |
+
"""Clean up temporary Python file"""
|
| 276 |
+
try:
|
| 277 |
+
if os.path.exists(file_path):
|
| 278 |
+
os.remove(file_path)
|
| 279 |
+
except Exception as e:
|
| 280 |
+
print(f"Warning: Could not remove temporary file {file_path}: {str(e)}")
|
| 281 |
+
|
| 282 |
+
def _run(self, task_id: str) -> List[Document]:
|
| 283 |
+
"""Process Python file and return documents with extracted information"""
|
| 284 |
+
temp_path = None
|
| 285 |
+
try:
|
| 286 |
+
# Get file content using base class method
|
| 287 |
+
content = self._get_file_from_task_id(task_id, "python")
|
| 288 |
+
|
| 289 |
+
# Save to temporary file for PythonLoader
|
| 290 |
+
temp_path = self._save_temp_python(content, task_id)
|
| 291 |
+
|
| 292 |
+
# Use PythonLoader to process the file
|
| 293 |
+
loader = PythonLoader(temp_path)
|
| 294 |
+
documents = loader.load()
|
| 295 |
+
|
| 296 |
+
# Add metadata to documents
|
| 297 |
+
for doc in documents:
|
| 298 |
+
doc.metadata.update({
|
| 299 |
+
"source": task_id,
|
| 300 |
+
"type": "python",
|
| 301 |
+
"content_type": "python_code"
|
| 302 |
+
})
|
| 303 |
+
|
| 304 |
+
return documents
|
| 305 |
+
|
| 306 |
+
except Exception as e:
|
| 307 |
+
error_msg = f"Error processing Python file: {str(e)}"
|
| 308 |
+
logging.error(error_msg)
|
| 309 |
+
return [Document(
|
| 310 |
+
page_content=error_msg,
|
| 311 |
+
metadata={"source": task_id, "type": "python", "error": str(e)}
|
| 312 |
+
)]
|
| 313 |
+
|
| 314 |
+
finally:
|
| 315 |
+
# Clean up temporary file
|
| 316 |
+
if temp_path:
|
| 317 |
+
self._clean_temp_file(temp_path)
|
| 318 |
+
|
| 319 |
+
async def _arun(self, task_id: str) -> List[Document]:
|
| 320 |
+
"""Async version of _run"""
|
| 321 |
+
return self._run(task_id)
|
| 322 |
+
|
| 323 |
+
class ImageTool(BaseContentTool):
|
| 324 |
+
"""Tool for processing images using GPT-4V"""
|
| 325 |
+
name: str = "image_processor"
|
| 326 |
+
description: str = "Process images from task IDs using GPT-4V"
|
| 327 |
+
client: OpenAI = Field(default_factory=OpenAI)
|
| 328 |
+
base_system_prompt: str = """You are an expert at analyzing images with strong attention to detail.
|
| 329 |
+
Your task is to provide a detailed, objective description of the image content.
|
| 330 |
+
Focus on:
|
| 331 |
+
1. Key visual elements and their relationships
|
| 332 |
+
2. Any text or numbers present in the image
|
| 333 |
+
3. Specific details that might be relevant to answering questions about the image
|
| 334 |
+
4. Technical or specialized content (diagrams, charts, game positions, etc.)
|
| 335 |
+
|
| 336 |
+
Provide your analysis in a clear, structured format that can be used by a language model to answer specific questions about the image."""
|
| 337 |
+
|
| 338 |
+
def _generate_context_aware_prompt(self, question: str) -> str:
|
| 339 |
+
"""Generate a context-aware system prompt based on the question"""
|
| 340 |
+
# Extract key information from the question
|
| 341 |
+
question_lower = question.lower()
|
| 342 |
+
|
| 343 |
+
# Add specialized instructions based on question context
|
| 344 |
+
specialized_instructions = []
|
| 345 |
+
|
| 346 |
+
if "chess" in question_lower:
|
| 347 |
+
specialized_instructions.append("""
|
| 348 |
+
For chess positions:
|
| 349 |
+
- Describe the position of all pieces using algebraic notation
|
| 350 |
+
- Note any significant tactical or strategic elements
|
| 351 |
+
- If asked about moves, specify them in algebraic notation""")
|
| 352 |
+
|
| 353 |
+
if any(word in question_lower for word in ["count", "number", "how many"]):
|
| 354 |
+
specialized_instructions.append("""
|
| 355 |
+
Pay special attention to counting and quantifying elements in the image.
|
| 356 |
+
Provide specific numbers and ensure accuracy in counting.""")
|
| 357 |
+
|
| 358 |
+
if "text" in question_lower or "write" in question_lower or "read" in question_lower:
|
| 359 |
+
specialized_instructions.append("""
|
| 360 |
+
Focus on any text content:
|
| 361 |
+
- Read and transcribe all visible text
|
| 362 |
+
- Note the location and context of text elements
|
| 363 |
+
- Pay attention to any numbers, symbols, or special characters""")
|
| 364 |
+
|
| 365 |
+
if "color" in question_lower or "colour" in question_lower:
|
| 366 |
+
specialized_instructions.append("""
|
| 367 |
+
Pay special attention to colors:
|
| 368 |
+
- Describe colors precisely
|
| 369 |
+
- Note color patterns or relationships
|
| 370 |
+
- Mention any color-based groupings or distinctions""")
|
| 371 |
+
|
| 372 |
+
# Combine base prompt with specialized instructions
|
| 373 |
+
full_prompt = self.base_system_prompt
|
| 374 |
+
if specialized_instructions:
|
| 375 |
+
full_prompt += "\n\nSpecific focus areas for this image:\n" + "\n".join(specialized_instructions)
|
| 376 |
+
|
| 377 |
+
return full_prompt
|
| 378 |
+
|
| 379 |
+
def _process_image_with_gpt4v(self, image_content: bytes, question: str) -> str:
|
| 380 |
+
"""Process image using GPT-4V API with context from the question"""
|
| 381 |
+
try:
|
| 382 |
+
# Convert image to base64
|
| 383 |
+
base64_image = encode_image_to_base64(image_content)
|
| 384 |
+
|
| 385 |
+
# Generate context-aware system prompt
|
| 386 |
+
system_prompt = self._generate_context_aware_prompt(question)
|
| 387 |
+
|
| 388 |
+
# Prepare the messages
|
| 389 |
+
messages = [
|
| 390 |
+
{
|
| 391 |
+
"role": "system",
|
| 392 |
+
"content": system_prompt
|
| 393 |
+
},
|
| 394 |
+
{
|
| 395 |
+
"role": "user",
|
| 396 |
+
"content": [
|
| 397 |
+
{
|
| 398 |
+
"type": "image_url",
|
| 399 |
+
"image_url": {
|
| 400 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 401 |
+
}
|
| 402 |
+
},
|
| 403 |
+
{
|
| 404 |
+
"type": "text",
|
| 405 |
+
"text": f"Analyze this image in detail, keeping in mind the following question: {question}"
|
| 406 |
+
}
|
| 407 |
+
]
|
| 408 |
+
}
|
| 409 |
+
]
|
| 410 |
+
|
| 411 |
+
# Call GPT-4V
|
| 412 |
+
response = self.client.chat.completions.create(
|
| 413 |
+
model="gpt-4-vision-preview",
|
| 414 |
+
messages=messages,
|
| 415 |
+
max_tokens=500,
|
| 416 |
+
temperature=0.2 # Lower temperature for more focused analysis
|
| 417 |
+
)
|
| 418 |
+
|
| 419 |
+
return response.choices[0].message.content
|
| 420 |
+
|
| 421 |
+
except Exception as e:
|
| 422 |
+
raise ImageProcessingError(f"Error processing image with GPT-4V: {str(e)}")
|
| 423 |
+
|
| 424 |
+
def _run(self, task_id: str, question: str = "") -> Document:
|
| 425 |
+
"""Process image and create a document with analysis"""
|
| 426 |
+
try:
|
| 427 |
+
# Get image content
|
| 428 |
+
image_content = self._get_file_from_task_id(task_id, "image")
|
| 429 |
+
|
| 430 |
+
# Process image with GPT-4V
|
| 431 |
+
analysis = self._process_image_with_gpt4v(image_content, question)
|
| 432 |
+
|
| 433 |
+
# Create document with metadata
|
| 434 |
+
return Document(
|
| 435 |
+
page_content=analysis,
|
| 436 |
+
metadata={
|
| 437 |
+
"source": task_id,
|
| 438 |
+
"type": "image",
|
| 439 |
+
"content_type": "gpt4v_analysis",
|
| 440 |
+
"question_context": question
|
| 441 |
+
}
|
| 442 |
+
)
|
| 443 |
+
except Exception as e:
|
| 444 |
+
raise ImageProcessingError(f"Error processing image: {str(e)}")
|
| 445 |
+
|
| 446 |
+
def _arun(self, task_id: str) -> Document:
|
| 447 |
+
"""Async version of _run"""
|
| 448 |
+
# Implement if needed
|
| 449 |
+
raise NotImplementedError("Async version not implemented yet")
|
| 450 |
+
|
| 451 |
+
class AudioTool(BaseContentTool):
|
| 452 |
+
"""Tool for processing audio files using Whisper"""
|
| 453 |
+
name: str = "audio_processor"
|
| 454 |
+
description: str = "Process audio files from task IDs using Whisper"
|
| 455 |
+
client: OpenAI = Field(default_factory=OpenAI)
|
| 456 |
+
temp_dir: str = Field(default="temp_audio")
|
| 457 |
+
|
| 458 |
+
def __init__(self, **kwargs):
|
| 459 |
+
super().__init__(**kwargs)
|
| 460 |
+
os.makedirs(self.temp_dir, exist_ok=True)
|
| 461 |
+
|
| 462 |
+
def _save_temp_audio(self, audio_content: bytes, task_id: str) -> str:
|
| 463 |
+
"""Save audio content to temporary file"""
|
| 464 |
+
# Create a temporary file with the task ID as name
|
| 465 |
+
temp_path = os.path.join(self.temp_dir, f"{task_id}.mp3")
|
| 466 |
+
try:
|
| 467 |
+
with open(temp_path, "wb") as f:
|
| 468 |
+
f.write(audio_content)
|
| 469 |
+
return temp_path
|
| 470 |
+
except Exception as e:
|
| 471 |
+
raise AudioProcessingError(f"Error saving temporary audio file: {str(e)}")
|
| 472 |
+
|
| 473 |
+
def _clean_temp_file(self, file_path: str):
|
| 474 |
+
"""Clean up temporary audio file"""
|
| 475 |
+
try:
|
| 476 |
+
if os.path.exists(file_path):
|
| 477 |
+
os.remove(file_path)
|
| 478 |
+
except Exception as e:
|
| 479 |
+
print(f"Warning: Could not remove temporary file {file_path}: {str(e)}")
|
| 480 |
+
|
| 481 |
+
def _transcribe_with_whisper(self, audio_path: str, question: str = "") -> dict:
|
| 482 |
+
"""Transcribe audio using Whisper API"""
|
| 483 |
+
try:
|
| 484 |
+
with open(audio_path, "rb") as audio_file:
|
| 485 |
+
# Determine if we need timestamps based on the question
|
| 486 |
+
timestamps_needed = any(word in question.lower()
|
| 487 |
+
for word in ["when", "time", "moment", "timestamp"])
|
| 488 |
+
|
| 489 |
+
# Call Whisper API
|
| 490 |
+
response = self.client.audio.transcriptions.create(
|
| 491 |
+
model="whisper-1",
|
| 492 |
+
file=audio_file,
|
| 493 |
+
response_format="verbose_json",
|
| 494 |
+
timestamp_granularities=["segment"] if timestamps_needed else None,
|
| 495 |
+
language="en" # You might want to make this dynamic based on the content
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
return response
|
| 499 |
+
|
| 500 |
+
except Exception as e:
|
| 501 |
+
raise AudioProcessingError(f"Error transcribing audio with Whisper: {str(e)}")
|
| 502 |
+
|
| 503 |
+
def _extract_relevant_info(self, transcription: dict, question: str) -> str:
|
| 504 |
+
"""Extract relevant information from transcription based on question"""
|
| 505 |
+
# Basic question analysis
|
| 506 |
+
question_lower = question.lower()
|
| 507 |
+
|
| 508 |
+
# Initialize the content parts
|
| 509 |
+
content_parts = []
|
| 510 |
+
|
| 511 |
+
# Add full transcription
|
| 512 |
+
if "text" in transcription:
|
| 513 |
+
content_parts.append(f"Full transcription: {transcription['text']}")
|
| 514 |
+
|
| 515 |
+
# Add timestamps if present and relevant
|
| 516 |
+
if "segments" in transcription and any(word in question_lower
|
| 517 |
+
for word in ["when", "time", "moment", "timestamp"]):
|
| 518 |
+
timestamps = "\n".join([
|
| 519 |
+
f"[{segment['start']:.2f}s - {segment['end']:.2f}s]: {segment['text']}"
|
| 520 |
+
for segment in transcription["segments"]
|
| 521 |
+
])
|
| 522 |
+
content_parts.append(f"\nDetailed segments with timestamps:\n{timestamps}")
|
| 523 |
+
|
| 524 |
+
# If looking for specific numbers or lists
|
| 525 |
+
if any(word in question_lower for word in ["number", "list", "page", "pages"]):
|
| 526 |
+
# Extract numbers and potential list items
|
| 527 |
+
import re
|
| 528 |
+
numbers = re.findall(r'\b\d+\b', transcription["text"])
|
| 529 |
+
if numbers:
|
| 530 |
+
content_parts.append(f"\nNumbers mentioned: {', '.join(numbers)}")
|
| 531 |
+
|
| 532 |
+
return "\n".join(content_parts)
|
| 533 |
+
|
| 534 |
+
def _run(self, task_id: str, question: str = "") -> List[Document]:
|
| 535 |
+
"""Process audio file and create a document with transcription"""
|
| 536 |
+
temp_path = None
|
| 537 |
+
try:
|
| 538 |
+
# Get audio content using base class method
|
| 539 |
+
audio_content = self._get_file_from_task_id(task_id, "audio")
|
| 540 |
+
|
| 541 |
+
# Save to temporary file
|
| 542 |
+
temp_path = self._save_temp_audio(audio_content, task_id)
|
| 543 |
+
|
| 544 |
+
# Transcribe with Whisper
|
| 545 |
+
transcription = self._transcribe_with_whisper(temp_path, question)
|
| 546 |
+
|
| 547 |
+
# Extract relevant information
|
| 548 |
+
processed_content = self._extract_relevant_info(transcription, question)
|
| 549 |
+
|
| 550 |
+
# Create document with metadata
|
| 551 |
+
return [Document(
|
| 552 |
+
page_content=processed_content,
|
| 553 |
+
metadata={
|
| 554 |
+
"source": task_id,
|
| 555 |
+
"type": "audio",
|
| 556 |
+
"content_type": "whisper_transcription",
|
| 557 |
+
"question_context": question,
|
| 558 |
+
"language": transcription.get("language", "en"),
|
| 559 |
+
"duration": transcription.get("duration", None)
|
| 560 |
+
}
|
| 561 |
+
)]
|
| 562 |
+
|
| 563 |
+
except Exception as e:
|
| 564 |
+
error_msg = f"Error processing audio: {str(e)}"
|
| 565 |
+
logging.error(error_msg)
|
| 566 |
+
return [Document(
|
| 567 |
+
page_content=error_msg,
|
| 568 |
+
metadata={"source": task_id, "type": "audio", "error": str(e)}
|
| 569 |
+
)]
|
| 570 |
+
|
| 571 |
+
finally:
|
| 572 |
+
# Clean up temporary file
|
| 573 |
+
if temp_path:
|
| 574 |
+
self._clean_temp_file(temp_path)
|
| 575 |
+
|
| 576 |
+
async def _arun(self, task_id: str, question: str = "") -> List[Document]:
|
| 577 |
+
"""Async version of _run"""
|
| 578 |
+
return self._run(task_id, question)
|
| 579 |
+
|
| 580 |
+
class ExcelTool(BaseContentTool):
|
| 581 |
+
name: str = "excel_tool"
|
| 582 |
+
description: str = "Tool for processing Excel files and extracting their content"
|
| 583 |
+
|
| 584 |
+
def _process_excel_content(self, content: bytes) -> pd.DataFrame:
|
| 585 |
+
"""Process Excel content and return a pandas DataFrame."""
|
| 586 |
+
try:
|
| 587 |
+
return pd.read_excel(io.BytesIO(content))
|
| 588 |
+
except Exception as e:
|
| 589 |
+
logging.error(f"Error reading Excel content: {str(e)}")
|
| 590 |
+
raise ValueError(f"Failed to read Excel content: {str(e)}")
|
| 591 |
+
|
| 592 |
+
def _dataframe_to_text(self, df: pd.DataFrame) -> str:
|
| 593 |
+
"""Convert DataFrame to a readable text format with summary information."""
|
| 594 |
+
text_parts = []
|
| 595 |
+
|
| 596 |
+
# Basic DataFrame information
|
| 597 |
+
text_parts.append(f"Total Rows: {len(df)}")
|
| 598 |
+
text_parts.append(f"Total Columns: {len(df.columns)}")
|
| 599 |
+
text_parts.append("\nColumns:")
|
| 600 |
+
text_parts.append(", ".join(df.columns.tolist()))
|
| 601 |
+
|
| 602 |
+
# Data preview (first 50 rows)
|
| 603 |
+
preview_rows = min(50, len(df))
|
| 604 |
+
text_parts.append(f"\nData Preview (first {preview_rows} rows):")
|
| 605 |
+
text_parts.append(df.head(preview_rows).to_string())
|
| 606 |
+
|
| 607 |
+
# Summary statistics for numeric columns
|
| 608 |
+
numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns
|
| 609 |
+
if len(numeric_cols) > 0:
|
| 610 |
+
text_parts.append("\nSummary Statistics for Numeric Columns:")
|
| 611 |
+
text_parts.append(df[numeric_cols].describe().to_string())
|
| 612 |
+
|
| 613 |
+
return "\n".join(text_parts)
|
| 614 |
+
|
| 615 |
+
def _run(self, task_id: str) -> List[Document]:
|
| 616 |
+
"""Process Excel file content and return documents with extracted information."""
|
| 617 |
+
try:
|
| 618 |
+
# Get file content using base class method
|
| 619 |
+
content = self._get_file_from_task_id(task_id, "excel")
|
| 620 |
+
|
| 621 |
+
# Process Excel content
|
| 622 |
+
df = self._process_excel_content(content)
|
| 623 |
+
|
| 624 |
+
# Convert DataFrame to text
|
| 625 |
+
text_content = self._dataframe_to_text(df)
|
| 626 |
+
|
| 627 |
+
# Create metadata
|
| 628 |
+
metadata = {
|
| 629 |
+
"source": task_id,
|
| 630 |
+
"content_type": "excel",
|
| 631 |
+
"row_count": len(df),
|
| 632 |
+
"column_count": len(df.columns),
|
| 633 |
+
"columns": df.columns.tolist()
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
# Create and return document
|
| 637 |
+
return [Document(
|
| 638 |
+
page_content=text_content,
|
| 639 |
+
metadata=metadata
|
| 640 |
+
)]
|
| 641 |
+
|
| 642 |
+
except Exception as e:
|
| 643 |
+
error_msg = f"Error processing Excel file: {str(e)}"
|
| 644 |
+
logging.error(error_msg)
|
| 645 |
+
return [Document(
|
| 646 |
+
page_content=error_msg,
|
| 647 |
+
metadata={"source": task_id, "content_type": "error"}
|
| 648 |
+
)]
|
| 649 |
+
|
| 650 |
+
async def _arun(self, task_id: str) -> List[Document]:
|
| 651 |
+
"""Async version of _run."""
|
| 652 |
+
return self._run(task_id)
|
| 653 |
+
|
| 654 |
+
class WebContentTool(BaseContentTool):
|
| 655 |
+
"""Tool for web search and content processing"""
|
| 656 |
+
name: str = "web_content_processor"
|
| 657 |
+
description: str = "Search the web and process webpage content"
|
| 658 |
+
search_tool: DuckDuckGoSearchRun = Field(default_factory=DuckDuckGoSearchRun)
|
| 659 |
+
|
| 660 |
+
text_splitter: RecursiveCharacterTextSplitter = Field(default_factory=lambda: RecursiveCharacterTextSplitter(
|
| 661 |
+
chunk_size=1000,
|
| 662 |
+
chunk_overlap=200,
|
| 663 |
+
length_function=len,
|
| 664 |
+
separators=["\n\n", "\n", " ", ""]
|
| 665 |
+
))
|
| 666 |
+
|
| 667 |
+
def _run(self, query: str) -> List[Document]:
|
| 668 |
+
"""Search web and process content based on query"""
|
| 669 |
+
try:
|
| 670 |
+
# Attempt web search
|
| 671 |
+
search_result = self.search_tool.invoke(query)
|
| 672 |
+
if not search_result:
|
| 673 |
+
raise WebProcessingError("No search results found")
|
| 674 |
+
|
| 675 |
+
# Create documents from search result
|
| 676 |
+
documents = self.text_splitter.create_documents(
|
| 677 |
+
[search_result],
|
| 678 |
+
metadatas=[{
|
| 679 |
+
"source": "duckduckgo",
|
| 680 |
+
"type": "web_content",
|
| 681 |
+
"query": query
|
| 682 |
+
}]
|
| 683 |
+
)
|
| 684 |
+
logging.info(f"Successfully retrieved search results for query: {query[:100]}...")
|
| 685 |
+
return documents
|
| 686 |
+
|
| 687 |
+
except Exception as e:
|
| 688 |
+
error_msg = f"Web search failed: {str(e)}"
|
| 689 |
+
logging.error(error_msg)
|
| 690 |
+
raise WebProcessingError(error_msg)
|
| 691 |
+
|
| 692 |
+
async def _arun(self, query: str) -> List[Document]:
|
| 693 |
+
"""Async version of _run"""
|
| 694 |
+
return self._run(query)
|
| 695 |
+
|
| 696 |
+
class ChromaDBManager:
|
| 697 |
+
"""Manager for ChromaDB operations"""
|
| 698 |
+
def __init__(self, persist_directory: str = "./chroma_db"):
|
| 699 |
+
self.persist_directory = persist_directory
|
| 700 |
+
self.client = chromadb.Client(Settings(
|
| 701 |
+
persist_directory=persist_directory,
|
| 702 |
+
is_persistent=True
|
| 703 |
+
))
|
| 704 |
+
|
| 705 |
+
def create_collection(self, name: str):
|
| 706 |
+
"""Create a new collection or get existing one"""
|
| 707 |
+
try:
|
| 708 |
+
return self.client.create_collection(name=name)
|
| 709 |
+
except ValueError:
|
| 710 |
+
return self.client.get_collection(name=name)
|
| 711 |
+
|
| 712 |
+
def _generate_document_id(self, content: str, metadata: dict) -> str:
|
| 713 |
+
"""Generate a unique ID for a document based on its content and metadata"""
|
| 714 |
+
# Use content and key metadata fields for ID generation
|
| 715 |
+
id_parts = [content[:100]] # First 100 chars of content
|
| 716 |
+
if metadata:
|
| 717 |
+
source = metadata.get('source', '')
|
| 718 |
+
doc_type = metadata.get('type', '')
|
| 719 |
+
if source:
|
| 720 |
+
id_parts.append(str(source))
|
| 721 |
+
if doc_type:
|
| 722 |
+
id_parts.append(str(doc_type))
|
| 723 |
+
|
| 724 |
+
# Generate hash from combined parts
|
| 725 |
+
combined = "_".join(id_parts)
|
| 726 |
+
return f"doc_{hash(combined)}"
|
| 727 |
+
|
| 728 |
+
def add_documents_with_metadata(self, collection_name: str, documents: List[str], metadatas: List[dict]):
|
| 729 |
+
"""Add documents with their metadata to a collection"""
|
| 730 |
+
if not documents or not metadatas or len(documents) != len(metadatas):
|
| 731 |
+
raise ValueError("Invalid documents or metadata")
|
| 732 |
+
|
| 733 |
+
collection = self.create_collection(collection_name)
|
| 734 |
+
|
| 735 |
+
# Generate unique IDs for documents
|
| 736 |
+
ids = [self._generate_document_id(doc, meta)
|
| 737 |
+
for doc, meta in zip(documents, metadatas)]
|
| 738 |
+
|
| 739 |
+
try:
|
| 740 |
+
# First try to add documents
|
| 741 |
+
collection.add(
|
| 742 |
+
documents=documents,
|
| 743 |
+
metadatas=metadatas,
|
| 744 |
+
ids=ids
|
| 745 |
+
)
|
| 746 |
+
except Exception as e:
|
| 747 |
+
# If documents exist, update them
|
| 748 |
+
logging.info(f"Updating existing documents in collection {collection_name}")
|
| 749 |
+
collection.upsert(
|
| 750 |
+
documents=documents,
|
| 751 |
+
metadatas=metadatas,
|
| 752 |
+
ids=ids
|
| 753 |
+
)
|
| 754 |
+
|
| 755 |
+
def query_collection(self, collection_name: str, query: str, n_results: int = 5) -> Dict:
|
| 756 |
+
"""Query a collection with improved retrieval"""
|
| 757 |
+
try:
|
| 758 |
+
collection = self.client.get_collection(collection_name)
|
| 759 |
+
results = collection.query(
|
| 760 |
+
query_texts=[query],
|
| 761 |
+
n_results=n_results
|
| 762 |
+
)
|
| 763 |
+
|
| 764 |
+
# Add debug logging
|
| 765 |
+
logging.debug(f"Query: {query}")
|
| 766 |
+
logging.debug(f"Number of results: {len(results['documents'][0]) if results['documents'] else 0}")
|
| 767 |
+
|
| 768 |
+
return results
|
| 769 |
+
except Exception as e:
|
| 770 |
+
logging.error(f"Error querying collection {collection_name}: {str(e)}")
|
| 771 |
+
return {"documents": [], "metadatas": [], "distances": []}
|