嵌入
BaseRagasEmbeddings
Bases: Embeddings
, ABC
Abstract base class for Ragas embeddings.
This class extends the Embeddings class and provides methods for embedding text and managing run configurations.
Attributes: run_config (RunConfig): Configuration for running the embedding operations.
embed_text
async
embed_texts
async
Embed multiple texts.
Source code in src/ragas/embeddings/base.py
set_run_config
set_run_config(run_config: RunConfig)
HuggingfaceEmbeddings
Bases: BaseRagasEmbeddings
Hugging Face embeddings class for generating embeddings using pre-trained models.
This class provides functionality to load and use Hugging Face models for generating embeddings of text inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
Name of the pre-trained model to use, by default DEFAULT_MODEL_NAME. |
required |
cache_folder
|
str
|
Path to store downloaded models. Can also be set by SENTENCE_TRANSFORMERS_HOME environment variable. |
required |
model_kwargs
|
dict
|
Additional keyword arguments to pass to the model. |
required |
encode_kwargs
|
dict
|
Additional keyword arguments to pass to the encoding method. |
required |
Attributes:
Name | Type | Description |
---|---|---|
model |
Union[SentenceTransformer, CrossEncoder]
|
The loaded Hugging Face model. |
is_cross_encoder |
bool
|
Flag indicating whether the model is a cross-encoder. |
Methods:
Name | Description |
---|---|
embed_query |
Embed a single query text. |
embed_documents |
Embed multiple documents. |
predict |
Make predictions using a cross-encoder model. |
Notes
This class requires the sentence_transformers
and transformers
packages
to be installed.
Examples:
>>> embeddings = HuggingfaceEmbeddings(model_name="bert-base-uncased")
>>> query_embedding = embeddings.embed_query("What is the capital of France?")
>>> doc_embeddings = embeddings.embed_documents(["Paris is the capital of France.", "London is the capital of the UK."])
embed_query
embed_documents
Embed multiple documents.
Source code in src/ragas/embeddings/base.py
predict
Make predictions using a cross-encoder model.
Source code in src/ragas/embeddings/base.py
LangchainEmbeddingsWrapper
LangchainEmbeddingsWrapper(
embeddings: Embeddings,
run_config: Optional[RunConfig] = None,
)
Bases: BaseRagasEmbeddings
Wrapper for any embeddings from langchain.
Source code in src/ragas/embeddings/base.py
embed_query
embed_documents
aembed_query
async
aembed_documents
async
set_run_config
set_run_config(run_config: RunConfig)
Set the run configuration for the embedding operations.
Source code in src/ragas/embeddings/base.py
LlamaIndexEmbeddingsWrapper
LlamaIndexEmbeddingsWrapper(
embeddings: BaseEmbedding,
run_config: Optional[RunConfig] = None,
)
Bases: BaseRagasEmbeddings
Wrapper for any embeddings from llama-index.
This class provides a wrapper for llama-index embeddings, allowing them to be used within the Ragas framework. It supports both synchronous and asynchronous embedding operations for queries and documents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
embeddings
|
BaseEmbedding
|
The llama-index embedding model to be wrapped. |
required |
run_config
|
RunConfig
|
Configuration for the run. If not provided, a default RunConfig will be used. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
embeddings |
BaseEmbedding
|
The wrapped llama-index embedding model. |
Examples:
>>> from llama_index.embeddings import OpenAIEmbedding
>>> from ragas.embeddings import LlamaIndexEmbeddingsWrapper
>>> llama_embeddings = OpenAIEmbedding()
>>> wrapped_embeddings = LlamaIndexEmbeddingsWrapper(llama_embeddings)
>>> query_embedding = wrapped_embeddings.embed_query("What is the capital of France?")
>>> document_embeddings = wrapped_embeddings.embed_documents(["Paris is the capital of France.", "London is the capital of the UK."])
Source code in src/ragas/embeddings/base.py
embedding_factory
embedding_factory(
model: str = "text-embedding-ada-002",
run_config: Optional[RunConfig] = None,
) -> BaseRagasEmbeddings
Create and return a BaseRagasEmbeddings instance. Used for default embeddings used in Ragas (OpenAI).
This factory function creates an OpenAIEmbeddings instance and wraps it with LangchainEmbeddingsWrapper to provide a BaseRagasEmbeddings compatible object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
The name of the OpenAI embedding model to use, by default "text-embedding-ada-002". |
'text-embedding-ada-002'
|
run_config
|
RunConfig
|
Configuration for the run, by default None. |
None
|
Returns:
Type | Description |
---|---|
BaseRagasEmbeddings
|
An instance of BaseRagasEmbeddings configured with the specified parameters. |