🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
lmstudio integration (#2193)
JINO ROHIT97e53197de4f
diff --git a/.env b/.envindex 3a5f4facd..f44cea2a2 100644--- a/.env+++ b/.env@@ -104,7 +104,8 @@# OPENBB_TOKEN="Fill your API key here"# AWS API (https://aws.amazon.com/)-# AWS_ACCESS_KEY_ID="Fill your Access Key ID here"-# AWS_SECRET_ACCESS_KEY="Fill your Secret Access Key here"+# BEDROCK_API_BASE_URL="Fill your Access Key ID here"+# BEDROCK_API_KEY="Fill your Secret Access Key here"+# Bocha Platform API(https://open.bochaai.com)# BOCHA_API_KEY="Fill your API key here"\ No newline at end of filediff --git a/camel/configs/__init__.py b/camel/configs/__init__.pyindex b0371ce9a..44a1b0260 100644--- a/camel/configs/__init__.py+++ b/camel/configs/__init__.py@@ -21,6 +21,7 @@ from .gemini_config import Gemini_API_PARAMS, GeminiConfigfrom .groq_config import GROQ_API_PARAMS, GroqConfigfrom .internlm_config import INTERNLM_API_PARAMS, InternLMConfigfrom .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig+from .lmstudio_config import LMSTUDIO_API_PARAMS, LMStudioConfigfrom .mistral_config import MISTRAL_API_PARAMS, MistralConfigfrom .modelscope_config import MODELSCOPE_API_PARAMS, ModelScopeConfigfrom .moonshot_config import MOONSHOT_API_PARAMS, MoonshotConfig@@ -100,4 +101,6 @@ __all__ = ['AIML_API_PARAMS','OpenRouterConfig','OPENROUTER_API_PARAMS',+ 'LMSTUDIO_API_PARAMS',+ 'LMStudioConfig',]diff --git a/camel/configs/lmstudio_config.py b/camel/configs/lmstudio_config.pynew file mode 100644index 000000000..6f65eac59--- /dev/null+++ b/camel/configs/lmstudio_config.py@@ -0,0 +1,101 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from __future__ import annotations++from typing import Optional, Sequence, Union++from camel.configs.base_config import BaseConfig+++class LMStudioConfig(BaseConfig):+ r"""Defines the parameters for generating chat completions using OpenAI+ compatibility.++ Args:+ temperature (float, optional): Sampling temperature to use, between+ :obj:`0` and :obj:`2`. Higher values make the output more random,+ while lower values make it more focused and deterministic.+ (default: :obj:`None`)+ top_p (float, optional): An alternative to sampling with temperature,+ called nucleus sampling, where the model considers the results of+ the tokens with top_p probability mass. So :obj:`0.1` means only+ the tokens comprising the top 10% probability mass are considered.+ (default: :obj:`None`)+ n (int, optional): How many chat completion choices to generate for+ each input message. (default: :obj:`None`)+ response_format (object, optional): An object specifying the format+ that the model must output. Compatible with GPT-4 Turbo and all+ GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to+ {"type": "json_object"} enables JSON mode, which guarantees the+ message the model generates is valid JSON. Important: when using+ JSON mode, you must also instruct the model to produce JSON+ yourself via a system or user message. Without this, the model+ may generate an unending stream of whitespace until the generation+ reaches the token limit, resulting in a long-running and seemingly+ "stuck" request. Also note that the message content may be+ partially cut off if finish_reason="length", which indicates the+ generation exceeded max_tokens or the conversation exceeded the+ max context length.+ stream (bool, optional): If True, partial message deltas will be sent+ as data-only server-sent events as they become available.+ (default: :obj:`None`)+ stop (str or list, optional): Up to :obj:`4` sequences where the API+ will stop generating further tokens. (default: :obj:`None`)+ max_tokens (int, optional): The maximum number of tokens to generate+ in the chat completion. The total length of input tokens and+ generated tokens is limited by the model's context length.+ (default: :obj:`None`)+ presence_penalty (float, optional): Number between :obj:`-2.0` and+ :obj:`2.0`. Positive values penalize new tokens based on whether+ they appear in the text so far, increasing the model's likelihood+ to talk about new topics. See more information about frequency and+ presence penalties. (default: :obj:`None`)+ frequency_penalty (float, optional): Number between :obj:`-2.0` and+ :obj:`2.0`. Positive values penalize new tokens based on their+ existing frequency in the text so far, decreasing the model's+ likelihood to repeat the same line verbatim. See more information+ about frequency and presence penalties. (default: :obj:`None`)+ user (str, optional): A unique identifier representing your end-user,+ which can help OpenAI to monitor and detect abuse.+ (default: :obj:`None`)+ tools (list[FunctionTool], optional): A list of tools the model may+ call. Currently, only functions are supported as a tool. Use this+ to provide a list of functions the model may generate JSON inputs+ for. A max of 128 functions are supported.+ tool_choice (Union[dict[str, str], str], optional): Controls which (if+ any) tool is called by the model. :obj:`"none"` means the model+ will not call any tool and instead generates a message.+ :obj:`"auto"` means the model can pick between generating a+ message or calling one or more tools. :obj:`"required"` means the+ model must call one or more tools. Specifying a particular tool+ via {"type": "function", "function": {"name": "my_function"}}+ forces the model to call that tool. :obj:`"none"` is the default+ when no tools are present. :obj:`"auto"` is the default if tools+ are present.+ """++ temperature: Optional[float] = None+ top_p: Optional[float] = None+ n: Optional[int] = None+ stream: Optional[bool] = None+ stop: Optional[Union[str, Sequence[str]]] = None+ max_tokens: Optional[int] = None+ presence_penalty: Optional[float] = None+ response_format: Optional[dict] = None+ frequency_penalty: Optional[float] = None+ user: Optional[str] = None+ tool_choice: Optional[Union[dict[str, str], str]] = None+++LMSTUDIO_API_PARAMS = {param for param in LMStudioConfig.model_fields.keys()}diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex 86236415d..aa5b71402 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -24,6 +24,7 @@ from .gemini_model import GeminiModelfrom .groq_model import GroqModelfrom .internlm_model import InternLMModelfrom .litellm_model import LiteLLMModel+from .lmstudio_model import LMStudioModelfrom .mistral_model import MistralModelfrom .model_factory import ModelFactoryfrom .model_manager import ModelManager, ModelProcessingError@@ -88,4 +89,5 @@ __all__ = ['BaseAudioModel','SiliconFlowModel','VolcanoModel',+ 'LMStudioModel',]diff --git a/camel/models/lmstudio_model.py b/camel/models/lmstudio_model.pynew file mode 100644index 000000000..8f7472bf2--- /dev/null+++ b/camel/models/lmstudio_model.py@@ -0,0 +1,81 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import os+from typing import Any, Dict, Optional, Union++from camel.configs import LMSTUDIO_API_PARAMS, LMStudioConfig+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelType+from camel.utils import BaseTokenCounter+++class LMStudioModel(OpenAICompatibleModel):+ r"""LLM served by LMStudio in a unified OpenAICompatibleModel interface.++ Args:+ model_type (Union[ModelType, str]): Model for which a backend is+ created.+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary+ that will be fed into:obj:`openai.ChatCompletion.create()`.+ If:obj:`None`, :obj:`LMStudioConfig().as_dict()` will be used.+ (default: :obj:`None`)+ api_key (Optional[str], optional): The API key for authenticating+ with the LMStudio service. (default: :obj:`None`).+ url (Optional[str], optional): The url to the LMStudio service.+ (default: :obj:`None`)+ token_counter (Optional[BaseTokenCounter], optional): Token counter to+ use for the model. If not provided, :obj:`OpenAITokenCounter(+ ModelType.GPT_4O_MINI)` will be used.+ (default: :obj:`None`)+ timeout (Optional[float], optional): The timeout value in seconds for+ API calls. If not provided, will fall back to the MODEL_TIMEOUT+ environment variable or default to 180 seconds.+ (default: :obj:`None`)+ """++ def __init__(+ self,+ model_type: Union[ModelType, str],+ model_config_dict: Optional[Dict[str, Any]] = None,+ api_key: Optional[str] = None,+ url: Optional[str] = None,+ token_counter: Optional[BaseTokenCounter] = None,+ timeout: Optional[float] = None,+ ) -> None:+ if model_config_dict is None:+ model_config_dict = LMStudioConfig().as_dict()+ api_key = "NA"+ url = url or os.environ.get(+ "LMSTUDIO_API_BASE_URL", "http://localhost:1234/v1"+ )+ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))+ super().__init__(+ model_type, model_config_dict, api_key, url, token_counter, timeout+ )++ def check_model_config(self):+ r"""Check whether the model configuration contains any unexpected+ arguments to LMStudio API. But LMStudio API does not have any+ additional arguments to check.++ Raises:+ ValueError: If the model configuration dictionary contains any+ unexpected arguments to LMStudio API.+ """+ for param in self.model_config_dict:+ if param not in LMSTUDIO_API_PARAMS:+ raise ValueError(+ f"Unexpected argument `{param}` is "+ "input into LMStudio model backend."+ )diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex aa6d6d24b..d8f97c89f 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -27,6 +27,7 @@ from camel.models.gemini_model import GeminiModelfrom camel.models.groq_model import GroqModelfrom camel.models.internlm_model import InternLMModelfrom camel.models.litellm_model import LiteLLMModel+from camel.models.lmstudio_model import LMStudioModelfrom camel.models.mistral_model import MistralModelfrom camel.models.modelscope_model import ModelScopeModelfrom camel.models.moonshot_model import MoonshotModel@@ -131,6 +132,8 @@ class ModelFactory:model_class = AnthropicModelelif model_platform.is_groq and model_type.is_groq:model_class = GroqModel+ elif model_platform.is_lmstudio and model_type.is_lmstudio:+ model_class = LMStudioModelelif model_platform.is_openrouter and model_type.is_openrouter:model_class = OpenRouterModelelif model_platform.is_zhipuai and model_type.is_zhipuai:diff --git a/camel/types/enums.py b/camel/types/enums.pyindex 3aa124498..377a8c5fd 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -84,6 +84,12 @@ class ModelType(UnifiedModelType, Enum):OPENROUTER_LLAMA_4_SCOUT_FREE = "meta-llama/llama-4-scout:free"OPENROUTER_OLYMPICODER_7B = "open-r1/olympiccoder-7b:free"+ # LMStudio models+ LMSTUDIO_GEMMA_3_1B = "gemma-3-1b"+ LMSTUDIO_GEMMA_3_4B = "gemma-3-4b"+ LMSTUDIO_GEMMA_3_12B = "gemma-3-12b"+ LMSTUDIO_GEMMA_3_27B = "gemma-3-27b"+# TogetherAI platform models support tool callingTOGETHER_LLAMA_3_1_8B = "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"TOGETHER_LLAMA_3_1_70B = "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"@@ -317,6 +323,7 @@ class ModelType(UnifiedModelType, Enum):self.is_sambanova,self.is_groq,self.is_openrouter,+ self.is_lmstudio,self.is_sglang,self.is_moonshot,self.is_siliconflow,@@ -437,6 +444,16 @@ class ModelType(UnifiedModelType, Enum):ModelType.OPENROUTER_OLYMPICODER_7B,}+ @property+ def is_lmstudio(self) -> bool:+ r"""Returns whether this type of models is served by LMStudio."""+ return self in {+ ModelType.LMSTUDIO_GEMMA_3_1B,+ ModelType.LMSTUDIO_GEMMA_3_4B,+ ModelType.LMSTUDIO_GEMMA_3_12B,+ ModelType.LMSTUDIO_GEMMA_3_27B,+ }+@propertydef is_together(self) -> bool:r"""Returns whether this type of models is served by Together AI."""@@ -713,6 +730,10 @@ class ModelType(UnifiedModelType, Enum):ModelType.GLM_4V_FLASH,ModelType.GLM_4_AIRX,ModelType.OPENROUTER_OLYMPICODER_7B,+ ModelType.LMSTUDIO_GEMMA_3_1B,+ ModelType.LMSTUDIO_GEMMA_3_4B,+ ModelType.LMSTUDIO_GEMMA_3_12B,+ ModelType.LMSTUDIO_GEMMA_3_27B,}:return 8_192elif self in {@@ -1072,6 +1093,7 @@ class ModelPlatformType(Enum):OPENROUTER = "openrouter"OLLAMA = "ollama"LITELLM = "litellm"+ LMSTUDIO = "lmstudio"ZHIPU = "zhipuai"GEMINI = "gemini"VLLM = "vllm"@@ -1132,6 +1154,11 @@ class ModelPlatformType(Enum):r"""Returns whether this platform is openrouter."""return self is ModelPlatformType.OPENROUTER+ @property+ def is_lmstudio(self) -> bool:+ r"""Returns whether this platform is lmstudio."""+ return self is ModelPlatformType.LMSTUDIO+@propertydef is_ollama(self) -> bool:r"""Returns whether this platform is ollama."""diff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.pyindex 21ca20f38..dffc4ecbd 100644--- a/camel/types/unified_model_type.py+++ b/camel/types/unified_model_type.py@@ -88,6 +88,11 @@ class UnifiedModelType(str):r"""Returns whether the model is a OpenRouter served model."""return True+ @property+ def is_lmstudio(self) -> bool:+ r"""Returns whether the model is a LMStudio served model."""+ return True+@propertydef is_ppio(self) -> bool:r"""Returns whether the model is a PPIO served model."""diff --git a/examples/models/lmstudio_model_example.py b/examples/models/lmstudio_model_example.pynew file mode 100644index 000000000..74e78a35d--- /dev/null+++ b/examples/models/lmstudio_model_example.py@@ -0,0 +1,48 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++from camel.agents import ChatAgent+from camel.configs import LMStudioConfig+from camel.models import ModelFactory+from camel.types import ModelPlatformType, ModelType++model = ModelFactory.create(+ model_platform=ModelPlatformType.LMSTUDIO,+ model_type=ModelType.LMSTUDIO_GEMMA_3_1B,+ model_config_dict=LMStudioConfig(temperature=0.2).as_dict(),+)++# Define system message+sys_msg = "You are a helpful assistant."++# Set agent+camel_agent = ChatAgent(system_message=sys_msg, model=model)++user_msg = """Say hi to CAMEL AI, one open-source community+ dedicated to the study of autonomous and communicative agents."""++# Get response information+response = camel_agent.step(user_msg)+print(response.msgs[0].content)++'''+===============================================================================+Hello to the CAMEL AI community. It's great to see a group of like-minded+individuals coming together to explore and advance the field of autonomous and+communicative agents. Your open-source approach is truly commendable, as it+fosters collaboration, innovation, and transparency. I'm excited to learn more+about your projects and initiatives, and I'm happy to help in any way I can.+Keep pushing the boundaries of AI research and development!+===============================================================================+'''diff --git a/test/models/test_aws_bedrock_model.py b/test/models/test_aws_bedrock_model.pyindex aa30d7936..ed01ce609 100644--- a/test/models/test_aws_bedrock_model.py+++ b/test/models/test_aws_bedrock_model.py@@ -70,9 +70,9 @@ def test_aws_bedrock_model_unexpected_argument():async def test_aws_bedrock_async_not_implemented():"""Test AWSBedrockModel async method raising NotImplementedError."""model = AWSBedrockModel(ModelType.AWS_CLAUDE_3_HAIKU)-+with pytest.raises(NotImplementedError,- match="AWS Bedrock does not support async inference."+ match="AWS Bedrock does not support async inference.",):await model.agenerate("Test message")diff --git a/test/models/test_lmstudio_model.py b/test/models/test_lmstudio_model.pynew file mode 100644index 000000000..91e7ff76e--- /dev/null+++ b/test/models/test_lmstudio_model.py@@ -0,0 +1,61 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import re++import pytest++from camel.configs import LMStudioConfig+from camel.models import LMStudioModel+from camel.types import ModelType+from camel.utils import OpenAITokenCounter++[email protected]_backend+ "model_type",+ [+ ModelType.LMSTUDIO_GEMMA_3_1B,+ ModelType.LMSTUDIO_GEMMA_3_4B,+ ],+)+def test_lmstudio_model(model_type: ModelType):+ model = LMStudioModel(model_type)+ assert model.model_type == model_type+ assert model.model_config_dict == LMStudioConfig().as_dict()+ assert isinstance(model.token_counter, OpenAITokenCounter)+ assert isinstance(model.model_type.value_for_tiktoken, str)+ assert isinstance(model.model_type.token_limit, int)++[email protected]_backend+def test_lmstudio_model_unexpected_argument():+ model_type = ModelType.LMSTUDIO_GEMMA_3_1B+ model_config_dict = {"model_path": "vicuna_70B"}++ with pytest.raises(+ ValueError,+ match=re.escape(+ (+ "Unexpected argument `model_path` is "+ "input into LMStudio model backend."+ )+ ),+ ):+ _ = LMStudioModel(model_type, model_config_dict)++[email protected]_backend+def test_lmstudio_model_stream_property():+ model = LMStudioModel(ModelType.LMSTUDIO_GEMMA_3_1B)+ assert model.stream is False