fix(core): update
This commit is contained in:
parent
a636556fdb
commit
04d505d29e
@ -5,17 +5,26 @@
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"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",
|
||||
"license": "MIT",
|
||||
"projectDomain": "push.rocks"
|
||||
"projectDomain": "push.rocks",
|
||||
"keywords": [
|
||||
"AI models integration",
|
||||
"OpenAI GPT",
|
||||
"Anthropic AI",
|
||||
"text-to-speech",
|
||||
"conversation stream",
|
||||
"TypeScript",
|
||||
"ESM"
|
||||
]
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"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"
|
||||
}
|
||||
}
|
14
package.json
14
package.json
@ -2,7 +2,7 @@
|
||||
"name": "@push.rocks/smartai",
|
||||
"version": "0.0.8",
|
||||
"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",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
@ -22,6 +22,7 @@
|
||||
"@types/node": "^20.8.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",
|
||||
@ -50,5 +51,14 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"keywords": [
|
||||
"AI models integration",
|
||||
"OpenAI GPT",
|
||||
"Anthropic AI",
|
||||
"text-to-speech",
|
||||
"conversation stream",
|
||||
"TypeScript",
|
||||
"ESM"
|
||||
]
|
||||
}
|
||||
}
|
19
pnpm-lock.yaml
generated
19
pnpm-lock.yaml
generated
@ -5,6 +5,9 @@ settings:
|
||||
excludeLinksFromLockfile: false
|
||||
|
||||
dependencies:
|
||||
'@anthropic-ai/sdk':
|
||||
specifier: ^0.19.1
|
||||
version: 0.19.1
|
||||
'@push.rocks/qenv':
|
||||
specifier: ^6.0.5
|
||||
version: 6.0.5
|
||||
@ -43,6 +46,22 @@ devDependencies:
|
||||
|
||||
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:
|
||||
resolution: {integrity: sha512-O/AdHnk9NDcBfb/Qcpi3Bq/7I6uAJHXfIT5peeKODvLUYysmuxG/c3t9NWQvfSOXQUKnVKb0WrCGjraOL7Pyww==}
|
||||
|
||||
|
143
readme.md
143
readme.md
@ -1,31 +1,124 @@
|
||||
# @push.rocks/smartai
|
||||
a standardaized interface to talk to AI models
|
||||
a standardized interface to talk to AI models
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartai)
|
||||
* [gitlab.com (source)](https://code.foss.global/push.rocks/smartai)
|
||||
* [github.com (source mirror)](https://github.com/push.rocks/smartai)
|
||||
* [docs (typedoc)](https://push.rocks.gitlab.io/smartai/)
|
||||
## Install
|
||||
To install `@push.rocks/smartai`, run the following command in your terminal:
|
||||
```bash
|
||||
npm install @push.rocks/smartai
|
||||
```
|
||||
|
||||
## Status for master
|
||||
|
||||
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)
|
||||
This will add the package to your project's dependencies.
|
||||
|
||||
## Usage
|
||||
Use TypeScript for best in class intellisense
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
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.
|
||||
|
||||
### 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.
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartai',
|
||||
version: '0.0.8',
|
||||
description: 'a standardaized interface to talk to AI models'
|
||||
version: '0.0.9',
|
||||
description: 'Provides a standardized interface for integrating and conversing with multiple AI models, supporting operations like chat and potentially audio responses.'
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
export abstract class MultiModal {
|
||||
export abstract class MultiModalModel {
|
||||
/**
|
||||
* starts the model
|
||||
*/
|
||||
abstract start(): Promise<void>;
|
||||
|
||||
/**
|
||||
* stops the model
|
||||
*/
|
||||
abstract stop(): Promise<void>;
|
||||
|
||||
// Defines a streaming interface for chat interactions.
|
||||
|
@ -1,18 +1,53 @@
|
||||
import type { SmartAi } from "./classes.smartai.js";
|
||||
import { OpenAiProvider } from "./provider.openai.js";
|
||||
|
||||
type TProcessFunction = (input: string) => Promise<string>;
|
||||
|
||||
export interface ISmartAiOptions {
|
||||
export interface IConversationOptions {
|
||||
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 inputStreamWriter: WritableStreamDefaultWriter<string> | null = null;
|
||||
private outputStreamController: ReadableStreamDefaultController<string> | null = null;
|
||||
|
||||
constructor(options: ISmartAiOptions) {
|
||||
constructor(smartairefArg: SmartAi, options: IConversationOptions) {
|
||||
this.processFunction = options.processFunction;
|
||||
}
|
||||
|
||||
setSystemMessage(systemMessage: string) {
|
||||
this.systemMessage = systemMessage;
|
||||
}
|
||||
|
||||
private setupOutputStream(): ReadableStream<string> {
|
||||
return new ReadableStream<string>({
|
||||
start: (controller) => {
|
30
ts/classes.smartai.ts
Normal file
30
ts/classes.smartai.ts
Normal 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
15
ts/classes.tts.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
export * from './smartai.classes.smartai.js';
|
||||
export * from './classes.smartai.js';
|
||||
export * from './abstract.classes.multimodal.js';
|
||||
export * from './provider.openai.js';
|
||||
|
@ -19,8 +19,10 @@ export {
|
||||
}
|
||||
|
||||
// third party
|
||||
import * as anthropic from '@anthropic-ai/sdk';
|
||||
import * as openai from 'openai';
|
||||
|
||||
export {
|
||||
anthropic,
|
||||
openai,
|
||||
}
|
||||
|
75
ts/provider.anthropic.ts
Normal file
75
ts/provider.anthropic.ts
Normal 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.');
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
import * as plugins from './plugins.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;
|
||||
public openAiApiClient: plugins.openai.default;
|
||||
|
||||
@ -59,6 +59,7 @@ export class OpenAiProvider extends MultiModal {
|
||||
) {
|
||||
const result = await this.openAiApiClient.chat.completions.create({
|
||||
model: 'gpt-4-turbo-preview',
|
||||
|
||||
messages: [
|
||||
{ role: 'system', content: systemMessage },
|
||||
...messageHistory,
|
||||
|
Loading…
Reference in New Issue
Block a user