Spaces:
Running
Running
| import { env, assertEmbeddingsConfigured } from "../config/env"; | |
| interface EmbeddingResponse { | |
| data?: Array<{ | |
| embedding?: number[]; | |
| }>; | |
| } | |
| export async function createEmbedding(input: string): Promise<number[]> { | |
| assertEmbeddingsConfigured(); | |
| const response = await fetch(`${env.EMBEDDING_BASE_URL.replace(/\/$/, "")}/embeddings`, { | |
| method: "POST", | |
| headers: { | |
| Authorization: `Bearer ${env.EMBEDDING_API_KEY}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify({ | |
| model: env.EMBEDDING_MODEL, | |
| input, | |
| dimensions: env.EMBEDDING_DIMENSIONS | |
| }) | |
| }); | |
| if (!response.ok) { | |
| const text = await response.text(); | |
| throw new Error(`Embedding provider failed with ${response.status}: ${text}`); | |
| } | |
| const payload = (await response.json()) as EmbeddingResponse; | |
| const embedding = payload.data?.[0]?.embedding; | |
| if (!embedding || embedding.length !== env.EMBEDDING_DIMENSIONS) { | |
| throw new Error(`Embedding provider returned an invalid vector. Expected ${env.EMBEDDING_DIMENSIONS} dimensions.`); | |
| } | |
| return embedding; | |
| } | |