Define Purple Flea Tools
import litellm
import json
import requests
PURPLEFLEA_API_KEY = "YOUR_API_KEY"
BASE = "https://api.purpleflea.com/v1"
tools = [
{
"type": "function",
"function": {
"name": "get_crypto_price",
"description": "Get current price for any cryptocurrency",
"parameters": {
"type": "object",
"properties": {
"symbol": {"type": "string", "description": "e.g. BTC, ETH, SOL"}
},
"required": ["symbol"]
}
}
},
{
"type": "function",
"function": {
"name": "open_trade",
"description": "Open a perpetual futures trade",
"parameters": {
"type": "object",
"properties": {
"symbol": {"type": "string"},
"side": {"type": "string", "enum": ["long", "short"]},
"size": {"type": "number"},
"leverage": {"type": "integer", "default": 1}
},
"required": ["symbol", "side", "size"]
}
}
}
]
Run with Any LLM
def execute_tool(tool_call):
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
if name == "get_crypto_price":
r = requests.get(
f"{BASE}/trading/price/{args['symbol']}",
headers={"X-API-Key": PURPLEFLEA_API_KEY}
)
return r.json()
elif name == "open_trade":
r = requests.post(
f"{BASE}/trading/open",
json=args,
headers={"X-API-Key": PURPLEFLEA_API_KEY}
)
return r.json()
response = litellm.completion(
model="gpt-4o",
messages=[{"role": "user", "content": "Is BTC bullish? Open a long if yes."}],
tools=tools
)
response = litellm.completion(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": "Is BTC bullish? Open a long if yes."}],
tools=tools
)
response = litellm.completion(
model="ollama/llama3.1:70b",
messages=[{"role": "user", "content": "Is BTC bullish? Open a long if yes."}],
tools=tools
)
Full Agentic Loop
def run_agent(model: str, task: str):
messages = [{"role": "user", "content": task}]
while True:
response = litellm.completion(
model=model,
messages=messages,
tools=tools
)
msg = response.choices[0].message
if not msg.tool_calls:
return msg.content
messages.append(msg)
for tc in msg.tool_calls:
result = execute_tool(tc)
messages.append({
"role": "tool",
"tool_call_id": tc.id,
"content": json.dumps(result)
})
task = "Check BTC price, evaluate market conditions, and trade accordingly"
print(run_agent("gpt-4o", task))
print(run_agent("claude-3-5-sonnet-20241022", task))
print(run_agent("gemini/gemini-1.5-pro", task))
Cost-Optimized Routing
from litellm import Router
router = Router(model_list=[
{
"model_name": "analysis",
"litellm_params": {"model": "gpt-4o"}
},
{
"model_name": "execution",
"litellm_params": {"model": "gpt-4o-mini"}
}
])
analysis = router.completion(
model="analysis",
messages=[{"role": "user",
"content": "Analyze BTC technicals in depth"}],
tools=[get_crypto_price_tool]
)
execution = router.completion(
model="execution",
messages=[{"role": "user",
"content": f"Execute: {analysis.choices[0].message.content}"}],
tools=[open_trade_tool]
)