Skip to content

Commit

Permalink
Adds Allora Network machine learning price prediction to the CDP Agen…
Browse files Browse the repository at this point in the history
…tKit
  • Loading branch information
fernandofcampos committed Jan 15, 2025
1 parent 638244c commit 7f3a7e0
Show file tree
Hide file tree
Showing 10 changed files with 714 additions and 3 deletions.
1 change: 1 addition & 0 deletions cdp-agentkit-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Added

- Added `wrap_eth` action to wrap ETH to WETH on Base.
- Added support for Allora Chain machine learning (`get_price_prediction` and `get_all_topics` actions).

## [0.0.7] - 2025-01-08

Expand Down
20 changes: 20 additions & 0 deletions cdp-agentkit-core/cdp_agentkit_core/actions/allora/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from cdp_agentkit_core.actions.allora.action import AlloraAction
from cdp_agentkit_core.actions.allora.get_all_topics import GetAllTopicsAction
from cdp_agentkit_core.actions.allora.get_price_prediction import GetPricePredictionAction


def get_all_allora_actions() -> list[type[AlloraAction]]:
actions = []
for action in AlloraAction.__subclasses__():
actions.append(action())

return actions


ALLORA_ACTIONS = get_all_allora_actions()

__all__ = [
"ALLORA_ACTIONS",
"GetPricePredictionAction",
"GetAllTopicsAction",
]
12 changes: 12 additions & 0 deletions cdp-agentkit-core/cdp_agentkit_core/actions/allora/action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from collections.abc import Callable

from pydantic import BaseModel


class AlloraAction(BaseModel):
"""Allora Action Base Class."""

name: str
description: str
args_schema: type[BaseModel] | None = None
func: Callable[..., str]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
from collections.abc import Callable

from allora_sdk.v2.api_client import AlloraAPIClient
from pydantic import BaseModel

from cdp_agentkit_core.actions.allora.action import AlloraAction

GET_ALL_TOPICS_PROMPT = """
This tool will get all available topics from Allora Network.
"""


async def get_all_topics(client: AlloraAPIClient) -> str:
"""Get all available topics from Allora Network.
Args:
client (AlloraAPIClient): The Allora API client.
Returns:
str: A list of available topics from Allora Network in JSON format
"""
try:
topics = await client.get_all_topics()
topics_json = json.dumps(topics, indent=4)
return f"The available topics at Allora Network are:\n{topics_json}"
except Exception as e:
return f"Error getting all topics: {e}"


class GetAllTopicsAction(AlloraAction):
"""Get all topics action."""

name: str = "get_all_topics"
description: str = GET_ALL_TOPICS_PROMPT
args_schema: type[BaseModel] | None = None
func: Callable[..., str] = get_all_topics
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from collections.abc import Callable

from allora_sdk.v2.api_client import AlloraAPIClient
from pydantic import BaseModel, Field

from cdp_agentkit_core.actions.allora.action import AlloraAction

GET_PRICE_PREDICTION_PROMPT = """
This tool will get the future price prediction for a given crypto asset from Allora Network.
It takes the crypto asset and timeframe as inputs.
"""


class GetPricePredictionInput(BaseModel):
"""Input argument schema for get price prediction action."""

token: str = Field(
..., description="The crypto asset to get the price prediction for, e.g. `BTC`"
)
timeframe: str = Field(
..., description="The timeframe to get the price prediction for, e.g. `5m` or `8h`"
)


async def get_price_prediction(client: AlloraAPIClient, token: str, timeframe: str) -> str:
"""Get the future price prediction for a given crypto asset from Allora Network.
Args:
client (AlloraAPIClient): The Allora API client.
token (str): The crypto asset to get the price prediction for, e.g. `BTC`
timeframe (str): The timeframe to get the price prediction for, e.g. `5m` or `8h`
Returns:
str: The future price prediction for the given crypto asset
"""
try:
price_prediction = await client.get_price_prediction(token, timeframe)
return f"The future price prediction for {token} in {timeframe} is {price_prediction.inference_data.network_inference_normalized}"
except Exception as e:
return f"Error getting price prediction: {e}"


class GetPricePredictionAction(AlloraAction):
"""Get price prediction action."""

name: str = "get_price_prediction"
description: str = GET_PRICE_PREDICTION_PROMPT
args_schema: type[BaseModel] | None = GetPricePredictionInput
func: Callable[..., str] = get_price_prediction
Loading

0 comments on commit 7f3a7e0

Please sign in to comment.