fix(core): update
This commit is contained in:
parent
63d3b7c9bb
commit
92c382c16e
18
package.json
18
package.json
@ -17,17 +17,19 @@
|
|||||||
"@git.zone/tsbuild": "^2.1.25",
|
"@git.zone/tsbuild": "^2.1.25",
|
||||||
"@git.zone/tsbundle": "^2.0.5",
|
"@git.zone/tsbundle": "^2.0.5",
|
||||||
"@git.zone/tsrun": "^1.2.46",
|
"@git.zone/tsrun": "^1.2.46",
|
||||||
"@git.zone/tstest": "^1.0.44",
|
"@git.zone/tstest": "^1.0.90",
|
||||||
"@push.rocks/tapbundle": "^5.0.15",
|
"@push.rocks/qenv": "^6.0.5",
|
||||||
"@types/node": "^20.8.7"
|
"@push.rocks/tapbundle": "^5.0.23",
|
||||||
|
"@types/node": "^20.12.7"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@anthropic-ai/sdk": "^0.19.1",
|
"@anthropic-ai/sdk": "^0.20.7",
|
||||||
"@push.rocks/qenv": "^6.0.5",
|
"@push.rocks/smartexpose": "^1.0.5",
|
||||||
"@push.rocks/smartfile": "^11.0.4",
|
"@push.rocks/smartfile": "^11.0.14",
|
||||||
"@push.rocks/smartpath": "^5.0.11",
|
"@push.rocks/smartpath": "^5.0.18",
|
||||||
"@push.rocks/smartpromise": "^4.0.3",
|
"@push.rocks/smartpromise": "^4.0.3",
|
||||||
"openai": "^4.31.0"
|
"@push.rocks/webstream": "^1.0.8",
|
||||||
|
"openai": "^4.38.3"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
678
pnpm-lock.yaml
generated
678
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
4
qenv.yml
4
qenv.yml
@ -1,2 +1,4 @@
|
|||||||
required:
|
required:
|
||||||
- OPENAI_TOKEN
|
- OPENAI_TOKEN
|
||||||
|
- ANTHROPIC_TOKEN
|
||||||
|
- PERPLEXITY_TOKEN
|
15
test/test.ts
15
test/test.ts
@ -1,8 +1,17 @@
|
|||||||
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
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 () => {
|
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||||
console.log(smartai)
|
|
||||||
|
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()
|
tap.start()
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartai',
|
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.'
|
description: 'Provides a standardized interface for integrating and conversing with multiple AI models, supporting operations like chat and potentially audio responses.'
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,22 @@ export abstract class MultiModalModel {
|
|||||||
* stops the model
|
* stops the model
|
||||||
*/
|
*/
|
||||||
abstract stop(): Promise<void>;
|
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.
|
* Defines a streaming interface for chat interactions.
|
||||||
abstract chatStream(input: ReadableStream<string>): ReadableStream<string>;
|
* The implementation will vary based on the specific AI model.
|
||||||
|
* @param input
|
||||||
|
*/
|
||||||
|
public abstract chatStream(input: ReadableStream<string>): Promise<ReadableStream<string>>;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,35 @@
|
|||||||
import { Conversation } from './classes.conversation.js';
|
import { Conversation } from './classes.conversation.js';
|
||||||
import * as plugins from './plugins.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 {
|
export interface ISmartAiOptions {
|
||||||
openaiToken: string;
|
openaiToken?: string;
|
||||||
anthropicToken: string;
|
anthropicToken?: string;
|
||||||
|
perplexityToken?: string;
|
||||||
|
exposeCredentials?: plugins.smartexpose.ISmartExposeOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SmartAi {
|
export class SmartAi {
|
||||||
public options: ISmartAiOptions;
|
public options: ISmartAiOptions;
|
||||||
|
|
||||||
|
public openaiProvider: OpenAiProvider;
|
||||||
|
public anthropicProvider: AnthropicProvider;
|
||||||
|
public perplexityProvider: PerplexityProvider;
|
||||||
|
public ollamaProvider: OllamaProvider;
|
||||||
|
|
||||||
constructor(optionsArg: ISmartAiOptions) {
|
constructor(optionsArg: ISmartAiOptions) {
|
||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async start() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async stop() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates an OpenAI conversation
|
* creates an OpenAI conversation
|
||||||
|
0
ts/interfaces.ts
Normal file
0
ts/interfaces.ts
Normal file
@ -7,15 +7,19 @@ export {
|
|||||||
|
|
||||||
// @push.rocks scope
|
// @push.rocks scope
|
||||||
import * as qenv from '@push.rocks/qenv';
|
import * as qenv from '@push.rocks/qenv';
|
||||||
|
import * as smartexpose from '@push.rocks/smartexpose';
|
||||||
import * as smartpath from '@push.rocks/smartpath';
|
import * as smartpath from '@push.rocks/smartpath';
|
||||||
import * as smartpromise from '@push.rocks/smartpromise';
|
import * as smartpromise from '@push.rocks/smartpromise';
|
||||||
import * as smartfile from '@push.rocks/smartfile';
|
import * as smartfile from '@push.rocks/smartfile';
|
||||||
|
import * as webstream from '@push.rocks/webstream';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
qenv,
|
qenv,
|
||||||
|
smartexpose,
|
||||||
smartpath,
|
smartpath,
|
||||||
smartpromise,
|
smartpromise,
|
||||||
smartfile,
|
smartfile,
|
||||||
|
webstream,
|
||||||
}
|
}
|
||||||
|
|
||||||
// third party
|
// third party
|
||||||
|
3
ts/provider.ollama.ts
Normal file
3
ts/provider.ollama.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
|
export class OllamaProvider {}
|
@ -4,10 +4,11 @@ import * as paths from './paths.js';
|
|||||||
import { MultiModalModel } from './abstract.classes.multimodal.js';
|
import { MultiModalModel } from './abstract.classes.multimodal.js';
|
||||||
|
|
||||||
export class OpenAiProvider extends MultiModalModel {
|
export class OpenAiProvider extends MultiModalModel {
|
||||||
|
public smartexposeInstance: plugins.smartexpose.SmartExpose;
|
||||||
private openAiToken: string;
|
private openAiToken: string;
|
||||||
public openAiApiClient: plugins.openai.default;
|
public openAiApiClient: plugins.openai.default;
|
||||||
|
|
||||||
constructor(openaiToken: string) {
|
constructor(openaiToken: string, expose) {
|
||||||
super();
|
super();
|
||||||
this.openAiToken = openaiToken; // Ensure the token is stored
|
this.openAiToken = openaiToken; // Ensure the token is stored
|
||||||
}
|
}
|
||||||
@ -21,49 +22,31 @@ export class OpenAiProvider extends MultiModalModel {
|
|||||||
|
|
||||||
async stop() {}
|
async stop() {}
|
||||||
|
|
||||||
chatStream(input: ReadableStream<string>): ReadableStream<string> {
|
public async chatStream(input: ReadableStream<string>): Promise<ReadableStream<string>> {
|
||||||
const decoder = new TextDecoder();
|
// TODO: implement for OpenAI
|
||||||
let messageHistory: { role: 'assistant' | 'user'; content: string }[] = [];
|
|
||||||
|
|
||||||
return new ReadableStream({
|
const returnStream = new ReadableStream();
|
||||||
async start(controller) {
|
return returnStream;
|
||||||
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);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementing the synchronous chat interaction
|
// Implementing the synchronous chat interaction
|
||||||
public async chat(
|
public async chat(
|
||||||
systemMessage: string,
|
optionsArg: {
|
||||||
userMessage: string,
|
systemMessage: string,
|
||||||
messageHistory: {
|
userMessage: string,
|
||||||
role: 'assistant' | 'user';
|
messageHistory: {
|
||||||
content: string;
|
role: 'assistant' | 'user';
|
||||||
}[]
|
content: string;
|
||||||
|
}[]
|
||||||
|
}
|
||||||
) {
|
) {
|
||||||
const result = await this.openAiApiClient.chat.completions.create({
|
const result = await this.openAiApiClient.chat.completions.create({
|
||||||
model: 'gpt-4-turbo-preview',
|
model: 'gpt-4-turbo-preview',
|
||||||
|
|
||||||
messages: [
|
messages: [
|
||||||
{ role: 'system', content: systemMessage },
|
{ role: 'system', content: optionsArg.systemMessage },
|
||||||
...messageHistory,
|
...optionsArg.messageHistory,
|
||||||
{ role: 'user', content: userMessage },
|
{ role: 'user', content: optionsArg.userMessage },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
@ -71,19 +54,49 @@ export class OpenAiProvider extends MultiModalModel {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async audio(messageArg: string) {
|
public async audio(optionsArg: { message: string }): Promise<NodeJS.ReadableStream> {
|
||||||
const done = plugins.smartpromise.defer();
|
const done = plugins.smartpromise.defer<NodeJS.ReadableStream>();
|
||||||
const result = await this.openAiApiClient.audio.speech.create({
|
const result = await this.openAiApiClient.audio.speech.create({
|
||||||
model: 'tts-1-hd',
|
model: 'tts-1-hd',
|
||||||
input: messageArg,
|
input: optionsArg.message,
|
||||||
voice: 'nova',
|
voice: 'nova',
|
||||||
response_format: 'mp3',
|
response_format: 'mp3',
|
||||||
speed: 1,
|
speed: 1,
|
||||||
});
|
});
|
||||||
const stream = result.body.pipe(plugins.smartfile.fsStream.createWriteStream(plugins.path.join(paths.nogitDir, 'output.mp3')));
|
const stream = result.body;
|
||||||
stream.on('finish', () => {
|
done.resolve(stream);
|
||||||
done.resolve();
|
|
||||||
});
|
|
||||||
return done.promise;
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
3
ts/provider.perplexity.ts
Normal file
3
ts/provider.perplexity.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
|
export class PerplexityProvider {}
|
Loading…
Reference in New Issue
Block a user