Skip to content

Ragas wrapper

This file is used to wrap the LLM and embeddings for Ragas.

RagasEmbeddingsWrapper(em_invoker)

Bases: BaseRagasEmbeddings

Ragas embeddings wrapper.

This wrapper is used to wrap the embeddings for Ragas.

Attributes:

Name Type Description
em_invoker BaseEMInvoker

The embeddings invoker.

Initialize the Ragas embeddings wrapper.

Parameters:

Name Type Description Default
em_invoker BaseEMInvoker

The embeddings invoker.

required

aembed_documents(texts) async

Embed documents asynchronously.

Parameters:

Name Type Description Default
texts list[str]

The texts to embed.

required

Returns:

Type Description
list[list[float]]

list[list[float]]: The embeddings of the documents.

aembed_query(query) async

Embed a query asynchronously.

Parameters:

Name Type Description Default
query str

The query to embed.

required

Returns:

Type Description
list[float]

list[float]: The embedding of the query.

embed_documents(texts)

Embed documents synchronously.

This method is not implemented because our embeddings invoker does not support synchronous embedding.

Raises:

Type Description
NotImplementedError

Always.

embed_query(query)

Embed a query synchronously.

This method is not implemented because our embeddings invoker does not support synchronous embedding.

Raises:

Type Description
NotImplementedError

Always.

RagasLLMWrapper(lm_invoker, fallback_lms=None)

Bases: BaseRagasLLM, LMComponent

Ragas LLM wrapper backed by LMComponent.

Provides the BaseRagasLLM interface with automatic fallback chain support. When the primary invoker raises BaseInvokerError or TimeoutError during agenerate_text, the next invoker in fallback_lms is tried in order.

Attributes:

Name Type Description
lm_invoker BaseLMInvoker

Primary LM invoker.

fallback_lms list[BaseLMInvoker]

Ordered fallback invokers.

Initialize the Ragas LLM wrapper.

Parameters:

Name Type Description Default
lm_invoker BaseLMInvoker

The LLM invoker.

required
fallback_lms list[BaseLMInvoker] | None

Ordered fallback invokers. Defaults to None.

None

winning_invoker property

The invoker that handled the most recent successful request.

agenerate_text(prompt, n=1, temperature=None, stop=None, callbacks=None) async

Generate text using the LLM with automatic fallback chain.

Converts the Ragas PromptValue to gllm Messages and iterates through the fallback chain until a successful response is returned.

Parameters:

Name Type Description Default
prompt PromptValue

The prompt to generate text from.

required
n int

The number of responses to generate. Default is 1.

1
temperature float | None

The temperature to use for the generation. Default is None.

None
stop list[str] | None

The stop tokens to use for the generation. Default is None.

None
callbacks Callbacks | None

The callbacks to use for the generation. Default is None.

None

Returns:

Name Type Description
LLMResult LLMResult

The result of the generation.

Raises:

Type Description
InvokerRuntimeError

If n != 1 (only single-generation is supported).

generate_text(prompt, n=1, temperature=None, stop=None, callbacks=None)

Generate text using the LLM.

This method is not implemented because our LM invoker does not support synchronous generation.

Raises:

Type Description
NotImplementedError

Always.

is_finished(response)

Check if the response is finished.

Since we didn't do any modification to the response, we assume that the response is finished.

Parameters:

Name Type Description Default
response LLMResult

The response to check.

required