š« 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
feat: Integrate together ai (#843)
Wendong-Fan9214a65da485
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.pyindex a4eb67185..00182606b 100644--- a/camel/configs/__init__.py+++ b/camel/configs/__init__.py@@ -19,6 +19,7 @@ from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfigfrom .mistral_config import MISTRAL_API_PARAMS, MistralConfigfrom .ollama_config import OLLAMA_API_PARAMS, OllamaConfigfrom .openai_config import OPENAI_API_PARAMS, ChatGPTConfig, OpenSourceConfig+from .togetherai_config import TOGETHERAI_API_PARAMS, TogetherAIConfigfrom .vllm_config import VLLM_API_PARAMS, VLLMConfigfrom .zhipuai_config import ZHIPUAI_API_PARAMS, ZhipuAIConfig@@ -43,4 +44,6 @@ __all__ = ['VLLM_API_PARAMS','MistralConfig','MISTRAL_API_PARAMS',+ 'TogetherAIConfig',+ 'TOGETHERAI_API_PARAMS',]diff --git a/camel/configs/togetherai_config.py b/camel/configs/togetherai_config.pynew file mode 100644index 000000000..6162f9252--- /dev/null+++ b/camel/configs/togetherai_config.py@@ -0,0 +1,106 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+from __future__ import annotations++from typing import Any, Sequence, Union++from openai._types import NOT_GIVEN, NotGiven+from pydantic import Field++from camel.configs.base_config import BaseConfig+++class TogetherAIConfig(BaseConfig):+ r"""Defines the parameters for generating chat completions using the+ OpenAI API.++ 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:`0.2`)+ 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:`1.0`)+ n (int, optional): How many chat completion choices to generate for+ each input message. (default: :obj:`1`)+ 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:`False`)+ 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:`0.0`)+ 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:`0.0`)+ logit_bias (dict, optional): Modify the likelihood of specified tokens+ appearing in the completion. Accepts a json object that maps tokens+ (specified by their token ID in the tokenizer) to an associated+ bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias+ is added to the logits generated by the model prior to sampling.+ The exact effect will vary per model, but values between:obj:` -1`+ and :obj:`1` should decrease or increase likelihood of selection;+ values like :obj:`-100` or :obj:`100` should result in a ban or+ exclusive selection of the relevant token. (default: :obj:`{}`)+ user (str, optional): A unique identifier representing your end-user,+ which can help OpenAI to monitor and detect abuse.+ (default: :obj:`""`)+ """++ temperature: float = 0.2 # openai default: 1.0+ top_p: float = 1.0+ n: int = 1+ stream: bool = False+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN+ max_tokens: Union[int, NotGiven] = NOT_GIVEN+ presence_penalty: float = 0.0+ response_format: Union[dict, NotGiven] = NOT_GIVEN+ frequency_penalty: float = 0.0+ logit_bias: dict = Field(default_factory=dict)+ user: str = ""++ def as_dict(self) -> dict[str, Any]:+ config_dict = super().as_dict()+ config_dict.pop("tools", None) # Tool calling is not support+ return config_dict+++TOGETHERAI_API_PARAMS = {+ param for param in TogetherAIConfig.model_fields.keys()+}diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex eed3cd2dc..fc85db71a 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -27,6 +27,7 @@ from .openai_audio_models import OpenAIAudioModelsfrom .openai_compatibility_model import OpenAICompatibilityModelfrom .openai_model import OpenAIModelfrom .stub_model import StubModel+from .togetherai_model import TogetherAIModelfrom .vllm_model import VLLMModelfrom .zhipuai_model import ZhipuAIModel@@ -49,4 +50,5 @@ __all__ = ['GeminiModel','InternLMXComposerModel','OpenAICompatibilityModel',+ 'TogetherAIModel',]diff --git a/camel/models/groq_model.py b/camel/models/groq_model.pyindex 97c2eabdf..509f3b21d 100644--- a/camel/models/groq_model.py+++ b/camel/models/groq_model.py@@ -54,7 +54,7 @@ class GroqModel(BaseModelBackend)::obj:`None`)token_counter (Optional[BaseTokenCounter]): Token counter to usefor the model. If not provided, `OpenAITokenCounter(ModelType.- GPT_3_5_TURBO)` will be used.+ GPT_4O_MINI)` will be used."""super().__init__(model_type, model_config_dict, api_key, url, token_counter@@ -80,7 +80,7 @@ class GroqModel(BaseModelBackend):# Make sure you have the access to these open-source model in# HuggingFaceif not self._token_counter:- self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)return self._token_counter@api_keys_required("GROQ_API_KEY")diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.pyindex 8d62e7f6d..f94899756 100644--- a/camel/models/litellm_model.py+++ b/camel/models/litellm_model.py@@ -147,7 +147,7 @@ class LiteLLMModel:@propertydef token_limit(self) -> int:- """Returns the maximum token limit for the given model.+ r"""Returns the maximum token limit for the given model.Returns:int: The maximum token limit for the given model.diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 9756a0125..3bb360f81 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -26,6 +26,7 @@ from camel.models.open_source_model import OpenSourceModelfrom camel.models.openai_compatibility_model import OpenAICompatibilityModelfrom camel.models.openai_model import OpenAIModelfrom camel.models.stub_model import StubModel+from camel.models.togetherai_model import TogetherAIModelfrom camel.models.vllm_model import VLLMModelfrom camel.models.zhipuai_model import ZhipuAIModelfrom camel.types import ModelPlatformType, ModelType@@ -114,6 +115,11 @@ class ModelFactory:return model_class(model_type, model_config_dict)elif model_platform.is_openai_compatibility_model:model_class = OpenAICompatibilityModel+ elif model_platform.is_together:+ model_class = TogetherAIModel+ return model_class(+ model_type, model_config_dict, api_key, token_counter+ )else:raise ValueError(f"Unknown pair of model platform `{model_platform}` "diff --git a/camel/models/ollama_model.py b/camel/models/ollama_model.pyindex 52b55025a..207f5ca54 100644--- a/camel/models/ollama_model.py+++ b/camel/models/ollama_model.py@@ -43,7 +43,7 @@ class OllamaModel::obj:`None`)token_counter (Optional[BaseTokenCounter]): Token counter to usefor the model. If not provided, `OpenAITokenCounter(ModelType.- GPT_3_5_TURBO)` will be used.+ GPT_4O_MINI)` will be used."""self.model_type = model_typeself.model_config_dict = model_config_dict@@ -66,7 +66,7 @@ class OllamaModel:tokenization style."""if not self._token_counter:- self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)return self._token_counterdef check_model_config(self):diff --git a/camel/models/openai_compatibility_model.py b/camel/models/openai_compatibility_model.pyindex d7ca16fe7..f31a66527 100644--- a/camel/models/openai_compatibility_model.py+++ b/camel/models/openai_compatibility_model.py@@ -38,7 +38,7 @@ class OpenAICompatibilityModel:r"""Constructor for model backend.Args:- model_type (ModelType): Model for which a backend is created.+ model_type (str): Model for which a backend is created.model_config_dict (Dict[str, Any]): A dictionary that willbe fed into openai.ChatCompletion.create().api_key (str): The API key for authenticating with the@@ -47,7 +47,7 @@ class OpenAICompatibilityModel::obj:`None`)token_counter (Optional[BaseTokenCounter]): Token counter to usefor the model. If not provided, `OpenAITokenCounter(ModelType.- GPT_3_5_TURBO)` will be used.+ GPT_4O_MINI)` will be used."""self.model_type = model_typeself.model_config_dict = model_config_dict@@ -91,7 +91,7 @@ class OpenAICompatibilityModel:"""if not self._token_counter:- self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)return self._token_counter@propertydiff --git a/camel/models/togetherai_model.py b/camel/models/togetherai_model.pynew file mode 100644index 000000000..2641fa486--- /dev/null+++ b/camel/models/togetherai_model.py@@ -0,0 +1,144 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========++import os+from typing import Any, Dict, List, Optional, Union++from openai import OpenAI, Stream++from camel.configs import TOGETHERAI_API_PARAMS+from camel.messages import OpenAIMessage+from camel.types import ChatCompletion, ChatCompletionChunk, ModelType+from camel.utils import (+ BaseTokenCounter,+ OpenAITokenCounter,+ api_keys_required,+)+++class TogetherAIModel:+ r"""Constructor for Together AI backend with OpenAI compatibility.+ TODO: Add function calling support+ """++ def __init__(+ self,+ model_type: str,+ model_config_dict: Dict[str, Any],+ api_key: Optional[str] = None,+ token_counter: Optional[BaseTokenCounter] = None,+ ) -> None:+ r"""Constructor for TogetherAI backend.++ Args:+ model_type (str): Model for which a backend is created, supported+ model can be found here: https://docs.together.ai/docs/chat-models+ model_config_dict (Dict[str, Any]): A dictionary that will+ be fed into openai.ChatCompletion.create().+ api_key (Optional[str]): The API key for authenticating with the+ Together service. (default: :obj:`None`)+ token_counter (Optional[BaseTokenCounter]): Token counter to use+ for the model. If not provided, `OpenAITokenCounter(ModelType.+ GPT_4O_MINI)` will be used.+ """+ self.model_type = model_type+ self.model_config_dict = model_config_dict+ self._token_counter = token_counter+ self._api_key = api_key or os.environ.get("TOGETHER_API_KEY")++ self._client = OpenAI(+ timeout=60,+ max_retries=3,+ api_key=self._api_key,+ base_url="https://api.together.xyz/v1",+ )++ @api_keys_required("TOGETHER_API_KEY")+ def run(+ self,+ messages: List[OpenAIMessage],+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ r"""Runs inference of OpenAI chat completion.++ Args:+ messages (List[OpenAIMessage]): Message list with the chat history+ in OpenAI API format.++ Returns:+ Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ `ChatCompletion` in the non-stream mode, or+ `Stream[ChatCompletionChunk]` in the stream mode.+ """+ # Use OpenAI cilent as interface call Together AI+ # Reference: https://docs.together.ai/docs/openai-api-compatibility+ response = self._client.chat.completions.create(+ messages=messages,+ model=self.model_type,+ **self.model_config_dict,+ )+ return response++ @property+ def token_counter(self) -> BaseTokenCounter:+ r"""Initialize the token counter for the model backend.++ Returns:+ OpenAITokenCounter: The token counter following the model's+ tokenization style.+ """++ if not self._token_counter:+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)+ return self._token_counter++ def check_model_config(self):+ r"""Check whether the model configuration contains any+ unexpected arguments to TogetherAI API.++ Raises:+ ValueError: If the model configuration dictionary contains any+ unexpected arguments to TogetherAI API.+ """+ for param in self.model_config_dict:+ if param not in TOGETHERAI_API_PARAMS:+ raise ValueError(+ f"Unexpected argument `{param}` is "+ "input into TogetherAI model backend."+ )++ @property+ def stream(self) -> bool:+ r"""Returns whether the model is in stream mode, which sends partial+ results each time.++ Returns:+ bool: Whether the model is in stream mode.+ """+ return self.model_config_dict.get('stream', False)++ @property+ def token_limit(self) -> int:+ r"""Returns the maximum token limit for the given model.++ Returns:+ int: The maximum token limit for the given model.+ """+ max_tokens = self.model_config_dict.get("max_tokens")+ if isinstance(max_tokens, int):+ return max_tokens+ print(+ "Must set `max_tokens` as an integer in `model_config_dict` when"+ " setting up the model. Using 4096 as default value."+ )+ return 4096diff --git a/camel/models/vllm_model.py b/camel/models/vllm_model.pyindex 111ef7379..93082586c 100644--- a/camel/models/vllm_model.py+++ b/camel/models/vllm_model.py@@ -47,7 +47,7 @@ class VLLMModel:model service.token_counter (Optional[BaseTokenCounter]): Token counter to usefor the model. If not provided, `OpenAITokenCounter(ModelType.- GPT_3_5_TURBO)` will be used.+ GPT_4O_MINI)` will be used."""self.model_type = model_typeself.model_config_dict = model_config_dict@@ -70,7 +70,7 @@ class VLLMModel:tokenization style."""if not self._token_counter:- self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)return self._token_counterdef check_model_config(self):diff --git a/camel/models/zhipuai_model.py b/camel/models/zhipuai_model.pyindex 5115d3c7e..a674fd3c9 100644--- a/camel/models/zhipuai_model.py+++ b/camel/models/zhipuai_model.py@@ -52,7 +52,7 @@ class ZhipuAIModel(BaseModelBackend)::obj:`None`)token_counter (Optional[BaseTokenCounter]): Token counter to usefor the model. If not provided, `OpenAITokenCounter(ModelType.- GPT_3_5_TURBO)` will be used.+ GPT_4O_MINI)` will be used."""super().__init__(model_type, model_config_dict, api_key, url, token_counter@@ -105,7 +105,7 @@ class ZhipuAIModel(BaseModelBackend):"""if not self._token_counter:- self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)return self._token_counterdef check_model_config(self):diff --git a/camel/types/enums.py b/camel/types/enums.pyindex 23b329a0b..2a3c64e6d 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -458,6 +458,7 @@ class ModelPlatformType(Enum):GEMINI = "gemini"VLLM = "vllm"MISTRAL = "mistral"+ TOGETHER = "together"OPENAI_COMPATIBILITY_MODEL = "openai-compatibility-model"INTERNLM = "internlm"@@ -491,6 +492,11 @@ class ModelPlatformType(Enum):r"""Returns whether this platform is vllm."""return self is ModelPlatformType.VLLM+ @property+ def is_together(self) -> bool:+ r"""Returns whether this platform is together."""+ return self is ModelPlatformType.TOGETHER+@propertydef is_litellm(self) -> bool:r"""Returns whether this platform is litellm."""diff --git a/examples/models/togetherai_model_example.py b/examples/models/togetherai_model_example.pynew file mode 100644index 000000000..67148883a--- /dev/null+++ b/examples/models/togetherai_model_example.py@@ -0,0 +1,56 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+from camel.agents import ChatAgent+from camel.configs import TogetherAIConfig+from camel.messages import BaseMessage+from camel.models import ModelFactory+from camel.types import ModelPlatformType++model = ModelFactory.create(+ model_platform=ModelPlatformType.TOGETHER,+ model_type="meta-llama/Llama-3-8b-chat-hf",+ model_config_dict=TogetherAIConfig(temperature=0.2).as_dict(),+)++# Define system message+sys_msg = BaseMessage.make_assistant_message(+ role_name="Assistant",+ content="You are a helpful assistant.",+)++# Set agent+camel_agent = ChatAgent(system_message=sys_msg, model=model, token_limit=500)++user_msg = BaseMessage.make_user_message(+ role_name="User",+ content="""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 CAMEL AI community!++I'm thrilled to be here and assist you with any questions or topics related to+autonomous and communicative agents. As an open-source community, I'm excited+to see the innovative projects and research being developed by your members.++What's on your mind? Do you have a specific question, project, or topic you'd+like to discuss? I'm here to help and provide any assistance I can. Let's get+started!+===============================================================================+'''diff --git a/test/models/test_togetherai_model.py b/test/models/test_togetherai_model.pynew file mode 100644index 000000000..aff6cc17d--- /dev/null+++ b/test/models/test_togetherai_model.py@@ -0,0 +1,33 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========++import pytest++from camel.configs import TogetherAIConfig+from camel.models import TogetherAIModel+from camel.utils import OpenAITokenCounter++[email protected]_backend+ "model_type",+ [+ "meta-llama/Llama-3-8b-chat-hf",+ ],+)+def test_litellm_model(model_type: str):+ model_config_dict = TogetherAIConfig().as_dict()+ model = TogetherAIModel(model_type, model_config_dict)+ assert model.model_config_dict == model_config_dict+ assert isinstance(model.token_counter, OpenAITokenCounter)diff --git a/test/retrievers/test_auto_retriever.py b/test/retrievers/test_auto_retriever.pyindex 21f04237c..bb995b8de 100644--- a/test/retrievers/test_auto_retriever.py+++ b/test/retrievers/test_auto_retriever.py@@ -69,32 +69,11 @@ def test_get_file_modified_date_from_file(auto_retriever):def test_run_vector_retriever(auto_retriever):# Define mock data for testing- query_related = "what is camel ai"query_unrealted = "unrelated query"contents = "https://www.camel-ai.org/"top_k = 1similarity_threshold = 0.5- # Test with query related to the content in mock data- result_related = auto_retriever.run_vector_retriever(- query_related,- contents,- top_k,- similarity_threshold,- return_detailed_info=True,- )-- assert "similarity score" in str(- result_related- ), "result_related missing 'similarity score'"- assert "content path" in str(- result_related- ), "result_related missing 'content path'"- assert "metadata" in str(- result_related- ), "result_related missing 'metadata'"- assert "text" in str(result_related), "result_related missing 'text'"-# Test with query unrelated to the content in mock dataresult_unrelated = auto_retriever.run_vector_retriever(query_unrealted, contents, top_k, similarity_thresholddiff --git a/test/storages/graph_storages/test_neo4j_graph.py b/test/storages/graph_storages/test_neo4j_graph.pyindex 241f7646f..17de206e8 100644--- a/test/storages/graph_storages/test_neo4j_graph.py+++ b/test/storages/graph_storages/test_neo4j_graph.py@@ -116,7 +116,7 @@ def test_cypher_return_correct_schema() -> None:def test_neo4j_timeout() -> None:r"""Test that neo4j uses the timeout correctly."""graph = Neo4jGraph(- url=url, username=username, password=password, timeout=0.1+ url=url, username=username, password=password, timeout=5)try:graph.query("UNWIND range(0,100000,1) AS i MERGE (:Foo {id:i})")