|
| 1 | +import { OpenAI } from "openai/index"; |
| 2 | +import { |
| 3 | + ChatCompletionChunk, |
| 4 | + ChatCompletionCreateParams, |
| 5 | + ChatCompletionCreateParamsStreaming, |
| 6 | +} from "openai/resources/index"; |
| 7 | +import { z } from "zod"; |
| 8 | +import { AzureConfigSchema } from "../types.js"; |
| 9 | +import { customFetch } from "../util.js"; |
| 10 | +import { OpenAIApi } from "./OpenAI.js"; |
| 11 | + |
| 12 | +export class AzureApi extends OpenAIApi { |
| 13 | + constructor(private azureConfig: z.infer<typeof AzureConfigSchema>) { |
| 14 | + super({ |
| 15 | + ...azureConfig, |
| 16 | + provider: "openai", |
| 17 | + }); |
| 18 | + |
| 19 | + this.openai = new OpenAI({ |
| 20 | + apiKey: azureConfig.apiKey, |
| 21 | + baseURL: this._getAzureBaseURL(azureConfig), |
| 22 | + defaultQuery: azureConfig.env?.apiVersion |
| 23 | + ? { "api-version": azureConfig.env.apiVersion } |
| 24 | + : {}, |
| 25 | + fetch: customFetch(azureConfig.requestOptions), |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Default is `azure-openai`, but previously was `azure` |
| 31 | + * @param apiType |
| 32 | + * @returns |
| 33 | + */ |
| 34 | + private _isAzureOpenAI(apiType?: string): boolean { |
| 35 | + return apiType === "azure-openai" || apiType === "azure"; |
| 36 | + } |
| 37 | + |
| 38 | + private _getAzureBaseURL(config: z.infer<typeof AzureConfigSchema>): string { |
| 39 | + const baseURL = new URL(this.apiBase).toString().replace(/\/$/, ""); |
| 40 | + |
| 41 | + // Default is `azure-openai` in docs, but previously was `azure` |
| 42 | + if (this._isAzureOpenAI(config.env?.apiType)) { |
| 43 | + if (!config.env?.deployment) { |
| 44 | + throw new Error( |
| 45 | + "Azure deployment is required if `apiType` is `azure-openai` or `azure`", |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + return `${baseURL}/openai/deployments/${config.env.deployment}`; |
| 50 | + } |
| 51 | + |
| 52 | + return baseURL; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Filters out empty text content parts from messages. |
| 57 | + * |
| 58 | + * Azure models may not support empty content parts, which can cause issues. |
| 59 | + * This function removes any text content parts that are empty or contain only whitespace. |
| 60 | + */ |
| 61 | + private _filterEmptyContentParts<T extends ChatCompletionCreateParams>( |
| 62 | + body: T, |
| 63 | + ): T { |
| 64 | + const result = { ...body }; |
| 65 | + |
| 66 | + result.messages = result.messages.map((message: any) => { |
| 67 | + if (Array.isArray(message.content)) { |
| 68 | + const filteredContent = message.content.filter((part: any) => { |
| 69 | + return !( |
| 70 | + part.type === "text" && |
| 71 | + (!part.text || part.text.trim() === "") |
| 72 | + ); |
| 73 | + }); |
| 74 | + return { |
| 75 | + ...message, |
| 76 | + content: |
| 77 | + filteredContent.length > 0 ? filteredContent : message.content, |
| 78 | + }; |
| 79 | + } |
| 80 | + return message; |
| 81 | + }) as any; |
| 82 | + |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T { |
| 87 | + let modifiedBody = super.modifyChatBody(body); |
| 88 | + modifiedBody = this._filterEmptyContentParts(modifiedBody); |
| 89 | + return modifiedBody; |
| 90 | + } |
| 91 | + |
| 92 | + async *chatCompletionStream( |
| 93 | + body: ChatCompletionCreateParamsStreaming, |
| 94 | + signal: AbortSignal, |
| 95 | + ): AsyncGenerator<ChatCompletionChunk, any, unknown> { |
| 96 | + const response = await this.openai.chat.completions.create( |
| 97 | + this.modifyChatBody(body), |
| 98 | + { signal }, |
| 99 | + ); |
| 100 | + |
| 101 | + for await (const result of response) { |
| 102 | + // Skip chunks with no choices (common with Azure content filtering) |
| 103 | + if (result.choices && result.choices.length > 0) { |
| 104 | + yield result; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments