Spaces:
Sleeping
Sleeping
Add certificate and detailed project report; update agent configuration
Browse files- .gradio/certificate.pem +31 -0
- Spiegazione.md +159 -0
- __pycache__/Gradio_UI.cpython-311.pyc +0 -0
- app.py +4 -4
- tools/__pycache__/final_answer.cpython-311.pyc +0 -0
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
| 9 |
+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
| 10 |
+
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0D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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 📌 Report Dettagliato sul Progetto Basato su CodeAgent e Qwen2.5-Coder-32B-Instruct
|
| 2 |
+
|
| 3 |
+
## 1️⃣ Identificazione del Codice Principale
|
| 4 |
+
Il file principale del progetto è **`app.py`**, che definisce e avvia un agente (`CodeAgent`). Questo agente utilizza il modello **Qwen2.5-Coder-32B-Instruct**, che opera tramite **Hugging Face** e si interfaccia con gli utenti tramite **Gradio**, un framework per UI interattive.
|
| 5 |
+
|
| 6 |
+
### 🔹 Funzionalità Principali
|
| 7 |
+
Il codice carica strumenti personalizzati per dare all'agente capacità di:
|
| 8 |
+
- ✅ **Generare immagini** (usando un tool da Hugging Face).
|
| 9 |
+
- ✅ **Recuperare l'orario attuale in una specifica zona**.
|
| 10 |
+
- ✅ **Restituire risposte finali** mediante un tool dedicato (`final_answer`).
|
| 11 |
+
- ✅ **Eseguire codice per generare risposte passo-passo**, utilizzando un ciclo di **"Thought → Code → Observation"**.
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
## 2️⃣ Analisi Strutturale del Codice
|
| 16 |
+
### 📌 Importazioni e Dipendenze
|
| 17 |
+
Il codice importa diverse librerie:
|
| 18 |
+
|
| 19 |
+
```python
|
| 20 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 21 |
+
import datetime
|
| 22 |
+
import requests
|
| 23 |
+
import pytz
|
| 24 |
+
import yaml
|
| 25 |
+
from tools.final_answer import FinalAnswerTool
|
| 26 |
+
from Gradio_UI import GradioUI
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
### 🔹 Ruolo nel contesto LLM/RAG/Agenti:
|
| 30 |
+
- **`smolagents`** → Fornisce classi per agenti conversazionali.
|
| 31 |
+
- **`HfApiModel`** → Permette l'uso di modelli AI di Hugging Face.
|
| 32 |
+
- **`load_tool`** → Carica strumenti esterni.
|
| 33 |
+
- **`FinalAnswerTool`** e **`GradioUI`** → Moduli interni per la gestione delle risposte e della UI.
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
### 📌 Strumenti e Tool Personalizzati
|
| 38 |
+
Il progetto utilizza tool registrati tramite `@tool`, che li rende accessibili all'agente.
|
| 39 |
+
|
| 40 |
+
#### 🔹 Esempio di Tool Personalizzato (Placeholder)
|
| 41 |
+
```python
|
| 42 |
+
@tool
|
| 43 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 44 |
+
"""A tool that does nothing yet"""
|
| 45 |
+
return "What magic will you build?"
|
| 46 |
+
```
|
| 47 |
+
➡ Questo è solo un **placeholder**, ma mostra come è possibile registrare strumenti personalizzati.
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
|
| 51 |
+
#### 🔹 Tool per il Recupero dell'Ora Attuale
|
| 52 |
+
```python
|
| 53 |
+
@tool
|
| 54 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 55 |
+
try:
|
| 56 |
+
tz = pytz.timezone(timezone)
|
| 57 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 61 |
+
```
|
| 62 |
+
✅ **Scopo**: Permette all'agente di recuperare l'orario in tempo reale, cosa impossibile per un modello LLM addestrato staticamente.
|
| 63 |
+
✅ **Ruolo in RAG**: Fornisce dati aggiornati all'agente senza necessità di conoscenza pregressa.
|
| 64 |
+
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
### 📌 Configurazione del Modello AI
|
| 68 |
+
```python
|
| 69 |
+
model = HfApiModel(
|
| 70 |
+
max_tokens=2096,
|
| 71 |
+
temperature=0.5,
|
| 72 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 73 |
+
)
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
### 🔹 Cosa fa questa configurazione?
|
| 77 |
+
- `max_tokens=2096` → Controlla la lunghezza massima della risposta.
|
| 78 |
+
- `temperature=0.5` → Bilancia creatività e coerenza.
|
| 79 |
+
- `model_id` → Specifica il modello AI da utilizzare.
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
### 📌 Inizializzazione dell'Agente
|
| 84 |
+
```python
|
| 85 |
+
agent = CodeAgent(
|
| 86 |
+
model=model,
|
| 87 |
+
tools=[final_answer, get_current_time_in_timezone, image_generation_tool],
|
| 88 |
+
max_steps=6,
|
| 89 |
+
name="MyAgent",
|
| 90 |
+
description="An agent that can fetch time, search, and generate images.",
|
| 91 |
+
prompt_templates=prompt_templates
|
| 92 |
+
)
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
### 🔹 Elementi chiave:
|
| 96 |
+
- **`tools`** → Definisce gli strumenti disponibili per l'agente.
|
| 97 |
+
- **`max_steps=6`** → L'agente può compiere fino a 6 passi per risolvere un task.
|
| 98 |
+
- **`prompt_templates`** → Guida la generazione delle risposte.
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## 3️⃣ Approfondimenti sul Funzionamento
|
| 103 |
+
### 🔹 Come l'LLM sa che deve usare un tool?
|
| 104 |
+
Il **prompt YAML** (`prompts.yaml`) guida il modello in un ciclo **"Thought → Code → Observation"**.
|
| 105 |
+
|
| 106 |
+
```yaml
|
| 107 |
+
Thought: I need to get the current time in Tokyo.
|
| 108 |
+
Code:
|
| 109 |
+
```py
|
| 110 |
+
result = get_current_time_in_timezone("Asia/Tokyo")
|
| 111 |
+
print(result)
|
| 112 |
+
```<end_code>
|
| 113 |
+
```
|
| 114 |
+
✅ Il modello **genera codice Python** per chiamare il tool.
|
| 115 |
+
✅ Il codice viene **eseguito esternamente all'LLM**.
|
| 116 |
+
✅ Il risultato viene **salvato nell'Observation** e usato come nuovo input per il modello.
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
### 🔹 Cosa succede senza `final_answer`?
|
| 121 |
+
Se il modello riceve solo l'Observation, potrebbe:
|
| 122 |
+
- Continuare a generare codice inutilmente.
|
| 123 |
+
- Non sapere quando fermarsi.
|
| 124 |
+
- Non restituire una risposta chiara.
|
| 125 |
+
|
| 126 |
+
Ecco perché il modello genera:
|
| 127 |
+
```python
|
| 128 |
+
final_answer("The current time in Tokyo is 12:30 PM")
|
| 129 |
+
```
|
| 130 |
+
che **segna la fine del processo** e restituisce l'output finale.
|
| 131 |
+
|
| 132 |
+
---
|
| 133 |
+
|
| 134 |
+
## 4️⃣ Flusso Completo dell'Interazione
|
| 135 |
+
✅ **Passo 1**: L'utente invia una query (`"What is the time in Tokyo?"`).
|
| 136 |
+
✅ **Passo 2**: L'LLM genera codice per chiamare un tool.
|
| 137 |
+
✅ **Passo 3**: Il codice viene eseguito e genera un'Observation.
|
| 138 |
+
✅ **Passo 4**: L'Observation viene passata di nuovo all'LLM.
|
| 139 |
+
✅ **Passo 5**: L'LLM decide di chiamare `final_answer()`.
|
| 140 |
+
✅ **Passo 6**: `final_answer` restituisce la risposta all'utente.
|
| 141 |
+
|
| 142 |
+
---
|
| 143 |
+
|
| 144 |
+
## 5️⃣ Confronto con Altri Modelli
|
| 145 |
+
- **Qwen2.5-Coder-32B-Instruct** non è stato addestrato per rispondere autonomamente con la sequenza "Thought → Code → Observation".
|
| 146 |
+
- **LLaMA 3.2**, se ben addestrato, potrebbe riconoscere automaticamente quando fermarsi, ma senza una pipeline ben definita **potrebbe comunque non gestire correttamente i tool esterni**.
|
| 147 |
+
|
| 148 |
+
➡ **Conclusione**: La necessità di `final_answer` dipende dal **framework di gestione dell'agente**, non solo dal modello AI usato.
|
| 149 |
+
|
| 150 |
+
---
|
| 151 |
+
|
| 152 |
+
# 📌 CONCLUSIONI
|
| 153 |
+
✅ **L'LLM non esegue codice direttamente** ma lo genera e lo passa a un ambiente di esecuzione esterno.
|
| 154 |
+
✅ **L'Observation è il risultato grezzo dell'esecuzione del tool**, che viene poi dato in input all'LLM.
|
| 155 |
+
✅ **`final_answer` è necessario per strutturare l'output e segnalare la fine del processo**.
|
| 156 |
+
✅ **Il progetto sfrutta un ciclo iterativo di reasoning step-by-step**, tipico di un framework RAG.
|
| 157 |
+
|
| 158 |
+
🚀 **In sintesi, il sistema crea un agente AI autonomo in grado di eseguire codice, interrogare il web e rispondere in modo dinamico, sfruttando un approccio multi-step basato su Retrieval-Augmented Generation (RAG).**
|
| 159 |
+
|
__pycache__/Gradio_UI.cpython-311.pyc
ADDED
|
Binary file (13.3 kB). View file
|
|
|
app.py
CHANGED
|
@@ -48,20 +48,20 @@ custom_role_conversions=None,
|
|
| 48 |
|
| 49 |
|
| 50 |
# Import tool from Hub
|
| 51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer, get_current_time_in_timezone
|
| 59 |
-
max_steps=
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
name="MyAgent",
|
| 64 |
-
description="An agent that can fetch time
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
# Import tool from Hub
|
| 51 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
+
tools=[final_answer, get_current_time_in_timezone], # Add more tools
|
| 59 |
+
max_steps=2,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
name="MyAgent",
|
| 64 |
+
description="An agent that can fetch time.",
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
tools/__pycache__/final_answer.cpython-311.pyc
ADDED
|
Binary file (1.15 kB). View file
|
|
|