Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

anthropic-server.py

301 lines10.5 KB

1"""
2FastAPI server example for TanStack AI
3Streams Anthropic API events in SSE format compatible with TanStack AI client
4Now with agentic tool calling support!
5"""
6import os
7import logging
8from typing import List, Dict, Any, Optional
9​
10from dotenv import load_dotenv
11from fastapi import FastAPI, HTTPException
12from fastapi.responses import StreamingResponse
13from fastapi.middleware.cors import CORSMiddleware
14from pydantic import BaseModel
15​
16from tanstack_ai import (
17 AnthropicAdapter,
18 AIAdapterConfig,
19 chat,
20 tool,
21 max_iterations,
22 format_sse_chunk,
23 format_sse_done,
25​
26# Configure logging
27logging.basicConfig(
28 level=logging.INFO,
29 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
31logger = logging.getLogger(__name__)
32​
33# Load environment variables from .env file
34load_dotenv()
35​
36# Initialize FastAPI app
37app = FastAPI(title="TanStack AI Python FastAPI Example")
38​
39# CORS middleware
40app.add_middleware(
41 CORSMiddleware,
42 allow_origins=["*"], # In production, specify your frontend origin
43 allow_credentials=True,
44 allow_methods=["*"],
45 allow_headers=["*"],
47​
48# Initialize Anthropic client
49ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
50if not ANTHROPIC_API_KEY:
51 raise ValueError(
52 "ANTHROPIC_API_KEY environment variable is required. "
53 "Please set it in your .env file or environment."
54 )
55​
56# Validate API key format
57if ANTHROPIC_API_KEY.startswith("op://"):
58 raise ValueError(
59 "⚠️ ERROR: API key appears to be a 1Password reference (op://...).\n"
60 "You need to use the actual API key value, not the 1Password reference.\n"
61 "Please copy the actual key from 1Password (starts with 'sk-ant-') and update your .env file."
62 )
63​
64if not ANTHROPIC_API_KEY.startswith("sk-ant-"):
65 print(f"⚠️ WARNING: API key doesn't start with 'sk-ant-'. This may not be a valid Anthropic API key.")
66 print(f" Key starts with: {ANTHROPIC_API_KEY[:10]}...")
67​
68if len(ANTHROPIC_API_KEY) < 40:
69 print(f"⚠️ WARNING: API key seems too short ({len(ANTHROPIC_API_KEY)} chars). Anthropic keys are typically 50+ characters.")
70​
71# Display API key info on startup (masked for security)
72def mask_api_key(key: str) -> str:
73 """Mask API key showing only first 7 and last 4 characters"""
74 if len(key) <= 11:
75 return "*" * len(key)
76 return f"{key[:7]}...{key[-4:]}"
77​
78print(f"\n{'='*60}")
79print("🚀 TanStack AI FastAPI Server Starting...")
80print(f"{'='*60}")
81print(f"✅ ANTHROPIC_API_KEY loaded: {mask_api_key(ANTHROPIC_API_KEY)}")
82print(f" Key length: {len(ANTHROPIC_API_KEY)} characters")
83print(f"🤖 Agentic tool calling: ENABLED")
84print(f"🔧 Available tools: get_weather, get_time")
85print(f"🌐 Server will start on: http://0.0.0.0:8000")
86print(f" (Note: If running with uvicorn manually, use: uvicorn anthropic-server:app --reload --port 8000)")
87print(f"{'='*60}\n")
88​
89# Initialize TanStack AI adapter
90adapter = AnthropicAdapter(
91 AIAdapterConfig(api_key=ANTHROPIC_API_KEY)
93​
94# Define tools
95def get_weather_impl(args: Dict[str, Any]) -> Dict[str, Any]:
96 """Get weather for a city (returns static data for demo)"""
97 location = args.get("location", "Unknown")
98 unit = args.get("unit", "fahrenheit")
100 # Static weather data for different cities
101 weather_data = {
102 "san francisco": {"temp_f": 62, "temp_c": 17, "conditions": "Foggy"},
103 "new york": {"temp_f": 75, "temp_c": 24, "conditions": "Partly Cloudy"},
104 "london": {"temp_f": 55, "temp_c": 13, "conditions": "Rainy"},
105 "tokyo": {"temp_f": 70, "temp_c": 21, "conditions": "Clear"},
106 "paris": {"temp_f": 68, "temp_c": 20, "conditions": "Sunny"},
107 "sydney": {"temp_f": 80, "temp_c": 27, "conditions": "Sunny"},
108 }
110 # Get weather for the city (case-insensitive)
111 location_lower = location.lower()
112 city_weather = weather_data.get(location_lower, {
113 "temp_f": 72,
114 "temp_c": 22,
115 "conditions": "Clear"
116 })
118 temperature = city_weather["temp_c"] if unit == "celsius" else city_weather["temp_f"]
120 return {
121 "location": location,
122 "temperature": temperature,
123 "unit": unit,
124 "conditions": city_weather["conditions"],
125 "forecast": "Clear skies expected"
126 }
127​
128def get_time_impl(args: Dict[str, Any]) -> Dict[str, Any]:
129 """Get current time in a timezone (returns static data for demo)"""
130 timezone = args.get("timezone", "UTC")
132 # Static time data for different timezones
133 times = {
134 "UTC": "14:30",
135 "PST": "06:30",
136 "EST": "09:30",
137 "GMT": "14:30",
138 "JST": "23:30",
139 "AEST": "00:30",
140 }
142 time = times.get(timezone.upper(), "12:00")
144 return {
145 "timezone": timezone,
146 "time": time,
147 "format": "24-hour"
148 }
149​
150# Define tools using the tool helper
151weather_tool = tool(
152 name="get_weather",
153 description="Get the current weather for a location. Returns temperature, conditions, and forecast. Supports multiple cities including San Francisco, New York, London, Tokyo, Paris, and Sydney.",
154 input_schema={
155 "type": "object",
156 "properties": {
157 "location": {
158 "type": "string",
159 "description": "The city name (e.g., 'San Francisco', 'New York', 'London')",
160 },
161 "unit": {
162 "type": "string",
163 "enum": ["celsius", "fahrenheit"],
164 "description": "Temperature unit (default: fahrenheit)",
165 },
166 },
167 "required": ["location"],
168 },
169 execute=get_weather_impl,
171​
172time_tool = tool(
173 name="get_time",
174 description="Get the current time in a specific timezone. Supports UTC, PST, EST, GMT, JST, and AEST.",
175 input_schema={
176 "type": "object",
177 "properties": {
178 "timezone": {
179 "type": "string",
180 "description": "The timezone (e.g., 'PST', 'EST', 'UTC')",
181 },
182 },
183 "required": ["timezone"],
184 },
185 execute=get_time_impl,
187​
188# Available tools
189AVAILABLE_TOOLS = [weather_tool, time_tool]
190​
191# Request/Response models
192class Message(BaseModel):
193 role: str
194 content: str | None = None
195 name: Optional[str] = None
196 toolCalls: Optional[List[Dict[str, Any]]] = None
197 toolCallId: Optional[str] = None
198​
199class ChatRequest(BaseModel):
200 messages: List[Message]
201 data: Optional[Dict[str, Any]] = None
202​
203@app.post("/chat")
204async def chat_endpoint(request: ChatRequest):
205 """
206 Chat endpoint that streams responses in SSE format
207 Compatible with TanStack AI client's fetchServerSentEvents adapter
208 Now with automatic tool execution!
209 """
210 try:
211 logger.info(f"📥 POST /chat received - {len(request.messages)} messages")
213 # Convert Pydantic models to dict format for TanStack AI
214 messages = [msg.model_dump() for msg in request.messages]
215 logger.info(f"✅ Prepared {len(messages)} messages for chat")
217 # Default model - claude-3-haiku is fast and works well for tool calling
218 model = request.data.get("model") if request.data and request.data.get("model") else "claude-sonnet-4-5-20250929"
219 logger.info(f"🤖 Using model: {model}")
220 logger.info(f"🔧 Tools enabled: {len(AVAILABLE_TOOLS)} tools available")
222 async def generate_stream():
223 """Generate SSE stream with automatic tool execution"""
224 chunk_count = 0
225 tool_calls_count = 0
226 try:
227 logger.info(f"🚀 Starting agentic chat stream with {len(AVAILABLE_TOOLS)} tools")
229 # Use the new chat function with tools and agentic loop
230 async for chunk in chat(
231 adapter=adapter,
232 model=model,
233 messages=messages,
234 tools=AVAILABLE_TOOLS,
235 agent_loop_strategy=max_iterations(5), # Allow up to 5 iterations
236 options={
237 "max_tokens": 1024,
238 "temperature": 0.7,
239 },
240 ):
241 chunk_count += 1
242 chunk_type = chunk.get("type", "unknown")
244 # Log tool-related events
245 if chunk_type == "tool_call":
246 tool_calls_count += 1
247 tool_name = chunk.get("toolCall", {}).get("function", {}).get("name")
248 logger.info(f"🔧 Tool call #{tool_calls_count}: {tool_name}")
249 elif chunk_type == "tool_result":
250 tool_id = chunk.get("toolCallId")
251 result = chunk.get("content", "")[:100]
252 logger.info(f"✅ Tool result for {tool_id}: {result}...")
254 logger.debug(f"📤 Sending chunk #{chunk_count} (type: {chunk_type})")
255 yield format_sse_chunk(chunk)
257 logger.info(f"✅ Stream complete - {chunk_count} chunks sent, {tool_calls_count} tools called")
259 # Send completion marker
260 logger.info("📤 Sending [DONE] marker")
261 yield format_sse_done()
263 except Exception as e:
264 logger.error(f"❌ Error in stream: {type(e).__name__}: {str(e)}", exc_info=True)
265 # Send error chunk
266 error_chunk = {
267 "type": "error",
268 "id": "error",
269 "model": model,
270 "timestamp": 0,
271 "error": {
272 "message": str(e),
273 "code": type(e).__name__,
274 }
275 }
276 yield format_sse_chunk(error_chunk)
278 logger.info("📡 Returning StreamingResponse with agentic tool support")
279 return StreamingResponse(
280 generate_stream(),
281 media_type="text/event-stream",
282 headers={
283 "Cache-Control": "no-cache",
284 "Connection": "keep-alive",
285 "X-Accel-Buffering": "no" # Disable buffering for nginx
286 }
287 )
289 except Exception as e:
290 logger.error(f"❌ Error in chat_endpoint: {type(e).__name__}: {str(e)}", exc_info=True)
291 raise HTTPException(status_code=500, detail=str(e))
292​
293@app.get("/health")
294async def health_check():
295 """Health check endpoint"""
296 return {"status": "ok", "service": "tanstack-ai-fastapi"}
297​
298if __name__ == "__main__":
299 import uvicorn
300 uvicorn.run(app, host="0.0.0.0", port=8000)
301​