LiteLLM Model
rankify.generator.models.litellm_model
BaseRAGModel
Bases: ABC
Base RAG Model for Retrieval-Augmented Generation (RAG).
This is an abstract base class for implementing LLM endpoints in rankify. It defines the interface for generating responses and optional embedding generation.
Methods:
| Name | Description |
|---|---|
generate |
str, **kwargs) -> str: Abstract method to generate a response based on the given prompt. |
embed |
str, **kwargs) -> List[float]: Optional method to generate embeddings for the given text. |
Notes
- This class serves as a blueprint for RAG models like
OpenAIModelandHuggingFaceModel. - The
embedmethod is optional and can be implemented if needed. - This class needs to be extended to include new LLM endpoints in Rankify.
Source code in rankify/generator/models/base_rag_model.py
generate(prompt, **kwargs)
abstractmethod
embed(text, **kwargs)
Optional: Generate embeddings for the given text.
PromptGenerator
PromptGenerator for Retrieval-Augmented Generation (RAG).
This class manages prompt construction for different RAG methods and model types in Rankify. It selects and formats prompt templates based on the specified method and model, enabling flexible and consistent prompt generation.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
The RAG method or strategy (e.g., "basic-rag", "chain-of-thought-rag"). |
prompt_template |
PromptTemplate
|
The prompt template used for formatting prompts. |
model_type |
str
|
The type of model (e.g., "huggingface", "openai"). |
Methods:
| Name | Description |
|---|---|
generate_user_prompt |
Generates a user prompt by formatting the question and contexts with the selected template. |
_select_template |
Selects the appropriate prompt template for the given method. |
Notes
- Supports custom prompt templates via the
custom_promptargument. - If no contexts are provided, generates prompts with only the question.
- Ensures consistent prompt formatting across different RAG methods and models.
- Automatically selects a default template if none is specified.
Source code in rankify/generator/prompt_generator.py
__init__(method, model_type, prompt_template=None)
Initialize the PromptGenerator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The RAG method or strategy (e.g., "basic-rag", "chain-of-thought-rag"). |
required |
model_type
|
str
|
The type of model (e.g., "huggingface", "openai"). |
required |
prompt_template
|
PromptTemplate
|
Custom prompt template to use. If None, selects based on method. |
None
|
Notes
- If no custom template is provided, selects a default template for the specified method.
- Stores the method, model type, and selected prompt template for prompt generation.
Source code in rankify/generator/prompt_generator.py
generate_user_prompt(question, contexts, custom_prompt=None)
Generates a user prompt by formatting the question and contexts with the selected template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The question to be answered. |
required |
contexts
|
List[str]
|
List of context passages to include in the prompt. |
required |
custom_prompt
|
str
|
Custom prompt template string. If provided, overrides the default template. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted prompt string. |
Notes
- If no contexts are provided, generates a prompt with only the question.
- Custom prompt templates can use
{question}and{contexts}placeholders. - Ensures consistent prompt formatting for all RAG methods and models.
Source code in rankify/generator/prompt_generator.py
LitellmClient
Source code in rankify/utils/api/litellmclient.py
LitellmModel
Bases: BaseRAGModel
LiteLLM Model for Retrieval-Augmented Generation (RAG).
This class integrates LiteLLM's API for text generation in a RAG pipeline. It uses the LiteLLM API to generate responses based on input prompts.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
Name of the LiteLLM model. |
prompt_generator |
PromptGenerator
|
Instance for generating prompts. |
client |
LitellmClient
|
Client for interacting with the LiteLLM API. |
Notes
- This model uses LiteLLM's API for text generation.
- It supports additional parameters like
max_tokensandtemperature.
Source code in rankify/generator/models/litellm_model.py
__init__(model_name, api_key, prompt_generator)
Initialize the LitellmModel with the LitellmClient.
:param model_name: Name of the LiteLLM model. :param api_key: API key for LiteLLM. :param prompt_generator: Instance of PromptGenerator for generating prompts.
Source code in rankify/generator/models/litellm_model.py
generate(prompt, **kwargs)
Generate a response using LiteLLM's API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The input prompt for the model. |
required |
**kwargs
|
Additional parameters for the LiteLLM API call, such as: - model (str): Model name to use (default: self.model_name). - max_tokens (int): Maximum number of tokens to generate (default: 128). - temperature (float): Sampling temperature (default: 0.7). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated response as a string. |
Notes
- Default parameters: model=self.model_name, max_tokens=128, temperature=0.7.
- All generation parameters can be overridden via
kwargs.