import opengradient as og
from pydantic import BaseModel, Field
# Example 1: Simple tool with no input schema
def price_data_provider():
"""Function that provides input data to the model."""
return {
"open_high_low_close": [
[2535.79, 2535.79, 2505.37, 2515.36],
[2515.37, 2516.37, 2497.27, 2506.94],
[2506.94, 2515, 2506.35, 2508.77],
[2508.77, 2519, 2507.55, 2518.79],
[2518.79, 2522.1, 2513.79, 2517.92],
[2517.92, 2521.4, 2514.65, 2518.13],
[2518.13, 2525.4, 2517.2, 2522.6],
[2522.59, 2528.81, 2519.49, 2526.12],
[2526.12, 2530, 2524.11, 2529.99],
[2529.99, 2530.66, 2525.29, 2526],
]
}
def format_volatility(inference_result):
"""Function that formats the model output."""
return format(float(inference_result.model_output["Y"].item()), ".3%")
# Create the tool
volatility_tool = toolkit.create_run_model_tool(
model_cid="QmRhcpDXfYCKsimTmJYrAVM4Bbvck59Zb2onj3MHv9Kw5N",
tool_name="eth_volatility",
model_input_provider=price_data_provider,
model_output_formatter=format_volatility,
tool_description="Generates volatility measurement for ETH/USDT trading pair",
inference_mode=og.InferenceMode.VANILLA,
)
# Example 2: Tool with input schema from the agent
class TokenInputSchema(BaseModel):
token: str = Field(description="Token name (ethereum or bitcoin)")
def token_data_provider(**inputs):
"""Dynamic function that changes behavior based on agent input."""
token = inputs.get("token")
if token == "bitcoin":
return {"price_series": [100001.1, 100013.2, 100149.2, 99998.1]}
else: # ethereum
return {"price_series": [2010.1, 2012.3, 2020.1, 2019.2]}
# Create the tool with schema
token_tool = toolkit.create_run_model_tool(
model_cid="QmZdSfHWGJyzBiB2K98egzu3MypPcv4R1ASypUxwZ1MFUG",
tool_name="token_volatility",
model_input_provider=token_data_provider,
model_output_formatter=lambda x: format(float(x.model_output["std"].item()), ".3%"),
tool_input_schema=TokenInputSchema,
tool_description="Measures return volatility for a specified token",
)
# Add tools to the toolkit
toolkit.add_tool(volatility_tool)
toolkit.add_tool(token_tool)