Adding New Features¶
This guide covers how to add new features to GenAI Bench, including model providers, storage providers, and tasks.
Adding a New Model Provider¶
- Create auth provider in
genai_bench/auth/ - Create user class in
genai_bench/user/ - Update
UnifiedAuthFactory - Add validation in
cli/validation.py - Write tests
Adding a New Storage Provider¶
- Create storage auth in
genai_bench/auth/ - Create storage implementation in
genai_bench/storage/ - Update
StorageFactory - Write tests
Adding a New Task¶
This guide explains how to add support for a new task in genai-bench. Follow the steps below to ensure consistency and compatibility with the existing codebase.
1. Define the Request and Response in protocol.py¶
Steps¶
- Add relevant fields to the appropriate request/response data classes in
protocol.py - If the new task involves a new input-output modality, create a new request/response class.
- Use existing request/response classes (
UserChatRequest,UserEmbeddingRequest,UserImageChatRequest, etc.) if they suffice.
Example¶
class UserTextToImageRequest(UserRequest):
"""Represents a request for generating images from text."""
prompt: str
num_images: int = Field(..., description="Number of images to generate.")
image_resolution: Tuple[int, int] = Field(..., description="Resolution of the generated images.")
2. Update or Create a Sampler¶
2.1 If Input Modality Is Supported by an Existing Sampler¶
- Check if the current
TextSamplerorImageSamplersupports the input-modality. - Add request creation logic in the relevant
TextSamplerorImageSamplerclass. - Refactor the sampler's
_create_requestmethod to support the new task. - Tip: Avoid adding long
if-elsechains for new tasks. Utilize helper methods or design a request creator pattern if needed.
2.2 If Input Modality Is Not Supported¶
- Create a new sampler class inheriting from
BaseSampler. - Define the
samplemethod to generate requests for the new task. - Refer to
TextSamplerandImageSamplerfor implementation patterns. - Add utility functions for data preprocessing or validation specific to the new modality if necessary.
Example for a New Sampler¶
class AudioSampler(Sampler):
input_modality = "audio"
supported_tasks = {"audio-to-text", "audio-to-embeddings"}
def sample(self, scenario: Scenario) -> UserRequest:
# Validate scenario
self._validate_scenario(scenario)
if self.output_modality == "text":
return self._create_audio_to_text_request(scenario)
elif self.output_modality == "embeddings":
return self._create_audio_to_embeddings_request(scenario)
else:
raise ValueError(f"Unsupported output_modality: {self.output_modality}")
3. Add Task Support in the User Class¶
Each User corresponds to one API backend, such as OpenAIUser for OpenAI. Users can have multiple tasks, each corresponding to an endpoint.
Steps¶
- Add the new task to the
supported_tasksdictionary in the relevantUserclass. - Map the new task to its corresponding function name in the dictionary.
- Implement the new function in the
Userclass for handling the task logic. - If the new task uses an existing endpoint, refactor the function to support both tasks without duplicating logic.
- Important: Avoid creating multiple functions for tasks that use the same endpoint.
Example¶
class OpenAIUser(BaseUser):
supported_tasks = {
"text-to-text": "chat",
"image-text-to-text": "chat",
"text-to-embeddings": "embeddings",
"audio-to-text": "audio_to_text", # New task added
}
def audio_to_text(self):
# Implement the logic for audio-to-text task
endpoint = "/v1/audio/transcriptions"
user_request = self.sample()
# Add payload and send request
payload = {"audio": user_request.audio_file}
self.send_request(False, endpoint, payload, self.parse_audio_response)
4. Add Unit Tests¶
Steps¶
- Add tests for the new task in the appropriate test files.
- Include tests for:
- Request creation in the sampler.
- Task validation in the
Userclass. - End-to-end workflow using the new task.
5. Update Documentation¶
Steps¶
- Add the new task to the list of supported tasks in the Task Definition guide.
- Provide sample commands and explain any required configuration changes.
- Mention the new task in this development guide for future developers.