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

openai-server.py

182 lines6.5 KB

1"""
2FastAPI server example for TanStack AI
3Streams OpenAI API events in SSE format compatible with TanStack AI client
4"""
5import os
6import logging
7from typing import List, Dict, Any, Optional
8​
9from dotenv import load_dotenv
10from fastapi import FastAPI, HTTPException
11from fastapi.responses import StreamingResponse
12from fastapi.middleware.cors import CORSMiddleware
13from pydantic import BaseModel
14from openai import AsyncOpenAI
15​
16from tanstack_ai import StreamChunkConverter, format_messages_for_openai, format_sse_chunk, format_sse_done
17​
18# Configure logging
19logging.basicConfig(
20 level=logging.INFO,
21 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
23logger = logging.getLogger(__name__)
24​
25# Load environment variables from .env file
26load_dotenv()
27​
28# Initialize FastAPI app
29app = FastAPI(title="TanStack AI Python FastAPI Example - OpenAI")
30​
31# CORS middleware
32app.add_middleware(
33 CORSMiddleware,
34 allow_origins=["*"], # In production, specify your frontend origin
35 allow_credentials=True,
36 allow_methods=["*"],
37 allow_headers=["*"],
39​
40# Initialize OpenAI client
41OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
42if not OPENAI_API_KEY:
43 raise ValueError(
44 "OPENAI_API_KEY environment variable is required. "
45 "Please set it in your .env file or environment."
46 )
47​
48# Validate API key format
49if OPENAI_API_KEY.startswith("op://"):
50 raise ValueError(
51 "⚠️ ERROR: API key appears to be a 1Password reference (op://...).\n"
52 "You need to use the actual API key value, not the 1Password reference.\n"
53 "Please copy the actual key from 1Password (starts with 'sk-') and update your .env file."
54 )
55​
56if not OPENAI_API_KEY.startswith("sk-"):
57 print(f"⚠️ WARNING: API key doesn't start with 'sk-'. This may not be a valid OpenAI API key.")
58 print(f" Key starts with: {OPENAI_API_KEY[:10]}...")
59​
60if len(OPENAI_API_KEY) < 40:
61 print(f"⚠️ WARNING: API key seems too short ({len(OPENAI_API_KEY)} chars). OpenAI keys are typically 50+ characters.")
62​
63# Display API key info on startup (masked for security)
64def mask_api_key(key: str) -> str:
65 """Mask API key showing only first 7 and last 4 characters"""
66 if len(key) <= 11:
67 return "*" * len(key)
68 return f"{key[:7]}...{key[-4:]}"
69​
70print(f"\n{'='*60}")
71print("🚀 TanStack AI FastAPI Server Starting (OpenAI)...")
72print(f"{'='*60}")
73print(f"✅ OPENAI_API_KEY loaded: {mask_api_key(OPENAI_API_KEY)}")
74print(f" Key length: {len(OPENAI_API_KEY)} characters")
75print(f"🌐 Server will start on: http://0.0.0.0:8001")
76print(f" (Note: If running with uvicorn manually, use: uvicorn openai-server:app --reload --port 8001)")
77print(f"{'='*60}\n")
78​
79client = AsyncOpenAI(api_key=OPENAI_API_KEY)
80​
81# Request/Response models
82class Message(BaseModel):
83 role: str
84 content: str | None = None
85 name: Optional[str] = None
86 toolCalls: Optional[List[Dict[str, Any]]] = None
87 toolCallId: Optional[str] = None
88​
89​
90class ChatRequest(BaseModel):
91 messages: List[Message]
92 data: Optional[Dict[str, Any]] = None
93​
94​
95@app.post("/chat")
96async def chat_endpoint(request: ChatRequest):
97 """
98 Chat endpoint that streams responses in SSE format
99 Compatible with TanStack AI client's fetchServerSentEvents adapter
100 """
101 try:
102 logger.info(f"📥 POST /chat received - {len(request.messages)} messages")
104 # Convert messages to OpenAI format
105 openai_messages = format_messages_for_openai(request.messages)
106 logger.info(f"✅ Converted {len(openai_messages)} messages to OpenAI format")
108 # Default model - gpt-4o is a good default
109 model = request.data.get("model") if request.data and request.data.get("model") else "gpt-4o"
110 logger.info(f"🤖 Using model: {model}")
112 # Initialize converter (specify provider for better performance)
113 converter = StreamChunkConverter(model=model, provider="openai")
115 async def generate_stream():
116 """Generate SSE stream from OpenAI events"""
117 event_count = 0
118 chunk_count = 0
119 try:
120 logger.info(f"🚀 Starting OpenAI stream for model: {model}")
122 # Stream from OpenAI
123 stream = await client.chat.completions.create(
124 model=model,
125 messages=openai_messages,
126 max_tokens=1024,
127 temperature=0.7,
128 stream=True
129 )
130 logger.info("✅ OpenAI stream created, starting to receive events...")
132 async for event in stream:
133 event_count += 1
134 logger.debug(f"📨 Received OpenAI event #{event_count}: {type(event).__name__}")
136 # Convert OpenAI event to StreamChunk format using TanStack converter
137 chunks = await converter.convert_event(event)
139 for chunk in chunks:
140 chunk_count += 1
141 chunk_type = chunk.get("type", "unknown")
142 logger.debug(f"📤 Sending chunk #{chunk_count} (type: {chunk_type})")
143 yield format_sse_chunk(chunk)
145 logger.info(f"✅ Stream complete - {event_count} events, {chunk_count} chunks sent")
147 # Send completion marker
148 logger.info("📤 Sending [DONE] marker")
149 yield format_sse_done()
151 except Exception as e:
152 logger.error(f"❌ Error in stream: {type(e).__name__}: {str(e)}", exc_info=True)
153 # Send error chunk
154 error_chunk = await converter.convert_error(e)
155 yield format_sse_chunk(error_chunk)
157 logger.info("📡 Returning StreamingResponse")
158 return StreamingResponse(
159 generate_stream(),
160 media_type="text/event-stream",
161 headers={
162 "Cache-Control": "no-cache",
163 "Connection": "keep-alive",
164 "X-Accel-Buffering": "no" # Disable buffering for nginx
165 }
166 )
168 except Exception as e:
169 logger.error(f"❌ Error in chat_endpoint: {type(e).__name__}: {str(e)}", exc_info=True)
170 raise HTTPException(status_code=500, detail=str(e))
171​
172​
173@app.get("/health")
174async def health_check():
175 """Health check endpoint"""
176 return {"status": "ok", "service": "tanstack-ai-fastapi-openai"}
177​
178​
179if __name__ == "__main__":
180 import uvicorn
181 uvicorn.run(app, host="0.0.0.0", port=8001)
182​