Skip to content

Commit

Permalink
chore: azure使用新配置,并提供旧配置兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Oct 23, 2024
1 parent 7002ccd commit 146d416
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 45 deletions.
47 changes: 13 additions & 34 deletions src/agent/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,25 @@ import type { ChatAgent, ChatStreamTextHandler, ImageAgent, LLMChatParams, Respo
import { createAzure } from '@ai-sdk/azure';
import { requestChatCompletionsV2 } from './request';

class AzureBase {
export class AzureChatAI implements ChatAgent {
readonly name = 'azure';
readonly modelFromURI = (uri: string | null): string => {
if (!uri) {
return '';
}
try {
const url = new URL(uri);
return url.pathname.split('/')[3];
} catch {
return uri;
}
};
}

export class AzureChatAI extends AzureBase implements ChatAgent {
readonly modelKey = 'AZURE_COMPLETIONS_API';
readonly modelKey = 'AZURE_CHAT_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
return !!(context.AZURE_API_KEY && context.AZURE_COMPLETIONS_API);
return !!(context.AZURE_API_KEY && context.AZURE_RESOURCE_NAME && context.AZURE_CHAT_MODEL);
};

readonly model = (ctx: AgentUserConfig) => {
return this.modelFromURI(ctx.AZURE_COMPLETIONS_API);
return ctx.AZURE_CHAT_MODEL || '';
};

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<ResponseMessage[]> => {
const url = context.AZURE_COMPLETIONS_API || '';
// RESOURCE_NAME.openai.azure.com/openai/deployments/MODEL_NAME/chat/completions
// get RESOURCE_NAME MODEL_NAME from the url with regex
const regex = /(?<resource>[^/]+)\/openai\/deployments\/(?<model>[^/]+)\/chat\/completions/;
const match = url.match(regex);
if (!match) {
throw new Error('Invalid Azure Chat API URL');
}
const [, resource, model] = match;

const provider = createAzure({
resourceName: resource,
resourceName: context.AZURE_RESOURCE_NAME || undefined,
apiKey: context.AZURE_API_KEY || undefined,
});
const languageModelV1 = provider.languageModel(model, undefined);
const languageModelV1 = provider.languageModel(context.AZURE_CHAT_MODEL || '', undefined);
return requestChatCompletionsV2({
model: languageModelV1,
prompt: params.prompt,
Expand All @@ -53,19 +30,21 @@ export class AzureChatAI extends AzureBase implements ChatAgent {
};
}

export class AzureImageAI extends AzureBase implements ImageAgent {
readonly modelKey = 'AZURE_DALLE_API';
export class AzureImageAI implements ImageAgent {
readonly name = 'azure';

readonly modelKey = 'AZURE_IMAGE_MODEL';

readonly enable = (context: AgentUserConfig): boolean => {
return !!(context.AZURE_API_KEY && context.AZURE_DALLE_API);
return !!(context.AZURE_API_KEY && context.AZURE_RESOURCE_NAME && context.AZURE_IMAGE_MODEL);
};

readonly model = (ctx: AgentUserConfig) => {
return this.modelFromURI(ctx.AZURE_DALLE_API);
return ctx.AZURE_IMAGE_MODEL || '';
};

readonly request = async (prompt: string, context: AgentUserConfig): Promise<string> => {
const url = context.AZURE_DALLE_API;
const url = `https://${context.AZURE_RESOURCE_NAME}.openai.azure.com/openai/deployments/${context.AZURE_IMAGE_MODEL}/chat/completions?${context.AZURE_API_VERSION}`;
if (!url || !context.AZURE_API_KEY) {
throw new Error('Azure DALL-E API is not set');
}
Expand Down
18 changes: 10 additions & 8 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ export class OpenAIConfig {
// -- DALLE 配置 --
export class DalleAIConfig {
// DALL-E的模型名称
DALL_E_MODEL = 'dall-e-2';
DALL_E_MODEL = 'dall-e-3';
// DALL-E图片尺寸
DALL_E_IMAGE_SIZE = '512x512';
DALL_E_IMAGE_SIZE = '1024x1024';
// DALL-E图片质量
DALL_E_IMAGE_QUALITY = 'standard';
// DALL-E图片风格
Expand All @@ -124,12 +124,14 @@ export class DalleAIConfig {
export class AzureConfig {
// Azure API Key
AZURE_API_KEY: string | null = null;
// Azure Completions API
// https://RESOURCE_NAME.openai.azure.com/openai/deployments/MODEL_NAME/chat/completions?api-version=VERSION_NAME
AZURE_COMPLETIONS_API: string | null = null;
// Azure DallE API
// https://RESOURCE_NAME.openai.azure.com/openai/deployments/MODEL_NAME/images/generations?api-version=VERSION_NAME
AZURE_DALLE_API: string | null = null;
// Azure Resource Name
AZURE_RESOURCE_NAME: string | null = null;
// Azure Chat Model
AZURE_CHAT_MODEL: string | null = null;
// Azure Image Model
AZURE_IMAGE_MODEL: string | null = null;
// Azure API version
AZURE_API_VERSION = '2024-06-01';
}

// -- Workers 配置 --
Expand Down
21 changes: 18 additions & 3 deletions src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,24 @@ class Environment extends EnvironmentConfig {
this.USER_CONFIG.SYSTEM_INIT_MESSAGE = this.I18N?.env?.system_init_message || 'You are a helpful assistant';
}

// TODO: 兼容旧版 GOOGLE_COMPLETIONS_API
// // Google Gemini API: Cloudflare AI gateway: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai-studio/v1/models
// GOOGLE_COMPLETIONS_API = 'https://generativelanguage.googleapis.com/v1beta/models/';
// 兼容旧版 GOOGLE_COMPLETIONS_API
if (source.GOOGLE_COMPLETIONS_API && !this.USER_CONFIG.GOOGLE_BASE_URL) {
this.USER_CONFIG.GOOGLE_BASE_URL = source.GOOGLE_COMPLETIONS_API.replace(/\/models\/?$/, '');
}
// 兼容旧版 AZURE_COMPLETIONS_API
if (source.AZURE_COMPLETIONS_API && !this.USER_CONFIG.AZURE_CHAT_MODEL) {
const url = new URL(source.AZURE_COMPLETIONS_API);
this.USER_CONFIG.AZURE_RESOURCE_NAME = url.hostname.split('.').at(0) || null;
this.USER_CONFIG.AZURE_CHAT_MODEL = url.pathname.split('/').at(3) || null;
this.USER_CONFIG.AZURE_API_VERSION = url.searchParams.get('api-version') || '2024-06-01';
}
// 兼容旧版 AZURE_DALLE_API
if (source.AZURE_DALLE_API && !this.USER_CONFIG.AZURE_IMAGE_MODEL) {
const url = new URL(source.AZURE_DALLE_API);
this.USER_CONFIG.AZURE_RESOURCE_NAME = url.hostname.split('.').at(0) || null;
this.USER_CONFIG.AZURE_IMAGE_MODEL = url.pathname.split('/').at(3) || null;
this.USER_CONFIG.AZURE_API_VERSION = url.searchParams.get('api-version') || '2024-06-01';
}
}
}

Expand Down

0 comments on commit 146d416

Please sign in to comment.