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
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |
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. |