|
| 1 | +from ollama import Client, Options |
| 2 | + |
| 3 | +from .illm import ILlm |
| 4 | +from .._utils.constants import PROMPT_EMPTY_EXCEPTION, OLLAMA_CONFIG_REQUIRED |
| 5 | +from .._utils import logger |
| 6 | + |
| 7 | +log = logger.init_loggers("Ollama Client") |
| 8 | + |
| 9 | + |
| 10 | +class Ollama(ILlm): |
| 11 | + def __init__(self, model_config: dict, client_config=None, client: Client = None): |
| 12 | + """ |
| 13 | + Initialize the class with an optional config parameter. |
| 14 | +
|
| 15 | + Parameters: |
| 16 | + model_config (dict): The model configuration parameter. |
| 17 | + config (dict): The configuration parameter. |
| 18 | + client (Client): The client parameter. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + None |
| 22 | + """ |
| 23 | + self.client = client |
| 24 | + self.client_config = client_config |
| 25 | + self.model_config = model_config |
| 26 | + |
| 27 | + if self.client is not None: |
| 28 | + if self.client_config is not None: |
| 29 | + log.warning("Client object provided. Ignoring client_config parameter.") |
| 30 | + return |
| 31 | + |
| 32 | + if client_config is None: |
| 33 | + raise ValueError(OLLAMA_CONFIG_REQUIRED.format(type="Client")) |
| 34 | + |
| 35 | + if model_config is None: |
| 36 | + raise ValueError(OLLAMA_CONFIG_REQUIRED.format(type="Model")) |
| 37 | + |
| 38 | + if 'model' not in model_config: |
| 39 | + raise ValueError(OLLAMA_CONFIG_REQUIRED.format(type="Model name")) |
| 40 | + |
| 41 | + self.client = Client(**client_config) |
| 42 | + |
| 43 | + def system_message(self, message: str) -> any: |
| 44 | + """ |
| 45 | + Create a system message. |
| 46 | +
|
| 47 | + Parameters: |
| 48 | + message (str): The message parameter. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + any |
| 52 | + """ |
| 53 | + return {"role": "system", "content": message} |
| 54 | + |
| 55 | + def user_message(self, message: str) -> any: |
| 56 | + """ |
| 57 | + Create a user message. |
| 58 | +
|
| 59 | + Parameters: |
| 60 | + message (str): The message parameter. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + any |
| 64 | + """ |
| 65 | + return {"role": "user", "content": message} |
| 66 | + |
| 67 | + def assistant_message(self, message: str) -> any: |
| 68 | + """ |
| 69 | + Create an assistant message. |
| 70 | +
|
| 71 | + Parameters: |
| 72 | + message (str): The message parameter. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + any |
| 76 | + """ |
| 77 | + return {"role": "assistant", "content": message} |
| 78 | + |
| 79 | + def invoke(self, prompt, **kwargs) -> str: |
| 80 | + """ |
| 81 | + Submit a prompt to the model for generating a response. |
| 82 | +
|
| 83 | + Parameters: |
| 84 | + prompt (str): The prompt parameter. |
| 85 | + **kwargs: Additional keyword arguments (optional). |
| 86 | + - temperature (float): The temperature parameter for controlling randomness in generation. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + str |
| 90 | + """ |
| 91 | + if not prompt: |
| 92 | + raise ValueError(PROMPT_EMPTY_EXCEPTION) |
| 93 | + |
| 94 | + model = self.model_config.get('model') |
| 95 | + temperature = kwargs.get('temperature', 0.1) |
| 96 | + |
| 97 | + response = self.client.chat( |
| 98 | + model=model, |
| 99 | + messages=[self.user_message(prompt)], |
| 100 | + options=Options( |
| 101 | + temperature=temperature |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + return response['message']['content'] |
0 commit comments