-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodels.ts
More file actions
36 lines (33 loc) · 1.01 KB
/
models.ts
File metadata and controls
36 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { createServerFileRoute } from '@tanstack/react-start/server'
import OpenAI from 'openai'
const SUPPORTED_MODEL_PREFIXES = [
'gpt-4o',
'chatgpt-4o',
'gpt-4.1',
'o1',
'o3',
'o4-mini',
]
function isSupportedModel(id: string) {
return SUPPORTED_MODEL_PREFIXES.some((prefix) => id.startsWith(prefix))
}
export const ServerRoute = createServerFileRoute('/api/models').methods({
async GET({ request }) {
try {
const client = new OpenAI()
const models = await client.models.list()
const filtered = models.data
.filter((model) => isSupportedModel(model.id))
.sort((a, b) => a.id.localeCompare(b.id))
return new Response(JSON.stringify(filtered), {
headers: { 'Content-Type': 'application/json' },
})
} catch (error) {
console.error('Error fetching models:', error)
return new Response(JSON.stringify({ error: 'Failed to fetch models' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
})
}
},
})