Buckets:

download
raw
3.47 kB
"""POST /signal/reasoning — server-side AI-assisted reasoning generation.
Generates a reasoning string via Claude Haiku on the server side, so the
ANTHROPIC_API_KEY never needs to be held by SDK clients. The response
explicitly marks the text as AI-generated; submitting it as signal reasoning
will display an 'AI-assisted' badge on the leaderboard and profile.
"""
from __future__ import annotations
import os
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel, Field
from tradearena.api.deps import require_api_key
router = APIRouter(tags=["signals"])
class ReasoningRequest(BaseModel):
asset: str = Field(..., min_length=1, max_length=20, description="Asset symbol, e.g. BTC/USDT")
action: str = Field(..., description="Predicted direction: buy, sell, long, short, yes, no")
supporting_data: dict = Field(default_factory=dict, description="Key/value evidence")
class ReasoningResponse(BaseModel):
reasoning: str = Field(..., description="AI-generated reasoning text (>= 20 words)")
ai_assisted: bool = Field(True, description="Always true — this text was generated by Claude")
model: str = Field(..., description="Model used to generate the reasoning")
@router.post(
"/signal/reasoning",
response_model=ReasoningResponse,
summary="Generate AI-assisted reasoning for a signal",
responses={
503: {"description": "ANTHROPIC_API_KEY not configured on this server"},
},
)
async def generate_reasoning(
body: ReasoningRequest,
creator_id: str = Depends(require_api_key),
) -> dict:
"""Generate a reasoning string for a pending signal using Claude Haiku.
The returned text is explicitly AI-generated. Any signal submitted with
this reasoning will be displayed with an 'AI-assisted' badge. The badge
is informational only and does not affect scoring.
Rate limited to the standard per-key API limit.
ANTHROPIC_API_KEY must be configured on the server; it is never exposed
to clients.
"""
api_key = os.getenv("ANTHROPIC_API_KEY", "")
if not api_key:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="AI reasoning generation is not available on this server",
)
try:
import anthropic
except ImportError:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="anthropic package not available on this server",
)
client = anthropic.Anthropic(api_key=api_key)
data_summary = "\n".join(f" {k}: {v}" for k, v in body.supporting_data.items())
prompt = (
f"You are a market analyst. Generate concise, factual reasoning for the "
f"following prediction. Reference the provided data specifically.\n\n"
f"Asset: {body.asset}\n"
f"Direction: {body.action}\n"
f"Supporting data:\n{data_summary or ' (none provided)'}\n\n"
f"Write 2-4 sentences of reasoning. Be factual and descriptive — do not say "
f"'you should buy' or give investment advice. Do not use bullet points."
)
message = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=256,
messages=[{"role": "user", "content": prompt}],
)
text = message.content[0].text.strip()
return {
"reasoning": text,
"ai_assisted": True,
"model": "claude-haiku-4-5-20251001",
}

Xet Storage Details

Size:
3.47 kB
·
Xet hash:
1bedacfbd4e4314cc8f3629b27627b33651d28fb0d636880ba694880d5195e5d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.