Elffuss BraidHub

kikocis/continue public

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