( embeddingModel: string, workspaceId?: string | null )
| 198 | } |
| 199 | |
| 200 | async function resolveProvider( |
| 201 | embeddingModel: string, |
| 202 | workspaceId?: string | null |
| 203 | ): Promise<ResolvedProvider> { |
| 204 | const azureApiKey = env.AZURE_OPENAI_API_KEY |
| 205 | const azureEndpoint = env.AZURE_OPENAI_ENDPOINT |
| 206 | const azureApiVersion = env.AZURE_OPENAI_API_VERSION |
| 207 | const isOpenAIModel = SUPPORTED_EMBEDDING_MODELS[embeddingModel]?.provider === 'openai' |
| 208 | /** |
| 209 | * Azure deployment names default to the embedding model name when |
| 210 | * `KB_OPENAI_MODEL_NAME` is unset — this matches the pre-existing |
| 211 | * convention where deployments are named after the model they host. |
| 212 | */ |
| 213 | const azureDeploymentName = env.KB_OPENAI_MODEL_NAME || embeddingModel |
| 214 | const useAzure = Boolean(isOpenAIModel && azureApiKey && azureEndpoint && azureApiVersion) |
| 215 | |
| 216 | const info = getEmbeddingModelInfo(embeddingModel) |
| 217 | |
| 218 | if (useAzure) { |
| 219 | return { |
| 220 | modelName: azureDeploymentName, |
| 221 | pricingId: info.pricingId, |
| 222 | isBYOK: false, |
| 223 | tokenizerProvider: info.tokenizerProvider, |
| 224 | buildRequest: buildAzureOpenAIProvider( |
| 225 | azureDeploymentName, |
| 226 | azureApiKey!, |
| 227 | azureEndpoint!, |
| 228 | azureApiVersion! |
| 229 | ), |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (info.provider === 'openai') { |
| 234 | const { apiKey, isBYOK } = await resolveOpenAIKey(workspaceId) |
| 235 | return { |
| 236 | modelName: embeddingModel, |
| 237 | pricingId: info.pricingId, |
| 238 | isBYOK, |
| 239 | tokenizerProvider: info.tokenizerProvider, |
| 240 | buildRequest: buildOpenAIProvider(embeddingModel, apiKey), |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (info.provider === 'gemini') { |
| 245 | const { apiKey, isBYOK } = await resolveGeminiKey(workspaceId) |
| 246 | return { |
| 247 | modelName: embeddingModel, |
| 248 | pricingId: info.pricingId, |
| 249 | isBYOK, |
| 250 | tokenizerProvider: info.tokenizerProvider, |
| 251 | maxItemsPerRequest: GEMINI_MAX_ITEMS_PER_REQUEST, |
| 252 | buildRequest: buildGeminiProvider(embeddingModel, apiKey), |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | throw new Error(`Unknown embedding provider for model ${embeddingModel}`) |
| 257 | } |
no test coverage detected