| |
|
|
| from abc import ABC, abstractmethod |
| from typing import List |
|
|
| class BaseModel(ABC): |
| """ |
| Abstract base class for all model implementations. |
| """ |
| def __init__(self, model_name: str, model_path: str, **kwargs): |
| """ |
| Initialize the model. |
| |
| :param model_path: Model path or API name (e.g., 'qwen-turbo', '/path/to/local/model') |
| :param kwargs: Other parameters such as api_key, temperature, etc. |
| """ |
| self.model_name = model_name |
| self.model_path = model_path |
| self.kwargs = kwargs |
|
|
| @abstractmethod |
| def load_model(self): |
| pass |
|
|
| @abstractmethod |
| def generate(self, prompt: str) -> str: |
| """ |
| Generate response for a single prompt. |
| |
| :param prompt: Input prompt string |
| :return: Text generated by the model |
| """ |
| pass |
| |
| @abstractmethod |
| def generate_batch(self, prompts: List[str]) -> List[str]: |
| """ |
| Generate responses for a batch of prompts (optional, but recommended for efficiency). |
| |
| :param prompts: List of input prompt strings |
| :return: List of texts generated by the model |
| """ |
| |
| return [self.generate(prompt) for prompt in prompts] |