Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@singlestore/ai

Package Overview
Dependencies
Maintainers
0
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@singlestore/ai - npm Package Compare versions

Comparing version 0.0.25 to 0.0.26

288

dist/index.d.ts

@@ -7,16 +7,2 @@ import z$1, { z } from 'zod';

/**
* Represents the function signature for a ChatCompletionTool's `call` method.
* This method is responsible for executing the tool's primary logic.
*
* @typeParam T - The name of the tool, represented as a string literal type.
* @typeParam U - A Zod object schema or undefined, representing the parameters the tool requires.
* If undefined, no parameters are expected.
* @typeParam K - The return type of the `call` function, representing the value produced by the tool.
*
* @param params - The parameters passed to the tool, inferred from the Zod schema `U` if provided.
* If `U` is undefined, this parameter is void.
*
* @returns A Promise that resolves to an object containing the tool's name, the result value, and optionally the parameters.
*/
type ChatCompletionToolCall<T extends string, U extends z.AnyZodObject | undefined, K> = (params: U extends z.AnyZodObject ? z.infer<U> : void) => Promise<{

@@ -27,14 +13,2 @@ name: T;

}>;
/**
* Configuration object for initializing a ChatCompletionTool.
*
* @typeParam T - The name of the tool, represented as a string literal type.
* @typeParam U - A Zod object schema or undefined, representing the parameters the tool requires.
* @typeParam K - The function signature for the tool's `call` method.
*
* @property {T} name - The unique name of the tool.
* @property {string} description - A description of the tool's functionality.
* @property {U} [params] - Optional Zod schema defining the parameters for the tool.
* @property {K} call - The function that implements the tool's primary logic.
*/
interface ChatCompletionToolConfig<T extends string, U extends z.AnyZodObject | undefined, K extends ChatCompletionToolCall<T, U, any>> {

@@ -46,27 +20,4 @@ name: T;

}
/**
* Represents any ChatCompletionTool with generic types for name, parameters, and call signature.
* This type is useful for handling tools in a generic context.
*
* @typeParam T - The name of the tool, represented as a string.
* @typeParam U - A Zod object schema or undefined, representing the parameters the tool requires.
* @typeParam K - The function signature for the tool's `call` method.
*/
type AnyChatCompletionTool = ChatCompletionTool<string, z.AnyZodObject | undefined, ChatCompletionToolCall<string, any | undefined, any>>;
/**
* Merges two arrays of ChatCompletionTools, preserving their types.
*
* @typeParam T - An array of ChatCompletionTools or undefined.
* @typeParam U - Another array of ChatCompletionTools or undefined.
*
* @returns An array containing all tools from both input arrays, or undefined if both inputs are undefined.
*/
type MergeChatCompletionTools<T extends AnyChatCompletionTool[] | undefined, U extends AnyChatCompletionTool[] | undefined> = T extends AnyChatCompletionTool[] ? U extends AnyChatCompletionTool[] ? [...T, ...U] : T : U extends AnyChatCompletionTool[] ? U : undefined;
/**
* Represents a tool used in chat completions, encapsulating a name, description, optional parameters, and a call function.
*
* @typeParam T - The name of the tool, represented as a string literal type.
* @typeParam U - A Zod object schema or undefined, representing the parameters the tool requires.
* @typeParam K - The function signature for the tool's `call` method.
*/
declare class ChatCompletionTool<T extends string, U extends z.AnyZodObject | undefined, K extends ChatCompletionToolCall<T, U, any>> {

@@ -77,32 +28,10 @@ name: T;

call: K;
/**
* Constructs a new ChatCompletionTool instance.
*
* @param {ChatCompletionToolConfig<T, U, K>} config - Configuration object for initializing the tool.
*/
constructor(config: ChatCompletionToolConfig<T, U, K>);
}
/**
* Represents a single chat completion with the generated content.
*/
interface ChatCompletion {
content: string;
}
/**
* Represents a stream of chat completions, which can be asynchronously generated.
* The stream yields `ChatCompletion` objects.
*/
type ChatCompletionStream = AsyncGenerator<ChatCompletion>;
/**
* Callback function type for handling individual chunks of a chat completion stream.
*
* @param chunk - A chunk of chat completion content.
*
* @returns A Promise that resolves when the chunk is processed, or void if no async operation is needed.
*/
type OnChatCompletionChunk = (chunk: ChatCompletion) => Promise<void> | void;
/**
* Represents a message in a chat completion, including the role (system, assistant, user) and content.
*/
interface ChatCompletionMessage {

@@ -112,19 +41,2 @@ role: Extract<ChatCompletionMessageParam["role"], "system" | "assistant" | "user">;

}
/**
* Parameters for creating a chat completion.
*
* @typeParam TStream - Indicates whether the completion should be streamed (boolean).
* @typeParam TChatCompletionTool - An array of tools that can be used during the chat completion.
*
* @property {string} [prompt] - The initial prompt to generate the chat completion.
* @property {string} [model] - The model to use for generating the completion.
* @property {string} [systemRole] - The role of the system in the conversation.
* @property {TStream} [stream] - Whether the completion should be streamed.
* @property {ChatCompletionMessage[]} [messages] - An array of previous messages in the conversation.
* @property {TChatCompletionTool} [tools] - An array of tools that can be used during the completion.
*
* @property {Record<string, (tool: AnyChatCompletionTool, params: any) => Promise<void>>} [toolCallHandlers] - Optional handlers for when a tool is called during the completion.
*
* @property {Record<string, (tool: AnyChatCompletionTool, result: any, params: any) => Promise<void>>} [toolCallResultHandlers] - Optional handlers for when a tool returns a result during the completion.
*/
interface CreateChatCompletionParams<TStream extends boolean | undefined, TChatCompletionTool extends AnyChatCompletionTool[] | undefined> {

@@ -144,198 +56,71 @@ prompt?: string;

}
/**
* The result of creating a chat completion.
*
* @typeParam TStream - Indicates whether the result is a stream or a single completion.
*
* @returns If T is true, returns a `ChatCompletionStream`, otherwise returns a single `ChatCompletion`.
*/
type CreateChatCompletionResult<TStream extends boolean | undefined> = TStream extends true ? ChatCompletionStream : ChatCompletion;
/**
* Abstract class representing a chat completions generator, capable of handling various tools.
*
* @typeParam TChatCompletionTool - An array of tools that can be used during the chat completion.
*/
declare abstract class ChatCompletions<TChatCompletionTool extends AnyChatCompletionTool[] | undefined> {
/**
* The tools that can be used during chat completion. Initialized to undefined.
*/
tools: TChatCompletionTool;
/**
* Initializes the tools to be used in chat completion.
*
* @param tools - An array of tools to be used in the chat completion.
*/
initTools(tools: TChatCompletionTool): void;
/**
* Retrieves the models available for generating chat completions.
*
* @returns A promise that resolves to an array of model names or an array of model names directly.
*/
abstract getModels(): Promise<string[]> | string[];
/**
* Handles a stream of chat completions, optionally processing each chunk.
*
* @param stream - The stream of chat completions.
* @param onChunk - Optional callback function to handle each chunk as it is received.
*
* @returns A promise that resolves to the final chat completion, with all chunks combined.
*/
handleStream(stream: ChatCompletionStream, onChunk?: OnChatCompletionChunk): Promise<ChatCompletion>;
/**
* Creates a chat completion based on the provided parameters.
*
* @param params - Parameters for creating the chat completion, including the prompt, model, messages, tools, and handlers.
*
* @returns A promise that resolves to either a single chat completion or a stream of chat completions, depending on the `stream` parameter.
*/
abstract create(params: CreateChatCompletionParams<any, any>): Promise<CreateChatCompletionResult<any>>;
}
/**
* Represents the model type used in OpenAI chat completions.
*/
type OpenAIChatCompletionModel = ChatCompletionCreateParamsBase["model"];
/**
* Internal interface that extends the base parameters for creating an OpenAI chat completion,
* excluding properties already defined in `CreateChatCompletionParams`.
*/
interface _OpenAICreateChatCompletionParams extends Omit<Partial<ChatCompletionCreateParamsBase>, keyof CreateChatCompletionParams<any, any>> {
}
/**
* Parameters for creating an OpenAI chat completion.
*
* @typeParam TStream - Indicates whether the completion should be streamed (boolean).
* @typeParam TChatCompletionTool - An array of tools that can be used during the chat completion.
*
* @property {OpenAIChatCompletionModel} [model] - The model to use for generating the completion.
*/
interface OpenAICreateChatCompletionParams<TStream extends boolean | undefined, TChatCompletionTool extends AnyChatCompletionTool[] | undefined> extends CreateChatCompletionParams<TStream, TChatCompletionTool>, _OpenAICreateChatCompletionParams {
model?: OpenAIChatCompletionModel;
}
/**
* Class representing chat completions using OpenAI's API, with support for tools and streaming.
*
* @typeParam TChatCompletionTool - An array of tools that can be used during the chat completion.
*/
declare class OpenAIChatCompletions<TChatCompletionTool extends AnyChatCompletionTool[] | undefined> extends ChatCompletions<TChatCompletionTool> {
private _openai;
constructor(_openai: OpenAI);
/**
* Retrieves the list of models available for OpenAI chat completions.
*
* @returns An array of model names.
*/
getModels(): OpenAIChatCompletionModel[];
/**
* Creates a chat completion using OpenAI's API, with optional support for streaming and tool integration.
*
* @typeParam TStream - Indicates whether the completion should be streamed (boolean).
* @typeParam TChatCompletionTool - An array of tools that can be used during the chat completion.
*
* @param {OpenAICreateChatCompletionParams<TStream, MergeChatCompletionTools<TChatCompletionTool, TChatCompletionTool>>} params - Parameters for creating the chat completion, including prompt, system role, messages, tools, and handlers.
*
* @returns A promise that resolves to either a single chat completion or a stream of chat completions, depending on the `stream` parameter.
*/
create<TStream extends boolean | undefined = false, TChatCompletionTool extends AnyChatCompletionTool[] | undefined = undefined>({ prompt, systemRole, messages, tools, toolCallHandlers, toolCallResultHandlers, ...params }: OpenAICreateChatCompletionParams<TStream, MergeChatCompletionTools<TChatCompletionTool, TChatCompletionTool>>): Promise<CreateChatCompletionResult<TStream>>;
}
/**
* Represents a single embedding vector as an array of numbers.
*/
type Embedding = number[];
/**
* Parameters for creating embeddings.
*
* @property {string} [model] - The model to use for creating embeddings. If not provided, a default model may be used.
*/
interface CreateEmbeddingsParams {
model?: string;
}
/**
* Abstract class representing an embeddings generator.
* Implementations must provide methods to get available models and create embeddings.
*/
declare abstract class Embeddings {
/**
* Returns a list of available models that can be used for creating embeddings.
*
* @returns {string[]} - Array of model names.
*/
abstract getModels(): string[];
/**
* Creates embeddings from input data using the specified model.
*
* @param {string | string[]} input - The input text or array of texts to create embeddings for.
* @param {CreateEmbeddingsParams} [params] - Optional parameters for embedding creation.
* @returns {Promise<Embedding[]>} - A promise that resolves to an array of embedding vectors.
*/
abstract create(input: string | string[], params?: CreateEmbeddingsParams): Promise<Embedding[]>;
}
/**
* A type that omits specific properties from EmbeddingCreateParams
* to allow for partial customization when creating embeddings.
*
* @typeParam _OpenAICreateEmbeddingsParams - Partial EmbeddingCreateParams excluding "input" and any properties defined in CreateEmbeddingsParams.
*/
type _OpenAICreateEmbeddingsParams = Omit<Partial<EmbeddingCreateParams>, "input" | keyof CreateEmbeddingsParams>;
/**
* Represents the possible models that can be used with OpenAI embeddings.
*
* @type OpenAIEmbeddingModel - The model name from EmbeddingCreateParams.
*/
type OpenAIEmbeddingModel = EmbeddingCreateParams["model"];
/**
* Parameters for creating embeddings with OpenAI, extending the base CreateEmbeddingsParams
* and additional OpenAI-specific options.
*
* @extends CreateEmbeddingsParams - Inherits the base parameters for embedding creation.
* @property {OpenAIEmbeddingModel} [model] - The OpenAI model to use for creating embeddings. Defaults to a model if not provided.
*/
interface OpenAICreateEmbeddingsParams extends CreateEmbeddingsParams, _OpenAICreateEmbeddingsParams {
model?: OpenAIEmbeddingModel;
}
/**
* A class that implements the Embeddings interface using OpenAI's API.
* This class allows for the creation of embeddings via OpenAI's models.
*
* @class OpenAIEmbeddings
* @extends Embeddings - Inherits the abstract class for embedding generation.
*
* @param {OpenAI} _openai - An instance of the OpenAI API client.
*/
declare class OpenAIEmbeddings extends Embeddings {
private _openai;
constructor(_openai: OpenAI);
/**
* Returns a list of supported OpenAI models that can be used to generate embeddings.
*
* @returns {OpenAIEmbeddingModel[]} - An array of model names supported by OpenAI.
*/
getModels(): OpenAIEmbeddingModel[];
/**
* Creates embeddings using the specified input and OpenAI model.
* The input can be a single string or an array of strings.
*
* @param {string | string[]} input - The input text or array of texts to generate embeddings for.
* @param {OpenAICreateEmbeddingsParams} [params] - Optional parameters, including the OpenAI model to use.
* @returns {Promise<Embedding[]>} - A promise that resolves to an array of embedding vectors generated by OpenAI.
*
* @throws {Error} - Throws an error if the OpenAI API call fails.
*/
create(input: string | string[], params?: OpenAICreateEmbeddingsParams): Promise<Embedding[]>;
}
/**
* Configuration object for initializing the `AI` class.
*
* @typeParam TEmbeddings - The type of `Embeddings` to be used, defaulting to `OpenAIEmbeddings`.
* @typeParam TChatCompletionTool - An array of tools that can be used during chat completions, or undefined.
* @typeParam TChatCompletion - The type of `ChatCompletions` to be used, defaulting to `OpenAIChatCompletions`.
*
* @property {string} [openAIApiKey] - The API key for authenticating with the OpenAI API.
* @property {TEmbeddings} [embeddings] - An instance of `Embeddings` used for generating embeddings. Defaults to `OpenAIEmbeddings`.
* @property {TChatCompletion} [chatCompletions] - An instance of `ChatCompletions` used for generating chat completions. Defaults to `OpenAIChatCompletions`.
* @property {TChatCompletionTool} [chatCompletionTools] - An optional array of tools that can be used during chat completions.
*/
declare function parseLengthErrorMessage(message: Error["message"]): [length: number | undefined, maxLength: number | undefined];
declare class MessageLengthExceededError extends Error {
message: string;
length?: number;
maxLength?: number;
cause?: unknown;
constructor(message: string, options?: {
length?: number;
maxLength?: number;
cause?: unknown;
});
}
declare class MessagesLengthExceededError extends Error {
message: string;
length?: number;
maxLength?: number;
cause?: unknown;
constructor(message: string, options?: {
length?: number;
maxLength?: number;
cause?: unknown;
});
}
interface AIConfig<TEmbeddings extends Embeddings, TChatCompletionTool extends AnyChatCompletionTool[] | undefined, TChatCompletion extends ChatCompletions<TChatCompletionTool>> {

@@ -347,28 +132,9 @@ openAIApiKey?: string;

}
/**
* Represents any `AI` instance with generic types for embeddings, tools, and chat completions.
* This type is useful for handling `AI` instances in a generic context.
*/
type AnyAI = AI<Embeddings, AnyChatCompletionTool[] | undefined, ChatCompletions<any>>;
/**
* Main class for handling AI operations, including embeddings and chat completions.
*
* @typeParam TEmbeddings - The type of `Embeddings` to be used, defaulting to `OpenAIEmbeddings`.
* @typeParam TChatCompletionTool - An array of tools that can be used during chat completions, or undefined.
* @typeParam TChatCompletions - The type of `ChatCompletions` to be used, defaulting to `OpenAIChatCompletions`.
*
* @property {TEmbeddings} embeddings - An instance of `Embeddings` used for generating embeddings.
* @property {TChatCompletions} chatCompletions - An instance of `ChatCompletions` used for generating chat completions.
*/
declare class AI<TEmbeddings extends Embeddings = OpenAIEmbeddings, TChatCompletionTool extends AnyChatCompletionTool[] | undefined = undefined, TChatCompletions extends ChatCompletions<any> = OpenAIChatCompletions<TChatCompletionTool>> {
embeddings: TEmbeddings;
chatCompletions: TChatCompletions;
/**
* Constructs a new `AI` instance.
*
* @param {AIConfig<TEmbeddings, TChatCompletionTool, TChatCompletions>} config - The configuration object for initializing the `AI` instance.
*/
constructor(config: AIConfig<TEmbeddings, TChatCompletionTool, TChatCompletions>);
}
export { AI, type AIConfig, type AnyAI, type AnyChatCompletionTool, type ChatCompletion, type ChatCompletionMessage, type ChatCompletionStream, ChatCompletionTool, type ChatCompletionToolCall, ChatCompletions, type CreateChatCompletionParams, type CreateChatCompletionResult, type CreateEmbeddingsParams, type Embedding, Embeddings, type MergeChatCompletionTools, type OnChatCompletionChunk, type OpenAIChatCompletionModel, OpenAIChatCompletions, type OpenAICreateChatCompletionParams, type OpenAICreateEmbeddingsParams, type OpenAIEmbeddingModel, OpenAIEmbeddings };
export { AI, type AIConfig, type AnyAI, type AnyChatCompletionTool, type ChatCompletion, type ChatCompletionMessage, type ChatCompletionStream, ChatCompletionTool, type ChatCompletionToolCall, ChatCompletions, type CreateChatCompletionParams, type CreateChatCompletionResult, type CreateEmbeddingsParams, type Embedding, Embeddings, type MergeChatCompletionTools, MessageLengthExceededError, MessagesLengthExceededError, type OnChatCompletionChunk, type OpenAIChatCompletionModel, OpenAIChatCompletions, type OpenAICreateChatCompletionParams, type OpenAICreateEmbeddingsParams, type OpenAIEmbeddingModel, OpenAIEmbeddings, parseLengthErrorMessage };

142

dist/index.js

@@ -36,29 +36,16 @@ "use strict";

ChatCompletions: () => ChatCompletions,
Embeddings: () => Embeddings
Embeddings: () => Embeddings,
MessageLengthExceededError: () => MessageLengthExceededError,
MessagesLengthExceededError: () => MessagesLengthExceededError,
parseLengthErrorMessage: () => parseLengthErrorMessage
});
module.exports = __toCommonJS(src_exports);
var import_openai = require("openai");
var import_openai2 = require("openai");
// src/chat-completions/index.ts
var ChatCompletions = class {
/**
* The tools that can be used during chat completion. Initialized to undefined.
*/
tools = void 0;
/**
* Initializes the tools to be used in chat completion.
*
* @param tools - An array of tools to be used in the chat completion.
*/
initTools(tools) {
this.tools = tools;
}
/**
* Handles a stream of chat completions, optionally processing each chunk.
*
* @param stream - The stream of chat completions.
* @param onChunk - Optional callback function to handle each chunk as it is received.
*
* @returns A promise that resolves to the final chat completion, with all chunks combined.
*/
async handleStream(stream, onChunk) {

@@ -75,3 +62,40 @@ let completion = { content: "" };

// src/chat-completions/openai.ts
var import_openai = require("openai");
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
// src/chat-completions/errors/lib/parse-length-error-message.ts
function parseLengthErrorMessage(message) {
const [maxLength, length] = [...message.matchAll(/length\s+(\d+)/g)].map((match) => Number(match[1]));
return [length, maxLength];
}
// src/chat-completions/errors/message-length-exceeded.ts.ts
var MessageLengthExceededError = class extends Error {
constructor(message, options) {
super(message, { cause: options?.cause });
this.message = message;
this.name = "MessageLengthExceededError";
this.length = options?.length;
this.maxLength = options?.maxLength;
}
length;
maxLength;
cause;
};
// src/chat-completions/errors/messages-length-exceeded.ts
var MessagesLengthExceededError = class extends Error {
constructor(message, options) {
super(message, { cause: options?.cause });
this.message = message;
this.name = "MessagesLengthExceededError";
this.length = options?.length;
this.maxLength = options?.maxLength;
}
length;
maxLength;
cause;
};
// src/chat-completions/openai.ts
var OpenAIChatCompletions = class extends ChatCompletions {

@@ -82,7 +106,2 @@ constructor(_openai) {

}
/**
* Retrieves the list of models available for OpenAI chat completions.
*
* @returns An array of model names.
*/
getModels() {

@@ -108,12 +127,2 @@ return [

}
/**
* Creates a chat completion using OpenAI's API, with optional support for streaming and tool integration.
*
* @typeParam TStream - Indicates whether the completion should be streamed (boolean).
* @typeParam TChatCompletionTool - An array of tools that can be used during the chat completion.
*
* @param {OpenAICreateChatCompletionParams<TStream, MergeChatCompletionTools<TChatCompletionTool, TChatCompletionTool>>} params - Parameters for creating the chat completion, including prompt, system role, messages, tools, and handlers.
*
* @returns A promise that resolves to either a single chat completion or a stream of chat completions, depending on the `stream` parameter.
*/
async create({

@@ -135,12 +144,27 @@ prompt,

if (tools?.length) _tools = [..._tools, ...tools];
const response = await this._openai.chat.completions.create({
model: "gpt-4o-mini",
temperature: 0,
...params,
messages: _messages,
tools: _tools.length ? _tools.map(({ name, description, params: params2 }) => ({
type: "function",
function: { name, description, parameters: params2 ? (0, import_zod_to_json_schema.default)(params2) : void 0 }
})) : void 0
});
let response;
try {
response = await this._openai.chat.completions.create({
model: "gpt-4o-mini",
temperature: 0,
...params,
messages: _messages,
tools: _tools.length ? _tools.map(({ name, description, params: params2 }) => ({
type: "function",
function: { name, description, parameters: params2 ? (0, import_zod_to_json_schema.default)(params2) : void 0 }
})) : void 0
});
} catch (error) {
if (error instanceof import_openai.BadRequestError) {
if (error.code === "array_above_max_length") {
const [length, maxLength] = parseLengthErrorMessage(error.message);
throw new MessagesLengthExceededError(error.message, { length, maxLength, cause: error });
}
if (error.code === "string_above_max_length") {
const [length, maxLength] = parseLengthErrorMessage(error.message);
throw new MessageLengthExceededError(error.message, { length, maxLength, cause: error });
}
}
throw error;
}
const handleToolCalls = (toolCalls = []) => {

@@ -252,7 +276,2 @@ return Promise.all(

call;
/**
* Constructs a new ChatCompletionTool instance.
*
* @param {ChatCompletionToolConfig<T, U, K>} config - Configuration object for initializing the tool.
*/
constructor(config) {

@@ -276,20 +295,5 @@ this.name = config.name;

}
/**
* Returns a list of supported OpenAI models that can be used to generate embeddings.
*
* @returns {OpenAIEmbeddingModel[]} - An array of model names supported by OpenAI.
*/
getModels() {
return ["text-embedding-3-small", "text-embedding-3-large", "text-embedding-ada-002"];
}
/**
* Creates embeddings using the specified input and OpenAI model.
* The input can be a single string or an array of strings.
*
* @param {string | string[]} input - The input text or array of texts to generate embeddings for.
* @param {OpenAICreateEmbeddingsParams} [params] - Optional parameters, including the OpenAI model to use.
* @returns {Promise<Embedding[]>} - A promise that resolves to an array of embedding vectors generated by OpenAI.
*
* @throws {Error} - Throws an error if the OpenAI API call fails.
*/
async create(input, params) {

@@ -306,9 +310,4 @@ const _input = Array.isArray(input) ? input : [input];

chatCompletions;
/**
* Constructs a new `AI` instance.
*
* @param {AIConfig<TEmbeddings, TChatCompletionTool, TChatCompletions>} config - The configuration object for initializing the `AI` instance.
*/
constructor(config) {
const openai = new import_openai.OpenAI({ apiKey: config.openAIApiKey });
const openai = new import_openai2.OpenAI({ apiKey: config.openAIApiKey });
this.embeddings = config.embeddings ?? new OpenAIEmbeddings(openai);

@@ -326,4 +325,7 @@ this.chatCompletions = config.chatCompletions ?? new OpenAIChatCompletions(openai);

ChatCompletions,
Embeddings
Embeddings,
MessageLengthExceededError,
MessagesLengthExceededError,
parseLengthErrorMessage
});
//# sourceMappingURL=index.js.map
{
"name": "@singlestore/ai",
"version": "0.0.25",
"version": "0.0.26",
"license": "Apache-2.0",

@@ -5,0 +5,0 @@ "sideEffects": false,

# SingleStoreAI
The official SingleStore AI client.
A module that enhances the `@singlestore/client` package with AI functionality, allowing you to integrate advanced AI features like embeddings and chat completions.
## Table of Contents
- [Installation](#installation)
- [Usage Examples](#usage-examples)
- [Create an Instance](#create-an-instance)
- [Generate Embeddings](#generate-embeddings)
- [Create a Chat Completion](#create-a-chat-completion)
- [Stream Chat Completions](#stream-chat-completions)
- [Develop a Chat Completion Tool](#develop-a-chat-completion-tool)
- [Custom Chat Completions](#custom-chat-completions)
- [Custom Embeddings](#custom-embeddings)
## Installation
To install the `@singlestore/ai` package, run the following command:
```bash
npm install @singlestore/ai
```
## Usage Examples
### Create an Instance
First, create an instance of the `SingleStoreAI` class using your OpenAI API key.
```ts
import { AI } from "@singlestore/ai";
const ai = new SingleStoreAI({ openAIApiKey: "<OPENAI_API_KEY>" });
```
### Generate Embeddings
Generate embeddings for a given input text using the `create` method.
```ts
const input = "Hi!";
const embeddings = await ai.embeddings.create(input);
console.log(embeddings);
```
### Create a Chat Completion
Create a chat completion.
```ts
const prompt = "Hi, how are you?";
const chatCompletion = await ai.chatCompletions.create({
prompt,
model: "gpt-4o",
systemRole: "You are a helpful assistant",
});
console.log(chatCompletion);
```
### Stream Chat Completions
Stream chat completions to handle responses in real time.
```ts
const prompt = "Hi, how are you?";
const stream = await ai.chatCompletions.create({
prompt,
model: "gpt-4o",
systemRole: "You are a helpful assistant",
stream: true,
});
const onChunk: OnChatCompletionChunk = (chunk) => {
console.log("onChunk:", chunk);
};
const chatCompletion = await ai.chatCompletions.handleStream(stream, onChunk);
console.log(chatCompletion);
```
### Develop a Chat Completion Tool
Create a custom chat completion tool to handle specific tasks.
```ts
import { AI, ChatCompletionTool } from "@singlestore/ai";
import { z } from "zod";
const findCityInfoTool = new ChatCompletionTool({
name: "find_city_info",
description: "Useful for finding and displaying information about a city.",
params: z.object({ name: z.string().describe("The city name") }),
call: async (params) => {
const info = `${params.name} is known as a great city!`;
return { name: "find_city_info", params, value: JSON.stringify(info) };
},
});
const ai = new AI({
openAIApiKey: "<OPENAI_API_KEY>",
chatCompletionTools: [findCityInfoTool],
});
const chatCompletion = await ai.chatCompletions.create({ prompt: "Find info about Vancouver." });
console.log(chatCompletion);
```
### Custom Chat Completions
Extend the ChatCompletions class to use a custom LLM for creating chat completions.
```ts
import { AI, type AnyChatCompletionTool, ChatCompletions } from "@singlestore/ai";
class CustomChatCompletions<
TChatCompletionTool extends AnyChatCompletionTool[] | undefined,
> extends ChatCompletions<TChatCompletionTool> {
constructor() {
super();
}
getModels(): Promise<string[]> | string[] {
// Your implementation
return [];
}
async create<TStream extends boolean | undefined>(
params: CreateChatCompletionParams<TStream, TChatCompletionTool>,
): Promise<CreateChatCompletionResult<TStream>> {
// Your implementation
return {} as CreateChatCompletionResult<TStream>;
}
}
const ai = new AI({
openAIApiKey: "<OPENAI_API_KEY>",
chatCompletions: new CustomChatCompletions(),
});
```
### Custom Embeddings
Create a custom embeddings class to use a custom LLM for creating embeddings.
```ts
import { AI, Embeddings } from "@singlestore/ai";
class CustomEmbeddings extends Embeddings {
constructor() {
super();
}
getModels(): string[] {
// Your implementation
return [];
}
async create(input: string | string[], params?: CreateEmbeddingsParams): Promise<Embedding[]> {
// Your implementation
return [];
}
}
const ai = new AI({
openAIApiKey: "<OPENAI_API_KEY>",
embeddings: new CustomEmbeddings(),
});
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc