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

fix: parallel tool call sequence bug, upadte doc and readme (#2148)

Wendong-Fane5770589d531
diff --git a/README.md b/README.md
index a221e0332..ecff18c76 100644
--- a/README.md
+++ b/README.md
@@ -416,6 +416,13 @@ For more information please contact [email protected]
- **Ambassador Project:** Advocate for CAMEL-AI, host events, and contribute content. [Learn more](https://www.camel-ai.org/community)
+- **WeChat Community:** Scan the QR code below to join our WeChat community.
+
+ <div align="center">
+ <img src="misc/wechat.jpg" alt="WeChat QR Code" width="200">
+ </div>
+
+
<br>
## Citation
diff --git a/camel/embeddings/azure_embedding.py b/camel/embeddings/azure_embedding.py
index f7228cabb..c7424b972 100644
--- a/camel/embeddings/azure_embedding.py
+++ b/camel/embeddings/azure_embedding.py
@@ -31,7 +31,7 @@ class AzureEmbedding(BaseEmbedding[str]):
Args:
model_type (EmbeddingModelType, optional): The model type to be
used for text embeddings.
- (default: :obj:`TEXT_EMBEDDING_ADA_2`)
+ (default: :obj:`TEXT_EMBEDDING_3_SMALL`)
url (Optional[str], optional): The url to the Azure OpenAI service.
(default: :obj:`None`)
api_key (str, optional): The API key for authenticating with the
diff --git a/camel/models/base_model.py b/camel/models/base_model.py
index fe10acbb0..55d1c7c79 100644
--- a/camel/models/base_model.py
+++ b/camel/models/base_model.py
@@ -108,42 +108,125 @@ class BaseModelBackend(ABC, metaclass=ModelBackendMeta):
) -> List[OpenAIMessage]:
r"""Preprocess messages before sending to model API.
Removes thinking content from assistant and user messages.
+ Automatically formats messages for parallel tool calls if tools are
+ detected.
Args:
- messages (List[OpenAIMessage]): Original messages
+ messages (List[OpenAIMessage]): Original messages.
Returns:
List[OpenAIMessage]: Preprocessed messages
"""
-
- def should_process_thinking(msg: OpenAIMessage) -> bool:
- # Only process thinking content for assistant and user messages
- return msg['role'] in ['assistant', 'user'] and isinstance(
- msg['content'], str
+ # Process all messages in a single pass
+ processed_messages = []
+ tool_calls_buffer: List[OpenAIMessage] = []
+ tool_responses_buffer: Dict[str, OpenAIMessage] = {}
+ has_tool_calls = False
+
+ for msg in messages:
+ # Remove thinking content if needed
+ role = msg.get('role')
+ content = msg.get('content')
+ if role in ['assistant', 'user'] and isinstance(content, str):
+ if '<think>' in content and '</think>' in content:
+ content = re.sub(
+ r'<think>.*?</think>', '', content, flags=re.DOTALL
+ ).strip()
+ processed_msg = dict(msg)
+ processed_msg['content'] = content
+ else:
+ processed_msg = dict(msg)
+
+ # Check and track tool calls/responses
+ is_tool_call = (
+ processed_msg.get("role") == "assistant"
+ and "tool_calls" in processed_msg
+ )
+ is_tool_response = (
+ processed_msg.get("role") == "tool"
+ and "tool_call_id" in processed_msg
)
- def remove_thinking(content: str) -> str:
- # Only remove thinking content if the tags are present
- if '<think>' in content and '</think>' in content:
- return re.sub(
- r'<think>.*?</think>',
- '',
- content,
- flags=re.DOTALL,
- ).strip()
- return content
-
- return [
- { # type: ignore[misc]
- **msg,
- 'content': (
- remove_thinking(msg['content']) # type: ignore[arg-type]
- if should_process_thinking(msg)
- else msg['content']
- ),
- }
- for msg in messages
- ]
+ if is_tool_call or is_tool_response:
+ has_tool_calls = True
+
+ # Store the processed message for later formatting if needed
+ processed_messages.append(processed_msg)
+
+ # If no tool calls detected, return the processed messages
+ if not has_tool_calls:
+ return processed_messages # type: ignore[return-value]
+
+ # Format messages for parallel tool calls
+ formatted_messages = []
+ tool_calls_buffer = []
+ tool_responses_buffer = {}
+
+ for msg in processed_messages: # type: ignore[assignment]
+ # If this is an assistant message with tool calls, add it to the
+ # buffer
+ if msg.get("role") == "assistant" and "tool_calls" in msg:
+ tool_calls_buffer.append(msg)
+ continue
+
+ # If this is a tool response, add it to the responses buffer
+ if msg.get("role") == "tool" and "tool_call_id" in msg:
+ tool_call_id = msg.get("tool_call_id")
+ if isinstance(tool_call_id, str):
+ tool_responses_buffer[tool_call_id] = msg
+ continue
+
+ # Process any complete tool call + responses before adding regular
+ # messages
+ if tool_calls_buffer and tool_responses_buffer:
+ # Add the assistant message with tool calls
+ assistant_msg = tool_calls_buffer[0]
+ formatted_messages.append(assistant_msg)
+
+ # Add all matching tool responses for this assistant message
+ tool_calls = assistant_msg.get("tool_calls", [])
+ if isinstance(tool_calls, list):
+ for tool_call in tool_calls:
+ tool_call_id = tool_call.get("id")
+ if (
+ isinstance(tool_call_id, str)
+ and tool_call_id in tool_responses_buffer
+ ):
+ formatted_messages.append(
+ tool_responses_buffer[tool_call_id]
+ )
+ del tool_responses_buffer[tool_call_id]
+
+ tool_calls_buffer.pop(0)
+
+ # Add the current regular message
+ formatted_messages.append(msg)
+
+ # Process any remaining buffered tool calls and responses
+ while tool_calls_buffer:
+ assistant_msg = tool_calls_buffer[0]
+ formatted_messages.append(assistant_msg)
+
+ tool_calls = assistant_msg.get("tool_calls", [])
+ if isinstance(tool_calls, list):
+ for tool_call in tool_calls:
+ tool_call_id = tool_call.get("id")
+ if (
+ isinstance(tool_call_id, str)
+ and tool_call_id in tool_responses_buffer
+ ):
+ formatted_messages.append(
+ tool_responses_buffer[tool_call_id]
+ )
+ del tool_responses_buffer[tool_call_id]
+
+ tool_calls_buffer.pop(0)
+
+ # Add any remaining tool responses
+ for response in tool_responses_buffer.values():
+ formatted_messages.append(response)
+
+ return formatted_messages
@abstractmethod
def _run(
diff --git a/camel/toolkits/google_calendar_toolkit.py b/camel/toolkits/google_calendar_toolkit.py
index d74e9341c..bef60c1e0 100644
--- a/camel/toolkits/google_calendar_toolkit.py
+++ b/camel/toolkits/google_calendar_toolkit.py
@@ -20,7 +20,7 @@ from typing import Any, Dict, List, Optional, Union
from camel.logger import get_logger
from camel.toolkits import FunctionTool
from camel.toolkits.base import BaseToolkit
-from camel.utils.commons import MCPServer, api_keys_required
+from camel.utils import MCPServer, api_keys_required
logger = get_logger(__name__)
diff --git a/docs/key_modules/embeddings.md b/docs/key_modules/embeddings.md
index 97d24aa4e..73d74d9ad 100644
--- a/docs/key_modules/embeddings.md
+++ b/docs/key_modules/embeddings.md
@@ -37,6 +37,9 @@ Utilizes OpenAI's models for generating image embeddings. This will requires Ope
### 2.5. `AzureOpenAI`
Utilizes OpenAI's models for generating text embeddings. This will requires Azure OpenAI API Key.
+### 2.6. `TogetherEmbedding`
+Utilizes Together AI's models for generating text embeddings. This will requires Together AI API Key.
+
## 3. Get Started
To use the embedding functionalities, you need to import the necessary classes.
@@ -100,3 +103,14 @@ azure_openai_embedding = AzureEmbedding(model_type=EmbeddingModelType.TEXT_EMBED
# Generate embeddings for a list of texts
embeddings = azure_openai_embedding.embed_list(["Hello, world!", "Another example"])
```
+
+### 3.6. Using `TogetherEmbedding`
+```python
+from camel.embeddings import TogetherEmbedding
+
+# Initialize the Together AI embedding with a specific model
+together_embedding = TogetherEmbedding(model_name="togethercomputer/m2-bert-80M-8k-retrieval")
+
+# Generate embeddings for a list of texts
+embeddings = together_embedding.embed_list(["Hello, world!", "Another example"])
+```
diff --git a/misc/wechat.jpg b/misc/wechat.jpg
new file mode 100644
index 000000000..f78676d2e
Binary files /dev/null and b/misc/wechat.jpg differ
diff --git a/test/models/test_base_model.py b/test/models/test_base_model.py
index aaa326c0b..52c0cabb3 100644
--- a/test/models/test_base_model.py
+++ b/test/models/test_base_model.py
@@ -13,6 +13,8 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+import uuid
+
from camel.models import BaseModelBackend
from camel.types import ModelType
@@ -92,6 +94,17 @@ class TestBaseModelBackend:
processed = model.preprocess_messages(messages)
assert processed[0]['content'] == 'BeforeAfter'
+ # Test multiple thinking tags in one message
+ messages = [
+ {
+ 'role': 'assistant',
+ 'content': 'Start <think>First thought</think> middle '
+ '<think>Second thought</think> end',
+ }
+ ]
+ processed = model.preprocess_messages(messages)
+ assert processed[0]['content'] == 'Start middle end'
+
def test_metaclass_preprocessing(self):
r"""Test that metaclass automatically preprocesses messages in run
method."""
@@ -125,3 +138,311 @@ class TestBaseModelBackend:
model.run(messages)
assert processed_messages is not None
assert processed_messages[0]['content'] == 'Hello world'
+
+ def test_preprocess_tool_calls(self):
+ r"""Test message preprocessing formats tool calls and responses
+ correctly.
+ """
+
+ class DummyModel(BaseModelBackend):
+ @property
+ def token_counter(self):
+ pass
+
+ def run(self, messages):
+ pass
+
+ def check_model_config(self):
+ pass
+
+ def _run(self, messages, response_format=None, tools=None):
+ pass
+
+ async def _arun(self, messages, response_format=None, tools=None):
+ pass
+
+ model = DummyModel(ModelType.GPT_4O_MINI)
+
+ # Helper function to create tool call objects
+ def create_tool_call(id, name, args):
+ return {
+ 'id': id,
+ 'type': 'function',
+ 'function': {'name': name, 'arguments': args},
+ }
+
+ # Test 1: No tool calls - should return processed messages unchanged
+ messages = [
+ {'role': 'user', 'content': 'Hello world'},
+ {'role': 'assistant', 'content': 'Hi there!'},
+ ]
+ processed = model.preprocess_messages(messages)
+ assert processed == messages
+
+ # Test 2: Simple tool call and response
+ tool_call_id = str(uuid.uuid4())
+ messages = [
+ {'role': 'user', 'content': 'What is the weather?'},
+ {
+ 'role': 'assistant',
+ 'content': None,
+ 'tool_calls': [
+ create_tool_call(
+ tool_call_id, 'get_weather', '{"location":"New York"}'
+ )
+ ],
+ },
+ {
+ 'role': 'tool',
+ 'tool_call_id': tool_call_id,
+ 'content': '{"temperature": 72, "condition": "sunny"}',
+ },
+ {
+ 'role': 'assistant',
+ 'content': 'The weather in New York is sunny and 72°F.',
+ },
+ ]
+
+ processed = model.preprocess_messages(messages)
+ assert processed == messages
+
+ # Test 3: Out of order tool calls and responses
+ tool_call_id1 = str(uuid.uuid4())
+ tool_call_id2 = str(uuid.uuid4())
+ messages = [
+ {'role': 'user', 'content': 'What is the weather and time?'},
+ # First tool response appears before the tool call
+ {
+ 'role': 'tool',
+ 'tool_call_id': tool_call_id1,
+ 'content': '{"temperature": 72, "condition": "sunny"}',
+ },
+ {
+ 'role': 'assistant',
+ 'content': None,
+ 'tool_calls': [
+ create_tool_call(
+ tool_call_id1, 'get_weather', '{"location":"New York"}'
+ ),
+ create_tool_call(
+ tool_call_id2, 'get_time', '{"timezone":"EST"}'
+ ),
+ ],
+ },
+ {
+ 'role': 'tool',
+ 'tool_call_id': tool_call_id2,
+ 'content': '{"time": "10:30 AM"}',
+ },
+ {
+ 'role': 'assistant',
+ 'content': 'The weather in New York is sunny and 72°F. '
+ 'The time is 10:30 AM.',
+ },
+ ]
+
+ processed = model.preprocess_messages(messages)
+ assert len(processed) == 5
+ assert processed[0]['role'] == 'user'
+ assert (
+ processed[1]['role'] == 'assistant'
+ and 'tool_calls' in processed[1]
+ )
+ assert (
+ processed[2]['role'] == 'tool'
+ and processed[2]['tool_call_id'] == tool_call_id1
+ )
+ assert (
+ processed[3]['role'] == 'tool'
+ and processed[3]['tool_call_id'] == tool_call_id2
+ )
+ assert processed[4]['role'] == 'assistant'
+
+ # Test 4: Complex conversation with multiple tool calls
+ # Create IDs for tool calls
+ ids = [str(uuid.uuid4()) for _ in range(3)]
+
+ messages = [
+ {'role': 'user', 'content': 'What is the weather?'},
+ # First assistant message with tool call
+ {
+ 'role': 'assistant',
+ 'content': None,
+ 'tool_calls': [
+ create_tool_call(
+ ids[0], 'get_weather', '{"location":"New York"}'
+ )
+ ],
+ },
+ {
+ 'role': 'tool',
+ 'tool_call_id': ids[0],
+ 'content': '{"temperature": 72, "condition": "sunny"}',
+ },
+ {'role': 'user', 'content': 'What about the time?'},
+ # Second assistant message with multiple tool calls
+ {
+ 'role': 'assistant',
+ 'content': None,
+ 'tool_calls': [
+ create_tool_call(ids[1], 'get_time', '{"timezone":"EST"}'),
+ create_tool_call(ids[2], 'get_date', '{"format":"short"}'),
+ ],
+ },
+ {
+ 'role': 'tool',
+ 'tool_call_id': ids[1],
+ 'content': '{"time": "10:30 AM"}',
+ },
+ {
+ 'role': 'tool',
+ 'tool_call_id': ids[2],
+ 'content': '{"date": "2023-04-15"}',
+ },
+ {
+ 'role': 'assistant',
+ 'content': 'The time is 10:30 AM EST and the date is '
+ 'April 15, 2023.',
+ },
+ ]
+
+ processed = model.preprocess_messages(messages)
+ assert len(processed) == 8
+
+ # Verify correct ordering of tool calls and responses
+ assert (
+ processed[1]['role'] == 'assistant'
+ and processed[1]['tool_calls'][0]['id'] == ids[0]
+ )
+ assert (
+ processed[2]['role'] == 'tool'
+ and processed[2]['tool_call_id'] == ids[0]
+ )
+ assert (
+ processed[4]['role'] == 'assistant'
+ and len(processed[4]['tool_calls']) == 2
+ )
+ assert (
+ processed[5]['role'] == 'tool'
+ and processed[5]['tool_call_id'] == ids[1]
+ )
+ assert (
+ processed[6]['role'] == 'tool'
+ and processed[6]['tool_call_id'] == ids[2]
+ )
+
+ # Test 5: Edge cases - Missing tool responses and orphaned tool
+ # responses
+ tests = [
+ # Missing tool response
+ {
+ 'messages': [
+ {
+ 'role': 'user',
+ 'content': 'What is the weather and time?',
+ },
+ {
+ 'role': 'assistant',
+ 'content': None,
+ 'tool_calls': [
+ create_tool_call(
+ ids[0],
+ 'get_weather',
+ '{"location":"New York"}',
+ ),
+ create_tool_call(
+ ids[1], 'get_time', '{"timezone":"EST"}'
+ ),
+ ],
+ },
+ # Only one tool response is provided
+ {
+ 'role': 'tool',
+ 'tool_call_id': ids[0],
+ 'content': '{"temperature": 72}',
+ },
+ {
+ 'role': 'assistant',
+ 'content': 'The weather in New York is 72°F.',
+ },
+ ],
+ 'assertions': lambda p: (
+ len(p) == 4
+ and p[0]['role'] == 'user'
+ and p[1]['role'] == 'assistant'
+ and 'tool_calls' in p[1]
+ and p[2]['role'] == 'tool'
+ and p[2]['tool_call_id'] == ids[0]
+ and p[3]['role'] == 'assistant'
+ ),
+ },
+ # Orphaned tool response
+ {
+ 'messages': [
+ {'role': 'user', 'content': 'What is the weather?'},
+ {
+ 'role': 'tool',
+ 'tool_call_id': ids[0],
+ 'content': '{"temperature": 72}',
+ },
+ {'role': 'assistant', 'content': 'The weather is 72°F.'},
+ ],
+ 'assertions': lambda p: (
+ len(p) == 3
+ and p[0]['role'] == 'user'
+ and p[1]['role'] == 'assistant'
+ and p[2]['role'] == 'tool'
+ and p[2]['tool_call_id'] == ids[0]
+ ),
+ },
+ ]
+
+ for test in tests:
+ processed = model.preprocess_messages(test['messages'])
+ assert test['assertions'](processed)
+
+ # Test 6: Combined thinking removal and tool call formatting
+ tool_call_id = str(uuid.uuid4())
+ messages = [
+ {
+ 'role': 'user',
+ 'content': 'What is the weather <think>in New York</think>?',
+ },
+ {
+ 'role': 'assistant',
+ 'content': '<think>I should check the weather</think>',
+ 'tool_calls': [
+ create_tool_call(
+ tool_call_id, 'get_weather', '{"location":"New York"}'
+ )
+ ],
+ },
+ {
+ 'role': 'tool',
+ 'tool_call_id': tool_call_id,
+ 'content': '{"temperature": 72}',
+ },
+ {
+ 'role': 'assistant',
+ 'content': '<think>Format nicely</think>The weather is 72°F.',
+ },
+ ]
+
+ processed = model.preprocess_messages(messages)
+ assert len(processed) == 4
+ assert (
+ processed[0]['role'] == 'user'
+ and processed[0]['content'] == 'What is the weather ?'
+ )
+ assert (
+ processed[1]['role'] == 'assistant'
+ and processed[1]['content'] == ''
+ )
+ assert (
+ processed[2]['role'] == 'tool'
+ and processed[2]['tool_call_id'] == tool_call_id
+ )
+ assert (
+ processed[3]['role'] == 'assistant'
+ and processed[3]['content'] == 'The weather is 72°F.'
+ )