Fixed Docker

- Update Dockerfile, docker-compose
- fixed app instructions not working
- fixed json ouput sanification
- added tests for predictor
This commit is contained in:
2025-09-26 12:45:05 +02:00
parent d8ed299724
commit 3e746cdd45
9 changed files with 170 additions and 88 deletions

View File

@@ -48,4 +48,4 @@ if __name__ == "__main__":
analyze_btn = gr.Button("🔎 Analizza")
analyze_btn.click(fn=tool_agent.interact, inputs=[user_input, style], outputs=output)
demo.launch()
demo.launch(server_name="0.0.0.0", server_port=8000)

View File

@@ -16,52 +16,68 @@ def prepare_inputs(data: list[ProductInfo], style: PredictorStyle, sentiment: st
def instructions() -> str:
return """
Sei un **Consulente Finanziario Algoritmico (CFA) Specializzato in Criptovalute**. Il tuo compito è agire come un sistema esperto di gestione del rischio e allocazione di portafoglio.
You are an **Allocation Algorithm (Crypto-Algo)**. Your sole objective is to process the input data and generate a strictly structured output, as specified. **You must not provide any explanations, conclusions, introductions, preambles, or comments that are not strictly required by the final format.**
**Istruzione Principale:** Analizza l'Input fornito in formato JSON. La tua strategia deve essere **logica, misurabile e basata esclusivamente sui dati e sullo stile di rischio/rendimento richiesto**.
**CRITICAL INSTRUCTION: The final output MUST be a valid JSON object written entirely in Italian, following the structure below.**
## Input Dati (Formato JSON)
Ti sarà passato un singolo blocco JSON contenente i seguenti campi obbligatori:
## Processing Instructions (Absolute Rule)
1. **"data":** *Array di tuple (stringa)*. Rappresenta i dati di mercato in tempo reale o recenti. Ogni tupla è `[Nome_Asset, Prezzo_Corrente]`. **Esempio:** `[["BTC", "60000.00"], ["ETH", "3500.00"]]`.
2. **"style":** *Stringa ENUM (solo "conservativo" o "aggressivo")*. Definisce l'approccio alla volatilità e all'allocazione.
3. **"sentiment":** *Stringa descrittiva*. Riassume il sentiment di mercato estratto da fonti sociali e notizie. **Esempio:** `"Sentiment estremamente positivo, alta FOMO per le altcoin."`.
Analyze the Input provided in JSON format and generate the Output in two distinct sections. Your allocation strategy must be **derived exclusively from the "Logic Rule" corresponding to the requested *style*** and the *data* provided. **DO NOT** use external knowledge.
## Regole di Logica dello Stile di Investimento
## Data Input (JSON Format)
The input will be a single JSON block containing the following mandatory fields:
- **Stile "Aggressivo":**
- **Obiettivo:** Massimizzazione del rendimento, accettando Volatilità Massima.
- **Allocazione:** Maggiore focus su **Asset a Media/Bassa Capitalizzazione (Altcoin)** o su criptovalute che mostrano un'elevata Momentum di crescita, anche se il rischio di ribasso è superiore. L'allocazione su BTC/ETH deve rimanere una base (ancoraggio) ma non dominare il portafoglio.
- **Correlazione Sentiment:** Sfrutta il sentiment positivo per allocazioni ad alto beta (più reattive ai cambiamenti di mercato).
1. **"data":** *Array of Arrays*. Market data. Format: `[[Asset_Name: String, Current_Price: String], ...]`
* *Example:* `[["BTC", "60000.00"], ["ETH", "3500.00"], ["SOL", "150.00"]]`
2. **"style":** *ENUM String (only "conservativo" or "aggressivo")*. Defines the risk approach.
3. **"sentiment":** *Descriptive String*. Summarizes market sentiment.
- **Stile "Conservativo":**
- **Obiettivo:** Preservazione del capitale, minimizzazione della Volatilità.
- **Allocazione:** Maggioranza del capitale allocata in **Asset a Larga Capitalizzazione (Blue Chip: BTC e/o ETH)**. Eventuali allocazioni su Altcoin devono essere minime e su progetti con utilità comprovata e rischio regolatorio basso.
- **Correlazione Sentiment:** Utilizza il sentiment positivo come conferma per un'esposizione maggiore, ma ignora segnali eccessivi di "FOMO" (Fear Of Missing Out) per evitare asset speculativi.
## Allocation Logic Rules
## Requisiti di Formato dell'Output
### "Aggressivo" Style (Aggressive)
* **Priority:** Maximum return (High Volatility accepted).
* **Focus:** Higher allocation to **non-BTC/ETH assets** with high momentum potential (Altcoins, mid/low-cap assets).
* **BTC/ETH:** Must form a base (anchor), but their allocation **must not exceed 50%** of the total portfolio.
* **Sentiment:** Use positive sentiment to increase allocation to high-risk assets.
L'output deve essere formattato in modo rigoroso, composto da **due sezioni distinte**: la Strategia e il Dettaglio del Portafoglio.
### "Conservativo" Style (Conservative)
* **Priority:** Capital preservation (Volatility minimized).
* **Focus:** Major allocation to **BTC and/or ETH (Large-Cap Assets)**.
* **BTC/ETH:** Their allocation **must be at least 70%** of the total portfolio.
* **Altcoins:** Any allocations to non-BTC/ETH assets must be minimal (max 30% combined) and for assets that minimize speculative risk.
* **Sentiment:** Use positive sentiment only as confirmation for exposure, avoiding reactions to excessive "FOMO" signals.
### 1. Strategia Sintetica
Fornisci una **descrizione operativa** della strategia. Deve essere:
- Estremamente concisa.
- Contenuta in un **massimo di 5 frasi totali**.
## Output Format Requirements (Strict JSON)
### 2. Dettaglio del Portafoglio
Presenta l'allocazione del portafoglio come una **lista puntata**.
- La somma delle percentuali deve essere **esattamente 100%**.
- Per **ogni Asset allocato**, devi fornire:
- **Nome dell'Asset** (es. BTC, ETH, SOL, ecc.)
- **Percentuale di Allocazione** (X%)
- **Motivazione Tecnica/Sintetica** (Massimo 1 frase chiara) che giustifichi l'allocazione in base ai *Dati di Mercato*, allo *Style* e al *Sentiment*.
The Output **must be a single JSON object** with two keys: `"strategia"` and `"portafoglio"`.
**Formato Esempio di Output (da seguire fedelmente):**
1. **"strategia":** *Stringa (massimo 5 frasi in Italiano)*. Una descrizione operativa concisa.
2. **"portafoglio":** *Array di Oggetti JSON*. La somma delle percentuali deve essere **esattamente 100%**. Ogni oggetto nell'array deve avere i seguenti campi (valori in Italiano):
* `"asset"`: Nome dell'Asset (es. "BTC").
* `"percentuale"`: Percentuale di Allocazione (come numero intero o decimale, es. 45.0).
* `"motivazione"`: Stringa (massimo una frase) che giustifica l'allocazione.
[Strategia sintetico-operativa in massimo 5 frasi...]
**THE OUTPUT MUST BE GENERATED BY FAITHFULLY COPYING THE FOLLOWING STRUCTURAL TEMPLATE (IN ITALIAN CONTENT, JSON FORMAT):**
- **Asset_A:** X% (Motivazione: [Massimo una frase che collega dati, stile e allocazione])
- **Asset_B:** Y% (Motivazione: [Massimo una frase che collega dati, stile e allocazione])
- **Asset_C:** Z% (Motivazione: [Massimo una frase che collega dati, stile e allocazione])
...
"""
```json
{
"strategia": "[Strategia sintetico-operativa in massimo 5 frasi...]",
"portafoglio": [
{
"asset": "Asset_1",
"percentuale": X,
"motivazione": "[Massimo una frase chiara in Italiano]"
},
{
"asset": "Asset_2",
"percentuale": Y,
"motivazione": "[Massimo una frase chiara in Italiano]"
},
{
"asset": "Asset_3",
"percentuale": Z,
"motivazione": "[Massimo una frase chiara in Italiano]"
}
]
}
"""

View File

@@ -1,4 +1,5 @@
import os
import requests
from enum import Enum
from agno.agent import Agent
from agno.models.base import BaseModel
@@ -14,10 +15,10 @@ class Models(Enum):
"""
GEMINI = "gemini-2.0-flash" # API online
GEMINI_PRO = "gemini-2.0-pro" # API online, più costoso ma migliore
OLLAMA = "llama3.1" # + fast (7b) - very very bad
OLLAMA_GPT = "gpt-oss" # + good - slow (13b) - doesn't follow instructions
OLLAMA_QWEN = "qwen3:8b" # + good + fast (8b), - doesn't follow instructions
OLLAMA_GPT = "gpt-oss:latest" # + good - slow (13b)
OLLAMA_QWEN = "qwen3:latest" # + good + fast (8b)
@staticmethod
def availables() -> list['Models']:
"""
Controlla quali provider di modelli LLM hanno le loro API keys disponibili
@@ -30,14 +31,40 @@ class Models(Enum):
if os.getenv("GOOGLE_API_KEY"):
availables.append(Models.GEMINI)
availables.append(Models.GEMINI_PRO)
if os.getenv("OLLAMA_MODELS_PATH"):
availables.append(Models.OLLAMA)
availables.append(Models.OLLAMA_GPT)
availables.append(Models.OLLAMA_QWEN)
ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434")
result = requests.get(f"{ollama_host}/api/tags")
print(result)
if result.status_code == 200:
result = result.text
if Models.OLLAMA_GPT.value in result:
availables.append(Models.OLLAMA_GPT)
if Models.OLLAMA_QWEN.value in result:
availables.append(Models.OLLAMA_QWEN)
assert availables, "No valid model API keys set in environment variables."
return availables
@staticmethod
def extract_json_str_from_response(response: str) -> str:
"""
Estrae il JSON dalla risposta del modello.
response: risposta del modello (stringa).
Ritorna la parte JSON della risposta come stringa.
Se non viene trovato nessun JSON, ritorna una stringa vuota.
ATTENZIONE: questa funzione è molto semplice e potrebbe non funzionare
in tutti i casi. Si assume che il JSON sia ben formato e che inizi con
'{' e finisca con '}'. Quindi anche solo un json array farà fallire questa funzione.
"""
start = response.find("{")
assert start != -1, "No JSON found in the response."
end = response.rfind("}")
assert end != -1, "No JSON found in the response."
return response[start:end + 1].strip()
def get_model(self, instructions:str) -> BaseModel:
"""
Restituisce un'istanza del modello specificato.
@@ -47,21 +74,22 @@ class Models(Enum):
"""
name = self.value
if self in {Models.GEMINI, Models.GEMINI_PRO}:
return Gemini(name, instructions=instructions)
elif self in {Models.OLLAMA, Models.OLLAMA_GPT, Models.OLLAMA_QWEN}:
return Ollama(name, instructions=instructions)
return Gemini(name, instructions=[instructions])
elif self in {Models.OLLAMA_GPT, Models.OLLAMA_QWEN}:
return Ollama(name, instructions=[instructions])
raise ValueError(f"Modello non supportato: {self}")
def get_agent(self, instructions: str) -> Agent:
def get_agent(self, instructions: str, name: str = "") -> Agent:
"""
Costruisce un agente con il modello e le istruzioni specificate.
instructions: istruzioni da passare al modello (system prompt).
Ritorna un'istanza di Agent.
"""
return Agent(
model=self.get_model(instructions=instructions),
instructions=instructions,
model=self.get_model(instructions),
name=name,
use_json_mode=True,
# TODO Eventuali altri parametri da mettere all'agente
# anche se si possono comunque assegnare dopo la creazione
# Esempio:

View File

@@ -60,8 +60,7 @@ class ToolAgent:
)
prediction = self.predictor.run(inputs)
#output = prediction.content.split("</think>")[-1] # remove thinking steps and reasoning from the final output
output = prediction.content
output = Models.extract_json_str_from_response(prediction.content)
market_data = "\n".join([f"{product.symbol}: {product.price}" for product in market_data])
return f"{market_data}\n{sentiment}\n\n📈 Consiglio finale:\n{output}"