fix(core): update
This commit is contained in:
@@ -4,10 +4,11 @@ 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) {
|
||||
constructor(openaiToken: string, expose) {
|
||||
super();
|
||||
this.openAiToken = openaiToken; // Ensure the token is stored
|
||||
}
|
||||
@@ -21,49 +22,31 @@ export class OpenAiProvider extends MultiModalModel {
|
||||
|
||||
async stop() {}
|
||||
|
||||
chatStream(input: ReadableStream<string>): ReadableStream<string> {
|
||||
const decoder = new TextDecoder();
|
||||
let messageHistory: { role: 'assistant' | 'user'; content: string }[] = [];
|
||||
public async chatStream(input: ReadableStream<string>): Promise<ReadableStream<string>> {
|
||||
// TODO: implement for OpenAI
|
||||
|
||||
return new ReadableStream({
|
||||
async start(controller) {
|
||||
const reader = input.getReader();
|
||||
try {
|
||||
let done, value;
|
||||
while ((({ done, value } = await reader.read()), !done)) {
|
||||
const userMessage = decoder.decode(value, { stream: true });
|
||||
messageHistory.push({ role: 'user', content: userMessage });
|
||||
|
||||
const aiResponse = await this.chat('', userMessage, messageHistory);
|
||||
messageHistory.push({ role: 'assistant', content: aiResponse.message });
|
||||
|
||||
// Directly enqueue the string response instead of encoding it first
|
||||
controller.enqueue(aiResponse.message);
|
||||
}
|
||||
controller.close();
|
||||
} catch (err) {
|
||||
controller.error(err);
|
||||
}
|
||||
},
|
||||
});
|
||||
const returnStream = new ReadableStream();
|
||||
return returnStream;
|
||||
}
|
||||
|
||||
// Implementing the synchronous chat interaction
|
||||
public async chat(
|
||||
systemMessage: string,
|
||||
userMessage: string,
|
||||
messageHistory: {
|
||||
role: 'assistant' | 'user';
|
||||
content: string;
|
||||
}[]
|
||||
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: systemMessage },
|
||||
...messageHistory,
|
||||
{ role: 'user', content: userMessage },
|
||||
{ role: 'system', content: optionsArg.systemMessage },
|
||||
...optionsArg.messageHistory,
|
||||
{ role: 'user', content: optionsArg.userMessage },
|
||||
],
|
||||
});
|
||||
return {
|
||||
@@ -71,19 +54,49 @@ export class OpenAiProvider extends MultiModalModel {
|
||||
};
|
||||
}
|
||||
|
||||
public async audio(messageArg: string) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
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: messageArg,
|
||||
input: optionsArg.message,
|
||||
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();
|
||||
});
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user