@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
35 lines (27 loc) • 1.25 kB
text/typescript
import OpenAI, { ClientOptions } from 'openai';
import urlJoin from 'url-join';
import { getLLMConfig } from '@/config/llm';
import { ChatErrorType } from '@/types/fetch';
// create Azure OpenAI Instance
export const createAzureOpenai = (params: {
apiVersion?: string | null;
endpoint?: string | null;
model: string;
userApiKey?: string | null;
}) => {
const { AZURE_API_VERSION, AZURE_API_KEY } = getLLMConfig();
const OPENAI_PROXY_URL = process.env.OPENAI_PROXY_URL || '';
const endpoint = !params.endpoint ? OPENAI_PROXY_URL : params.endpoint;
const baseURL = urlJoin(endpoint, `/openai/deployments/${params.model.replace('.', '')}`); // refs: https://test-001.openai.azure.com/openai/deployments/gpt-35-turbo
const defaultApiVersion = AZURE_API_VERSION || '2023-08-01-preview';
const apiVersion = !params.apiVersion ? defaultApiVersion : params.apiVersion;
const apiKey = !params.userApiKey ? AZURE_API_KEY : params.userApiKey;
if (!apiKey) throw new Error('AZURE_API_KEY is empty', { cause: ChatErrorType.NoOpenAIAPIKey });
const config: ClientOptions = {
apiKey,
baseURL,
defaultHeaders: { 'api-key': apiKey },
defaultQuery: { 'api-version': apiVersion },
};
return new OpenAI(config);
};