🐫 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: Update anthropic config and integrate Claude 3.7 (#1688)
Xiaotian Jin7402cb605189
diff --git a/camel/configs/anthropic_config.py b/camel/configs/anthropic_config.pyindex 115e40207..828bdd3ed 100644--- a/camel/configs/anthropic_config.py+++ b/camel/configs/anthropic_config.py@@ -23,23 +23,24 @@ class AnthropicConfig(BaseConfig):r"""Defines the parameters for generating chat completions using theAnthropic API.- See: https://docs.anthropic.com/claude/reference/complete_post+ See: https://docs.anthropic.com/en/api/messagesArgs:max_tokens (int, optional): The maximum number of tokens togenerate before stopping. Note that Anthropic models may stopbefore reaching this maximum. This parameter only specifies theabsolute maximum number of tokens to generate.(default: :obj:`8192`)- stop_sequences (List[str], optional): Sequences that will cause the- model to stop generating completion text. Anthropic models stop- on "\n\nHuman:", and may include additional built-in stop sequences- in the future. By providing the stop_sequences parameter, you may- include additional strings that will cause the model to stop- generating. (default: :obj:`[]`)+ stop_sequences (List[str], optional): Custom text sequences that will+ cause the model to stop generating. The models will normally stop+ when they have naturally completed their turn. If the model+ encounters one of these custom sequences, the response will be+ terminated and the stop_reason will be "stop_sequence".+ (default: :obj:`[]`)temperature (float, optional): Amount of randomness injected into theresponse. Defaults to 1. Ranges from 0 to 1. Use temp closer to 0for analytical / multiple choice, and closer to 1 for creative- and generative tasks. (default: :obj:`1`)+ and generative tasks. Note that even with temperature of 0.0, the+ results will not be fully deterministic. (default: :obj:`1`)top_p (float, optional): Use nucleus sampling. In nucleus sampling, wecompute the cumulative distribution over all the options for eachsubsequent token in decreasing probability order and cut it off@@ -49,9 +50,20 @@ class AnthropicConfig(BaseConfig):top_k (int, optional): Only sample from the top K options for eachsubsequent token. Used to remove "long tail" low probabilityresponses. (default: :obj:`5`)- metadata: An object describing metadata about the request.stream (bool, optional): Whether to incrementally stream the responseusing server-sent events. (default: :obj:`False`)+ metadata (Union[dict, NotGiven], optional): An object describing+ metadata about the request. Can include user_id as an external+ identifier for the user associated with the request.+ (default: :obj:`NotGiven()`)+ thinking (Union[dict, NotGiven], optional): Configuration for enabling+ Claude's extended thinking. When enabled, responses include+ thinking content blocks showing Claude's thinking process.+ (default: :obj:`NotGiven()`)+ tool_choice (Union[dict, NotGiven], optional): How the model should+ use the provided tools. The model can use a specific tool, any+ available tool, decide by itself, or not use tools at all.+ (default: :obj:`NotGiven()`)"""max_tokens: int = 8192@@ -60,11 +72,33 @@ class AnthropicConfig(BaseConfig):top_p: Union[float, NotGiven] = 0.7top_k: Union[int, NotGiven] = 5stream: bool = False+ metadata: Union[dict, NotGiven] = NotGiven()+ thinking: Union[dict, NotGiven] = NotGiven()+ tool_choice: Union[dict, NotGiven] = NotGiven()def as_dict(self) -> dict[str, Any]:config_dict = super().as_dict()- if "tools" in config_dict:- del config_dict["tools"] # TODO: Support tool calling.+ # Create a list of keys to remove to avoid modifying dict+ keys_to_remove = [+ key+ for key, value in config_dict.items()+ if isinstance(value, NotGiven)+ ]++ for key in keys_to_remove:+ del config_dict[key]++ # remove some keys if thinking is enabled+ thinking_enabled = (+ not isinstance(self.thinking, NotGiven)+ and self.thinking["type"] == "enabled"+ )+ if thinking_enabled:+ # `top_p`, `top_k`, `temperature` must be unset when thinking is+ # enabled.+ config_dict.pop("top_k", None)+ config_dict.pop("top_p", None)+ config_dict.pop("temperature", None)return config_dictdiff --git a/camel/models/anthropic_model.py b/camel/models/anthropic_model.pyindex 6717d6a64..ac8517e31 100644--- a/camel/models/anthropic_model.py+++ b/camel/models/anthropic_model.py@@ -84,7 +84,11 @@ class AnthropicModel(BaseModelBackend):index=0,message={"role": "assistant",- "content": response.content[0].text,+ "content": next(+ content.text+ for content in response.content+ if content.type == "text"+ ),},finish_reason=response.stop_reason,)diff --git a/camel/types/enums.py b/camel/types/enums.pyindex 2754cd23d..1fc5cacbe 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -101,6 +101,7 @@ class ModelType(UnifiedModelType, Enum):CLAUDE_3_HAIKU = "claude-3-haiku-20240307"CLAUDE_3_5_SONNET = "claude-3-5-sonnet-latest"CLAUDE_3_5_HAIKU = "claude-3-5-haiku-latest"+ CLAUDE_3_7_SONNET = "claude-3-7-sonnet-latest"# Nvidia modelsNVIDIA_NEMOTRON_340B_INSTRUCT = "nvidia/nemotron-4-340b-instruct"@@ -315,6 +316,7 @@ class ModelType(UnifiedModelType, Enum):ModelType.CLAUDE_3_HAIKU,ModelType.CLAUDE_3_5_SONNET,ModelType.CLAUDE_3_5_HAIKU,+ ModelType.CLAUDE_3_7_SONNET,}@property@@ -683,6 +685,7 @@ class ModelType(UnifiedModelType, Enum):ModelType.CLAUDE_3_HAIKU,ModelType.CLAUDE_3_5_SONNET,ModelType.CLAUDE_3_5_HAIKU,+ ModelType.CLAUDE_3_7_SONNET,ModelType.YI_MEDIUM_200K,}:return 200_000diff --git a/examples/models/anthiropic_model_example.py b/examples/models/anthiropic_model_example.pynew file mode 100644index 000000000..3cf63bffe--- /dev/null+++ b/examples/models/anthiropic_model_example.py@@ -0,0 +1,138 @@+# ========= 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 AnthropicConfig+from camel.models import ModelFactory+from camel.types import ModelPlatformType, ModelType++"""+please set the below os environment:+export ANTHROPIC_API_KEY=""+"""++model = ModelFactory.create(+ model_platform=ModelPlatformType.ANTHROPIC,+ model_type=ModelType.CLAUDE_3_5_SONNET,+ model_config_dict=AnthropicConfig(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)+'''+===============================================================================+Hi CAMEL AI! It's great to meet an open-source community focused on advancing research in autonomous and communicative agents. Your work on developing and studying AI systems that can effectively communicate and operate autonomously is fascinating and important for the field. I appreciate communities like yours that contribute to open research and development in AI. Wishing you continued success in your mission!+===============================================================================+''' # noqa: E501++# Use the extended thinking model with Claude 3.7 Sonnet+config = AnthropicConfig(+ thinking={"type": "enabled", "budget_tokens": 2048}+).as_dict()++model = ModelFactory.create(+ model_platform=ModelPlatformType.ANTHROPIC,+ model_type=ModelType.CLAUDE_3_7_SONNET,+ model_config_dict=config,+)++camel_agent = ChatAgent(model=model)++user_msg = """Write a bash script that takes a matrix represented as a string with+format '[1,2],[3,4],[5,6]' and prints the transpose in the same format.+""" # noqa: E501++response = camel_agent.step(user_msg)+print(response.msgs[0].content)+'''+===============================================================================+# Matrix Transpose Bash Script++Here's a bash script that transposes a matrix from the format `[1,2],[3,4],[5,6]` to `[1,3,5],[2,4,6]`:++```bash+#!/bin/bash++# Check if input argument is provided+if [ $# -lt 1 ]; then+ echo "Usage: $0 '[row1],[row2],...'"+ exit 1+fi++# Input matrix as string+input="$1"++# Remove outer brackets and split into rows+input="${input//\]\,\[/]|[}" # Replace "],[" with "]|["+input="${input#\[}" # Remove leading "["+input="${input%\]}" # Remove trailing "]"+IFS='|' read -ra rows <<< "$input"++# Determine dimensions of the matrix+row_count="${#rows[@]}"+IFS=',' read -ra first_row <<< "${rows[0]//[\[\]]}" # Remove brackets from first row+col_count="${#first_row[@]}"++# Create transpose+result=""+for (( col=0; col<col_count; col++ )); do+ result+="["+ for (( row=0; row<row_count; row++ )); do+ # Extract current row without brackets+ current="${rows[row]//[\[\]]}"+ # Split by commas+ IFS=',' read -ra elements <<< "$current"+ # Add element to transpose+ result+="${elements[col]}"+ # Add comma if not the last element+ if (( row < row_count-1 )); then+ result+=","+ fi+ done+ result+="]"+ # Add comma if not the last row+ if (( col < col_count-1 )); then+ result+=","+ fi+done++echo "$result"+```++## How to Use:++1. Save the script to a file (e.g., `transpose.sh`)+2. Make it executable: `chmod +x transpose.sh`+3. Run it with your matrix: `./transpose.sh "[1,2],[3,4],[5,6]"`++## Example:+- Input: `[1,2],[3,4],[5,6]`+- Output: `[1,3,5],[2,4,6]`++The script works by:+1. Parsing the input string to extract rows and elements+2. Finding the dimensions of the original matrix+3. Creating the transpose by iterating through columns first, then rows+4. Formatting the result with proper brackets and commas+''' # noqa: E501