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 | from typing import Dict, List, Optional, Union |
| #3 | |
| #4 | from openai import OpenAI |
| #5 | |
| #6 | from mem0.configs.llms.base import BaseLlmConfig |
| #7 | from mem0.configs.llms.lmstudio import LMStudioConfig |
| #8 | from mem0.llms.base import LLMBase |
| #9 | from mem0.memory.utils import extract_json |
| #10 | |
| #11 | |
| #12 | class LMStudioLLM(LLMBase): |
| #13 | def __init__(self, config: Optional[Union[BaseLlmConfig, LMStudioConfig, Dict]] = None): |
| #14 | # Convert to LMStudioConfig if needed |
| #15 | if config is None: |
| #16 | config = LMStudioConfig() |
| #17 | elif isinstance(config, dict): |
| #18 | config = LMStudioConfig(**config) |
| #19 | elif isinstance(config, BaseLlmConfig) and not isinstance(config, LMStudioConfig): |
| #20 | # Convert BaseLlmConfig to LMStudioConfig |
| #21 | config = LMStudioConfig( |
| #22 | model=config.model, |
| #23 | temperature=config.temperature, |
| #24 | api_key=config.api_key, |
| #25 | max_tokens=config.max_tokens, |
| #26 | top_p=config.top_p, |
| #27 | top_k=config.top_k, |
| #28 | enable_vision=config.enable_vision, |
| #29 | vision_details=config.vision_details, |
| #30 | http_client_proxies=config.http_client, |
| #31 | ) |
| #32 | |
| #33 | super().__init__(config) |
| #34 | |
| #35 | self.config.model = ( |
| #36 | self.config.model |
| #37 | or "lmstudio-community/Meta-Llama-3.1-70B-Instruct-GGUF/Meta-Llama-3.1-70B-Instruct-IQ2_M.gguf" |
| #38 | ) |
| #39 | self.config.api_key = self.config.api_key or "lm-studio" |
| #40 | |
| #41 | self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key) |
| #42 | |
| #43 | def _parse_response(self, response, tools): |
| #44 | """ |
| #45 | Process the response based on whether tools are used or not. |
| #46 | |
| #47 | Args: |
| #48 | response: The raw response from API. |
| #49 | tools: The list of tools provided in the request. |
| #50 | |
| #51 | Returns: |
| #52 | str or dict: The processed response. |
| #53 | """ |
| #54 | if tools: |
| #55 | processed_response = { |
| #56 | "content": response.choices[0].message.content, |
| #57 | "tool_calls": [], |
| #58 | } |
| #59 | |
| #60 | if response.choices[0].message.tool_calls: |
| #61 | for tool_call in response.choices[0].message.tool_calls: |
| #62 | processed_response["tool_calls"].append( |
| #63 | { |
| #64 | "name": tool_call.function.name, |
| #65 | "arguments": json.loads(extract_json(tool_call.function.arguments)), |
| #66 | } |
| #67 | ) |
| #68 | |
| #69 | return processed_response |
| #70 | else: |
| #71 | return response.choices[0].message.content |
| #72 | |
| #73 | def generate_response( |
| #74 | self, |
| #75 | messages: List[Dict[str, str]], |
| #76 | response_format=None, |
| #77 | tools: Optional[List[Dict]] = None, |
| #78 | tool_choice: str = "auto", |
| #79 | **kwargs, |
| #80 | ): |
| #81 | """ |
| #82 | Generate a response based on the given messages using LM Studio. |
| #83 | |
| #84 | Args: |
| #85 | messages (list): List of message dicts containing 'role' and 'content'. |
| #86 | response_format (str or object, optional): Format of the response. Defaults to "text". |
| #87 | tools (list, optional): List of tools that the model can call. Defaults to None. |
| #88 | tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| #89 | **kwargs: Additional LM Studio-specific parameters. |
| #90 | |
| #91 | Returns: |
| #92 | str: The generated response. |
| #93 | """ |
| #94 | params = self._get_supported_params(messages=messages, **kwargs) |
| #95 | params.update( |
| #96 | { |
| #97 | "model": self.config.model, |
| #98 | "messages": messages, |
| #99 | } |
| #100 | ) |
| #101 | |
| #102 | if self.config.lmstudio_response_format: |
| #103 | params["response_format"] = self.config.lmstudio_response_format |
| #104 | elif response_format: |
| #105 | params["response_format"] = response_format |
| #106 | else: |
| #107 | params["response_format"] = {"type": "json_object"} |
| #108 | |
| #109 | if tools: |
| #110 | params["tools"] = tools |
| #111 | params["tool_choice"] = tool_choice |
| #112 | |
| #113 | response = self.client.chat.completions.create(**params) |
| #114 | return self._parse_response(response, tools) |
| #115 |