Spaces:
Running
Running
| import { OpenAI } from 'openai' | |
| const client = new OpenAI({ | |
| baseURL: 'https://router.huggingface.co/v1', | |
| apiKey: process.env.HF_TOKEN, | |
| defaultHeaders: { | |
| "X-HF-Bill-To": "huggingface" | |
| } | |
| }) | |
| export default async function handler(req, res) { | |
| if (req.method !== 'POST') { | |
| return res.status(405).json({ error: 'Method not allowed' }) | |
| } | |
| try { | |
| const { messages } = req.body | |
| if (!messages || !Array.isArray(messages)) { | |
| return res.status(400).json({ error: 'Messages array is required' }) | |
| } | |
| // Transform messages to OpenAI format | |
| const openAIMessages = messages.map(msg => { | |
| if (msg.image) { | |
| return { | |
| role: msg.role, | |
| content: [ | |
| { | |
| type: 'text', | |
| text: msg.content || 'Analyze this image' | |
| }, | |
| { | |
| type: 'image_url', | |
| image_url: { | |
| url: msg.image | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| return { | |
| role: msg.role, | |
| content: msg.content | |
| } | |
| }) | |
| // Create streaming response | |
| res.writeHead(200, { | |
| 'Content-Type': 'text/event-stream', | |
| 'Cache-Control': 'no-cache', | |
| 'Connection': 'keep-alive', | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Access-Control-Allow-Headers': 'Cache-Control', | |
| }) | |
| try { | |
| const stream = await client.chat.completions.create({ | |
| model: 'Qwen/Qwen3-VL-8B-Instruct:novita', | |
| messages: openAIMessages, | |
| stream: true, | |
| max_tokens: 1000, | |
| temperature: 0.7, | |
| }) | |
| for await (const chunk of stream) { | |
| const content = chunk.choices[0]?.delta?.content || '' | |
| if (content) { | |
| res.write(`data: ${JSON.stringify({ content })}\n\n`) | |
| } | |
| } | |
| res.write('data: [DONE]\n\n') | |
| res.end() | |
| } catch (streamError) { | |
| console.error('Streaming error:', streamError) | |
| res.write(`data: ${JSON.stringify({ error: streamError.message })}\n\n`) | |
| res.end() | |
| } | |
| } catch (error) { | |
| console.error('API error:', error) | |
| res.status(500).json({ error: error.message || 'Internal server error' }) | |
| } | |
| } |