feat: use correct deployment for azure
This commit is contained in:
parent
fae5c5db4d
commit
8e24fcf4a0
7 changed files with 152 additions and 18 deletions
108
packages/openai-adapters/src/apis/Azure.ts
Normal file
108
packages/openai-adapters/src/apis/Azure.ts
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
import { OpenAI } from "openai/index";
|
||||
import {
|
||||
ChatCompletionChunk,
|
||||
ChatCompletionCreateParams,
|
||||
ChatCompletionCreateParamsStreaming,
|
||||
} from "openai/resources/index";
|
||||
import { z } from "zod";
|
||||
import { AzureConfigSchema } from "../types.js";
|
||||
import { customFetch } from "../util.js";
|
||||
import { OpenAIApi } from "./OpenAI.js";
|
||||
|
||||
export class AzureApi extends OpenAIApi {
|
||||
constructor(private azureConfig: z.infer<typeof AzureConfigSchema>) {
|
||||
super({
|
||||
...azureConfig,
|
||||
provider: "openai",
|
||||
});
|
||||
|
||||
this.openai = new OpenAI({
|
||||
apiKey: azureConfig.apiKey,
|
||||
baseURL: this._getAzureBaseURL(azureConfig),
|
||||
defaultQuery: azureConfig.env?.apiVersion
|
||||
? { "api-version": azureConfig.env.apiVersion }
|
||||
: {},
|
||||
fetch: customFetch(azureConfig.requestOptions),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Default is `azure-openai`, but previously was `azure`
|
||||
* @param apiType
|
||||
* @returns
|
||||
*/
|
||||
private _isAzureOpenAI(apiType?: string): boolean {
|
||||
return apiType === "azure-openai" || apiType === "azure";
|
||||
}
|
||||
|
||||
private _getAzureBaseURL(config: z.infer<typeof AzureConfigSchema>): string {
|
||||
const baseURL = new URL(this.apiBase).toString().replace(/\/$/, "");
|
||||
|
||||
// Default is `azure-openai` in docs, but previously was `azure`
|
||||
if (this._isAzureOpenAI(config.env?.apiType)) {
|
||||
if (!config.env?.deployment) {
|
||||
throw new Error(
|
||||
"Azure deployment is required if `apiType` is `azure-openai` or `azure`",
|
||||
);
|
||||
}
|
||||
|
||||
return `${baseURL}/openai/deployments/${config.env.deployment}`;
|
||||
}
|
||||
|
||||
return baseURL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters out empty text content parts from messages.
|
||||
*
|
||||
* Azure models may not support empty content parts, which can cause issues.
|
||||
* This function removes any text content parts that are empty or contain only whitespace.
|
||||
*/
|
||||
private _filterEmptyContentParts<T extends ChatCompletionCreateParams>(
|
||||
body: T,
|
||||
): T {
|
||||
const result = { ...body };
|
||||
|
||||
result.messages = result.messages.map((message: any) => {
|
||||
if (Array.isArray(message.content)) {
|
||||
const filteredContent = message.content.filter((part: any) => {
|
||||
return !(
|
||||
part.type === "text" &&
|
||||
(!part.text || part.text.trim() === "")
|
||||
);
|
||||
});
|
||||
return {
|
||||
...message,
|
||||
content:
|
||||
filteredContent.length > 0 ? filteredContent : message.content,
|
||||
};
|
||||
}
|
||||
return message;
|
||||
}) as any;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T {
|
||||
let modifiedBody = super.modifyChatBody(body);
|
||||
modifiedBody = this._filterEmptyContentParts(modifiedBody);
|
||||
return modifiedBody;
|
||||
}
|
||||
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const response = await this.openai.chat.completions.create(
|
||||
this.modifyChatBody(body),
|
||||
{ signal },
|
||||
);
|
||||
|
||||
for await (const result of response) {
|
||||
// Skip chunks with no choices (common with Azure content filtering)
|
||||
if (result.choices && result.choices.length > 0) {
|
||||
yield result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@ import {
|
|||
Model,
|
||||
} from "openai/resources/index";
|
||||
import { z } from "zod";
|
||||
import { AzureConfigSchema, OpenAIConfigSchema } from "../types.js";
|
||||
import { OpenAIConfigSchema } from "../types.js";
|
||||
import { customFetch } from "../util.js";
|
||||
import {
|
||||
BaseLlmApi,
|
||||
|
|
@ -25,11 +25,7 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
openai: OpenAI;
|
||||
apiBase: string = "https://api.openai.com/v1/";
|
||||
|
||||
constructor(
|
||||
protected config: z.infer<
|
||||
typeof OpenAIConfigSchema | typeof AzureConfigSchema
|
||||
>,
|
||||
) {
|
||||
constructor(protected config: z.infer<typeof OpenAIConfigSchema>) {
|
||||
this.apiBase = config.apiBase ?? this.apiBase;
|
||||
this.openai = new OpenAI({
|
||||
apiKey: config.apiKey,
|
||||
|
|
@ -37,7 +33,6 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
fetch: customFetch(config.requestOptions),
|
||||
});
|
||||
}
|
||||
|
||||
modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T {
|
||||
// o-series models - only apply for official OpenAI API
|
||||
const isOfficialOpenAIAPI = this.apiBase === "https://api.openai.com/v1/";
|
||||
|
|
@ -74,6 +69,7 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import dotenv from "dotenv";
|
||||
import { z } from "zod";
|
||||
import { AnthropicApi } from "./apis/Anthropic.js";
|
||||
import { AzureApi } from "./apis/Azure.js";
|
||||
import { CohereApi } from "./apis/Cohere.js";
|
||||
import { DeepSeekApi } from "./apis/DeepSeek.js";
|
||||
import { GeminiApi } from "./apis/Gemini.js";
|
||||
|
|
@ -29,8 +30,9 @@ function openAICompatible(
|
|||
export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
|
||||
switch (config.provider) {
|
||||
case "openai":
|
||||
case "azure":
|
||||
return new OpenAIApi(config);
|
||||
case "azure":
|
||||
return new AzureApi(config);
|
||||
case "cohere":
|
||||
return new CohereApi(config);
|
||||
case "anthropic":
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { DEEPSEEK_API_BASE } from "../apis/DeepSeek.js";
|
|||
import { INCEPTION_API_BASE } from "../apis/Inception.js";
|
||||
import { OpenAIApi } from "../apis/OpenAI.js";
|
||||
import { constructLlmApi } from "../index.js";
|
||||
import { getLlmApi, testChat, testCompletion, testEmbed } from "./util.js";
|
||||
import { getLlmApi, testChat, testEmbed } from "./util.js";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
|
@ -17,10 +17,6 @@ function testConfig(config: ModelConfig) {
|
|||
env: config.env,
|
||||
});
|
||||
|
||||
if (false) {
|
||||
testCompletion(api, model);
|
||||
}
|
||||
|
||||
if (
|
||||
["chat", "summarize", "edit", "apply"].some((role) =>
|
||||
config.roles?.includes(role as any),
|
||||
|
|
@ -106,6 +102,28 @@ const TESTS: Omit<ModelConfig, "name">[] = [
|
|||
// apiKey: process.env.COHERE_API_KEY!,
|
||||
// roles: ["rerank"],
|
||||
// },
|
||||
{
|
||||
provider: "azure",
|
||||
model: "gpt-4.1",
|
||||
apiBase: "https://continue-openai.openai.azure.com",
|
||||
apiKey: process.env.AZURE_OPENAI_API_KEY,
|
||||
env: {
|
||||
deployment: "gpt-4.1",
|
||||
apiVersion: "2024-02-15-preview",
|
||||
apiType: "azure-openai",
|
||||
},
|
||||
roles: ["chat"],
|
||||
},
|
||||
{
|
||||
provider: "azure",
|
||||
model: "mistral-small-2503",
|
||||
apiBase: "https://nate-0276-resource.services.ai.azure.com/models",
|
||||
apiKey: process.env.AZURE_FOUNDRY_API_KEY,
|
||||
env: {
|
||||
deployment: "mistral-small-2503",
|
||||
},
|
||||
roles: ["chat"],
|
||||
},
|
||||
];
|
||||
|
||||
TESTS.forEach((config) => {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ export function getLlmApi(config: LLMConfig) {
|
|||
}
|
||||
return api;
|
||||
}
|
||||
|
||||
export function testEmbed(api: BaseLlmApi, model: string) {
|
||||
test("should successfully embed", async () => {
|
||||
const response = await api.embed({
|
||||
|
|
@ -155,7 +154,6 @@ export function testChat(api: BaseLlmApi, model: string) {
|
|||
let completion = "";
|
||||
for await (const result of stream) {
|
||||
completion += result.choices[0].delta.content ?? "";
|
||||
|
||||
expect(result.choices.length).toBeGreaterThan(0);
|
||||
}
|
||||
expect(completion.length).toBeGreaterThan(0);
|
||||
|
|
@ -313,7 +311,6 @@ export function testChat(api: BaseLlmApi, model: string) {
|
|||
},
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
console.log(JSON.stringify(chunk, null, 2));
|
||||
response += chunk.choices[0].delta.content ?? "";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -84,6 +84,19 @@ export type CohereConfig = z.infer<typeof CohereConfigSchema>;
|
|||
|
||||
export const AzureConfigSchema = OpenAIConfigSchema.extend({
|
||||
provider: z.literal("azure"),
|
||||
env: z
|
||||
.object({
|
||||
apiVersion: z.string().optional(),
|
||||
apiType: z
|
||||
.union([
|
||||
z.literal("azure-foundry"),
|
||||
z.literal("azure-openai"),
|
||||
z.literal("azure"), // Legacy
|
||||
])
|
||||
.optional(),
|
||||
deployment: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
});
|
||||
export type AzureConfig = z.infer<typeof AzureConfigSchema>;
|
||||
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@
|
|||
"noEmitOnError": false,
|
||||
"types": ["node", "jest"],
|
||||
"outDir": "dist",
|
||||
"declaration": true
|
||||
// "sourceMap": true
|
||||
"declaration": true,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue