-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathembeddings.ts
More file actions
40 lines (33 loc) · 1.19 KB
/
embeddings.ts
File metadata and controls
40 lines (33 loc) · 1.19 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
37
38
39
40
/**
* Generate embeddings for search queries using OpenAI API
*/
export async function generateSearchEmbedding(query: string): Promise<number[]> {
const apiKey = process.env.OPENAI_API_KEY
if (!apiKey) {
throw new Error('OPENAI_API_KEY environment variable is required')
}
const response = await fetch('https://api.openai.com/v1/embeddings', {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
input: query,
model: 'text-embedding-3-small',
encoding_format: 'float',
}),
})
if (!response.ok) {
const errorText = await response.text()
throw new Error(`OpenAI API failed: ${response.status} ${response.statusText} - ${errorText}`)
}
const data = await response.json()
if (!data?.data || !Array.isArray(data.data) || data.data.length === 0) {
throw new Error('OpenAI API returned invalid response structure: missing or empty data array')
}
if (!data.data[0]?.embedding || !Array.isArray(data.data[0].embedding)) {
throw new Error('OpenAI API returned invalid response structure: missing or invalid embedding')
}
return data.data[0].embedding
}