|
| 1 | +import json |
| 2 | +from typing import Dict, List, Optional |
| 3 | + |
| 4 | +try: |
| 5 | + import litellm |
| 6 | +except ImportError: |
| 7 | + raise ImportError("The 'litellm' library is required. Please install it using 'pip install litellm'.") |
| 8 | + |
| 9 | +from mem0.configs.llms.base import BaseLlmConfig |
| 10 | +from mem0.llms.base import LLMBase |
| 11 | + |
| 12 | + |
| 13 | +class OckamLLM(LLMBase): |
| 14 | + def __init__(self, config: Optional[BaseLlmConfig] = None): |
| 15 | + super().__init__(config) |
| 16 | + |
| 17 | + if not self.config.ockam_model: |
| 18 | + raise ValueError("'ockam_model' is required for 'OckamLLM'.") |
| 19 | + |
| 20 | + def _parse_response(self, response, tools): |
| 21 | + """ |
| 22 | + Process the response based on whether tools are used or not. |
| 23 | +
|
| 24 | + Args: |
| 25 | + response: The raw response from API. |
| 26 | + tools: The list of tools provided in the request. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + str or dict: The processed response. |
| 30 | + """ |
| 31 | + if tools: |
| 32 | + processed_response = { |
| 33 | + "content": response.choices[0].message.content, |
| 34 | + "tool_calls": [], |
| 35 | + } |
| 36 | + |
| 37 | + if response.choices[0].message.tool_calls: |
| 38 | + for tool_call in response.choices[0].message.tool_calls: |
| 39 | + processed_response["tool_calls"].append( |
| 40 | + { |
| 41 | + "name": tool_call.function.name, |
| 42 | + "arguments": json.loads(tool_call.function.arguments), |
| 43 | + } |
| 44 | + ) |
| 45 | + |
| 46 | + return processed_response |
| 47 | + else: |
| 48 | + return response.choices[0].message.content |
| 49 | + |
| 50 | + async def generate_response( |
| 51 | + self, |
| 52 | + messages: List[Dict[str, str]], |
| 53 | + response_format=None, |
| 54 | + tools: Optional[List[Dict]] = None, |
| 55 | + tool_choice: str = "auto", |
| 56 | + ): |
| 57 | + """ |
| 58 | + Generate a response based on the given messages using Litellm. |
| 59 | +
|
| 60 | + Args: |
| 61 | + messages (list): List of message dicts containing 'role' and 'content'. |
| 62 | + response_format (str or object, optional): Format of the response. Defaults to "text". |
| 63 | + tools (list, optional): List of tools that the model can call. Defaults to None. |
| 64 | + tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| 65 | +
|
| 66 | + Returns: |
| 67 | + str: The generated response. |
| 68 | + """ |
| 69 | + |
| 70 | + # FIXME |
| 71 | + # if not litellm.supports_function_calling(self.config.model): |
| 72 | + # raise ValueError(f"Model '{self.config.model}' in litellm does not support function calling.") |
| 73 | + |
| 74 | + ockam_model = self.config.ockam_model |
| 75 | + |
| 76 | + params = { |
| 77 | + "model": ockam_model.name, |
| 78 | + "messages": messages, |
| 79 | + "temperature": self.config.temperature, |
| 80 | + "max_tokens": self.config.max_tokens, |
| 81 | + "top_p": self.config.top_p, |
| 82 | + } |
| 83 | + if response_format: |
| 84 | + params["response_format"] = response_format |
| 85 | + if tools: # TODO: Remove tools if no issues found with new memory addition logic |
| 86 | + params["tools"] = tools |
| 87 | + params["tool_choice"] = tool_choice |
| 88 | + |
| 89 | + router = ockam_model.router() |
| 90 | + kwargs = {**ockam_model.kwargs, **params} |
| 91 | + |
| 92 | + response = await router.acompletion(**kwargs) |
| 93 | + return self._parse_response(response, tools) |
0 commit comments