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 | import json |
| #2 | import logging |
| #3 | import os |
| #4 | from typing import Dict, List, Optional, Union |
| #5 | |
| #6 | from openai import OpenAI |
| #7 | |
| #8 | from mem0.configs.llms.base import BaseLlmConfig |
| #9 | from mem0.configs.llms.openai import OpenAIConfig |
| #10 | from mem0.llms.base import LLMBase |
| #11 | from mem0.memory.utils import extract_json |
| #12 | |
| #13 | |
| #14 | class OpenAILLM(LLMBase): |
| #15 | def __init__(self, config: Optional[Union[BaseLlmConfig, OpenAIConfig, Dict]] = None): |
| #16 | # Convert to OpenAIConfig if needed |
| #17 | if config is None: |
| #18 | config = OpenAIConfig() |
| #19 | elif isinstance(config, dict): |
| #20 | config = OpenAIConfig(**config) |
| #21 | elif isinstance(config, BaseLlmConfig) and not isinstance(config, OpenAIConfig): |
| #22 | # Convert BaseLlmConfig to OpenAIConfig |
| #23 | config = OpenAIConfig( |
| #24 | model=config.model, |
| #25 | temperature=config.temperature, |
| #26 | api_key=config.api_key, |
| #27 | max_tokens=config.max_tokens, |
| #28 | top_p=config.top_p, |
| #29 | top_k=config.top_k, |
| #30 | enable_vision=config.enable_vision, |
| #31 | vision_details=config.vision_details, |
| #32 | http_client_proxies=config.http_client, |
| #33 | ) |
| #34 | |
| #35 | super().__init__(config) |
| #36 | |
| #37 | if not self.config.model: |
| #38 | self.config.model = "gpt-4.1-nano-2025-04-14" |
| #39 | |
| #40 | if os.environ.get("OPENROUTER_API_KEY"): # Use OpenRouter |
| #41 | self.client = OpenAI( |
| #42 | api_key=os.environ.get("OPENROUTER_API_KEY"), |
| #43 | base_url=self.config.openrouter_base_url |
| #44 | or os.getenv("OPENROUTER_API_BASE") |
| #45 | or "https://openrouter.ai/api/v1", |
| #46 | ) |
| #47 | else: |
| #48 | api_key = self.config.api_key or os.getenv("OPENAI_API_KEY") |
| #49 | base_url = self.config.openai_base_url or os.getenv("OPENAI_BASE_URL") or "https://api.openai.com/v1" |
| #50 | |
| #51 | self.client = OpenAI(api_key=api_key, base_url=base_url) |
| #52 | |
| #53 | def _parse_response(self, response, tools): |
| #54 | """ |
| #55 | Process the response based on whether tools are used or not. |
| #56 | |
| #57 | Args: |
| #58 | response: The raw response from API. |
| #59 | tools: The list of tools provided in the request. |
| #60 | |
| #61 | Returns: |
| #62 | str or dict: The processed response. |
| #63 | """ |
| #64 | if tools: |
| #65 | processed_response = { |
| #66 | "content": response.choices[0].message.content, |
| #67 | "tool_calls": [], |
| #68 | } |
| #69 | |
| #70 | if response.choices[0].message.tool_calls: |
| #71 | for tool_call in response.choices[0].message.tool_calls: |
| #72 | processed_response["tool_calls"].append( |
| #73 | { |
| #74 | "name": tool_call.function.name, |
| #75 | "arguments": json.loads(extract_json(tool_call.function.arguments)), |
| #76 | } |
| #77 | ) |
| #78 | |
| #79 | return processed_response |
| #80 | else: |
| #81 | return response.choices[0].message.content |
| #82 | |
| #83 | def generate_response( |
| #84 | self, |
| #85 | messages: List[Dict[str, str]], |
| #86 | response_format=None, |
| #87 | tools: Optional[List[Dict]] = None, |
| #88 | tool_choice: str = "auto", |
| #89 | **kwargs, |
| #90 | ): |
| #91 | """ |
| #92 | Generate a JSON response based on the given messages using OpenAI. |
| #93 | |
| #94 | Args: |
| #95 | messages (list): List of message dicts containing 'role' and 'content'. |
| #96 | response_format (str or object, optional): Format of the response. Defaults to "text". |
| #97 | tools (list, optional): List of tools that the model can call. Defaults to None. |
| #98 | tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| #99 | **kwargs: Additional OpenAI-specific parameters. |
| #100 | |
| #101 | Returns: |
| #102 | json: The generated response. |
| #103 | """ |
| #104 | params = self._get_supported_params(messages=messages, **kwargs) |
| #105 | |
| #106 | params.update({ |
| #107 | "model": self.config.model, |
| #108 | "messages": messages, |
| #109 | }) |
| #110 | |
| #111 | if os.getenv("OPENROUTER_API_KEY"): |
| #112 | openrouter_params = {} |
| #113 | if self.config.models: |
| #114 | openrouter_params["models"] = self.config.models |
| #115 | openrouter_params["route"] = self.config.route |
| #116 | params.pop("model") |
| #117 | |
| #118 | if self.config.site_url and self.config.app_name: |
| #119 | extra_headers = { |
| #120 | "HTTP-Referer": self.config.site_url, |
| #121 | "X-Title": self.config.app_name, |
| #122 | } |
| #123 | openrouter_params["extra_headers"] = extra_headers |
| #124 | |
| #125 | params.update(**openrouter_params) |
| #126 | |
| #127 | else: |
| #128 | openai_specific_generation_params = ["store"] |
| #129 | for param in openai_specific_generation_params: |
| #130 | if hasattr(self.config, param): |
| #131 | params[param] = getattr(self.config, param) |
| #132 | |
| #133 | if response_format: |
| #134 | params["response_format"] = response_format |
| #135 | if tools: # TODO: Remove tools if no issues found with new memory addition logic |
| #136 | params["tools"] = tools |
| #137 | params["tool_choice"] = tool_choice |
| #138 | response = self.client.chat.completions.create(**params) |
| #139 | parsed_response = self._parse_response(response, tools) |
| #140 | if self.config.response_callback: |
| #141 | try: |
| #142 | self.config.response_callback(self, response, params) |
| #143 | except Exception as e: |
| #144 | # Log error but don't propagate |
| #145 | logging.error(f"Error due to callback: {e}") |
| #146 | pass |
| #147 | return parsed_response |
| #148 |