smartai/ts/provider.openai.ts
2024-04-25 10:49:07 +02:00

103 lines
2.7 KiB
TypeScript

import * as plugins from './plugins.js';
import * as paths from './paths.js';
import { MultiModalModel } from './abstract.classes.multimodal.js';
export class OpenAiProvider extends MultiModalModel {
public smartexposeInstance: plugins.smartexpose.SmartExpose;
private openAiToken: string;
public openAiApiClient: plugins.openai.default;
constructor(openaiToken: string, expose) {
super();
this.openAiToken = openaiToken; // Ensure the token is stored
}
async start() {
this.openAiApiClient = new plugins.openai.default({
apiKey: this.openAiToken,
dangerouslyAllowBrowser: true,
});
}
async stop() {}
public async chatStream(input: ReadableStream<string>): Promise<ReadableStream<string>> {
// TODO: implement for OpenAI
const returnStream = new ReadableStream();
return returnStream;
}
// Implementing the synchronous chat interaction
public async chat(
optionsArg: {
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: optionsArg.systemMessage },
...optionsArg.messageHistory,
{ role: 'user', content: optionsArg.userMessage },
],
});
return {
message: result.choices[0].message,
};
}
public async audio(optionsArg: { message: string }): Promise<NodeJS.ReadableStream> {
const done = plugins.smartpromise.defer<NodeJS.ReadableStream>();
const result = await this.openAiApiClient.audio.speech.create({
model: 'tts-1-hd',
input: optionsArg.message,
voice: 'nova',
response_format: 'mp3',
speed: 1,
});
const stream = result.body;
done.resolve(stream);
return done.promise;
}
public async document(optionsArg: {
systemMessage: string,
userMessage: string,
documents: Uint8Array[],
messageHistory: {
role: 'assistant' | 'user';
content: any;
}[];
}) {
const result = await this.openAiApiClient.chat.completions.create({
model: 'gpt-4-vision-preview',
messages: [
{ role: 'system', content: optionsArg.systemMessage },
...optionsArg.messageHistory,
{ role: 'user', content: [
{type: 'text', text: optionsArg.userMessage},
...(() => {
const returnArray = [];
for (const document of optionsArg.documents) {
returnArray.push({type: 'image_url', image_url: })
}
return returnArray;
})()
] },
],
});
return {
message: result.choices[0].message,
};
}
}