fix(core): update

This commit is contained in:
Philipp Kunz 2024-04-25 10:49:07 +02:00
parent 63d3b7c9bb
commit 92c382c16e
12 changed files with 606 additions and 259 deletions

View File

@ -17,17 +17,19 @@
"@git.zone/tsbuild": "^2.1.25",
"@git.zone/tsbundle": "^2.0.5",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.44",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.8.7"
"@git.zone/tstest": "^1.0.90",
"@push.rocks/qenv": "^6.0.5",
"@push.rocks/tapbundle": "^5.0.23",
"@types/node": "^20.12.7"
},
"dependencies": {
"@anthropic-ai/sdk": "^0.19.1",
"@push.rocks/qenv": "^6.0.5",
"@push.rocks/smartfile": "^11.0.4",
"@push.rocks/smartpath": "^5.0.11",
"@anthropic-ai/sdk": "^0.20.7",
"@push.rocks/smartexpose": "^1.0.5",
"@push.rocks/smartfile": "^11.0.14",
"@push.rocks/smartpath": "^5.0.18",
"@push.rocks/smartpromise": "^4.0.3",
"openai": "^4.31.0"
"@push.rocks/webstream": "^1.0.8",
"openai": "^4.38.3"
},
"repository": {
"type": "git",

File diff suppressed because it is too large Load Diff

View File

@ -1,2 +1,4 @@
required:
- OPENAI_TOKEN
- OPENAI_TOKEN
- ANTHROPIC_TOKEN
- PERPLEXITY_TOKEN

View File

@ -1,8 +1,17 @@
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import * as smartai from '../ts/index.js'
import * as qenv from '@push.rocks/qenv';
tap.test('first test', async () => {
console.log(smartai)
const testQenv = new qenv.Qenv('./', './.nogit/');
import * as smartai from '../ts/index.js';
let testSmartai: smartai.SmartAi;
tap.test('should create a smartai instance', async () => {
testSmartai = new smartai.SmartAi({
openaiToken: await testQenv.getEnvVarOnDemand('OPENAI_TOKEN'),
});
})
tap.start()

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartai',
version: '0.0.9',
version: '0.0.10',
description: 'Provides a standardized interface for integrating and conversing with multiple AI models, supporting operations like chat and potentially audio responses.'
}

View File

@ -8,8 +8,22 @@ export abstract class MultiModalModel {
* stops the model
*/
abstract stop(): Promise<void>;
public abstract chat(optionsArg: {
systemMessage: string,
userMessage: string,
messageHistory: {
role: 'assistant' | 'user';
content: string;
}[]
}): Promise<{}>
// Defines a streaming interface for chat interactions.
// The implementation will vary based on the specific AI model.
abstract chatStream(input: ReadableStream<string>): ReadableStream<string>;
/**
* Defines a streaming interface for chat interactions.
* The implementation will vary based on the specific AI model.
* @param input
*/
public abstract chatStream(input: ReadableStream<string>): Promise<ReadableStream<string>>;
}

View File

@ -1,18 +1,35 @@
import { Conversation } from './classes.conversation.js';
import * as plugins from './plugins.js';
import type { AnthropicProvider } from './provider.anthropic.js';
import type { OllamaProvider } from './provider.ollama.js';
import type { OpenAiProvider } from './provider.openai.js';
import type { PerplexityProvider } from './provider.perplexity.js';
export interface ISmartAiOptions {
openaiToken: string;
anthropicToken: string;
openaiToken?: string;
anthropicToken?: string;
perplexityToken?: string;
exposeCredentials?: plugins.smartexpose.ISmartExposeOptions;
}
export class SmartAi {
public options: ISmartAiOptions;
public openaiProvider: OpenAiProvider;
public anthropicProvider: AnthropicProvider;
public perplexityProvider: PerplexityProvider;
public ollamaProvider: OllamaProvider;
constructor(optionsArg: ISmartAiOptions) {
this.options = optionsArg;
}
public async start() {
}
public async stop() {}
/**
* creates an OpenAI conversation

0
ts/interfaces.ts Normal file
View File

View File

@ -7,15 +7,19 @@ export {
// @push.rocks scope
import * as qenv from '@push.rocks/qenv';
import * as smartexpose from '@push.rocks/smartexpose';
import * as smartpath from '@push.rocks/smartpath';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartfile from '@push.rocks/smartfile';
import * as webstream from '@push.rocks/webstream';
export {
qenv,
smartexpose,
smartpath,
smartpromise,
smartfile,
webstream,
}
// third party

3
ts/provider.ollama.ts Normal file
View File

@ -0,0 +1,3 @@
import * as plugins from './plugins.js';
export class OllamaProvider {}

View File

@ -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,
};
}
}

View File

@ -0,0 +1,3 @@
import * as plugins from './plugins.js';
export class PerplexityProvider {}