Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: integrate the llama3 (8B, 70B) served by Groq (#531)

Appointat714e3fa2aefb
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.yml
index 2469951d2..743a36b14 100644
--- a/.github/workflows/build_package.yml
+++ b/.github/workflows/build_package.yml
@@ -34,7 +34,9 @@ jobs:
SEARCH_ENGINE_ID: "${{ secrets.SEARCH_ENGINE_ID }}"
OPENWEATHERMAP_API_KEY: "${{ secrets.OPENWEATHERMAP_API_KEY }}"
ANTHROPIC_API_KEY: "${{ secrets.ANTHROPIC_API_KEY }}"
+ GROQ_API_KEY: "${{ secrets.GROQ_API_KEY }}"
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
+ HF_TOKEN: "${{ secrets.HF_TOKEN }}"
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}"
AZURE_API_VERSION: ${{ secrets.AZURE_API_VERSION }}"
AZURE_DEPLOYMENT_NAME: ${{ secrets.AZURE_DEPLOYMENT_NAME }}"
diff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.yml
index 825fadd6b..2b8eba5af 100644
--- a/.github/workflows/pytest_package.yml
+++ b/.github/workflows/pytest_package.yml
@@ -28,11 +28,13 @@ jobs:
SEARCH_ENGINE_ID: "${{ secrets.SEARCH_ENGINE_ID }}"
OPENWEATHERMAP_API_KEY: "${{ secrets.OPENWEATHERMAP_API_KEY }}"
ANTHROPIC_API_KEY: "${{ secrets.ANTHROPIC_API_KEY }}"
+ GROQ_API_KEY: "${{ secrets.GROQ_API_KEY }}"
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
NVIDIA_API_BASE_URL: "${{ secrets.NVIDIA_API_BASE_URL }}"
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
+ HF_TOKEN: "${{ secrets.HF_TOKEN }}"
AZURE_OPENAI_API_KEY: "to-be-filled"
AZURE_API_VERSION: "to-be-filled"
AZURE_DEPLOYMENT_NAME: "to-be-filled"
@@ -54,11 +56,13 @@ jobs:
SEARCH_ENGINE_ID: "${{ secrets.SEARCH_ENGINE_ID }}"
OPENWEATHERMAP_API_KEY: "${{ secrets.OPENWEATHERMAP_API_KEY }}"
ANTHROPIC_API_KEY: "${{ secrets.ANTHROPIC_API_KEY }}"
+ GROQ_API_KEY: "${{ secrets.GROQ_API_KEY }}"
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
NVIDIA_API_BASE_URL: "${{ secrets.NVIDIA_API_BASE_URL }}"
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
+ HF_TOKEN: "${{ secrets.HF_TOKEN }}"
AZURE_OPENAI_API_KEY: "to-be-filled"
AZURE_API_VERSION: "to-be-filled"
AZURE_DEPLOYMENT_NAME: "to-be-filled"
@@ -80,11 +84,13 @@ jobs:
SEARCH_ENGINE_ID: "${{ secrets.SEARCH_ENGINE_ID }}"
OPENWEATHERMAP_API_KEY: "${{ secrets.OPENWEATHERMAP_API_KEY }}"
ANTHROPIC_API_KEY: "${{ secrets.ANTHROPIC_API_KEY }}"
+ GROQ_API_KEY: "${{ secrets.GROQ_API_KEY }}"
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
NVIDIA_API_BASE_URL: "${{ secrets.NVIDIA_API_BASE_URL }}"
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
+ HF_TOKEN: "${{ secrets.HF_TOKEN }}"
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}"
AZURE_API_VERSION: ${{ secrets.AZURE_API_VERSION }}"
AZURE_DEPLOYMENT_NAME: ${{ secrets.AZURE_DEPLOYMENT_NAME }}"
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.py
index 30fdec55f..83641f1fb 100644
--- a/camel/configs/__init__.py
+++ b/camel/configs/__init__.py
@@ -17,6 +17,7 @@ from .gemini_config import (
Gemini_API_PARAMS,
GeminiConfig,
)
+from .groq_config import GROQ_API_PARAMS, GroqConfig
from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig
from .ollama_config import OLLAMA_API_PARAMS, OllamaConfig
from .openai_config import (
@@ -33,6 +34,8 @@ __all__ = [
'OPENAI_API_PARAMS',
'AnthropicConfig',
'ANTHROPIC_API_PARAMS',
+ 'GROQ_API_PARAMS',
+ 'GroqConfig',
'OpenSourceConfig',
'LiteLLMConfig',
'LITELLM_API_PARAMS',
diff --git a/camel/configs/groq_config.py b/camel/configs/groq_config.py
new file mode 100644
index 000000000..b5cab2b2a
--- /dev/null
+++ b/camel/configs/groq_config.py
@@ -0,0 +1,119 @@
+# =========== 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 dataclasses import asdict, dataclass
+from typing import TYPE_CHECKING, Optional, Sequence
+
+from openai._types import NOT_GIVEN, NotGiven
+
+from camel.configs.base_config import BaseConfig
+
+if TYPE_CHECKING:
+ from camel.toolkits import OpenAIFunction
+
+
+@dataclass(frozen=True)
+class GroqConfig(BaseConfig):
+ r"""Defines the parameters for generating chat completions using OpenAI
+ compatibility.
+
+ Reference: https://console.groq.com/docs/openai
+
+ 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`)
+ user (str, optional): A unique identifier representing your end-user,
+ which can help OpenAI to monitor and detect abuse.
+ (default: :obj:`""`)
+ tools (list[OpenAIFunction], 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: float = 0.2 # openai default: 1.0
+ top_p: float = 1.0
+ n: int = 1
+ stream: bool = False
+ stop: str | Sequence[str] | NotGiven = NOT_GIVEN
+ max_tokens: int | NotGiven = NOT_GIVEN
+ presence_penalty: float = 0.0
+ response_format: dict | NotGiven = NOT_GIVEN
+ frequency_penalty: float = 0.0
+ user: str = ""
+ tools: Optional[list[OpenAIFunction]] = None
+ tool_choice: Optional[dict[str, str] | str] = "none"
+
+ def __post_init__(self):
+ if self.tools is not None:
+ object.__setattr__(
+ self,
+ 'tools',
+ [tool.get_openai_tool_schema() for tool in self.tools],
+ )
+
+
+GROQ_API_PARAMS = {param for param in asdict(GroqConfig()).keys()}
diff --git a/camel/configs/ollama_config.py b/camel/configs/ollama_config.py
index 613e3f646..3e983e41d 100644
--- a/camel/configs/ollama_config.py
+++ b/camel/configs/ollama_config.py
@@ -24,7 +24,7 @@ from camel.configs.base_config import BaseConfig
@dataclass(frozen=True)
class OllamaConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using OpenAI
- compatibility
+ compatibility.
Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
diff --git a/camel/models/__init__.py b/camel/models/__init__.py
index b57158fab..e5a13491f 100644
--- a/camel/models/__init__.py
+++ b/camel/models/__init__.py
@@ -15,6 +15,7 @@ from .anthropic_model import AnthropicModel
from .azure_openai_model import AzureOpenAIModel
from .base_model import BaseModelBackend
from .gemini_model import GeminiModel
+from .groq_model import GroqModel
from .litellm_model import LiteLLMModel
from .model_factory import ModelFactory
from .nemotron_model import NemotronModel
@@ -31,6 +32,7 @@ __all__ = [
'OpenAIModel',
'AzureOpenAIModel',
'AnthropicModel',
+ 'GroqModel',
'StubModel',
'ZhipuAIModel',
'OpenSourceModel',
diff --git a/camel/models/groq_model.py b/camel/models/groq_model.py
new file mode 100644
index 000000000..97c2eabdf
--- /dev/null
+++ b/camel/models/groq_model.py
@@ -0,0 +1,131 @@
+# =========== 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 GROQ_API_PARAMS
+from camel.messages import OpenAIMessage
+from camel.models import BaseModelBackend
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
+from camel.utils import (
+ BaseTokenCounter,
+ OpenAITokenCounter,
+ api_keys_required,
+)
+
+
+class GroqModel(BaseModelBackend):
+ r"""LLM API served by Groq in a unified BaseModelBackend interface."""
+
+ def __init__(
+ self,
+ model_type: ModelType,
+ model_config_dict: Dict[str, Any],
+ api_key: Optional[str] = None,
+ url: Optional[str] = None,
+ token_counter: Optional[BaseTokenCounter] = None,
+ ) -> None:
+ r"""Constructor for Groq backend.
+
+ Args:
+ model_type (str): Model for which a backend is created.
+ model_config_dict (Dict[str, Any]): A dictionary of parameters for
+ the model configuration.
+ api_key (Optional[str]): The API key for authenticating with the
+ Groq service. (default: :obj:`None`).
+ url (Optional[str]): The url to the Groq service. (default:
+ :obj:`None`)
+ token_counter (Optional[BaseTokenCounter]): Token counter to use
+ for the model. If not provided, `OpenAITokenCounter(ModelType.
+ GPT_3_5_TURBO)` will be used.
+ """
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
+ )
+ self._url = url or "https://api.groq.com/openai/v1"
+ self._api_key = api_key or os.environ.get("GROQ_API_KEY")
+ self._client = OpenAI(
+ timeout=60,
+ max_retries=3,
+ api_key=self._api_key,
+ base_url=self._url,
+ )
+ self._token_counter = token_counter
+
+ @property
+ def token_counter(self) -> BaseTokenCounter:
+ r"""Initialize the token counter for the model backend.
+
+ Returns:
+ BaseTokenCounter: The token counter following the model's
+ tokenization style.
+ """
+ # Make sure you have the access to these open-source model in
+ # HuggingFace
+ if not self._token_counter:
+ self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)
+ return self._token_counter
+
+ @api_keys_required("GROQ_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.
+ """
+ response = self._client.chat.completions.create(
+ messages=messages,
+ model=self.model_type.value,
+ **self.model_config_dict,
+ )
+
+ return response
+
+ def check_model_config(self):
+ r"""Check whether the model configuration contains any unexpected
+ arguments to Groq API. But Groq API does not have any additional
+ arguments to check.
+
+ Raises:
+ ValueError: If the model configuration dictionary contains any
+ unexpected arguments to Groq API.
+ """
+ for param in self.model_config_dict:
+ if param not in GROQ_API_PARAMS:
+ raise ValueError(
+ f"Unexpected argument `{param}` is "
+ "input into Groq model backend."
+ )
+
+ @property
+ def stream(self) -> bool:
+ r"""Returns whether the model supports streaming. But Groq API does
+ not support streaming.
+ """
+ return False
diff --git a/camel/models/model_factory.py b/camel/models/model_factory.py
index a3daded23..4ed6cd91c 100644
--- a/camel/models/model_factory.py
+++ b/camel/models/model_factory.py
@@ -17,6 +17,7 @@ from camel.models.anthropic_model import AnthropicModel
from camel.models.azure_openai_model import AzureOpenAIModel
from camel.models.base_model import BaseModelBackend
from camel.models.gemini_model import GeminiModel
+from camel.models.groq_model import GroqModel
from camel.models.litellm_model import LiteLLMModel
from camel.models.ollama_model import OllamaModel
from camel.models.open_source_model import OpenSourceModel
@@ -80,6 +81,8 @@ class ModelFactory:
model_class = AzureOpenAIModel
elif model_platform.is_anthropic and model_type.is_anthropic:
model_class = AnthropicModel
+ elif model_type.is_groq:
+ model_class = GroqModel
elif model_platform.is_zhipuai and model_type.is_zhipuai:
model_class = ZhipuAIModel
elif model_platform.is_gemini and model_type.is_gemini:
diff --git a/camel/types/enums.py b/camel/types/enums.py
index 9cfeedee5..ebc5562b7 100644
--- a/camel/types/enums.py
+++ b/camel/types/enums.py
@@ -36,6 +36,15 @@ class ModelType(Enum):
GLM_4V = 'glm-4v'
GLM_3_TURBO = "glm-3-turbo"
+ GROQ_LLAMA_3_1_8B = "llama-3.1-8b-instant"
+ GROQ_LLAMA_3_1_70B = "llama-3.1-70b-versatile"
+ GROQ_LLAMA_3_1_405B = "llama-3.1-405b-reasoning"
+ GROQ_LLAMA_3_8B = "llama3-8b-8192"
+ GROQ_LLAMA_3_70B = "llama3-70b-8192"
+ GROQ_MIXTRAL_8_7B = "mixtral-8x7b-32768"
+ GROQ_GEMMA_7B_IT = "gemma-7b-it"
+ GROQ_GEMMA_2_9B_IT = "gemma2-9b-it"
+
STUB = "stub"
LLAMA_2 = "llama-2"
@@ -135,6 +144,20 @@ class ModelType(Enum):
ModelType.CLAUDE_3_5_SONNET,
}
+ @property
+ def is_groq(self) -> bool:
+ r"""Returns whether this type of models is served by Groq."""
+ return self in {
+ ModelType.GROQ_LLAMA_3_1_8B,
+ ModelType.GROQ_LLAMA_3_1_70B,
+ ModelType.GROQ_LLAMA_3_1_405B,
+ ModelType.GROQ_LLAMA_3_8B,
+ ModelType.GROQ_LLAMA_3_70B,
+ ModelType.GROQ_MIXTRAL_8_7B,
+ ModelType.GROQ_GEMMA_7B_IT,
+ ModelType.GROQ_GEMMA_2_9B_IT,
+ }
+
@property
def is_nvidia(self) -> bool:
r"""Returns whether this type of models is Nvidia-released model.
@@ -178,6 +201,22 @@ class ModelType(Enum):
return 8192
elif self == ModelType.GLM_4V:
return 1024
+ elif self is ModelType.GROQ_LLAMA_3_1_8B:
+ return 131072
+ elif self is ModelType.GROQ_LLAMA_3_1_70B:
+ return 131072
+ elif self is ModelType.GROQ_LLAMA_3_1_405B:
+ return 131072
+ elif self is ModelType.GROQ_LLAMA_3_8B:
+ return 8192
+ elif self is ModelType.GROQ_LLAMA_3_70B:
+ return 8192
+ elif self is ModelType.GROQ_MIXTRAL_8_7B:
+ return 32768
+ elif self is ModelType.GROQ_GEMMA_7B_IT:
+ return 8192
+ elif self is ModelType.GROQ_GEMMA_2_9B_IT:
+ return 8192
elif self is ModelType.STUB:
return 4096
elif self is ModelType.LLAMA_2:
@@ -356,6 +395,7 @@ class ModelPlatformType(Enum):
OPENAI = "openai"
AZURE = "azure"
ANTHROPIC = "anthropic"
+ GROQ = "groq"
OPENSOURCE = "opensource"
OLLAMA = "ollama"
LITELLM = "litellm"
@@ -379,6 +419,11 @@ class ModelPlatformType(Enum):
r"""Returns whether this platform is anthropic."""
return self is ModelPlatformType.ANTHROPIC
+ @property
+ def is_groq(self) -> bool:
+ r"""Returns whether this platform is groq."""
+ return self is ModelPlatformType.GROQ
+
@property
def is_ollama(self) -> bool:
r"""Returns whether this platform is ollama."""
diff --git a/camel/utils/token_counting.py b/camel/utils/token_counting.py
index 2a4f59614..2bff5a6ed 100644
--- a/camel/utils/token_counting.py
+++ b/camel/utils/token_counting.py
@@ -51,7 +51,12 @@ def messages_to_prompt(messages: List[OpenAIMessage], model: ModelType) -> str:
system_message = messages[0]["content"]
ret: str
- if model == ModelType.LLAMA_2 or model == ModelType.LLAMA_3:
+ if model in [
+ ModelType.LLAMA_2,
+ ModelType.LLAMA_3,
+ ModelType.GROQ_LLAMA_3_8B,
+ ModelType.GROQ_LLAMA_3_70B,
+ ]:
# reference: https://github.com/facebookresearch/llama/blob/cfc3fc8c1968d390eb830e65c63865e980873a06/llama/generation.py#L212
seps = [" ", " </s><s>"]
role_map = {"user": "[INST]", "assistant": "[/INST]"}
@@ -74,7 +79,7 @@ def messages_to_prompt(messages: List[OpenAIMessage], model: ModelType) -> str:
else:
ret += role
return ret
- elif model == ModelType.VICUNA or model == ModelType.VICUNA_16K:
+ elif model in [ModelType.VICUNA, ModelType.VICUNA_16K]:
seps = [" ", "</s>"]
role_map = {"user": "USER", "assistant": "ASSISTANT"}
@@ -132,6 +137,40 @@ def messages_to_prompt(messages: List[OpenAIMessage], model: ModelType) -> str:
else:
ret += '<|im_start|>' + role + '\n'
return ret
+ elif model == ModelType.GROQ_MIXTRAL_8_7B:
+ # Mistral/Mixtral format
+ system_prompt = f"<s>[INST] {system_message} [/INST]\n"
+ ret = system_prompt
+
+ for msg in messages[1:]:
+ if msg["role"] == "user":
+ ret += f"[INST] {msg['content']} [/INST]\n"
+ elif msg["role"] == "assistant":
+ ret += f"{msg['content']}</s>\n"
+
+ if not isinstance(msg['content'], str):
+ raise ValueError(
+ "Currently multimodal context is not "
+ "supported by the token counter."
+ )
+
+ return ret.strip()
+ elif model in [ModelType.GROQ_GEMMA_7B_IT, ModelType.GROQ_GEMMA_2_9B_IT]:
+ # Gemma format
+ ret = f"<bos>{system_message}\n"
+ for msg in messages:
+ if msg["role"] == "user":
+ ret += f"Human: {msg['content']}\n"
+ elif msg["role"] == "assistant":
+ ret += f"Assistant: {msg['content']}\n"
+
+ if not isinstance(msg['content'], str):
+ raise ValueError(
+ "Currently multimodal context is not supported by the token counter."
+ )
+
+ ret += "<eos>"
+ return ret
else:
raise ValueError(f"Invalid model type: {model}")
diff --git a/examples/models/role_playing_with_groq.py b/examples/models/role_playing_with_groq.py
new file mode 100644
index 000000000..bfde64aec
--- /dev/null
+++ b/examples/models/role_playing_with_groq.py
@@ -0,0 +1,103 @@
+# =========== 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 colorama import Fore
+
+from camel.configs import GroqConfig
+from camel.models import ModelFactory
+from camel.societies import RolePlaying
+from camel.types import ModelPlatformType, ModelType
+from camel.utils import print_text_animated
+
+
+def main(model_type=None) -> None:
+ task_prompt = "Develop a trading bot for the stock market"
+
+ agent_kwargs = {
+ role: ModelFactory.create(
+ model_platform=ModelPlatformType.GROQ,
+ model_type=model_type,
+ model_config_dict=GroqConfig().__dict__,
+ )
+ for role in ["assistant", "user", "task-specify"]
+ }
+
+ role_play_session = RolePlaying(
+ assistant_role_name="Python Programmer",
+ assistant_agent_kwargs={'model': agent_kwargs["assistant"]},
+ user_role_name="Stock Trader",
+ user_agent_kwargs={'model': agent_kwargs["assistant"]},
+ task_prompt=task_prompt,
+ with_task_specify=True,
+ task_specify_agent_kwargs={'model': agent_kwargs["task-specify"]},
+ )
+
+ print(
+ Fore.GREEN
+ + f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n"
+ )
+ print(
+ Fore.BLUE + f"AI User sys message:\n{role_play_session.user_sys_msg}\n"
+ )
+
+ print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
+ print(
+ Fore.CYAN
+ + "Specified task prompt:"
+ + f"\n{role_play_session.specified_task_prompt}\n"
+ )
+ print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
+
+ chat_turn_limit, n = 50, 0
+ input_msg = role_play_session.init_chat()
+ while n < chat_turn_limit:
+ n += 1
+ assistant_response, user_response = role_play_session.step(input_msg)
+
+ if assistant_response.terminated:
+ print(
+ Fore.GREEN
+ + (
+ "AI Assistant terminated. Reason: "
+ f"{assistant_response.info['termination_reasons']}."
+ )
+ )
+ break
+ if user_response.terminated:
+ print(
+ Fore.GREEN
+ + (
+ "AI User terminated. "
+ f"Reason: {user_response.info['termination_reasons']}."
+ )
+ )
+ break
+
+ print_text_animated(
+ Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n",
+ delay=0.005,
+ )
+ print_text_animated(
+ Fore.GREEN + "AI Assistant:\n\n"
+ f"{assistant_response.msg.content}\n",
+ delay=0.005,
+ )
+
+ if "CAMEL_TASK_DONE" in user_response.msg.content:
+ break
+
+ input_msg = assistant_response.msg
+
+
+if __name__ == "__main__":
+ main(model_type=ModelType.GROQ_LLAMA_3_1_70B)
diff --git a/poetry.lock b/poetry.lock
index 81a31d708..6233a154f 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -1973,6 +1973,25 @@ requests = ">=2.0,<3.0"
typing-extensions = ">=4.0,<5.0"
websockets = ">=10.0,<12.0"
+[[package]]
+name = "groq"
+version = "0.5.0"
+description = "The official Python library for the groq API"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "groq-0.5.0-py3-none-any.whl", hash = "sha256:a7e6be1118bcdfea3ed071ec00f505a34d4e6ec28c435adb5a5afd33545683a1"},
+ {file = "groq-0.5.0.tar.gz", hash = "sha256:d476cdc3383b45d2a4dc1876142a9542e663ea1029f9e07a05de24f895cae48c"},
+]
+
+[package.dependencies]
+anyio = ">=3.5.0,<5"
+distro = ">=1.7.0,<2"
+httpx = ">=0.23.0,<1"
+pydantic = ">=1.9.0,<3"
+sniffio = "*"
+typing-extensions = ">=4.7,<5"
+
[[package]]
name = "grpcio"
version = "1.63.0"
@@ -8505,4 +8524,4 @@ vector-databases = ["pymilvus", "qdrant-client"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.9.0,<3.12"
-content-hash = "d2c859135c8ee6ebbc147740276e454d3ac8959eb7f8ef469d54ee2a3ed17772"
+content-hash = "0b301628e18a47b06f82d5f9893fe5052a09ae1e76f6aba6052b1b42e452ef70"
diff --git a/pyproject.toml b/pyproject.toml
index 4ef5a11e7..8e2edcaa1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -30,12 +30,13 @@ documentation = "https://docs.camel-ai.org"
python = ">=3.9.0,<3.12"
numpy = "^1"
openai = "^1.2.3"
+groq = "^0.5.0"
+anthropic = "^0.29.0"
tiktoken = "^0.7.0"
colorama = "^0"
jsonschema = "^4"
protobuf = "^4"
pathlib = "^1.0.1"
-anthropic = "^0.29.0"
docstring-parser = "^0.15"
pydantic = ">=1.9,<3"
curl_cffi = "0.6.2"
@@ -302,6 +303,8 @@ module = [
"tiktoken",
"openai",
"openai.error",
+ "groq",
+ "anthropic",
"pytest",
"_pytest.config",
"_pytest.nodes",
diff --git a/test/models/test_groq_llama3_model.py b/test/models/test_groq_llama3_model.py
new file mode 100644
index 000000000..226b1f2ae
--- /dev/null
+++ b/test/models/test_groq_llama3_model.py
@@ -0,0 +1,62 @@
+# =========== 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 re
+
+import pytest
+
+from camel.configs import GroqConfig, OpenSourceConfig
+from camel.models import GroqModel
+from camel.types import ModelType
+from camel.utils import OpenAITokenCounter
+
+
+ "model_type",
+ [
+ ModelType.GROQ_LLAMA_3_8B,
+ ModelType.GROQ_LLAMA_3_70B,
+ ],
+)
+def test_groq_llama3_model(model_type):
+ model_config_dict = GroqConfig().__dict__
+ model = GroqModel(model_type, model_config_dict)
+ assert model.model_type == model_type
+ assert model.model_config_dict == model_config_dict
+ # LLM served by Groq does not support token counter, so Camel uses
+ # OpenAITokenCounter as a placeholder.
+ assert isinstance(model.token_counter, OpenAITokenCounter)
+ assert isinstance(model.model_type.value_for_tiktoken, str)
+ assert isinstance(model.model_type.token_limit, int)
+
+
+def test_groq_llama3_model_unexpected_argument():
+ model_type = ModelType.GROQ_LLAMA_3_70B
+ model_config = OpenSourceConfig(
+ model_path="vicuna-7b-v1.5",
+ server_url="http://localhost:8000/v1",
+ )
+ model_config_dict = model_config.__dict__
+
+ with pytest.raises(
+ ValueError,
+ match=re.escape(
+ (
+ "Unexpected argument `model_path` is "
+ "input into Groq model backend."
+ )
+ ),
+ ):
+ _ = GroqModel(model_type, model_config_dict)