LLMs
BaseRagasLLM
dataclass
BaseRagasLLM(run_config: RunConfig = RunConfig(), multiple_completion_supported: bool = False, cache: Optional[CacheInterface] = None)
Bases: ABC
get_temperature
is_finished
abstractmethod
generate
async
generate(prompt: PromptValue, n: int = 1, temperature: Optional[float] = 0.01, stop: Optional[List[str]] = None, callbacks: Callbacks = None) -> LLMResult
Generate text using the given event loop.
Source code in src/ragas/llms/base.py
InstructorBaseRagasLLM
Bases: ABC
Base class for LLMs using the Instructor library pattern.
generate
abstractmethod
Generate a response using the configured LLM.
For async clients, this will run the async method in the appropriate event loop.
Source code in src/ragas/llms/base.py
agenerate
abstractmethod
async
Asynchronously generate a response using the configured LLM.
InstructorLLM
Bases: InstructorBaseRagasLLM
LLM wrapper using the Instructor library for structured outputs.
Source code in src/ragas/llms/base.py
generate
Generate a response using the configured LLM.
For async clients, this will run the async method in the appropriate event loop.
Source code in src/ragas/llms/base.py
agenerate
async
Asynchronously generate a response using the configured LLM.
Source code in src/ragas/llms/base.py
LangchainLLMWrapper
LangchainLLMWrapper(langchain_llm: BaseLanguageModel[BaseMessage], run_config: Optional[RunConfig] = None, is_finished_parser: Optional[Callable[[LLMResult], bool]] = None, cache: Optional[CacheInterface] = None, bypass_temperature: bool = False)
Bases: BaseRagasLLM
A simple base class for RagasLLMs that is based on Langchain's BaseLanguageModel interface. it implements 2 functions: - generate_text: for generating text from a given PromptValue - agenerate_text: for generating text from a given PromptValue asynchronously
Source code in src/ragas/llms/base.py
is_finished
Parse the response to check if the LLM finished by checking the finish_reason or stop_reason. Supports OpenAI and Vertex AI models.
Source code in src/ragas/llms/base.py
LlamaIndexLLMWrapper
LlamaIndexLLMWrapper(llm: BaseLLM, run_config: Optional[RunConfig] = None, cache: Optional[CacheInterface] = None, bypass_temperature: bool = False)
Bases: BaseRagasLLM
A Adaptor for LlamaIndex LLMs
Source code in src/ragas/llms/base.py
HaystackLLMWrapper
HaystackLLMWrapper(haystack_generator: Any, run_config: Optional[RunConfig] = None, cache: Optional[CacheInterface] = None)
Bases: BaseRagasLLM
A wrapper class for using Haystack LLM generators within the Ragas framework.
This class integrates Haystack's LLM components (e.g., OpenAIGenerator,
HuggingFaceAPIGenerator, etc.) into Ragas, enabling both synchronous and
asynchronous text generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
haystack_generator
|
AzureOpenAIGenerator | HuggingFaceAPIGenerator | HuggingFaceLocalGenerator | OpenAIGenerator
|
An instance of a Haystack generator. |
required |
run_config
|
RunConfig
|
Configuration object to manage LLM execution settings, by default None. |
None
|
cache
|
CacheInterface
|
A cache instance for storing results, by default None. |
None
|
Source code in src/ragas/llms/haystack_wrapper.py
instructor_llm_factory
instructor_llm_factory(provider: str, model: Optional[str] = None, client: Optional[Any] = None, **kwargs: Any) -> InstructorBaseRagasLLM
Factory function to create an InstructorLLM instance based on the provider.
Args: provider (str): The name of the LLM provider or provider/model string (e.g., "openai", "openai/gpt-4"). model (str, optional): The model name to use for generation. client (Any, optional): Pre-initialized client for the provider. **kwargs: Additional arguments for the LLM (model_args).
Returns: InstructorBaseRagasLLM: An instance of the specified LLM provider.
Examples: # OpenAI with separate parameters llm = instructor_llm_factory("openai", "gpt-4", client=openai_client)
# OpenAI with provider/model string
llm = instructor_llm_factory("openai/gpt-4", client=openai_client)
# Anthropic
llm = instructor_llm_factory("anthropic", "claude-3-sonnet-20240229", client=anthropic_client)
# Cohere
llm = instructor_llm_factory("cohere", "command-r-plus", client=cohere_client)
# Gemini
llm = instructor_llm_factory("gemini", "gemini-pro", client=gemini_client)
# LiteLLM (supports 100+ models)
llm = instructor_llm_factory("litellm", "gpt-4", client=litellm_client)
Raises: ValueError: If provider is unsupported or required parameters are missing.
Source code in src/ragas/llms/base.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | |
llm_factory
llm_factory(model: str = 'gpt-4o-mini', run_config: Optional[RunConfig] = None, default_headers: Optional[Dict[str, str]] = None, base_url: Optional[str] = None) -> BaseRagasLLM
Create and return a BaseRagasLLM instance. Used for running default LLMs used in Ragas (OpenAI).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The name of the model to use, by default "gpt-4o-mini". |
'gpt-4o-mini'
|
run_config
|
RunConfig
|
Configuration for the run, by default None. |
None
|
default_headers
|
dict of str
|
Default headers to be used in API requests, by default None. |
None
|
base_url
|
str
|
Base URL for the API, by default None. |
None
|
Returns:
| Type | Description |
|---|---|
BaseRagasLLM
|
An instance of BaseRagasLLM configured with the specified parameters. |