Added Prompt for tools #68
21
src/app/api/tools/instructions/__init__.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
__INSTRUCTIONS_PATH = Path(__file__).parent
|
||||||
|
|
||||||
|
def __load_tool_instruction(file_name: str) -> str:
|
||||||
|
file_path = __INSTRUCTIONS_PATH / file_name
|
||||||
|
return file_path.read_text(encoding='utf-8').strip()
|
||||||
|
|
||||||
|
MARKET_TOOL_INSTRUCTIONS = __load_tool_instruction("market_instructions.md")
|
||||||
|
NEWS_TOOL_INSTRUCTIONS = __load_tool_instruction("news_instructions.md")
|
||||||
|
SOCIAL_TOOL_INSTRUCTIONS = __load_tool_instruction("social_instructions.md")
|
||||||
|
PLAN_MEMORY_TOOL_INSTRUCTIONS = __load_tool_instruction("plan_memory_instructions.md")
|
||||||
|
SYMBOLS_TOOL_INSTRUCTIONS = __load_tool_instruction("symbols_instructions.md")
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"MARKET_TOOL_INSTRUCTIONS",
|
||||||
|
"NEWS_TOOL_INSTRUCTIONS",
|
||||||
|
"SOCIAL_TOOL_INSTRUCTIONS",
|
||||||
|
"PLAN_MEMORY_TOOL_INSTRUCTIONS",
|
||||||
|
"SYMBOLS_TOOL_INSTRUCTIONS",
|
||||||
|
]
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from pathlib import Path
|
|
||||||
from agno.tools import Toolkit
|
from agno.tools import Toolkit
|
||||||
|
from app.api.tools.instructions import MARKET_TOOL_INSTRUCTIONS
|
||||||
from app.api.wrapper_handler import WrapperHandler
|
from app.api.wrapper_handler import WrapperHandler
|
||||||
from app.api.core.markets import MarketWrapper, Price, ProductInfo
|
from app.api.core.markets import MarketWrapper, Price, ProductInfo
|
||||||
from app.api.markets import BinanceWrapper, CoinBaseWrapper, CryptoCompareWrapper, YFinanceWrapper
|
from app.api.markets import BinanceWrapper, CoinBaseWrapper, CryptoCompareWrapper, YFinanceWrapper
|
||||||
@@ -13,17 +13,6 @@ class MarketAPIsTool(MarketWrapper, Toolkit):
|
|||||||
Providers can be configured in configs.yaml under api.market_providers.
|
Providers can be configured in configs.yaml under api.market_providers.
|
||||||
|
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _load_instructions() -> str:
|
|
||||||
"""
|
|
||||||
Load the toolkit instructions from the external text file.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The content of the instructions file.
|
|
||||||
"""
|
|
||||||
instructions_path = Path(__file__).parent / "instructions" / "market_instructions.md"
|
|
||||||
return instructions_path.read_text(encoding="utf-8")
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Initialize the MarketAPIsTool with market API wrappers configured in configs.yaml.
|
Initialize the MarketAPIsTool with market API wrappers configured in configs.yaml.
|
||||||
@@ -41,6 +30,7 @@ class MarketAPIsTool(MarketWrapper, Toolkit):
|
|||||||
Toolkit.__init__( # type: ignore
|
Toolkit.__init__( # type: ignore
|
||||||
self,
|
self,
|
||||||
name="Market APIs Toolkit",
|
name="Market APIs Toolkit",
|
||||||
|
instructions=MARKET_TOOL_INSTRUCTIONS,
|
||||||
tools=[
|
tools=[
|
||||||
self.get_product,
|
self.get_product,
|
||||||
self.get_products,
|
self.get_products,
|
||||||
@@ -48,7 +38,6 @@ class MarketAPIsTool(MarketWrapper, Toolkit):
|
|||||||
self.get_products_aggregated,
|
self.get_products_aggregated,
|
||||||
self.get_historical_prices_aggregated,
|
self.get_historical_prices_aggregated,
|
||||||
],
|
],
|
||||||
instructions=self._load_instructions(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_product(self, asset_id: str) -> ProductInfo:
|
def get_product(self, asset_id: str) -> ProductInfo:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from pathlib import Path
|
|
||||||
from agno.tools import Toolkit
|
from agno.tools import Toolkit
|
||||||
|
from app.api.tools.instructions import NEWS_TOOL_INSTRUCTIONS
|
||||||
from app.api.wrapper_handler import WrapperHandler
|
from app.api.wrapper_handler import WrapperHandler
|
||||||
from app.api.core.news import NewsWrapper, Article
|
from app.api.core.news import NewsWrapper, Article
|
||||||
from app.api.news import NewsApiWrapper, GoogleNewsWrapper, CryptoPanicWrapper, DuckDuckGoWrapper
|
from app.api.news import NewsApiWrapper, GoogleNewsWrapper, CryptoPanicWrapper, DuckDuckGoWrapper
|
||||||
@@ -16,17 +16,6 @@ class NewsAPIsTool(NewsWrapper, Toolkit):
|
|||||||
If no wrapper succeeds, an exception is raised.
|
If no wrapper succeeds, an exception is raised.
|
||||||
|
The The `_load_instructions()` method lacks error handling for missing instruction files. If the markdown file doesn't exist or is unreadable, this will raise an unhandled exception during toolkit initialization. Consider adding a try-except block with a fallback to a default instruction string or a more informative error message.
```suggestion
try:
return instructions_path.read_text(encoding="utf-8")
except Exception as e:
# Fallback to a default instruction string if the file is missing or unreadable
return (
"News APIs Toolkit instructions could not be loaded. "
"The instructions file is missing or unreadable. "
"Please ensure 'news_instructions.md' exists in the 'instructions' directory."
)
```
Esiste già un metodo simile in Esiste già un metodo simile in `__init__.py` dentro agents/prompt.
Bastava richiamare quello o crearne uno universale da mettere da qualche parte, non replicarlo per ogni tool.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _load_instructions() -> str:
|
|
||||||
"""
|
|
||||||
Load the toolkit instructions from the external text file.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The content of the instructions file.
|
|
||||||
"""
|
|
||||||
instructions_path = Path(__file__).parent / "instructions" / "news_instructions.md"
|
|
||||||
return instructions_path.read_text(encoding="utf-8")
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Initialize the NewsAPIsTool with news API wrappers configured in configs.yaml.
|
Initialize the NewsAPIsTool with news API wrappers configured in configs.yaml.
|
||||||
@@ -44,13 +33,13 @@ class NewsAPIsTool(NewsWrapper, Toolkit):
|
|||||||
Toolkit.__init__( # type: ignore
|
Toolkit.__init__( # type: ignore
|
||||||
self,
|
self,
|
||||||
name="News APIs Toolkit",
|
name="News APIs Toolkit",
|
||||||
|
instructions=NEWS_TOOL_INSTRUCTIONS,
|
||||||
tools=[
|
tools=[
|
||||||
self.get_top_headlines,
|
self.get_top_headlines,
|
||||||
self.get_latest_news,
|
self.get_latest_news,
|
||||||
self.get_top_headlines_aggregated,
|
self.get_top_headlines_aggregated,
|
||||||
self.get_latest_news_aggregated,
|
self.get_latest_news_aggregated,
|
||||||
],
|
],
|
||||||
instructions=self._load_instructions(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_top_headlines(self, limit: int = 100) -> list[Article]:
|
def get_top_headlines(self, limit: int = 100) -> list[Article]:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from pathlib import Path
|
|
||||||
from agno.tools.toolkit import Toolkit
|
from agno.tools.toolkit import Toolkit
|
||||||
from typing import TypedDict, Literal
|
from typing import TypedDict, Literal
|
||||||
|
from app.api.tools.instructions import PLAN_MEMORY_TOOL_INSTRUCTIONS
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -11,21 +11,12 @@ class Task(TypedDict):
|
|||||||
|
|
||||||
|
Esiste già un metodo simile in Esiste già un metodo simile in `__init__.py` dentro agents/prompt.
Bastava richiamare quello o crearne uno universale da mettere da qualche parte, non replicarlo per ogni tool.
|
|||||||
|
|
||||||
class PlanMemoryTool(Toolkit):
|
class PlanMemoryTool(Toolkit):
|
||||||
@staticmethod
|
|
||||||
def _load_instructions() -> str:
|
|
||||||
"""
|
|
||||||
Load the toolkit instructions from the external markdown file.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The content of the instructions file.
|
|
||||||
"""
|
|
||||||
instructions_path = Path(__file__).parent / "instructions" / "plan_memory_instructions.md"
|
|
||||||
return instructions_path.read_text(encoding="utf-8")
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tasks: list[Task] = []
|
self.tasks: list[Task] = []
|
||||||
Toolkit.__init__(self, # type: ignore[call-arg]
|
Toolkit.__init__(self, # type: ignore[call-arg]
|
||||||
instructions=self._load_instructions(),
|
name="Plan Memory Toolkit",
|
||||||
|
instructions=PLAN_MEMORY_TOOL_INSTRUCTIONS,
|
||||||
tools=[
|
tools=[
|
||||||
self.add_tasks,
|
self.add_tasks,
|
||||||
self.get_next_pending_task,
|
self.get_next_pending_task,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from pathlib import Path
|
|
||||||
from agno.tools import Toolkit
|
from agno.tools import Toolkit
|
||||||
|
from app.api.tools.instructions import SOCIAL_TOOL_INSTRUCTIONS
|
||||||
from app.api.wrapper_handler import WrapperHandler
|
from app.api.wrapper_handler import WrapperHandler
|
||||||
from app.api.core.social import SocialPost, SocialWrapper
|
from app.api.core.social import SocialPost, SocialWrapper
|
||||||
from app.api.social import *
|
from app.api.social import *
|
||||||
@@ -17,17 +17,6 @@ class SocialAPIsTool(SocialWrapper, Toolkit):
|
|||||||
If no wrapper succeeds, an exception is raised.
|
If no wrapper succeeds, an exception is raised.
|
||||||
|
The The `_load_instructions()` method lacks error handling for missing instruction files. If the markdown file doesn't exist or is unreadable, this will raise an unhandled exception during toolkit initialization. Consider adding a try-except block with a fallback to a default instruction string or a more informative error message.
```suggestion
try:
return instructions_path.read_text(encoding="utf-8")
except Exception as e:
# Fallback to a default instruction string or log the error
return (
"SocialAPIsTool instructions could not be loaded from file. "
"Default instructions: Use the available tools to retrieve top cryptocurrency-related posts "
"from supported social media providers. Configure providers in configs.yaml."
)
```
Esiste già un metodo simile in Esiste già un metodo simile in `__init__.py` dentro agents/prompt.
Bastava richiamare quello o crearne uno universale da mettere da qualche parte, non replicarlo per ogni tool.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _load_instructions() -> str:
|
|
||||||
"""
|
|
||||||
Load the toolkit instructions from the external text file.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The content of the instructions file.
|
|
||||||
"""
|
|
||||||
instructions_path = Path(__file__).parent / "instructions" / "social_instructions.md"
|
|
||||||
return instructions_path.read_text(encoding="utf-8")
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Initialize the SocialAPIsTool with social media API wrappers configured in configs.yaml.
|
Initialize the SocialAPIsTool with social media API wrappers configured in configs.yaml.
|
||||||
@@ -44,12 +33,12 @@ class SocialAPIsTool(SocialWrapper, Toolkit):
|
|||||||
|
|
||||||
Toolkit.__init__( # type: ignore
|
Toolkit.__init__( # type: ignore
|
||||||
self,
|
self,
|
||||||
name="Socials Toolkit",
|
name="Socials APIs Toolkit",
|
||||||
|
instructions=SOCIAL_TOOL_INSTRUCTIONS,
|
||||||
tools=[
|
tools=[
|
||||||
self.get_top_crypto_posts,
|
self.get_top_crypto_posts,
|
||||||
self.get_top_crypto_posts_aggregated,
|
self.get_top_crypto_posts_aggregated,
|
||||||
],
|
],
|
||||||
instructions=self._load_instructions(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_top_crypto_posts(self, limit: int = 5) -> list[SocialPost]:
|
def get_top_crypto_posts(self, limit: int = 5) -> list[SocialPost]:
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from pathlib import Path
|
|
||||||
from agno.tools.toolkit import Toolkit
|
from agno.tools.toolkit import Toolkit
|
||||||
|
from app.api.tools.instructions import SYMBOLS_TOOL_INSTRUCTIONS
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
logging = logging.getLogger("crypto_symbols")
|
logging = logging.getLogger("crypto_symbols")
|
||||||
@@ -19,23 +19,12 @@ class CryptoSymbolsTools(Toolkit):
|
|||||||
Classe per ottenere i simboli delle criptovalute tramite Yahoo Finance.
|
Classe per ottenere i simboli delle criptovalute tramite Yahoo Finance.
|
||||||
|
The The `_load_instructions()` method lacks error handling for missing instruction files. If the markdown file doesn't exist or is unreadable, this will raise an unhandled exception during toolkit initialization. Consider adding a try-except block with a fallback to a default instruction string or a more informative error message.
```suggestion
try:
return instructions_path.read_text(encoding="utf-8")
except Exception as e:
logging.error(f"Could not read instructions file at {instructions_path}: {e}")
return (
"Crypto Symbols Tool Instructions:\n"
"This toolkit provides access to cryptocurrency symbols via Yahoo Finance.\n"
"Use the available tools to list all symbols or search by name."
)
```
It is as expected since the missing or corrupted instruction must be a blocking error It is as expected since the missing or corrupted instruction must be a blocking error
Esiste già un metodo simile in Esiste già un metodo simile in `__init__.py` dentro agents/prompt.
Bastava richiamare quello o crearne uno universale da mettere da qualche parte, non replicarlo per ogni tool.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _load_instructions() -> str:
|
|
||||||
"""
|
|
||||||
Load the toolkit instructions from the external markdown file.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The content of the instructions file.
|
|
||||||
"""
|
|
||||||
instructions_path = Path(__file__).parent / "instructions" / "symbols_instructions.md"
|
|
||||||
return instructions_path.read_text(encoding="utf-8")
|
|
||||||
|
|
||||||
def __init__(self, cache_file: str = 'resources/cryptos.csv'):
|
def __init__(self, cache_file: str = 'resources/cryptos.csv'):
|
||||||
self.cache_file = cache_file
|
self.cache_file = cache_file
|
||||||
self.final_table = pd.read_csv(self.cache_file) if os.path.exists(self.cache_file) else pd.DataFrame() # type: ignore
|
self.final_table = pd.read_csv(self.cache_file) if os.path.exists(self.cache_file) else pd.DataFrame() # type: ignore
|
||||||
Toolkit.__init__(self, # type: ignore
|
Toolkit.__init__(self, # type: ignore
|
||||||
name="Crypto Symbols Tool",
|
name="Crypto Symbols Tool",
|
||||||
instructions=self._load_instructions(),
|
instructions=SYMBOLS_TOOL_INSTRUCTIONS,
|
||||||
tools=[
|
tools=[
|
||||||
self.get_all_symbols,
|
self.get_all_symbols,
|
||||||
self.get_symbols_by_name,
|
self.get_symbols_by_name,
|
||||||
|
|||||||
The
_load_instructions()method lacks error handling for missing instruction files. If the markdown file doesn't exist or is unreadable, this will raise an unhandled exception during toolkit initialization. Consider adding a try-except block with a fallback to a default instruction string or a more informative error message.Esiste già un metodo simile in
__init__.pydentro agents/prompt.Bastava richiamare quello o crearne uno universale da mettere da qualche parte, non replicarlo per ogni tool.