@singlestore/ai
@singlestore/ai
is a module of the @singlestore/client
package that provides AI capabilities,
including chat completions and embeddings, integrated with OpenAI models. Additionally, it offers flexibility for users to integrate their own AI models and tools.
Features
- Chat Completions: Leverage OpenAI's chat models to generate completions, manage tools, and handle streams of chat data. Alternatively, integrate your own AI models.
- Embeddings: Generate text embeddings using OpenAI's embedding models to power various AI-driven functionalities, or use your custom embedding models.
Installation
To install the @singlestore/ai
module, use npm:
npm install @singlestore/client
Usage
Basic Setup
First, initialize the AI module with your OpenAI API key or your custom AI instance:
import { AI } from "@singlestore/ai";
const ai = new AI({
openAIApiKey: "your-openai-api-key",
});
Custom AI Models and Tools
You can provide your own AI models and tools instead of using OpenAI. For example:
import { AI, ChatCompletionTool } from "@singlestore/ai";
const customTool = new ChatCompletionTool({
name: "CustomTool",
description: "A custom tool for chat completion",
params: {},
call: async (params) => {
},
});
const ai = new AI({
chatCompletionTools: [customTool],
embeddings: new CustomEmbeddings(),
});
Chat Completions
Create chat completions with integrated tools and streaming capabilities:
const completion = await ai.chatCompletions.create({
prompt: "Tell me a joke.",
systemRole: "You are a helpful assistant.",
stream: false,
});
console.log(completion.content);
Embeddings
Generate embeddings for your text:
const embeddings = await ai.embeddings.create("This is a sample text.");
console.log(embeddings);
API Reference
The following section describes the main classes and methods available in the @singlestore/ai
module.
AI
The main class that provides access to chat completions and embeddings.
Constructor
new AI(config: AIConfig)
Parameters:
config.openAIApiKey
(string): Your OpenAI API key.config.chatCompletionTools
(ChatCompletionTool[] | undefined): An optional array of tools to be used in chat completions.config.embeddings
(Embeddings | undefined): An optional custom embeddings instance.
ChatCompletions
Handles creating chat completions and managing the stream of chat data.
Methods:
async create(params: OpenAICreateChatCompletionParams)
Creates a chat completion with optional support for tools and streaming.
Parameters:
prompt
(string | undefined): The initial user prompt.systemRole
(string | undefined): The system role for the AI.messages
(ChatMessage[] | undefined): An array of messages in the chat.stream
(boolean | undefined): If true, returns an async generator for streaming completions.tools
(ChatCompletionTool[] | undefined): Tools to be used during chat completion.toolCallHandlers
(ToolCallHandlers | undefined): Handlers for tool calls.toolCallResultHandlers
(ToolCallResultHandlers | undefined): Handlers for tool call results.
Returns:
Promise<ChatCompletion | AsyncGenerator<ChatCompletionStream>>
: A promise that resolves to either a single chat completion or a stream of chat completions.
initTools(tools: ChatCompletionTool[])
Initializes tools to be used in chat completions.
Parameters:
tools
(ChatCompletionTool[]): An array of tools.
Embeddings
Handles generating text embeddings using OpenAI models.
Methods:
async create(input: string | string[], params?: OpenAICreateEmbeddingsParams)
Generates embeddings for the provided input text.
Parameters:
input
(string | string[]): The input text or array of texts to generate embeddings for.params
(OpenAICreateEmbeddingsParams | undefined): Optional parameters for creating embeddings.
Returns:
Promise<Embedding[]>
: A promise that resolves to an array of embedding vectors generated by OpenAI.
Data Types
ChatCompletion
An object representing a chat completion.
Properties:
content
(string): The content of the chat completion.
ChatCompletionStream
An async generator that yields chunks of chat completions.
Embedding
An object representing a text embedding.
Properties:
embedding
(number[]): An array of numbers representing the embedding vector.
ChatMessage
An object representing a message in the chat.
Properties:
role
(string): The role of the message sender (e.g., 'system', 'user', 'assistant').content
(string): The content of the message.
ChatCompletionTool
An object representing a tool used in chat completions.
Properties:
name
(string): The name of the tool.description
(string): A description of what the tool does.params
(object | undefined): Parameters required by the tool.call
(function): The function that executes the tool.
OpenAICreateChatCompletionParams
Parameters used for creating a chat completion.
Properties:
model
(string): The model to use for the chat completion (e.g., 'gpt-4').temperature
(number | undefined): Sampling temperature for randomness in outputs.prompt
(string | undefined): The initial user prompt.systemRole
(string | undefined): The system role for the AI.messages
(ChatMessage[] | undefined): An array of messages in the chat.stream
(boolean | undefined): If true, returns an async generator for streaming completions.tools
(ChatCompletionTool[] | undefined): Tools to be used during chat completion.
OpenAICreateEmbeddingsParams
Parameters used for creating embeddings.
Properties:
model
(string): The model to use for generating embeddings (e.g., 'text-embedding-ada-002').input
(string | string[]): The input text or array of texts to generate embeddings for.