File size: 8,407 Bytes
28fa73f
337873f
 
28fa73f
 
b112eb6
89c14ba
b112eb6
28fa73f
b174d42
 
89c14ba
b174d42
 
 
89c14ba
b174d42
89c14ba
9f54091
 
b174d42
89c14ba
b174d42
89c14ba
b174d42
 
 
c71b7dd
b174d42
9f54091
 
b174d42
 
28fa73f
c2e33b6
337873f
b174d42
 
 
 
 
 
 
337873f
 
 
 
b174d42
337873f
 
 
 
b174d42
337873f
 
 
28fa73f
 
 
337873f
c71b7dd
337873f
 
 
 
 
 
 
fa472b6
b174d42
28fa73f
c71b7dd
337873f
 
 
c2e33b6
28fa73f
 
 
337873f
 
 
b174d42
337873f
 
 
28fa73f
 
 
 
89c14ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b174d42
89c14ba
 
 
 
 
 
 
 
 
28fa73f
337873f
89c14ba
b174d42
89c14ba
b174d42
89c14ba
 
 
c71b7dd
89c14ba
 
 
 
c71b7dd
89c14ba
 
 
 
c71b7dd
89c14ba
b174d42
89c14ba
 
 
 
 
 
b174d42
89c14ba
 
 
 
 
 
 
 
 
b174d42
89c14ba
 
 
 
 
 
b174d42
c71b7dd
 
 
b174d42
c71b7dd
 
89c14ba
 
 
 
 
 
 
 
 
 
 
 
 
b174d42
89c14ba
 
b174d42
89c14ba
 
 
 
b174d42
89c14ba
 
b174d42
89c14ba
 
 
 
 
 
 
28fa73f
337873f
 
 
28fa73f
 
337873f
b112eb6
 
337873f
 
 
 
 
 
b112eb6
337873f
b174d42
337873f
b112eb6
 
337873f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b112eb6
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
Configuration file for Smolagents GAIA Evaluation Agent
Contains all model parameters, system prompts, and tools settings
"""

import os
import json

MODEL_CONFIG = {
    "model_strategy": "remote",

    "local_models": {
        "llm_model": "./models/Llama-3.3-70B-Instruct",
        "fallback_model": "./models/zephyr-7b-beta",
        "embedding_model": "./models/gte-small",
    },

    "remote_models": {
        "llm_model": "mistralai/Mistral-7B-Instruct-v0.2",
        "fallback_model": "HuggingFaceH4/zephyr-7b-beta",
        "embedding_model": "sentence-transformers/all-MiniLM-L6-v2",
    },

    "local_light_models": {
        "llm_model": "./models/DialoGPT-medium",
        "fallback_model": "./models/Qwen2.5-7B-Instruct",
        "embedding_model": "./models/all-MiniLM-L6-v2",
    },

    "llm_model": "mistralai/Mistral-7B-Instruct-v0.2",
    "fallback_model": "HuggingFaceH4/zephyr-7b-beta",
    "embedding_model": "sentence-transformers/all-MiniLM-L6-v2",

    "temperature": 0.7,
    "max_new_tokens": 8192,
    "top_p": 0.9,

    "device": "auto",
    "torch_dtype": "auto",
    "load_in_8bit": False,
    "load_in_4bit": False,
    "use_cache": True,

    "enable_web_search": True,
    "enable_article_extraction": True,
    "enable_rag": True,
    "enable_calculator": True,

    "max_search_results": 5,
    "search_timeout": 15,
    "max_articles_to_read": 3,
    "max_article_length": 2000,

    "chunk_size": 500,
    "chunk_overlap": 50,
    "max_retrieved_chunks": 3,
}

SYSTEM_PROMPTS = {
    "code_agent": (
        "You are a general AI assistant tasked with solving complex questions accurately.\n"
        "Use tools and write Python code to gather information, perform calculations, or process data.\n"
        "Follow a Thought-Action-Observation cycle:\n"
        "- Thought: Plan the steps to solve the task\n"
        "- Action: Write and execute code or call tools\n"
        "- Observation: Analyze the output and adjust if needed\n"
        "Provide concise, exact answers. For calculations, show your work step by step.\n"
        "When using web search, synthesize information from multiple sources."
    ),

    "evaluation": (
        "You are answering tricky and hard questions. "
        "Provide accurate, comprehensive, and concise answers. "
        "Use available tools to gather current information or perform calculations. "
        "Focus on precision and exactness in your responses."
        "Penguins are birds."
    )
}

TOOL_DESCRIPTIONS = {
    "web_search": "Search the web for current information using DuckDuckGo",
    "article_extractor": "Extract full content from web articles and news sites",
    "calculator": "Perform mathematical calculations and evaluations",
    "file_processor": "Process and extract text from uploaded files",
    "wikipedia_search": "Search Wikipedia for factual information",
    "rag_search": "Search through a knowledge base using vector similarity"
}

SUPPORTED_FILE_TYPES = [".txt", ".pdf", ".csv", ".docx", ".xlsx", ".png", ".jpg", ".jpeg"]

CONFIG_FILE = "local_config.json"

def save_config():
    """Save current configuration to file"""
    try:
        config_data = {
            "model_strategy": MODEL_CONFIG["model_strategy"],
            "local_models": MODEL_CONFIG["local_models"],
            "local_light_models": MODEL_CONFIG["local_light_models"]
        }
        with open(CONFIG_FILE, 'w') as f:
            json.dump(config_data, f, indent=2)
    except Exception as e:
        print(f"Warning: Could not save config: {e}")

def load_config():
    """Load configuration from file"""
    try:
        if os.path.exists(CONFIG_FILE):
            with open(CONFIG_FILE, 'r') as f:
                config_data = json.load(f)

            MODEL_CONFIG["model_strategy"] = config_data.get("model_strategy", "auto")
            MODEL_CONFIG["local_models"].update(config_data.get("local_models", {}))
            MODEL_CONFIG["local_light_models"].update(config_data.get("local_light_models", {}))
            print(f"Loaded configuration from {CONFIG_FILE}")
    except Exception as e:
        print(f"Warning: Could not load config: {e}")

load_config()

def get_model_config():
    """Get the model configuration dictionary"""
    config = MODEL_CONFIG.copy()

    strategy = config["model_strategy"]

    if strategy == "local" or (strategy == "auto" and _check_local_models_available()):
        config.update(config["local_models"])
        config["use_local"] = True
        config["model_type"] = "local"
        print("Using local models")
    elif strategy == "local_light" or (strategy == "auto" and _check_local_light_models_available()):
        config.update(config["local_light_models"])
        config["use_local"] = True
        config["model_type"] = "local_light"
        print("Using local light models")
    else:
        config.update(config["remote_models"])
        config["use_local"] = False
        config["model_type"] = "remote"
        print("Using remote models")

    return config

def _check_local_models_available():
    """Check if local models are available"""
    import os
    local_models = MODEL_CONFIG["local_models"]

    for model_type, path in local_models.items():
        if not os.path.exists(path):
            return False
    return True

def _check_local_light_models_available():
    """Check if local light models are available"""
    import os
    local_models = MODEL_CONFIG["local_light_models"]

    for model_type, path in local_models.items():
        if not os.path.exists(path):
            return False
    return True

def set_model_strategy(strategy):
    valid_strategies = ["local", "local_light", "remote", "auto"]
    if strategy not in valid_strategies:
        raise ValueError(f"Invalid strategy. Must be one of: {valid_strategies}")

    MODEL_CONFIG["model_strategy"] = strategy
    save_config()

def set_local_model_paths(llm_path=None, fallback_path=None, embedding_path=None):
    """Set custom paths for local models"""
    if llm_path:
        MODEL_CONFIG["local_models"]["llm_model"] = llm_path
    if fallback_path:
        MODEL_CONFIG["local_models"]["fallback_model"] = fallback_path
    if embedding_path:
        MODEL_CONFIG["local_models"]["embedding_model"] = embedding_path
    save_config()

def get_device_config():
    """Get device configuration for local models"""
    import torch

    config = get_model_config()
    device = config["device"]

    if device == "auto":
        if torch.cuda.is_available():
            device = "cuda"
        elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
            device = "mps"
        else:
            device = "cpu"

    return {
        "device": device,
        "torch_dtype": config["torch_dtype"],
        "load_in_8bit": config["load_in_8bit"],
        "load_in_4bit": config["load_in_4bit"],
        "use_cache": config["use_cache"]
    }

def get_system_prompt(prompt_type: str = "code_agent") -> str:
    """Get system prompt for the specified type"""
    return SYSTEM_PROMPTS.get(prompt_type, SYSTEM_PROMPTS["code_agent"])

def get_supported_file_types():
    """Get list of supported file types"""
    return SUPPORTED_FILE_TYPES.copy()

def get_hf_token():
    """Get Hugging Face API token from environment"""
    return os.getenv("HF_TOKEN", "")

def get_smolagents_config():
    """Get configuration specific to smolagents setup"""
    return {
        "model_id": MODEL_CONFIG["llm_model"],
        "temperature": MODEL_CONFIG["temperature"],
        "max_new_tokens": MODEL_CONFIG["max_new_tokens"]
    }

def get_tools_config():
    """Get configuration for custom tools"""
    return {
        "web_search": {
            "enabled": MODEL_CONFIG["enable_web_search"],
            "max_results": MODEL_CONFIG["max_search_results"],
            "timeout": MODEL_CONFIG["search_timeout"]
        },
        "article_extraction": {
            "enabled": MODEL_CONFIG["enable_article_extraction"],
            "max_articles": MODEL_CONFIG["max_articles_to_read"],
            "max_length": MODEL_CONFIG["max_article_length"]
        },
        "rag": {
            "enabled": MODEL_CONFIG["enable_rag"],
            "embedding_model": MODEL_CONFIG["embedding_model"],
            "chunk_size": MODEL_CONFIG["chunk_size"],
            "max_chunks": MODEL_CONFIG["max_retrieved_chunks"]
        }
    }