repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | from abc import ABC, abstractmethod |
| #2 | from typing import Dict, List, Optional, Union |
| #3 | |
| #4 | from mem0.configs.llms.base import BaseLlmConfig |
| #5 | |
| #6 | |
| #7 | class LLMBase(ABC): |
| #8 | """ |
| #9 | Base class for all LLM providers. |
| #10 | Handles common functionality and delegates provider-specific logic to subclasses. |
| #11 | """ |
| #12 | |
| #13 | def __init__(self, config: Optional[Union[BaseLlmConfig, Dict]] = None): |
| #14 | """Initialize a base LLM class |
| #15 | |
| #16 | :param config: LLM configuration option class or dict, defaults to None |
| #17 | :type config: Optional[Union[BaseLlmConfig, Dict]], optional |
| #18 | """ |
| #19 | if config is None: |
| #20 | self.config = BaseLlmConfig() |
| #21 | elif isinstance(config, dict): |
| #22 | # Handle dict-based configuration (backward compatibility) |
| #23 | self.config = BaseLlmConfig(**config) |
| #24 | else: |
| #25 | self.config = config |
| #26 | |
| #27 | # Validate configuration |
| #28 | self._validate_config() |
| #29 | |
| #30 | def _validate_config(self): |
| #31 | """ |
| #32 | Validate the configuration. |
| #33 | Override in subclasses to add provider-specific validation. |
| #34 | """ |
| #35 | if not hasattr(self.config, "model"): |
| #36 | raise ValueError("Configuration must have a 'model' attribute") |
| #37 | |
| #38 | if not hasattr(self.config, "api_key") and not hasattr(self.config, "api_key"): |
| #39 | # Check if API key is available via environment variable |
| #40 | # This will be handled by individual providers |
| #41 | pass |
| #42 | |
| #43 | def _is_reasoning_model(self, model: str) -> bool: |
| #44 | """ |
| #45 | Check if the model is a reasoning model or GPT-5 series that doesn't support certain parameters. |
| #46 | |
| #47 | Args: |
| #48 | model: The model name to check |
| #49 | |
| #50 | Returns: |
| #51 | bool: True if the model is a reasoning model or GPT-5 series |
| #52 | """ |
| #53 | reasoning_models = { |
| #54 | "o1", "o1-preview", "o3-mini", "o3", |
| #55 | "gpt-5", "gpt-5o", "gpt-5o-mini", "gpt-5o-micro", |
| #56 | } |
| #57 | |
| #58 | if model.lower() in reasoning_models: |
| #59 | return True |
| #60 | |
| #61 | model_lower = model.lower() |
| #62 | if any(reasoning_model in model_lower for reasoning_model in ["gpt-5", "o1", "o3"]): |
| #63 | return True |
| #64 | |
| #65 | return False |
| #66 | |
| #67 | def _get_supported_params(self, **kwargs) -> Dict: |
| #68 | """ |
| #69 | Get parameters that are supported by the current model. |
| #70 | Filters out unsupported parameters for reasoning models and GPT-5 series. |
| #71 | |
| #72 | Args: |
| #73 | **kwargs: Additional parameters to include |
| #74 | |
| #75 | Returns: |
| #76 | Dict: Filtered parameters dictionary |
| #77 | """ |
| #78 | model = getattr(self.config, 'model', '') |
| #79 | |
| #80 | if self._is_reasoning_model(model): |
| #81 | supported_params = {} |
| #82 | |
| #83 | if "messages" in kwargs: |
| #84 | supported_params["messages"] = kwargs["messages"] |
| #85 | if "response_format" in kwargs: |
| #86 | supported_params["response_format"] = kwargs["response_format"] |
| #87 | if "tools" in kwargs: |
| #88 | supported_params["tools"] = kwargs["tools"] |
| #89 | if "tool_choice" in kwargs: |
| #90 | supported_params["tool_choice"] = kwargs["tool_choice"] |
| #91 | |
| #92 | return supported_params |
| #93 | else: |
| #94 | # For regular models, include all common parameters |
| #95 | return self._get_common_params(**kwargs) |
| #96 | |
| #97 | @abstractmethod |
| #98 | def generate_response( |
| #99 | self, messages: List[Dict[str, str]], tools: Optional[List[Dict]] = None, tool_choice: str = "auto", **kwargs |
| #100 | ): |
| #101 | """ |
| #102 | Generate a response based on the given messages. |
| #103 | |
| #104 | Args: |
| #105 | messages (list): List of message dicts containing 'role' and 'content'. |
| #106 | tools (list, optional): List of tools that the model can call. Defaults to None. |
| #107 | tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| #108 | **kwargs: Additional provider-specific parameters. |
| #109 | |
| #110 | Returns: |
| #111 | str or dict: The generated response. |
| #112 | """ |
| #113 | pass |
| #114 | |
| #115 | def _get_common_params(self, **kwargs) -> Dict: |
| #116 | """ |
| #117 | Get common parameters that most providers use. |
| #118 | |
| #119 | Returns: |
| #120 | Dict: Common parameters dictionary. |
| #121 | """ |
| #122 | params = { |
| #123 | "temperature": self.config.temperature, |
| #124 | "max_tokens": self.config.max_tokens, |
| #125 | "top_p": self.config.top_p, |
| #126 | } |
| #127 | |
| #128 | # Add provider-specific parameters from kwargs |
| #129 | params.update(kwargs) |
| #130 | |
| #131 | return params |
| #132 |