/** * Azure OpenAI text embeddings. * * Reuses the same Azure OpenAI resource as the agent (see agent/agent.ts). We * use the `text-embedding-3-small` deployment (1536 dimensions) to turn chat * memories and queries into vectors for Cosmos DB vector search. */ import { createAzure } from "@ai-sdk/azure"; import { embed, embedMany } from "ai"; /** Must match the vector embedding policy dimensions on the memory container. */ export const EMBEDDING_DIMENSIONS = 1536; function embeddingModel() { const endpoint = (process.env.AZURE_OPENAI_ENDPOINT ?? "").replace(/\/$/, ""); if (!endpoint) { throw new Error("AZURE_OPENAI_ENDPOINT is not set."); } const azure = createAzure({ baseURL: `${endpoint}/openai`, apiKey: process.env.AZURE_OPENAI_API_KEY, apiVersion: process.env.AZURE_OPENAI_API_VERSION ?? "preview", }); const deployment = process.env.AZURE_OPENAI_EMBEDDING_DEPLOYMENT ?? "text-embedding-3-small"; return azure.textEmbeddingModel(deployment); } /** Embed a single string into a 1536-dim vector. */ export async function embedText(text: string): Promise { const { embedding } = await embed({ model: embeddingModel(), value: text }); return embedding; } /** Embed many strings in one request (fewer round-trips than looping). */ export async function embedTexts(texts: string[]): Promise { if (texts.length === 0) return []; const { embeddings } = await embedMany({ model: embeddingModel(), values: texts, }); return embeddings; }