File size: 1,939 Bytes
f5bafc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Tool manager for the GAIA Agent.

This handles the coordination between different tools and provides them to the agent.
"""

from smolagents import Tool
from typing import Dict, List, Any
from langchain.docstore.document import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.retrievers import BM25Retriever
import functools

from config import load_knowledge_base
from tools.web_tools import WebSearchTool, WebContentTool
from tools.youtube_tool import YoutubeVideoTool
from tools.wikipedia_tool import WikipediaTool
from tools.knowledge_tool import GaiaRetrieverTool

class ToolManager:
    """
    Manages and initializes all available tools for the GAIA agent.
    Also provides tool selection logic based on question analysis.
    """

    def __init__(self):
        # Load and process knowledge base
        knowledge_text = load_knowledge_base()
        self.knowledge_docs = self._create_knowledge_documents(knowledge_text)
        
        # Initialize tools
        self.tools = self._initialize_tools()

    def _create_knowledge_documents(self, text: str) -> List[Document]:
        """Create searchable documents from knowledge base text."""
        text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=500,
            chunk_overlap=50,
            separators=["\n\n", "\n", ".", "!", "?", ",", " ", ""]
        )
        knowledge_chunks = text_splitter.split_text(text)
        return [Document(page_content=chunk) for chunk in knowledge_chunks]

    def _initialize_tools(self) -> List[Tool]:
        """Initialize all available tools."""
        return [
            GaiaRetrieverTool(self.knowledge_docs),
            WebSearchTool(),
            WebContentTool(),
            YoutubeVideoTool(),
            WikipediaTool(),
        ]

    def get_tools(self) -> List[Tool]:
        """Return all available tools."""
        return self.tools