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

chore: Extended BaseMessage to support reasoning fields and updated usage accounting in streaming mode (#3326)

Tao Sun030ba23454d4
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index d5d69621e..a96014476 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -189,6 +189,8 @@ class StreamContentAccumulator:
self.base_content = "" # Content before tool calls
self.current_content = [] # Accumulated streaming fragments
self.tool_status_messages = [] # Accumulated tool status messages
+ self.reasoning_content = [] # Accumulated reasoning content
+ self.is_reasoning_phase = True # Track if we're in reasoning phase
def set_base_content(self, content: str):
r"""Set the base content (usually empty or pre-tool content)."""
@@ -197,6 +199,13 @@ class StreamContentAccumulator:
def add_streaming_content(self, new_content: str):
r"""Add new streaming content."""
self.current_content.append(new_content)
+ self.is_reasoning_phase = (
+ False # Once we get content, we're past reasoning
+ )
+
+ def add_reasoning_content(self, new_reasoning: str):
+ r"""Add new reasoning content."""
+ self.reasoning_content.append(new_reasoning)
def add_tool_status(self, status_message: str):
r"""Add a tool status message."""
@@ -208,6 +217,10 @@ class StreamContentAccumulator:
current = "".join(self.current_content)
return self.base_content + tool_messages + current
+ def get_full_reasoning_content(self) -> str:
+ r"""Get the complete accumulated reasoning content."""
+ return "".join(self.reasoning_content)
+
def get_content_with_new_status(self, status_message: str) -> str:
r"""Get content with a new status message appended."""
tool_messages = "".join([*self.tool_status_messages, status_message])
@@ -217,6 +230,8 @@ class StreamContentAccumulator:
def reset_streaming_content(self):
r"""Reset only the streaming content, keep base and tool status."""
self.current_content = []
+ self.reasoning_content = []
+ self.is_reasoning_phase = True
class StreamingChatAgentResponse:
@@ -3647,12 +3662,17 @@ class ChatAgent(BaseAgent):
if logprobs_info := handle_logprobs(choice):
meta_dict["logprobs_info"] = logprobs_info
+ reasoning_content = getattr(
+ choice.message, "reasoning_content", None
+ )
+
chat_message = BaseMessage(
role_name=self.role_name,
role_type=self.role_type,
meta_dict=meta_dict,
content=choice.message.content or "",
parsed=getattr(choice.message, "parsed", None),
+ reasoning_content=reasoning_content,
)
output_messages.append(chat_message)
@@ -4069,6 +4089,10 @@ class ChatAgent(BaseAgent):
final_content = (
final_completion.choices[0].message.content or ""
)
+ final_reasoning = (
+ content_accumulator.get_full_reasoning_content()
+ or None
+ )
final_message = BaseMessage(
role_name=self.role_name,
@@ -4079,6 +4103,7 @@ class ChatAgent(BaseAgent):
"BaseModel | dict[str, Any] | None",
parsed_object,
), # type: ignore[arg-type]
+ reasoning_content=final_reasoning,
)
self.record_message(final_message)
@@ -4146,11 +4171,34 @@ class ChatAgent(BaseAgent):
stream_completed = False
for chunk in stream:
+ has_choices = bool(chunk.choices and len(chunk.choices) > 0)
+
# Process chunk delta
- if chunk.choices and len(chunk.choices) > 0:
+ if has_choices:
choice = chunk.choices[0]
delta = choice.delta
+ # Handle reasoning content streaming (for DeepSeek reasoner)
+ if (
+ hasattr(delta, 'reasoning_content')
+ and delta.reasoning_content
+ ):
+ content_accumulator.add_reasoning_content(
+ delta.reasoning_content
+ )
+ # Yield partial response with reasoning content
+ partial_response = (
+ self._create_streaming_response_with_accumulator(
+ content_accumulator,
+ "", # No regular content yet
+ step_token_usage,
+ getattr(chunk, 'id', ''),
+ tool_call_records.copy(),
+ reasoning_delta=delta.reasoning_content,
+ )
+ )
+ yield partial_response
+
# Handle content streaming
if delta.content:
# Use accumulator for proper content management
@@ -4197,11 +4245,16 @@ class ChatAgent(BaseAgent):
# will handle message recording.
final_content = content_accumulator.get_full_content()
if final_content.strip() and not accumulated_tool_calls:
+ final_reasoning = (
+ content_accumulator.get_full_reasoning_content()
+ or None
+ )
final_message = BaseMessage(
role_name=self.role_name,
role_type=self.role_type,
meta_dict={},
content=final_content,
+ reasoning_content=final_reasoning,
)
if response_format:
@@ -4210,8 +4263,8 @@ class ChatAgent(BaseAgent):
)
self.record_message(final_message)
- elif chunk.usage and not chunk.choices:
- # Handle final chunk with usage but empty choices
+ if chunk.usage:
+ # Handle final usage chunk, whether or not choices are present.
# This happens when stream_options={"include_usage": True}
# Update the final usage from this chunk
self._update_token_usage_tracker(
@@ -4219,40 +4272,50 @@ class ChatAgent(BaseAgent):
)
# Create final response with final usage
- final_content = content_accumulator.get_full_content()
- if final_content.strip():
- final_message = BaseMessage(
- role_name=self.role_name,
- role_type=self.role_type,
- meta_dict={},
- content=final_content,
- )
-
- if response_format:
- self._try_format_message(
- final_message, response_format
+ should_finalize = stream_completed or not has_choices
+ if should_finalize:
+ final_content = content_accumulator.get_full_content()
+ if final_content.strip():
+ final_reasoning = (
+ content_accumulator.get_full_reasoning_content()
+ or None
+ )
+ final_message = BaseMessage(
+ role_name=self.role_name,
+ role_type=self.role_type,
+ meta_dict={},
+ content=final_content,
+ reasoning_content=final_reasoning,
)
- # Create final response with final usage (not partial)
- final_response = ChatAgentResponse(
- msgs=[final_message],
- terminated=False,
- info={
- "id": getattr(chunk, 'id', ''),
- "usage": step_token_usage.copy(),
- "finish_reasons": ["stop"],
- "num_tokens": self._get_token_count(final_content),
- "tool_calls": tool_call_records or [],
- "external_tool_requests": None,
- "streaming": False,
- "partial": False,
- },
- )
- yield final_response
- break
+ if response_format:
+ self._try_format_message(
+ final_message, response_format
+ )
+
+ # Create final response with final usage (not partial)
+ final_response = ChatAgentResponse(
+ msgs=[final_message],
+ terminated=False,
+ info={
+ "id": getattr(chunk, 'id', ''),
+ "usage": step_token_usage.copy(),
+ "finish_reasons": ["stop"],
+ "num_tokens": self._get_token_count(
+ final_content
+ ),
+ "tool_calls": tool_call_records or [],
+ "external_tool_requests": None,
+ "streaming": False,
+ "partial": False,
+ },
+ )
+ yield final_response
+ break
elif stream_completed:
- # If we've already seen finish_reason but no usage chunk, exit
- break
+ # We've seen finish_reason but no usage chunk yet; keep
+ # consuming remaining chunks to capture final metadata.
+ continue
return stream_completed, tool_calls_complete
@@ -4826,6 +4889,10 @@ class ChatAgent(BaseAgent):
final_content = (
final_completion.choices[0].message.content or ""
)
+ final_reasoning = (
+ content_accumulator.get_full_reasoning_content()
+ or None
+ )
final_message = BaseMessage(
role_name=self.role_name,
@@ -4836,6 +4903,7 @@ class ChatAgent(BaseAgent):
"BaseModel | dict[str, Any] | None",
parsed_object,
), # type: ignore[arg-type]
+ reasoning_content=final_reasoning,
)
self.record_message(final_message)
@@ -4941,11 +5009,34 @@ class ChatAgent(BaseAgent):
stream_completed = False
async for chunk in stream:
+ has_choices = bool(chunk.choices and len(chunk.choices) > 0)
+
# Process chunk delta
- if chunk.choices and len(chunk.choices) > 0:
+ if has_choices:
choice = chunk.choices[0]
delta = choice.delta
+ # Handle reasoning content streaming (for DeepSeek reasoner)
+ if (
+ hasattr(delta, 'reasoning_content')
+ and delta.reasoning_content
+ ):
+ content_accumulator.add_reasoning_content(
+ delta.reasoning_content
+ )
+ # Yield partial response with reasoning content
+ partial_response = (
+ self._create_streaming_response_with_accumulator(
+ content_accumulator,
+ "", # No regular content yet
+ step_token_usage,
+ getattr(chunk, 'id', ''),
+ tool_call_records.copy(),
+ reasoning_delta=delta.reasoning_content,
+ )
+ )
+ yield partial_response
+
# Handle content streaming
if delta.content:
# Use accumulator for proper content management
@@ -5007,8 +5098,8 @@ class ChatAgent(BaseAgent):
)
self.record_message(final_message)
- elif chunk.usage and not chunk.choices:
- # Handle final chunk with usage but empty choices
+ if chunk.usage:
+ # Handle final usage chunk, whether or not choices are present.
# This happens when stream_options={"include_usage": True}
# Update the final usage from this chunk
self._update_token_usage_tracker(
@@ -5016,40 +5107,48 @@ class ChatAgent(BaseAgent):
)
# Create final response with final usage
- final_content = content_accumulator.get_full_content()
- if final_content.strip():
- final_message = BaseMessage(
- role_name=self.role_name,
- role_type=self.role_type,
- meta_dict={},
- content=final_content,
- )
-
- if response_format:
- self._try_format_message(
- final_message, response_format
+ should_finalize = stream_completed or not has_choices
+ if should_finalize:
+ final_content = content_accumulator.get_full_content()
+ if final_content.strip():
+ final_reasoning = (
+ content_accumulator.get_full_reasoning_content()
+ or None
+ )
+ final_message = BaseMessage(
+ role_name=self.role_name,
+ role_type=self.role_type,
+ meta_dict={},
+ content=final_content,
+ reasoning_content=final_reasoning,
)
- # Create final response with final usage (not partial)
- final_response = ChatAgentResponse(
- msgs=[final_message],
- terminated=False,
- info={
- "id": getattr(chunk, 'id', ''),
- "usage": step_token_usage.copy(),
- "finish_reasons": ["stop"],
- "num_tokens": self._get_token_count(final_content),
- "tool_calls": tool_call_records or [],
- "external_tool_requests": None,
- "streaming": False,
- "partial": False,
- },
- )
- yield final_response
- break
+ if response_format:
+ self._try_format_message(
+ final_message, response_format
+ )
+
+ # Create final response with final usage (not partial)
+ final_response = ChatAgentResponse(
+ msgs=[final_message],
+ terminated=False,
+ info={
+ "id": getattr(chunk, 'id', ''),
+ "usage": step_token_usage.copy(),
+ "finish_reasons": ["stop"],
+ "num_tokens": self._get_token_count(
+ final_content
+ ),
+ "tool_calls": tool_call_records or [],
+ "external_tool_requests": None,
+ "streaming": False,
+ "partial": False,
+ },
+ )
+ yield final_response
+ break
elif stream_completed:
- # If we've already seen finish_reason but no usage chunk, exit
- break
+ continue
# Yield the final status as a tuple
yield (stream_completed, tool_calls_complete)
@@ -5139,21 +5238,42 @@ class ChatAgent(BaseAgent):
step_token_usage: Dict[str, int],
response_id: str = "",
tool_call_records: Optional[List[ToolCallingRecord]] = None,
+ reasoning_delta: Optional[str] = None,
) -> ChatAgentResponse:
r"""Create a streaming response using content accumulator."""
# Add new content; only build full content when needed
- accumulator.add_streaming_content(new_content)
+ if new_content:
+ accumulator.add_streaming_content(new_content)
+
if self.stream_accumulate:
message_content = accumulator.get_full_content()
else:
message_content = new_content
+ # Build meta_dict with reasoning information
+ meta_dict: Dict[str, Any] = {}
+
+ # Add reasoning content info
+ full_reasoning = accumulator.get_full_reasoning_content()
+ reasoning_payload: Optional[str] = None
+ if full_reasoning:
+ reasoning_payload = (
+ full_reasoning
+ if self.stream_accumulate
+ else reasoning_delta or ""
+ )
+ if reasoning_payload:
+ meta_dict["is_reasoning"] = accumulator.is_reasoning_phase
+ else:
+ reasoning_payload = None
+
message = BaseMessage(
role_name=self.role_name,
role_type=self.role_type,
- meta_dict={},
+ meta_dict=meta_dict,
content=message_content,
+ reasoning_content=reasoning_payload,
)
return ChatAgentResponse(
diff --git a/camel/configs/deepseek_config.py b/camel/configs/deepseek_config.py
index 447bef034..9375f82e1 100644
--- a/camel/configs/deepseek_config.py
+++ b/camel/configs/deepseek_config.py
@@ -96,13 +96,12 @@ class DeepSeekConfig(BaseConfig):
tool_choice: Optional[Union[dict[str, str], str]] = None
logprobs: Optional[bool] = None
top_logprobs: Optional[int] = None
+ stream_options: Optional[dict[str, bool]] = None
def __init__(self, include_usage: bool = True, **kwargs):
+ if kwargs.get("stream") and "stream_options" not in kwargs:
+ kwargs["stream_options"] = {"include_usage": include_usage}
super().__init__(**kwargs)
- # Only set stream_options when stream is True
- # Otherwise, it will raise error when calling the API
- if self.stream:
- self.stream_options = {"include_usage": include_usage}
DEEPSEEK_API_PARAMS = {param for param in DeepSeekConfig.model_fields.keys()}
diff --git a/camel/messages/base.py b/camel/messages/base.py
index 337c5e3f4..7021db279 100644
--- a/camel/messages/base.py
+++ b/camel/messages/base.py
@@ -71,8 +71,10 @@ class BaseMessage:
images associated with the message. (default: :obj:`auto`)
video_detail (Literal["auto", "low", "high"]): Detail level of the
videos associated with the message. (default: :obj:`auto`)
- parsed: Optional[Union[Type[BaseModel], dict]]: Optional object which
+ parsed (Optional[Union[Type[BaseModel], dict]]): Optional object which
is parsed from the content. (default: :obj:`None`)
+ reasoning_content (Optional[str]): Optional reasoning trace associated
+ with the message. (default: :obj:`None`)
"""
role_name: str
@@ -85,6 +87,7 @@ class BaseMessage:
image_detail: Literal["auto", "low", "high"] = "auto"
video_detail: Literal["auto", "low", "high"] = "auto"
parsed: Optional[Union[BaseModel, dict]] = None
+ reasoning_content: Optional[str] = None
@classmethod
def make_user_message(
@@ -219,6 +222,12 @@ class BaseMessage:
role_type=self.role_type,
meta_dict=self.meta_dict,
content=content,
+ video_bytes=self.video_bytes,
+ image_list=self.image_list,
+ image_detail=self.image_detail,
+ video_detail=self.video_detail,
+ parsed=self.parsed,
+ reasoning_content=self.reasoning_content,
)
def __add__(self, other: Any) -> Union["BaseMessage", Any]:
diff --git a/camel/models/deepseek_model.py b/camel/models/deepseek_model.py
index 882289a90..16faf74dd 100644
--- a/camel/models/deepseek_model.py
+++ b/camel/models/deepseek_model.py
@@ -165,44 +165,6 @@ class DeepSeekModel(OpenAICompatibleModel):
return request_config
- def _post_handle_response(
- self, response: ChatCompletion
- ) -> ChatCompletion:
- r"""Handle reasoning content with <think> tags at the beginning."""
- if (
- self.model_type in [ModelType.DEEPSEEK_REASONER]
- and os.environ.get("GET_REASONING_CONTENT", "false").lower()
- == "true"
- ):
- reasoning_content = response.choices[0].message.reasoning_content # type: ignore[attr-defined]
- combined_content = ( # type: ignore[operator]
- f"<think>\n{reasoning_content}\n</think>\n"
- if reasoning_content
- else ""
- ) + response.choices[0].message.content
-
- response = ChatCompletion.construct(
- id=response.id,
- choices=[
- dict(
- index=response.choices[0].index,
- message={
- "role": response.choices[0].message.role,
- "content": combined_content,
- "tool_calls": None,
- },
- finish_reason=response.choices[0].finish_reason
- if response.choices[0].finish_reason
- else None,
- )
- ],
- created=response.created,
- model=response.model,
- object="chat.completion",
- usage=response.usage,
- )
- return response
-
@observe()
def _run(
self,
@@ -244,7 +206,7 @@ class DeepSeekModel(OpenAICompatibleModel):
**request_config,
)
- return self._post_handle_response(response)
+ return response
@observe()
async def _arun(
@@ -286,4 +248,4 @@ class DeepSeekModel(OpenAICompatibleModel):
**request_config,
)
- return self._post_handle_response(response)
+ return response
diff --git a/camel/models/modelscope_model.py b/camel/models/modelscope_model.py
index 338771421..111fea577 100644
--- a/camel/models/modelscope_model.py
+++ b/camel/models/modelscope_model.py
@@ -13,19 +13,11 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-import time
-from typing import Any, Dict, List, Optional, Union
-
-from openai import AsyncStream, Stream
+from typing import Any, Dict, Optional, Union
from camel.configs import ModelScopeConfig
-from camel.messages import OpenAIMessage
from camel.models.openai_compatible_model import OpenAICompatibleModel
-from camel.types import (
- ChatCompletion,
- ChatCompletionChunk,
- ModelType,
-)
+from camel.types import ModelType
from camel.utils import (
BaseTokenCounter,
api_keys_required,
@@ -96,168 +88,3 @@ class ModelScopeModel(OpenAICompatibleModel):
max_retries=max_retries,
**kwargs,
)
-
- def _post_handle_response(
- self, response: Union[ChatCompletion, Stream[ChatCompletionChunk]]
- ) -> ChatCompletion:
- r"""Handle reasoning content with <think> tags at the beginning."""
- if not isinstance(response, Stream):
- # Handle non-streaming response (existing logic)
- if self.model_config_dict.get("extra_body", {}).get(
- "enable_thinking", False
- ):
- reasoning_content = response.choices[
- 0
- ].message.reasoning_content # type: ignore[attr-defined]
- combined_content = (
- f"<think>\n{reasoning_content}\n</think>\n"
- if reasoning_content
- else ""
- )
- response_content = response.choices[0].message.content or ""
- combined_content += response_content
-
- # Construct a new ChatCompletion with combined content
- return ChatCompletion.construct(
- id=response.id,
- choices=[
- dict(
- finish_reason=response.choices[0].finish_reason,
- index=response.choices[0].index,
- logprobs=response.choices[0].logprobs,
- message=dict(
- role=response.choices[0].message.role,
- content=combined_content,
- ),
- )
- ],
- created=response.created,
- model=response.model,
- object="chat.completion",
- system_fingerprint=response.system_fingerprint,
- usage=response.usage,
- )
- else:
- return response # Return original if no thinking enabled
-
- # Handle streaming response
- accumulated_reasoning = ""
- accumulated_content = ""
- final_chunk = None
- usage_data = None # Initialize usage data
- role = "assistant" # Default role
-
- for chunk in response:
- final_chunk = chunk # Keep track of the last chunk for metadata
- if chunk.choices:
- delta = chunk.choices[0].delta
- if delta.role:
- role = delta.role # Update role if provided
- if (
- hasattr(delta, 'reasoning_content')
- and delta.reasoning_content
- ):
- accumulated_reasoning += delta.reasoning_content
- if delta.content:
- accumulated_content += delta.content
-
- if hasattr(chunk, 'usage') and chunk.usage:
- usage_data = chunk.usage
-
- combined_content = (
- f"<think>\n{accumulated_reasoning}\n</think>\n"
- if accumulated_reasoning
- else ""
- ) + accumulated_content
-
- # Construct the final ChatCompletion object from accumulated
- # stream data
- if final_chunk:
- finish_reason = "stop" # Default finish reason
- logprobs = None
- if final_chunk.choices:
- finish_reason = (
- final_chunk.choices[0].finish_reason or finish_reason
- )
- if hasattr(final_chunk.choices[0], 'logprobs'):
- logprobs = final_chunk.choices[0].logprobs
-
- return ChatCompletion.construct(
- # Use data from the final chunk or defaults
- id=final_chunk.id
- if hasattr(final_chunk, 'id')
- else "streamed-completion",
- choices=[
- dict(
- finish_reason=finish_reason,
- index=0,
- logprobs=logprobs,
- message=dict(
- role=role,
- content=combined_content,
- ),
- )
- ],
- created=final_chunk.created
- if hasattr(final_chunk, 'created')
- else int(time.time()),
- model=final_chunk.model
- if hasattr(final_chunk, 'model')
- else self.model_type,
- object="chat.completion",
- system_fingerprint=final_chunk.system_fingerprint
- if hasattr(final_chunk, 'system_fingerprint')
- else None,
- usage=usage_data,
- )
- else:
- # Handle cases where the stream was empty or invalid
- return ChatCompletion.construct(
- id="empty-stream",
- choices=[
- dict(
- finish_reason="error",
- index=0,
- message=dict(role="assistant", content=""),
- )
- ],
- created=int(time.time()),
- model=self.model_type,
- object="chat.completion",
- usage=usage_data,
- )
-
- def _request_chat_completion(
- self,
- messages: List[OpenAIMessage],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- request_config = self.model_config_dict.copy()
-
- if tools:
- request_config["tools"] = tools
-
- return self._post_handle_response(
- self._client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
- )
-
- async def _arequest_chat_completion(
- self,
- messages: List[OpenAIMessage],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- request_config = self.model_config_dict.copy()
-
- if tools:
- request_config["tools"] = tools
-
- response = await self._async_client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
- return self._post_handle_response(response)
diff --git a/camel/models/qwen_model.py b/camel/models/qwen_model.py
index 4fec46b1a..030b16826 100644
--- a/camel/models/qwen_model.py
+++ b/camel/models/qwen_model.py
@@ -13,19 +13,11 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-import time
-from typing import Any, Dict, List, Optional, Union
-
-from openai import AsyncStream, Stream
+from typing import Any, Dict, Optional, Union
from camel.configs import QwenConfig
-from camel.messages import OpenAIMessage
from camel.models.openai_compatible_model import OpenAICompatibleModel
-from camel.types import (
- ChatCompletion,
- ChatCompletionChunk,
- ModelType,
-)
+from camel.types import ModelType
from camel.utils import (
BaseTokenCounter,
api_keys_required,
@@ -94,168 +86,3 @@ class QwenModel(OpenAICompatibleModel):
max_retries=max_retries,
**kwargs,
)
-
- def _post_handle_response(
- self, response: Union[ChatCompletion, Stream[ChatCompletionChunk]]
- ) -> ChatCompletion:
- r"""Handle reasoning content with <think> tags at the beginning."""
- if not isinstance(response, Stream):
- # Handle non-streaming response (existing logic)
- if self.model_config_dict.get("extra_body", {}).get(
- "enable_thinking", False
- ):
- reasoning_content = response.choices[
- 0
- ].message.reasoning_content # type: ignore[attr-defined]
- combined_content = (
- f"<think>\n{reasoning_content}\n</think>\n"
- if reasoning_content
- else ""
- )
- response_content = response.choices[0].message.content or ""
- combined_content += response_content
-
- # Construct a new ChatCompletion with combined content
- return ChatCompletion.construct(
- id=response.id,
- choices=[
- dict(
- finish_reason=response.choices[0].finish_reason,
- index=response.choices[0].index,
- logprobs=response.choices[0].logprobs,
- message=dict(
- role=response.choices[0].message.role,
- content=combined_content,
- ),
- )
- ],
- created=response.created,
- model=response.model,
- object="chat.completion",
- system_fingerprint=response.system_fingerprint,
- usage=response.usage,
- )
- else:
- return response # Return original if no thinking enabled
-
- # Handle streaming response
- accumulated_reasoning = ""
- accumulated_content = ""
- final_chunk = None
- usage_data = None # Initialize usage data
- role = "assistant" # Default role
-
- for chunk in response:
- final_chunk = chunk # Keep track of the last chunk for metadata
- if chunk.choices:
- delta = chunk.choices[0].delta
- if delta.role:
- role = delta.role # Update role if provided
- if (
- hasattr(delta, 'reasoning_content')
- and delta.reasoning_content
- ):
- accumulated_reasoning += delta.reasoning_content
- if delta.content:
- accumulated_content += delta.content
-
- if hasattr(chunk, 'usage') and chunk.usage:
- usage_data = chunk.usage
-
- combined_content = (
- f"<think>\n{accumulated_reasoning}\n</think>\n"
- if accumulated_reasoning
- else ""
- ) + accumulated_content
-
- # Construct the final ChatCompletion object from accumulated
- # stream data
- if final_chunk:
- finish_reason = "stop" # Default finish reason
- logprobs = None
- if final_chunk.choices:
- finish_reason = (
- final_chunk.choices[0].finish_reason or finish_reason
- )
- if hasattr(final_chunk.choices[0], 'logprobs'):
- logprobs = final_chunk.choices[0].logprobs
-
- return ChatCompletion.construct(
- # Use data from the final chunk or defaults
- id=final_chunk.id
- if hasattr(final_chunk, 'id')
- else "streamed-completion",
- choices=[
- dict(
- finish_reason=finish_reason,
- index=0,
- logprobs=logprobs,
- message=dict(
- role=role,
- content=combined_content,
- ),
- )
- ],
- created=final_chunk.created
- if hasattr(final_chunk, 'created')
- else int(time.time()),
- model=final_chunk.model
- if hasattr(final_chunk, 'model')
- else self.model_type,
- object="chat.completion",
- system_fingerprint=final_chunk.system_fingerprint
- if hasattr(final_chunk, 'system_fingerprint')
- else None,
- usage=usage_data,
- )
- else:
- # Handle cases where the stream was empty or invalid
- return ChatCompletion.construct(
- id="empty-stream",
- choices=[
- dict(
- finish_reason="error",
- index=0,
- message=dict(role="assistant", content=""),
- )
- ],
- created=int(time.time()),
- model=self.model_type,
- object="chat.completion",
- usage=usage_data,
- )
-
- def _request_chat_completion(
- self,
- messages: List[OpenAIMessage],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- request_config = self.model_config_dict.copy()
-
- if tools:
- request_config["tools"] = tools
-
- return self._post_handle_response(
- self._client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
- )
-
- async def _arequest_chat_completion(
- self,
- messages: List[OpenAIMessage],
- tools: Optional[List[Dict[str, Any]]] = None,
- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- request_config = self.model_config_dict.copy()
-
- if tools:
- request_config["tools"] = tools
-
- response = await self._async_client.chat.completions.create(
- messages=messages,
- model=self.model_type,
- **request_config,
- )
- return self._post_handle_response(response)
diff --git a/examples/agents/chatagent_stream.py b/examples/agents/chatagent_stream.py
index a8dbb2ab3..f1e72d63e 100644
--- a/examples/agents/chatagent_stream.py
+++ b/examples/agents/chatagent_stream.py
@@ -15,7 +15,7 @@ from camel.agents import ChatAgent
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
-# Create a streaming model
+# Create a streaming-capable model backend
streaming_model = ModelFactory.create(
model_platform=ModelPlatformType.DEFAULT,
model_type=ModelType.GPT_4O_MINI,
@@ -32,33 +32,49 @@ agent_accumulated = ChatAgent(
)
# Example user message
-user_message = "Tell me about the benefits of renewable energy and how "
-"it impacts the environment."
+user_message = "How many Rs are there in the word 'strawberry'?"
-# Get streaming response
+# Accumulated streaming mode (default)
streaming_response = agent_accumulated.step(user_message)
-# Stream the response chunks
for chunk_response in streaming_response:
- # Each chunk_response is a ChatAgentResponse with incremental content
- chunk_content = chunk_response.msgs[0].content
- print(chunk_content, end="", flush=True)
+ message = chunk_response.msgs[0]
+ reasoning_text = message.reasoning_content
+ if reasoning_text:
+ print(reasoning_text, end="", flush=True)
+ content_text = message.content
+ if content_text:
+ print(content_text, end="", flush=True)
+usage = streaming_response.info.get("usage", {})
+print(
+ f"\n\nUsage: prompt={usage.get('prompt_tokens')}, "
+ f"completion={usage.get('completion_tokens')}, "
+ f"total={usage.get('total_tokens')}"
+)
print("\n\n---\nDelta streaming mode (stream_accumulate=False):\n")
-# Create an agent that yields delta chunks instead of accumulated content
+# Delta streaming mode (only new content per chunk)
agent_delta = ChatAgent(
system_message="You are a helpful assistant that provides concise "
"and informative responses.",
model=streaming_model,
- stream_accumulate=False, # Only yield the delta part per chunk
+ stream_accumulate=False,
)
-# Get streaming response (delta chunks)
streaming_response_delta = agent_delta.step(user_message)
-# Stream only the delta content per chunk; printing reconstructs the full text
for chunk_response in streaming_response_delta:
- delta_content = chunk_response.msgs[0].content
- print(delta_content, end="", flush=True)
-print()
+ message = chunk_response.msgs[0]
+ reasoning_delta = message.reasoning_content or ""
+ if reasoning_delta:
+ print(reasoning_delta, end="", flush=True)
+
+ if message.content:
+ print(message.content, end="", flush=True)
+usage = streaming_response.info.get("usage", {})
+print(
+ f"\n\nUsage: prompt={usage.get('prompt_tokens')}, "
+ f"completion={usage.get('completion_tokens')}, "
+ f"total={usage.get('total_tokens')}"
+)
diff --git a/examples/models/deepseek_chat_model_example.py b/examples/models/deepseek_chat_model_example.py
index 4f2b67179..e9ea68e45 100644
--- a/examples/models/deepseek_chat_model_example.py
+++ b/examples/models/deepseek_chat_model_example.py
@@ -38,6 +38,7 @@ user_msg = """How many Rs are there in the word 'strawberry'?"""
# Get response information
response = camel_agent.step(user_msg)
+print(response.msgs[0].reasoning_content)
print(response.msgs[0].content)
# ruff: noqa: E501
diff --git a/examples/models/deepseek_reasoner_model_example.py b/examples/models/deepseek_reasoner_model_example.py
index a2bcffc00..5db82eb6a 100644
--- a/examples/models/deepseek_reasoner_model_example.py
+++ b/examples/models/deepseek_reasoner_model_example.py
@@ -39,6 +39,7 @@ user_msg = """How many Rs are there in the word 'strawberry'?"""
# Get response information
response = camel_agent.step(user_msg)
+print(response.msgs[0].reasoning_content)
print(response.msgs[0].content)
'''
===============================================================================