Implementation:Explodinggradients Ragas OpenAIEmbeddings Provider
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Embeddings, OpenAI |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
OpenAI embeddings provider with batch optimization and automatic sync/async client detection.
Description
OpenAIEmbeddings extends BaseRagasEmbedding to provide text embeddings using the OpenAI Python client. It supports both sync and async clients with automatic detection, tracks usage analytics, and defaults to the text-embedding-3-small model.
Usage
Use this provider when working directly with OpenAI's embedding API. Requires a pre-configured OpenAI client.
Code Reference
Source Location
- Repository: Explodinggradients_Ragas
- File: src/ragas/embeddings/openai_provider.py
- Lines: 10-149
Signature
class OpenAIEmbeddings(BaseRagasEmbedding):
PROVIDER_NAME = "openai"
REQUIRES_CLIENT = True
DEFAULT_MODEL = "text-embedding-3-small"
def __init__(
self,
client: Any,
model: str = "text-embedding-3-small",
cache: Optional[CacheInterface] = None,
) -> None:
...
Import
from ragas.embeddings.openai_provider import OpenAIEmbeddings
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| client | Any | Yes | Pre-configured OpenAI client (sync or async) |
| model | str | No | Embedding model name (default "text-embedding-3-small") |
Outputs
| Name | Type | Description |
|---|---|---|
| embed_text returns | List[float] | Embedding vector |
| embed_texts returns | List[List[float]] | Batch embedding vectors |
Usage Examples
from openai import OpenAI
from ragas.embeddings.openai_provider import OpenAIEmbeddings
client = OpenAI()
embeddings = OpenAIEmbeddings(client=client, model="text-embedding-3-small")
vector = embeddings.embed_text("What is machine learning?")
print(len(vector))
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment