fix(core): update

This commit is contained in:
Philipp Kunz 2024-04-04 02:47:44 +02:00
parent a636556fdb
commit 04d505d29e
13 changed files with 335 additions and 39 deletions

View File

@ -5,17 +5,26 @@
"githost": "code.foss.global", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartai", "gitrepo": "smartai",
"description": "a standardaized interface to talk to AI models", "description": "Provides a standardized interface for integrating and conversing with multiple AI models, supporting operations like chat and potentially audio responses.",
"npmPackagename": "@push.rocks/smartai", "npmPackagename": "@push.rocks/smartai",
"license": "MIT", "license": "MIT",
"projectDomain": "push.rocks" "projectDomain": "push.rocks",
"keywords": [
"AI models integration",
"OpenAI GPT",
"Anthropic AI",
"text-to-speech",
"conversation stream",
"TypeScript",
"ESM"
]
} }
}, },
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "npmAccessLevel": "public"
}, },
"tsdocs": { "tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n" "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
} }
} }

View File

@ -2,7 +2,7 @@
"name": "@push.rocks/smartai", "name": "@push.rocks/smartai",
"version": "0.0.8", "version": "0.0.8",
"private": false, "private": false,
"description": "a standardaized interface to talk to AI models", "description": "Provides a standardized interface for integrating and conversing with multiple AI models, supporting operations like chat and potentially audio responses.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
@ -22,6 +22,7 @@
"@types/node": "^20.8.7" "@types/node": "^20.8.7"
}, },
"dependencies": { "dependencies": {
"@anthropic-ai/sdk": "^0.19.1",
"@push.rocks/qenv": "^6.0.5", "@push.rocks/qenv": "^6.0.5",
"@push.rocks/smartfile": "^11.0.4", "@push.rocks/smartfile": "^11.0.4",
"@push.rocks/smartpath": "^5.0.11", "@push.rocks/smartpath": "^5.0.11",
@ -50,5 +51,14 @@
"cli.js", "cli.js",
"npmextra.json", "npmextra.json",
"readme.md" "readme.md"
],
"keywords": [
"AI models integration",
"OpenAI GPT",
"Anthropic AI",
"text-to-speech",
"conversation stream",
"TypeScript",
"ESM"
] ]
} }

19
pnpm-lock.yaml generated
View File

@ -5,6 +5,9 @@ settings:
excludeLinksFromLockfile: false excludeLinksFromLockfile: false
dependencies: dependencies:
'@anthropic-ai/sdk':
specifier: ^0.19.1
version: 0.19.1
'@push.rocks/qenv': '@push.rocks/qenv':
specifier: ^6.0.5 specifier: ^6.0.5
version: 6.0.5 version: 6.0.5
@ -43,6 +46,22 @@ devDependencies:
packages: packages:
/@anthropic-ai/sdk@0.19.1:
resolution: {integrity: sha512-u9i8yN8wAr/ujaXSRjfYXiYzhCk2mdUG6G9y5IAKEAPJHwFTrEyf76Z4V1LqqFbDBlZqm0tkoMMpU8tmp65ocA==}
dependencies:
'@types/node': 18.19.28
'@types/node-fetch': 2.6.11
abort-controller: 3.0.0
agentkeepalive: 4.5.0
digest-fetch: 1.3.0
form-data-encoder: 1.7.2
formdata-node: 4.4.1
node-fetch: 2.6.7
web-streams-polyfill: 3.3.3
transitivePeerDependencies:
- encoding
dev: false
/@api.global/typedrequest-interfaces@3.0.18: /@api.global/typedrequest-interfaces@3.0.18:
resolution: {integrity: sha512-O/AdHnk9NDcBfb/Qcpi3Bq/7I6uAJHXfIT5peeKODvLUYysmuxG/c3t9NWQvfSOXQUKnVKb0WrCGjraOL7Pyww==} resolution: {integrity: sha512-O/AdHnk9NDcBfb/Qcpi3Bq/7I6uAJHXfIT5peeKODvLUYysmuxG/c3t9NWQvfSOXQUKnVKb0WrCGjraOL7Pyww==}

143
readme.md
View File

@ -1,31 +1,124 @@
# @push.rocks/smartai # @push.rocks/smartai
a standardaized interface to talk to AI models a standardized interface to talk to AI models
## Availabililty and Links ## Install
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartai) To install `@push.rocks/smartai`, run the following command in your terminal:
* [gitlab.com (source)](https://code.foss.global/push.rocks/smartai) ```bash
* [github.com (source mirror)](https://github.com/push.rocks/smartai) npm install @push.rocks/smartai
* [docs (typedoc)](https://push.rocks.gitlab.io/smartai/) ```
## Status for master This will add the package to your project's dependencies.
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://code.foss.global/push.rocks/smartai/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://code.foss.global/push.rocks/smartai/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartai)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/push.rocks/smartai)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartai)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartai)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartai)](https://lossless.cloud)
## Usage ## Usage
Use TypeScript for best in class intellisense
For further information read the linked docs at the top of this readme.
## Legal In the following guide, you'll learn how to leverage `@push.rocks/smartai` for integrating AI models into your applications using TypeScript with ESM syntax.
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) ### Getting Started
First, you'll need to import the necessary modules from `@push.rocks/smartai`. This typically includes the main `SmartAi` class along with any specific provider classes you intend to use, such as `OpenAiProvider` or `AnthropicProvider`.
```typescript
import { SmartAi, OpenAiProvider, AnthropicProvider } from '@push.rocks/smartai';
```
### Initialization
Create an instance of `SmartAi` by providing the required options, which include authentication tokens for the AI providers you plan to use.
```typescript
const smartAi = new SmartAi({
openaiToken: 'your-openai-token-here',
anthropicToken: 'your-anthropic-token-here'
});
```
### Creating a Conversation
`@push.rocks/smartai` offers a versatile way to handle conversations with AI. To create a conversation using OpenAI, for instance:
```typescript
async function createOpenAiConversation() {
const conversation = await smartAi.createOpenApiConversation();
}
```
For Anthropic-based conversations:
```typescript
async function createAnthropicConversation() {
const conversation = await smartAi.createAnthropicConversation();
}
```
### Advanced Usage: Streaming and Chat
Advanced use cases might require direct access to the streaming APIs provided by the AI models. For instance, handling a chat stream with OpenAI can be achieved as follows:
#### Set Up the Conversation Stream
First, create a conversation and obtain the input and output streams.
```typescript
const conversation = await smartAi.createOpenApiConversation();
const inputStreamWriter = conversation.getInputStreamWriter();
const outputStream = conversation.getOutputStream();
```
#### Write to Input Stream
To send messages to the AI model, use the input stream writer.
```typescript
await inputStreamWriter.write('Hello, SmartAI!');
```
#### Processing Output Stream
Output from the AI model can be processed by reading from the output stream.
```typescript
const reader = outputStream.getReader();
reader.read().then(function processText({ done, value }) {
if (done) {
console.log("Stream complete");
return;
}
console.log("Received from AI:", value);
reader.read().then(processText);
});
```
### Handling Audio
`@push.rocks/smartai` also supports handling audio responses from AI models. To generate and retrieve audio output:
```typescript
const tts = await TTS.createWithOpenAi(smartAi);
```
This code snippet initializes text-to-speech (TTS) capabilities using the OpenAI model. Further customization and usage of audio APIs will depend on the capabilities offered by the specific AI model and provider you are working with.
### Conclusion
`@push.rocks/smartai` offers a flexible and standardized interface for interacting with AI models, streamlining the development of applications that leverage AI capabilities. Through the outlined examples, you've seen how to initialize the library, create conversations, and handle both text and audio interactions with AI models in a TypeScript environment following ESM syntax.
For a comprehensive understanding of all features and to explore more advanced use cases, refer to the official [documentation](https://code.foss.global/push.rocks/smartai#readme) and check the `npmextra.json` file's `tsdocs` section for additional insights on module usage.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

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

View File

@ -1,5 +1,12 @@
export abstract class MultiModal { export abstract class MultiModalModel {
/**
* starts the model
*/
abstract start(): Promise<void>; abstract start(): Promise<void>;
/**
* stops the model
*/
abstract stop(): Promise<void>; abstract stop(): Promise<void>;
// Defines a streaming interface for chat interactions. // Defines a streaming interface for chat interactions.

View File

@ -1,18 +1,53 @@
import type { SmartAi } from "./classes.smartai.js";
import { OpenAiProvider } from "./provider.openai.js";
type TProcessFunction = (input: string) => Promise<string>; type TProcessFunction = (input: string) => Promise<string>;
export interface ISmartAiOptions { export interface IConversationOptions {
processFunction: TProcessFunction; processFunction: TProcessFunction;
} }
export class SmartAi { /**
* a conversation
*/
export class Conversation {
// STATIC
public static async createWithOpenAi(smartaiRef: SmartAi) {
const openaiProvider = new OpenAiProvider(smartaiRef.options.openaiToken);
const conversation = new Conversation(smartaiRef, {
processFunction: async (input) => {
return '' // TODO implement proper streaming
}
});
return conversation;
}
public static async createWithAnthropic(smartaiRef: SmartAi) {
const anthropicProvider = new OpenAiProvider(smartaiRef.options.anthropicToken);
const conversation = new Conversation(smartaiRef, {
processFunction: async (input) => {
return '' // TODO implement proper streaming
}
});
return conversation;
}
// INSTANCE
smartaiRef: SmartAi
private systemMessage: string;
private processFunction: TProcessFunction; private processFunction: TProcessFunction;
private inputStreamWriter: WritableStreamDefaultWriter<string> | null = null; private inputStreamWriter: WritableStreamDefaultWriter<string> | null = null;
private outputStreamController: ReadableStreamDefaultController<string> | null = null; private outputStreamController: ReadableStreamDefaultController<string> | null = null;
constructor(options: ISmartAiOptions) { constructor(smartairefArg: SmartAi, options: IConversationOptions) {
this.processFunction = options.processFunction; this.processFunction = options.processFunction;
} }
setSystemMessage(systemMessage: string) {
this.systemMessage = systemMessage;
}
private setupOutputStream(): ReadableStream<string> { private setupOutputStream(): ReadableStream<string> {
return new ReadableStream<string>({ return new ReadableStream<string>({
start: (controller) => { start: (controller) => {

30
ts/classes.smartai.ts Normal file
View File

@ -0,0 +1,30 @@
import { Conversation } from './classes.conversation.js';
import * as plugins from './plugins.js';
export interface ISmartAiOptions {
openaiToken: string;
anthropicToken: string;
}
export class SmartAi {
public options: ISmartAiOptions;
constructor(optionsArg: ISmartAiOptions) {
this.options = optionsArg;
}
/**
* creates an OpenAI conversation
*/
public async createOpenApiConversation() {
const conversation = await Conversation.createWithOpenAi(this);
}
/**
* creates an OpenAI conversation
*/
public async createAnthropicConversation() {
const conversation = await Conversation.createWithAnthropic(this);
}
}

15
ts/classes.tts.ts Normal file
View File

@ -0,0 +1,15 @@
import type { SmartAi } from './classes.smartai.js';
import * as plugins from './plugins.js';
export class TTS {
public static async createWithOpenAi(smartaiRef: SmartAi): Promise<TTS> {
return new TTS(smartaiRef);
}
// INSTANCE
smartaiRef: SmartAi;
constructor(smartairefArg: SmartAi) {
this.smartaiRef = smartairefArg;
}
}

View File

@ -1,3 +1,3 @@
export * from './smartai.classes.smartai.js'; export * from './classes.smartai.js';
export * from './abstract.classes.multimodal.js'; export * from './abstract.classes.multimodal.js';
export * from './provider.openai.js'; export * from './provider.openai.js';

View File

@ -19,8 +19,10 @@ export {
} }
// third party // third party
import * as anthropic from '@anthropic-ai/sdk';
import * as openai from 'openai'; import * as openai from 'openai';
export { export {
anthropic,
openai, openai,
} }

75
ts/provider.anthropic.ts Normal file
View File

@ -0,0 +1,75 @@
import * as plugins from './plugins.js';
import * as paths from './paths.js';
import { MultiModalModel } from './abstract.classes.multimodal.js';
export class AnthropicProvider extends MultiModalModel {
private anthropicToken: string;
public anthropicApiClient: plugins.anthropic.default;
constructor(anthropicToken: string) {
super();
this.anthropicToken = anthropicToken; // Ensure the token is stored
}
async start() {
this.anthropicApiClient = new plugins.anthropic.default({
apiKey: this.anthropicToken,
});
}
async stop() {}
chatStream(input: ReadableStream<string>): ReadableStream<string> {
const decoder = new TextDecoder();
let messageHistory: { role: 'assistant' | 'user'; content: string }[] = [];
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);
}
},
});
}
// Implementing the synchronous chat interaction
public async chat(
systemMessage: string,
userMessage: string,
messageHistory: {
role: 'assistant' | 'user';
content: string;
}[]
) {
const result = await this.anthropicApiClient.messages.create({
model: 'claude-3-opus-20240229',
system: systemMessage,
messages: [
...messageHistory,
{ role: 'user', content: userMessage },
],
max_tokens: 4000,
});
return {
message: result.content,
};
}
public async audio(messageArg: string) {
// Anthropic does not provide an audio API, so this method is not implemented.
throw new Error('Audio generation is not supported by Anthropic.');
}
}

View File

@ -1,9 +1,9 @@
import * as plugins from './plugins.js'; import * as plugins from './plugins.js';
import * as paths from './paths.js'; import * as paths from './paths.js';
import { MultiModal } from './abstract.classes.multimodal.js'; import { MultiModalModel } from './abstract.classes.multimodal.js';
export class OpenAiProvider extends MultiModal { export class OpenAiProvider extends MultiModalModel {
private openAiToken: string; private openAiToken: string;
public openAiApiClient: plugins.openai.default; public openAiApiClient: plugins.openai.default;
@ -59,6 +59,7 @@ export class OpenAiProvider extends MultiModal {
) { ) {
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: systemMessage },
...messageHistory, ...messageHistory,