From f8b41cd63a3d30d67fd2e9a277c7acebab84ec38 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Sat, 4 Oct 2025 19:46:17 +0200 Subject: [PATCH] Aggiunti type hints finali --- src/app/__main__.py | 12 ++++++------ src/app/agents/__init__.py | 6 +++--- src/app/agents/team.py | 3 +-- src/app/pipeline.py | 20 ++++++++++++-------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/app/__main__.py b/src/app/__main__.py index da59d42..744fe53 100644 --- a/src/app/__main__.py +++ b/src/app/__main__.py @@ -1,5 +1,5 @@ import gradio as gr -from agno.utils.log import log_info +from agno.utils.log import log_info #type: ignore from dotenv import load_dotenv from app.chat_manager import ChatManager @@ -12,24 +12,24 @@ if __name__ == "__main__": ######################################## # Funzioni Gradio ######################################## - def respond(message, history): + def respond(message: str, history: list[dict[str, str]]) -> tuple[list[dict[str, str]], list[dict[str, str]], str]: response = chat.send_message(message) history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": response}) return history, history, "" - def save_current_chat(): + def save_current_chat() -> str: chat.save_chat("chat.json") return "💾 Chat salvata in chat.json" - def load_previous_chat(): + def load_previous_chat() -> tuple[list[dict[str, str]], list[dict[str, str]]]: chat.load_chat("chat.json") - history = [] + history: list[dict[str, str]] = [] for m in chat.get_history(): history.append({"role": m["role"], "content": m["content"]}) return history, history - def reset_chat(): + def reset_chat() -> tuple[list[dict[str, str]], list[dict[str, str]]]: chat.reset_chat() return [], [] diff --git a/src/app/agents/__init__.py b/src/app/agents/__init__.py index eb1c2b0..42f97f6 100644 --- a/src/app/agents/__init__.py +++ b/src/app/agents/__init__.py @@ -6,7 +6,7 @@ from agno.models.base import Model from agno.models.google import Gemini from agno.models.ollama import Ollama from agno.tools import Toolkit -from agno.utils.log import log_warning +from agno.utils.log import log_warning #type: ignore from pydantic import BaseModel @@ -36,7 +36,7 @@ class AppModels(Enum): log_warning(f"Ollama is not running or not reachable {result}") return [] - availables = [] + availables: list[AppModels] = [] result = result.text for model in [model for model in AppModels if model.name.startswith("OLLAMA")]: if model.value in result: @@ -106,5 +106,5 @@ class AppModels(Enum): retries=2, tools=tools, delay_between_retries=5, # seconds - output_schema=output # se si usa uno schema di output, lo si passa qui + output_schema=output.__class__ if output else None # se si usa uno schema di output, lo si passa qui ) diff --git a/src/app/agents/team.py b/src/app/agents/team.py index f389539..01ad5cf 100644 --- a/src/app/agents/team.py +++ b/src/app/agents/team.py @@ -1,7 +1,6 @@ from agno.team import Team -from pydantic import BaseModel, Field from app.agents import AppModels -from app.markets import MarketAPIsTool, ProductInfo +from app.markets import MarketAPIsTool from app.news import NewsAPIsTool from app.social import SocialAPIsTool diff --git a/src/app/pipeline.py b/src/app/pipeline.py index 766805f..58819ca 100644 --- a/src/app/pipeline.py +++ b/src/app/pipeline.py @@ -2,6 +2,7 @@ from agno.run.agent import RunOutput from app.agents import AppModels from app.agents.team import create_team_with from app.agents.predictor import PREDICTOR_INSTRUCTIONS, PredictorInput, PredictorOutput, PredictorStyle +from app.markets.base import ProductInfo class Pipeline: @@ -64,19 +65,20 @@ class Pipeline: 4. Restituisce la strategia finale """ # Step 1: raccolta output dai membri del Team - team_outputs = self.team.run(query) + team_outputs = self.team.run(query) # type: ignore # Step 2: aggregazione output strutturati - all_products = [] - sentiments = [] + all_products: list[ProductInfo] = [] + sentiments: list[str] = [] for agent_output in team_outputs.member_responses: - if isinstance(agent_output, RunOutput): - if "products" in agent_output.metadata: + if isinstance(agent_output, RunOutput) and agent_output.metadata is not None: + keys = agent_output.metadata.keys() + if "products" in keys: all_products.extend(agent_output.metadata["products"]) - if "sentiment_news" in agent_output.metadata: + if "sentiment_news" in keys: sentiments.append(agent_output.metadata["sentiment_news"]) - if "sentiment_social" in agent_output.metadata: + if "sentiment_social" in keys: sentiments.append(agent_output.metadata["sentiment_social"]) aggregated_sentiment = "\n".join(sentiments) @@ -88,7 +90,9 @@ class Pipeline: sentiment=aggregated_sentiment ) - result = self.predictor.run(predictor_input) + result = self.predictor.run(predictor_input) # type: ignore + if not isinstance(result.content, PredictorOutput): + return "❌ Errore: il modello non ha restituito un output valido." prediction: PredictorOutput = result.content # Step 4: restituzione strategia finale