Update crypto data toolkit
Browse files
chatxbt-assistant.py
CHANGED
|
@@ -9,7 +9,7 @@ from phi.llm.openai import OpenAIChat
|
|
| 9 |
from phi.tools.duckduckgo import DuckDuckGo
|
| 10 |
from phi.tools.yfinance import YFinanceTools
|
| 11 |
from src.databases.postgres import sqlalchemy_engine
|
| 12 |
-
from src.tools.
|
| 13 |
from phi.storage.assistant.postgres import PgAssistantStorage
|
| 14 |
from src.knowledge_bases.combined import knowledge_base
|
| 15 |
|
|
|
|
| 9 |
from phi.tools.duckduckgo import DuckDuckGo
|
| 10 |
from phi.tools.yfinance import YFinanceTools
|
| 11 |
from src.databases.postgres import sqlalchemy_engine
|
| 12 |
+
from src.tools.crypto_data_toolkit import CryptoDataTools
|
| 13 |
from phi.storage.assistant.postgres import PgAssistantStorage
|
| 14 |
from src.knowledge_bases.combined import knowledge_base
|
| 15 |
|
src/tools/{coin_data_toolkit.py → crypto_data_toolkit.py}
RENAMED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
import json
|
| 2 |
-
|
| 3 |
from phi.tools import Toolkit
|
| 4 |
from phi.utils.log import logger
|
| 5 |
|
|
@@ -13,6 +11,8 @@ class CryptoDataTools(Toolkit):
|
|
| 13 |
|
| 14 |
self.register(self.get_coin_price)
|
| 15 |
self.register(self.get_coin_data)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
self.coingecko = CoinGecko()
|
| 18 |
self.crypto_compare = CryptoCompare()
|
|
@@ -92,3 +92,60 @@ class CryptoDataTools(Toolkit):
|
|
| 92 |
except Exception as e:
|
| 93 |
logger.warning(f"Failed to fetch coin data for {coin_symbol}: {e}")
|
| 94 |
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from phi.tools import Toolkit
|
| 2 |
from phi.utils.log import logger
|
| 3 |
|
|
|
|
| 11 |
|
| 12 |
self.register(self.get_coin_price)
|
| 13 |
self.register(self.get_coin_data)
|
| 14 |
+
self.register(self.get_exchange_data)
|
| 15 |
+
self.register(self.get_latest_crypto_news_data)
|
| 16 |
|
| 17 |
self.coingecko = CoinGecko()
|
| 18 |
self.crypto_compare = CryptoCompare()
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
logger.warning(f"Failed to fetch coin data for {coin_symbol}: {e}")
|
| 94 |
return f"Error: {e}"
|
| 95 |
+
|
| 96 |
+
def get_exchange_data(self, exchange: str) -> str:
|
| 97 |
+
"""
|
| 98 |
+
Fetches exchange data for a given cryptocurrency exchange from CoinGecko and DexScreener.
|
| 99 |
+
|
| 100 |
+
Args:
|
| 101 |
+
exchange (str): The unique identifier for the cryptocurrency exchange.
|
| 102 |
+
|
| 103 |
+
Returns:
|
| 104 |
+
str: A JSON string containing the exchange data from CoinGecko and DexScreener for the specified exchange.
|
| 105 |
+
|
| 106 |
+
Raises:
|
| 107 |
+
Exception: If an error occurs while fetching the exchange data.
|
| 108 |
+
|
| 109 |
+
Example:
|
| 110 |
+
>>> get_exchange_data("binance")
|
| 111 |
+
"""
|
| 112 |
+
logger.info(f"Fetching exchange data for {exchange} cryptocurrency exchange")
|
| 113 |
+
try:
|
| 114 |
+
coin_gecko_exchange_data = self.coingecko.get_exchange_data(id=exchange)
|
| 115 |
+
dexscreener_exchange_data = self.dex_screener.search(query=exchange)
|
| 116 |
+
|
| 117 |
+
logger.debug(f"coingecko exchange data: {coin_gecko_exchange_data}")
|
| 118 |
+
logger.debug(f"dexscreener exchange data: {dexscreener_exchange_data}")
|
| 119 |
+
|
| 120 |
+
return f"{(coin_gecko_exchange_data, dexscreener_exchange_data)}"
|
| 121 |
+
except Exception as e:
|
| 122 |
+
logger.warning(f"Failed to fetch exchange data for {exchange}: {e}")
|
| 123 |
+
return f"Error: {e}"
|
| 124 |
+
|
| 125 |
+
def get_latest_crypto_news_data(self) -> str:
|
| 126 |
+
"""
|
| 127 |
+
Fetches the latest cryptocurrency news data from CoinGecko and CryptoCompare.
|
| 128 |
+
|
| 129 |
+
Returns:
|
| 130 |
+
str: A JSON string containing the latest cryptocurrency news data from CoinGecko and CryptoCompare.
|
| 131 |
+
|
| 132 |
+
Raises:
|
| 133 |
+
Exception: If an error occurs while fetching the latest cryptocurrency news data.
|
| 134 |
+
|
| 135 |
+
Example:
|
| 136 |
+
>>> get_latest_crypto_news_data()
|
| 137 |
+
"""
|
| 138 |
+
logger.info("Fetching latest cryptocurrency news data")
|
| 139 |
+
try:
|
| 140 |
+
coin_gecko_trending_coins = self.coingecko.get_trending_coin_list()
|
| 141 |
+
cryptocompare_news_categories = self.crypto_compare.get_news_categories()
|
| 142 |
+
cryptocompare_lates_news_articles = self.crypto_compare.get_latest_news_articles()
|
| 143 |
+
|
| 144 |
+
logger.debug(f"coingecko trending coins: {coin_gecko_trending_coins}")
|
| 145 |
+
logger.debug(f"cryptocompare news categories: {cryptocompare_news_categories}")
|
| 146 |
+
logger.debug(f"cryptocompare latest news articles: {cryptocompare_lates_news_articles}")
|
| 147 |
+
|
| 148 |
+
return f"{(coin_gecko_trending_coins, cryptocompare_news_categories, cryptocompare_lates_news_articles)}"
|
| 149 |
+
except Exception as e:
|
| 150 |
+
logger.warning(f"Failed to fetch latest cryptocurrency news data: {e}")
|
| 151 |
+
return f"Error: {e}"
|