Merge pull request #6023 from continuedev/pe/watsonx-vi
feat: use correct deployment for azure
This commit is contained in:
commit
dc803d8888
8 changed files with 269 additions and 123 deletions
2
.github/workflows/pr_checks.yaml
vendored
2
.github/workflows/pr_checks.yaml
vendored
|
|
@ -435,6 +435,8 @@ jobs:
|
|||
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
|
||||
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}
|
||||
AZURE_FOUNDRY_API_KEY: ${{ secrets.AZURE_FOUNDRY_API_KEY }}
|
||||
AZURE_FOUNDRY_MISTRAL_SMALL_API_KEY: ${{ secrets.AZURE_FOUNDRY_MISTRAL_SMALL_API_KEY }}
|
||||
AZURE_OPENAI_GPT41_API_KEY: ${{ secrets.AZURE_OPENAI_GPT41_API_KEY }}
|
||||
|
||||
package-tests:
|
||||
runs-on: ubuntu-latest
|
||||
|
|
|
|||
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,11 +4,16 @@ 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();
|
||||
|
||||
function testConfig(config: ModelConfig) {
|
||||
export interface TestConfigOptions {
|
||||
skipTools: boolean;
|
||||
}
|
||||
|
||||
function testConfig(_config: ModelConfig & { options?: TestConfigOptions }) {
|
||||
const { options, ...config } = _config;
|
||||
const model = config.model;
|
||||
const api = getLlmApi({
|
||||
provider: config.provider as any,
|
||||
|
|
@ -17,16 +22,12 @@ 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),
|
||||
)
|
||||
) {
|
||||
testChat(api, model);
|
||||
testChat(api, model, options);
|
||||
}
|
||||
|
||||
if (config.roles?.includes("embed")) {
|
||||
|
|
@ -38,7 +39,7 @@ function testConfig(config: ModelConfig) {
|
|||
}
|
||||
}
|
||||
|
||||
const TESTS: Omit<ModelConfig, "name">[] = [
|
||||
const TESTS: Omit<ModelConfig & { options?: TestConfigOptions }, "name">[] = [
|
||||
{
|
||||
provider: "openai",
|
||||
model: "gpt-4o",
|
||||
|
|
@ -106,6 +107,26 @@ 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_GPT41_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_MISTRAL_SMALL_API_KEY,
|
||||
roles: ["chat"],
|
||||
options: { skipTools: true },
|
||||
},
|
||||
];
|
||||
|
||||
TESTS.forEach((config) => {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { BaseLlmApi, constructLlmApi } from "../index.js";
|
||||
import { LLMConfig } from "../types.js";
|
||||
import { TestConfigOptions } from "./main.test.js";
|
||||
|
||||
export function getLlmApi(config: LLMConfig) {
|
||||
const api = constructLlmApi(config);
|
||||
|
|
@ -8,7 +9,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({
|
||||
|
|
@ -73,7 +73,11 @@ export function testFim(api: BaseLlmApi, model: string) {
|
|||
});
|
||||
}
|
||||
|
||||
export function testChat(api: BaseLlmApi, model: string) {
|
||||
export function testChat(
|
||||
api: BaseLlmApi,
|
||||
model: string,
|
||||
options?: TestConfigOptions,
|
||||
) {
|
||||
test("should successfully stream chat", async () => {
|
||||
const stream = api.chatCompletionStream(
|
||||
{
|
||||
|
|
@ -155,7 +159,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);
|
||||
|
|
@ -201,124 +204,125 @@ export function testChat(api: BaseLlmApi, model: string) {
|
|||
expect(completion?.startsWith("RESPONSE: ")).toBe(true);
|
||||
});
|
||||
|
||||
test("Tool Call works", async () => {
|
||||
let args = "";
|
||||
let isFirstChunk = true;
|
||||
for await (const chunk of api.chatCompletionStream(
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content:
|
||||
"Hi, my name is Nate. Please call the say_hello function to respond to me.",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
function: {
|
||||
name: "say_hello",
|
||||
description: "Say Hello",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: {
|
||||
type: "string",
|
||||
description: "The name of the person to greet",
|
||||
if (options?.skipTools === false) {
|
||||
test("Tool Call works", async () => {
|
||||
let args = "";
|
||||
let isFirstChunk = true;
|
||||
for await (const chunk of api.chatCompletionStream(
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content:
|
||||
"Hi, my name is Nate. Please call the say_hello function to respond to me.",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
function: {
|
||||
name: "say_hello",
|
||||
description: "Say Hello",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: {
|
||||
type: "string",
|
||||
description: "The name of the person to greet",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
type: "function",
|
||||
},
|
||||
],
|
||||
tool_choice: {
|
||||
type: "function",
|
||||
function: {
|
||||
name: "say_hello",
|
||||
},
|
||||
},
|
||||
],
|
||||
tool_choice: {
|
||||
type: "function",
|
||||
function: {
|
||||
name: "say_hello",
|
||||
},
|
||||
stream: true,
|
||||
model,
|
||||
},
|
||||
stream: true,
|
||||
model,
|
||||
},
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
const toolCall = chunk.choices[0].delta.tool_calls?.[0];
|
||||
if (!toolCall) {
|
||||
continue;
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
const toolCall = chunk.choices[0].delta.tool_calls?.[0];
|
||||
if (!toolCall) {
|
||||
continue;
|
||||
}
|
||||
args += toolCall.function?.arguments ?? "";
|
||||
|
||||
expect(chunk.choices[0].delta.tool_calls).toHaveLength(1);
|
||||
|
||||
if (isFirstChunk) {
|
||||
isFirstChunk = false;
|
||||
expect(toolCall.id).toBeDefined();
|
||||
expect(toolCall.function!.name).toBe("say_hello");
|
||||
}
|
||||
}
|
||||
args += toolCall.function?.arguments ?? "";
|
||||
|
||||
expect(chunk.choices[0].delta.tool_calls).toHaveLength(1);
|
||||
const parsedArgs = JSON.parse(args);
|
||||
expect(parsedArgs.name).toBe("Nate");
|
||||
});
|
||||
|
||||
if (isFirstChunk) {
|
||||
isFirstChunk = false;
|
||||
expect(toolCall.id).toBeDefined();
|
||||
expect(toolCall.function!.name).toBe("say_hello");
|
||||
}
|
||||
}
|
||||
|
||||
const parsedArgs = JSON.parse(args);
|
||||
expect(parsedArgs.name).toBe("Nate");
|
||||
});
|
||||
|
||||
test("Tool Call second message works", async () => {
|
||||
let response = "";
|
||||
for await (const chunk of api.chatCompletionStream(
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content:
|
||||
"Hi, my name is Nate. Please call the say_hello function to respond to me, then respond to me again.",
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
tool_calls: [
|
||||
{
|
||||
function: {
|
||||
arguments: '{"name":"Nate"}',
|
||||
name: "say_hello",
|
||||
test("Tool Call second message works", async () => {
|
||||
let response = "";
|
||||
for await (const chunk of api.chatCompletionStream(
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content:
|
||||
"Hi, my name is Nate. Please call the say_hello function to respond to me, then respond to me again.",
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
tool_calls: [
|
||||
{
|
||||
function: {
|
||||
arguments: '{"name":"Nate"}',
|
||||
name: "say_hello",
|
||||
},
|
||||
id: "tool_call_1",
|
||||
type: "function",
|
||||
},
|
||||
id: "tool_call_1",
|
||||
type: "function",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
role: "tool",
|
||||
tool_call_id: "tool_call_1",
|
||||
content: "Nate was greeted",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
function: {
|
||||
name: "say_hello",
|
||||
description: "Say Hello",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: {
|
||||
type: "string",
|
||||
description: "The name of the person to greet",
|
||||
],
|
||||
},
|
||||
{
|
||||
role: "tool",
|
||||
tool_call_id: "tool_call_1",
|
||||
content: "Nate was greeted",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
function: {
|
||||
name: "say_hello",
|
||||
description: "Say Hello",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: {
|
||||
type: "string",
|
||||
description: "The name of the person to greet",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
type: "function",
|
||||
},
|
||||
type: "function",
|
||||
},
|
||||
],
|
||||
stream: true,
|
||||
model,
|
||||
},
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
console.log(JSON.stringify(chunk, null, 2));
|
||||
response += chunk.choices[0].delta.content ?? "";
|
||||
}
|
||||
],
|
||||
stream: true,
|
||||
model,
|
||||
},
|
||||
new AbortController().signal,
|
||||
)) {
|
||||
response += chunk.choices[0].delta.content ?? "";
|
||||
}
|
||||
|
||||
expect(response.length).toBeGreaterThan(0);
|
||||
});
|
||||
expect(response.length).toBeGreaterThan(0);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function testCompletion(api: BaseLlmApi, model: string) {
|
||||
|
|
|
|||
|
|
@ -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