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

@@ -19,4 +19,5 @@ COPY LICENSE .
COPY src ./src
# Comando di default all'avvio dell'applicazione
CMD ["python", "src/app.py"]
CMD ["echo", "Benvenuto in UPO AppAI!"]
CMD ["uv", "run", "src/app.py"]

View File

@@ -101,17 +101,13 @@ Usando la libreria ``gradio`` è stata creata un'interfaccia web semplice per in
- **Multiple provider**: Supporta sia Coinbase (trading) che CryptoCompare (market data)
- **Interfaccia unificata**: Un'unica API per accedere a tutti i provider
### Problemi con i modelli LLM:
1. **Ollama gpt-oss**: il modello `gpt-oss` funziona ma non riesce a seguire le istruzioni.
2. **Ollama-gwen**: il modello `gwen` funziona più veloce di `gpt-oss` ma comunque non segue le istruzioni.
### ToDo
1. [X] Per lo scraping online bisogna iscriversi e recuperare le chiavi API
2. [X] **Market Agent**: [CryptoCompare](https://www.cryptocompare.com/cryptopian/api-keys)
3. [X] **Market Agent**: [Coinbase](https://www.coinbase.com/cloud/discover/api-keys)
4. [] **News Agent**: [CryptoPanic](https://cryptopanic.com/)
5. [] **Social Agent**: [post più hot da r/CryptoCurrency (Reddit)](https://www.reddit.com/)
6. [] Capire come `gpt-oss` parsifica la risposta e per questioni "estetiche" si può pensare di visualizzare lo stream dei token. Vedere il sorgente `src/ollama_demo.py` per risolvere il problema.
- [X] Per lo scraping online bisogna iscriversi e recuperare le chiavi API
- [X] **Market Agent**: [CryptoCompare](https://www.cryptocompare.com/cryptopian/api-keys)
- [X] **Market Agent**: [Coinbase](https://www.coinbase.com/cloud/discover/api-keys)
- [] **News Agent**: [CryptoPanic](https://cryptopanic.com/)
- [] **Social Agent**: [post più hot da r/CryptoCurrency (Reddit)](https://www.reddit.com/)
- [] Capire come `gpt-oss` parsifica la risposta e per questioni "estetiche" si può pensare di visualizzare lo stream dei token. Vedere il sorgente `src/ollama_demo.py` per risolvere il problema.
## Tests

View File

@@ -8,30 +8,13 @@ services:
- .:/app
env_file:
- .env
# Aggiunte chiave:
environment:
# Questa variabile dice alla tua app dove trovare il servizio Ollama
- OLLAMA_HOST=http://ollama:11434
# Le tue API keys esistenti
# Modelli supportati
- OLLAMA_HOST=http://host.docker.internal:11434
- GOOGLE_API_KEY=${GOOGLE_API_KEY}
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
- DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY}
- OPENAI_API_KEY=${OPENAI_API_KEY}
# Assicura che ollama parta prima della tua app
depends_on:
- ollama
# Nuovo servizio per Ollama
ollama:
image: ollama/ollama
container_name: ollama
# Aggiungi il runtime NVIDIA per GPU support
runtime: nvidia
environment:
- NVIDIA_VISIBLE_DEVICES=all
ports:
- "11434:11434"
volumes:
# Mappa la cartella dei modelli del tuo PC a quella interna del container
# ${OLLAMA_MODELS_PATH} sarà letto dal file .env
- ${OLLAMA_MODELS_PATH}:/root/.ollama
# Chiavi per le crypto API
- CDP_API_KEY_NAME=${CDP_API_KEY_NAME}
- CDP_API_PRIVATE_KEY=${CDP_API_PRIVATE_KEY}
- CRYPTOCOMPARE_API_KEY=${CRYPTOCOMPARE_API_KEY}
- BINANCE_API_KEY=${BINANCE_API_KEY}
- BINANCE_API_SECRET=${BINANCE_API_SECRET}

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}"

View File

@@ -0,0 +1,43 @@
import json
import pytest
from app.agents import predictor
from app.models import Models
def unified_checks(model: Models, input):
llm = model.get_agent(predictor.instructions())
result = llm.run(input)
print(result.content)
potential_json = Models.extract_json_str_from_response(result.content)
content = json.loads(potential_json) # Verifica che l'output sia un JSON valido
assert content['strategia'] is not None
assert isinstance(content['portafoglio'], list)
assert abs(sum(item['percentuale'] for item in content['portafoglio']) - 100) < 0.01 # La somma deve essere esattamente 100
class TestPredictor:
@pytest.fixture(scope="class")
def inputs(self):
data = []
for symbol, price in [("BTC", 60000.00), ("ETH", 3500.00), ("SOL", 150.00)]:
product_info = predictor.ProductInfo()
product_info.symbol = symbol
product_info.price = price
data.append(product_info)
return predictor.prepare_inputs(
data=data,
style=predictor.PredictorStyle.AGGRESSIVE,
sentiment="positivo"
)
def test_gemini_model_output(self, inputs):
unified_checks(Models.GEMINI, inputs)
@pytest.mark.slow
def test_ollama_gpt_oss_model_output(self, inputs):
unified_checks(Models.OLLAMA_GPT, inputs)
def test_ollama_qwen_model_output(self, inputs):
unified_checks(Models.OLLAMA_QWEN, inputs)

View File

@@ -30,6 +30,15 @@ def pytest_configure(config):
config.addinivalue_line(
"markers", "cryptocompare: marks tests that require CryptoCompare credentials"
)
config.addinivalue_line(
"markers", "gemini: marks tests that use Gemini model"
)
config.addinivalue_line(
"markers", "ollama_gpt: marks tests that use Ollama GPT model"
)
config.addinivalue_line(
"markers", "ollama_qwen: marks tests that use Ollama Qwen model"
)
def pytest_collection_modifyitems(config, items):
@@ -42,3 +51,10 @@ def pytest_collection_modifyitems(config, items):
# Aggiungi marker 'slow' ai test che potrebbero essere lenti
if "overview" in item.name.lower() or "analysis" in item.name.lower():
item.add_marker(pytest.mark.slow)
if "gemini" in item.name.lower():
item.add_marker(pytest.mark.gemini)
if "ollama_gpt" in item.name.lower():
item.add_marker(pytest.mark.ollama_gpt)
if "ollama_qwen" in item.name.lower():
item.add_marker(pytest.mark.ollama_qwen)