
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@singlestore/ai
Advanced tools
A module that enhances the [`@singlestore/client`](https://github.com/singlestore-labs/singlestore/tree/main/packages/client) package with AI functionality, allowing you to integrate AI features like embeddings and chat completions.
A module that enhances the @singlestore/client
package with AI functionality, allowing you to integrate AI features like embeddings and chat completions.
npm install @singlestore/ai
The AI
class can be initialized in various ways depending on your requirements. You can start with the default setup, or extend it with custom managers for embeddings and chat completions, or even add custom tools.
This is the simplest way to initialize the AI
class, using an OpenAI API key.
import { AI } from "@singlestore/ai";
const ai = new AI({ openAIApiKey: "<OPENAI_API_KEY>" });
You can define a custom embeddings manager by extending the EmbeddingsManager
class to handle how embeddings are created and models are selected.
import { type CreateEmbeddingsParams, type Embedding, EmbeddingsManager } from "@singlestore/ai/embeddings";
class CustomEmbeddingsManager extends EmbeddingsManager {
getModels(): string[] {
return ["<MODEL_NAME>"];
}
async create(input: string | string[], params?: CreateEmbeddingsParams): Promise<Embedding[]> {
const embeddings: Embedding[] = await customFnCall();
return embeddings;
}
}
const ai = new AI({
openAIApiKey: "<OPENAI_API_KEY>",
embeddingsManager: new CustomEmbeddingsManager(),
});
You can define a custom chat completions manager by extending the ChatCompletionsManager
class. This allows you to modify how chat completions are handled, whether in a streaming or non-streaming fashion.
import {
type AnyChatCompletionTool,
ChatCompletionsManager,
type CreateChatCompletionParams,
type CreateChatCompletionResult,
type MergeChatCompletionTools,
} from "@singlestore/ai/chat-completions";
type ChatCompletionTools = undefined; // If an array of custom tools is created, use `typeof tools`.
class CustomChatCompletionsManager extends ChatCompletionsManager<ChatCompletionTools> {
getModels(): Promise<string[]> | string[] {
return ["<MODEL_NAME>"];
}
create<TStream extends boolean, TTools extends AnyChatCompletionTool[] | undefined>(
params: CreateChatCompletionParams<TStream, MergeChatCompletionTools<ChatCompletionTools, TTools>>,
): Promise<CreateChatCompletionResult<TStream>> {
if (params.stream) {
const stream = customFnCall();
return stream as Promise<CreateChatCompletionResult<TStream>>;
}
const chatCompletion = await customFnCall();
return chatCompletion as Promise<CreateChatCompletionResult<TStream>>;
}
}
const ai = new AI({
openAIApiKey: "<OPENAI_API_KEY>",
chatCompletionsManager: new CustomChatCompletionsManager(),
});
You can also create custom tools to extend the functionality of the chat completions by defining them with the ChatCompletionTool
class.
import { ChatCompletionTool } from "@singlestore/ai/chat-completions";
import { z } from "zod";
const customTool = new ChatCompletionTool({
name: "<TOOL_NAME>",
description: "<TOOL_DESCRIPTION>",
params: z.object({ paramName: z.string().describe("<PARAM_DESCRIPTION>") }),
call: async (params) => {
const value = await anyFnCall(params);
return { name: "<TOOL_NAME>", params, value: JSON.stringify(value) };
},
});
const ai = new AI({
tools: [customTool],
...
});
openAIApiKey
parameter is not required.const models = ai.embeddings.getModels();
const embeddings = await ai.embeddings.create("<INPUT>", {
model: "<MODEL_NAME>", // Optional
dimensions: "<DIMENSION>", // Optional
});
const embeddings = await ai.embeddings.create(["<INPUT>", "<INPUT_2>"], ...);
EmbeddingsManager
is provided, all the parameters can still be passed to the ai.embeddings.create
method, allowing for custom handling and logic while preserving the same interface.const models = ai.chatCompletions.getModels();
The create
method allows you to generate chat completions either as a complete string or in a streamed fashion, depending on the stream
option.
Performs a chat completion and returns the result as a complete string.
const chatCompletion = await ai.chatCompletions.create({
stream: false,
prompt: "<PROMPT>",
model: "<MODEL_NAME>", // Optional
systemRole: "<SYSTEM_ROLE>", // Optional
messages: [{ role: "user", content: "<CONTENT>" }], // Optional
});
Performs a chat completion and returns the result as a stream of data chunks.
const stream = await ai.chatCompletions.create({
stream: true,
prompt: "<PROMPT>",
model: "<MODEL_NAME>", // Optional
systemRole: "<SYSTEM_ROLE>", // Optional
messages: [{ role: "user", content: "<CONTENT>" }], // Optional
tools: [...] // Optional
});
const chatCompletion = await ai.chatCompletions.handleStream(stream, async (chunk) => {
await customFnCall(chunk);
});
stream: true
, the handleStream
function processes the stream and accepts a callback function as the second argument. The callback handles each new chunk of data as it arrives.ChatCompletionsManager
is provided, all the parameters can still be passed to the ai.chatCompletions.create
method, allowing for custom handling and logic while preserving the same interface.FAQs
A module that enhances the [`@singlestore/client`](https://github.com/singlestore-labs/singlestore/tree/main/packages/client) package with AI functionality, allowing you to integrate AI features like embeddings and chat completions.
The npm package @singlestore/ai receives a total of 343 weekly downloads. As such, @singlestore/ai popularity was classified as not popular.
We found that @singlestore/ai demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.