platformservice/ts/aibridge/aibridge.classes.openaibridge.ts
2024-04-01 02:58:27 +02:00

59 lines
1.6 KiB
TypeScript

import { AiBridge } from './aibridge.classes.aibridge.js';
import * as plugins from './aibridge.plugins.js';
import * as paths from './aibridge.paths.js';
export class OpenAiBridge {
public aiBridgeRef: AiBridge;
public openAiApiClient: plugins.openai.default;
constructor(aiBridgeRefArg: AiBridge) {
this.aiBridgeRef = aiBridgeRefArg;
}
public async start() {
const openAiToken = await this.aiBridgeRef.serviceQenv.getEnvVarOnDemand('OPENAI_TOKEN');
this.openAiApiClient = new plugins.openai.default({
apiKey: openAiToken,
dangerouslyAllowBrowser: true,
});
}
public async stop() {}
public async chat(
systemMessage: string,
userMessage: string,
messageHistory: {
role: 'assistant' | 'user';
content: string;
}[]
) {
const result = await this.openAiApiClient.chat.completions.create({
model: 'gpt-4-turbo-preview',
messages: [
{ role: 'system', content: systemMessage },
...messageHistory,
{ role: 'user', content: userMessage },
],
});
return {
message: result.choices[0].message,
};
}
public async audio(messageArg: string) {
const done = plugins.smartpromise.defer();
const result = await this.openAiApiClient.audio.speech.create({
model: 'tts-1-hd',
input: messageArg,
voice: 'nova',
response_format: 'mp3',
speed: 1,
});
const stream = result.body.pipe(plugins.smartfile.fsStream.createWriteStream(plugins.path.join(paths.nogitDir, 'output.mp3')));
stream.on('finish', () => {
done.resolve();
});
return done.promise;
}
}