OpenAI
OpenAI embedding models convert text (words, sentences, or paragraphs) into high-dimensional vectors. These vectors capture semantic meaning so you can measure similarity (for example, via cosine similarity) without extra training. Typical use cases include semantic search, question answering, text clustering and classification, recommendation systems, and RAG.
Using OpenAI service requires you to follow OpenAI's pricing rules and may incur corresponding fees. Before proceeding, please visit their official website or refer to relevant documentation to confirm and accept their pricing standards. If you do not agree, please do not proceed.
Dependencies and authentication
- Install the
@seekdb/openaipackage. - In Node you can use the official OpenAI JavaScript SDK; in the browser you can use the
fetchAPI instead. - Create an API key in the OpenAI Platform and set it in your environment or pass it in code.
Example: create an OpenAI embedding function
Call the OpenAIEmbeddingFunction constructor with an optional config object.
-
Basic usage:
Omit the config to use the default model and the API key from the environment.
import { OpenAIEmbeddingFunction } from "@seekdb/openai";
const ef = new OpenAIEmbeddingFunction(); -
Advanced usage:
Override the API key environment variable and set
modelName,baseURL, and other options as needed.import { OpenAIEmbeddingFunction } from "@seekdb/openai";
const ef = new OpenAIEmbeddingFunction({
modelName: "text-embedding-3-small",
// baseURL: "https://api.openai.com/v1",
});
Configurations:
- apiKey: API key (optional; can be provided via the environment).
- apiKeyEnvVar: Environment variable name for the API key (default:
OPENAI_API_KEY). - modelName: Model name (default:
text-embedding-3-small). - dimensions: Output dimension (optional).
- organizationId: Organization ID (optional; default:
process.env.OPENAI_ORG_ID). - baseURL: Custom base URL (optional).