File size: 2,251 Bytes
6bb1177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import json
import requests
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, InferenceClientModel
from tools import duckduckgo_search, wikipedia_search, summarize_text, load_memory, save_memory


tokens = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
print(tokens)

token = os.getenv('HF_HOME/token')
#Token di Autorizzazione (verifica ambiente)
token = 'HUGGINGFACEHUB_API_TOKEN'
if not token:
    raise ValueError("Imposta la variabile d'ambiente HF_TOKEN")

# Scegli il modello
model = HfApiModel(model_id="google/flan-t5-base", token=token)

# Strumenti
tools = [DuckDuckGoSearchTool(), wikipedia_search]

agent = CodeAgent(
    tools=tools,
    model=model,
    additional_authorized_imports=["requests", "bs4"]
)

def run_agent(query: str) -> str:
    try:
        memory = load_memory()
        if query in memory:
            return memory[query]
        
        # Ricerca e sintesi
        duck_text = duckduckgo_search(query, max_results=3)
        wiki_text = wikipedia_search(query)
        
        duck_summary = summarize_text(duck_text, max_length=150)
        wiki_summary = summarize_text(wiki_text, max_length=150)
        
        prompt = (
            f"Domanda: {query}\n\n"
            f"Informazioni sintetizzate da Wikipedia:\n{wiki_summary}\n\n"
            f"Informazioni sintetizzate da DuckDuckGo:\n{duck_summary}\n\n"
            "Fornisci una risposta sintetica e chiara."
        )
        
        # Verifica tipo modello e chiama generate con argomento corretto
        if isinstance(model, InferenceClientModel):
            messages = [
                {"role": "system", "content": "Sei un assistente utile."},
                {"role": "user", "content": prompt}
            ]
            response = model.generate(messages=messages)
        else:
            response = model.generate(prompt=prompt, max_new_tokens=200)
        
        # Controllo per risposte vuote
        if not response.strip():
            response = "Non ho capito la domanda, per favore riprova."
        
        memory[query] = response
        save_memory(memory)
        
        return response
    
    except Exception as e:
        return f"Errore durante l'esecuzione dell'agent: {str(e)}"