Add config for baseUrl (#6645)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
Kristina 2024-09-19 21:31:28 +04:00 committed by GitHub
parent 8d9cebdd67
commit 42c602ee67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 8 deletions

View File

@ -30,6 +30,7 @@ interface Config {
Password: string
OpenAIKey: string
OpenAIModel: OpenAI.ChatModel
OpenAIBaseUrl: string
OpenAITranslateModel: OpenAI.ChatModel
MaxContentTokens: number
MaxHistoryRecords: number
@ -55,6 +56,7 @@ const config: Config = (() => {
OpenAIKey: process.env.OPENAI_API_KEY ?? '',
OpenAIModel: (process.env.OPENAI_MODEL ?? 'gpt-4o-mini') as OpenAI.ChatModel,
OpenAITranslateModel: (process.env.OPENAI_TRANSLATE_MODEL ?? 'gpt-4o-mini') as OpenAI.ChatModel,
OpenAIBaseUrl: process.env.OPENAI_BASE_URL ?? '',
MaxContentTokens: parseNumber(process.env.MAX_CONTENT_TOKENS) ?? 128 * 100,
MaxHistoryRecords: parseNumber(process.env.MAX_HISTORY_RECORDS) ?? 500,
Port: parseNumber(process.env.PORT) ?? 4010

View File

@ -50,7 +50,13 @@ export class AIBotController {
readonly storage: DbStorage,
private readonly ctx: MeasureContext
) {
this.aiClient = config.OpenAIKey === '' ? undefined : new OpenAI({ apiKey: config.OpenAIKey })
this.aiClient =
config.OpenAIKey !== ''
? new OpenAI({
apiKey: config.OpenAIKey,
baseURL: config.OpenAIBaseUrl === '' ? undefined : config.OpenAIBaseUrl
})
: undefined
this.intervalId = setInterval(() => {
void this.updateWorkspaceClients()

View File

@ -85,15 +85,23 @@ export async function createChatCompletion (
client: OpenAI,
message: OpenAI.ChatCompletionMessageParam,
user?: string,
history: OpenAI.ChatCompletionMessageParam[] = []
history: OpenAI.ChatCompletionMessageParam[] = [],
skipCache = true
): Promise<OpenAI.ChatCompletion | undefined> {
const opt: OpenAI.RequestOptions = {}
if (skipCache) {
opt.headers = { 'cf-skip-cache': 'true' }
}
try {
return await client.chat.completions.create({
messages: [...history, message],
model: config.OpenAIModel,
user,
stream: false
})
return await client.chat.completions.create(
{
messages: [...history, message],
model: config.OpenAIModel,
user,
stream: false
},
opt
)
} catch (e) {
console.error(e)
}