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: gemini stream; cicd; ernie naming (#3460)

Wendong-Fan151d70833b7e
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.yml
index 325307a01..3e73d2191 100644
--- a/.github/workflows/build_package.yml
+++ b/.github/workflows/build_package.yml
@@ -148,6 +148,7 @@ jobs:
CRYNUX_API_KEY: "${{ secrets.CRYNUX_API_KEY }}"
NEBIUS_API_KEY: "${{ secrets.NEBIUS_API_KEY }}"
COMETAPI_KEY: "${{ secrets.COMETAPI_KEY }}"
+ CEREBRAS_API_KEY: "${{ secrets.CEREBRAS_API_KEY }}"
run: |
source test_venv/bin/activate
pytest --fast-test-mode -m "not heavy_dependency" \
diff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.yml
index dedbdc7d8..f985f9487 100644
--- a/.github/workflows/pytest_package.yml
+++ b/.github/workflows/pytest_package.yml
@@ -91,6 +91,7 @@ jobs:
NEBIUS_API_KEY: "${{ secrets.NEBIUS_API_KEY }}"
COMETAPI_KEY: "${{ secrets.COMETAPI_KEY }}"
MINIMAX_API_KEY: "${{ secrets.MINIMAX_API_KEY }}"
+ CEREBRAS_API_KEY: "${{ secrets.CEREBRAS_API_KEY }}"
run: |
source .venv/bin/activate
uv pip install -e ".[all, dev, docs]"
@@ -179,6 +180,7 @@ jobs:
AMD_API_KEY: "${{ secrets.AMD_API_KEY }}"
COMETAPI_KEY: "${{ secrets.COMETAPI_KEY }}"
MINIMAX_API_KEY: "${{ secrets.MINIMAX_API_KEY }}"
+ CEREBRAS_API_KEY: "${{ secrets.CEREBRAS_API_KEY }}"
run: |
source .venv/bin/activate
uv pip install -e ".[all, dev, docs]"
@@ -265,6 +267,7 @@ jobs:
AMD_API_KEY: "${{ secrets.AMD_API_KEY }}"
COMETAPI_KEY: "${{ secrets.COMETAPI_KEY }}"
MINIMAX_API_KEY: "${{ secrets.MINIMAX_API_KEY }}"
+ CEREBRAS_API_KEY: "${{ secrets.CEREBRAS_API_KEY }}"
run: |
source .venv/bin/activate
uv pip install -e ".[all, dev, docs]"
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 97f7c7968..5b4aa42eb 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -4916,7 +4916,12 @@ class ChatAgent(BaseAgent):
return
# Handle streaming response
- if isinstance(response, AsyncStream):
+ # Note: Also check for async generators since some model backends
+ # (e.g., GeminiModel) wrap AsyncStream in async generators for
+ # additional processing
+ if isinstance(response, AsyncStream) or inspect.isasyncgen(
+ response
+ ):
stream_completed = False
tool_calls_complete = False
@@ -5121,7 +5126,10 @@ class ChatAgent(BaseAgent):
async def _aprocess_stream_chunks_with_accumulator(
self,
- stream: AsyncStream[ChatCompletionChunk],
+ stream: Union[
+ AsyncStream[ChatCompletionChunk],
+ AsyncGenerator[ChatCompletionChunk, None],
+ ],
content_accumulator: StreamContentAccumulator,
accumulated_tool_calls: Dict[str, Any],
tool_call_records: List[ToolCallingRecord],
diff --git a/camel/toolkits/image_analysis_toolkit.py b/camel/toolkits/image_analysis_toolkit.py
index ab1120806..58bc80aae 100644
--- a/camel/toolkits/image_analysis_toolkit.py
+++ b/camel/toolkits/image_analysis_toolkit.py
@@ -12,9 +12,6 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
-# Enables postponed evaluation of annotations (for string-based type hints)
-from __future__ import annotations
-
from io import BytesIO
from typing import List, Optional
from urllib.parse import urlparse
diff --git a/camel/toolkits/slack_toolkit.py b/camel/toolkits/slack_toolkit.py
index 051db2bfd..0b26a71e1 100644
--- a/camel/toolkits/slack_toolkit.py
+++ b/camel/toolkits/slack_toolkit.py
@@ -11,19 +11,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
-
-from __future__ import annotations
-
import json
import os
+from ssl import SSLContext
from typing import TYPE_CHECKING, List, Optional
from camel.toolkits.base import BaseToolkit
from camel.utils import MCPServer
if TYPE_CHECKING:
- from ssl import SSLContext
-
from slack_sdk import WebClient
from camel.logger import get_logger
@@ -57,7 +53,7 @@ class SlackToolkit(BaseToolkit):
self,
slack_token: Optional[str] = None,
ssl: Optional[SSLContext] = None,
- ) -> WebClient:
+ ) -> "WebClient":
r"""Authenticate using the Slack API.
Args:
diff --git a/examples/models/ernie_model_example.py b/examples/models/ernie_model_example.py
new file mode 100644
index 000000000..ea56fac9c
--- /dev/null
+++ b/examples/models/ernie_model_example.py
@@ -0,0 +1,109 @@
+# ========= 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. =========
+
+"""
+ERNIE Model Examples for CAMEL AI
+
+This module demonstrates how to use Baidu Qianfan Platform's ERNIE models
+within the CAMEL AI framework, including:
+1. Single agent with ERNIE 5.0 Thinking model (reasoning capabilities)
+2. Vision model with ERNIE 4.5 VL (image understanding)
+
+Prerequisites:
+ - Install CAMEL AI: pip install camel-ai
+ - Set environment variable: export QIANFAN_API_KEY="Your Qianfan Key"
+ - Get API key from: https://console.bce.baidu.com/qianfan/overview
+"""
+
+from camel.agents import ChatAgent
+from camel.configs import QianfanConfig
+from camel.messages import BaseMessage
+from camel.models import ModelFactory
+from camel.types import ModelPlatformType, ModelType
+
+# ============================================================================
+# Example 1: Single Agent with ERNIE 5.0 Thinking Model (Reasoning)
+# ============================================================================
+# ERNIE 5.0 Thinking is a reasoning model that provides both the thinking
+# process (reasoning_content) and the final response (content).
+
+print("=" * 60)
+print("Example 1: Single Agent with ERNIE 5.0 Thinking Model")
+print("=" * 60)
+
+# Create ERNIE 5.0 Thinking model configuration
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.QIANFAN,
+ model_type=ModelType.ERNIE_5_0_THINKING,
+ model_config_dict=QianfanConfig(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 = "Explain how Generative AI works."
+
+# Get response information
+response = camel_agent.step(user_msg)
+
+# For reasoning models, access both reasoning process and final response
+print("\n--- Reasoning Process ---")
+print(response.msgs[0].reasoning_content)
+print("\n--- Final Response ---")
+print(response.msgs[0].content)
+
+
+# ============================================================================
+# Example 2: Vision Model with ERNIE 4.5 VL
+# ============================================================================
+# ERNIE 4.5 VL is a vision-language model capable of understanding
+# and describing images.
+
+print("\n" + "=" * 60)
+print("Example 2: Vision Model with ERNIE 4.5 VL")
+print("=" * 60)
+
+# Create ERNIE 4.5 VL model configuration
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.QIANFAN,
+ model_type=ModelType.ERNIE_4_5_TURBO_VL,
+ model_config_dict=QianfanConfig(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)
+
+# Prepare image URLs
+image_list = [
+ "https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png"
+]
+
+# Create user message with image
+user_msg = BaseMessage.make_user_message(
+ role_name="User",
+ content="Please tell me what is in the image!",
+ image_list=image_list,
+)
+
+# Get response information
+response = camel_agent.step(user_msg)
+
+print("\n--- Image Description ---")
+print(response.msgs[0].content)
diff --git a/examples/models/qianfan_model_example.py b/examples/models/qianfan_model_example.py
deleted file mode 100644
index 343167fa4..000000000
--- a/examples/models/qianfan_model_example.py
+++ /dev/null
@@ -1,271 +0,0 @@
-# ========= 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 QianfanConfig
-from camel.messages import BaseMessage
-from camel.models import ModelFactory
-from camel.types import ModelPlatformType, ModelType
-
-# Create ERNIE 5.0 Thinking model configuration
-model = ModelFactory.create(
- model_platform=ModelPlatformType.QIANFAN,
- model_type=ModelType.ERNIE_5_0_THINKING,
- model_config_dict=QianfanConfig(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 = """Explain how Generative AI works."""
-
-# Get response information
-response = camel_agent.step(user_msg)
-print(response.msgs[0].reasoning_content)
-print("========================")
-print(response.msgs[0].content)
-
-'''
-===============================================================================
-The user is asking for an explanation of how Generative AI works. This is a
-broad topic, so I should start with a simple analogy to make it accessible,
-then gradually introduce more technical details.
-
-The analogy of a student learning from examples seems effective—it mirrors how
-generative models learn patterns from data. I can break it down into three key
-stages: training, learning representations, and generating new content.
-
-For the technical part, I should focus on the most common architectures like
-GANs and Transformers, since they represent distinct approaches (adversarial
-training vs. probabilistic modeling). It's important to highlight the role of
-latent space and probability distributions, as these are core to how
-generative models create novel outputs.
-
-I'll wrap up with a concise summary to reinforce the main ideas. The goal is
-to balance clarity with depth, avoiding unnecessary jargon unless I explain
-it.
-========================
-Let's break down how Generative AI works, from a simple analogy to the
-technical core.
-
-### The Simple Analogy: A Student Learning Art
-
-Imagine a student who wants to learn to paint like "Impressionist Art."
-
-1. **Training**: You show this student thousands of paintings by Monet,
-Renoir, and others. They don't memorize any single painting. Instead, they
-start to understand the *concepts*: the brush strokes, the use of light,
-the color palettes, the common subjects (water lilies, Parisian streets).
-2. **Learning the "Essence"**: The student's brain forms an internal,
-abstract understanding of what makes an Impressionist painting. This
-isn't a specific image; it's the *idea* or *essence* of Impressionism.
-3. **Creating (Generating)**: Now, you ask the student: "Paint me a *new*
-picture in the style of Impressionism." Using their learned internal model,
-they create a completely original painting that has never existed before,
-but it still looks like a valid Impressionist piece because it follows the
-rules and patterns they learned.
-
-**Generative AI works exactly like this student.** It is trained on a massive
-dataset (e.g., all of Wikipedia, a huge collection of images, or a library of
-music). It doesn't store copies of this data; instead, it learns the
-underlying patterns, relationships, and "essence" of the data. Then,
-it uses this learned model to create new, original content that fits
-within those patterns.
-
----
-
-### The Technical Core: How It Actually Works
-
-The "magic" happens through a type of machine learning model called a
-**neural network**, specifically designed for generation. The two most
-important architectures are **Generative Adversarial Networks (GANs)** and
-**Transformers** (which power models like GPT and DALL-E).
-Let's look at the process in three key stages:
-#### Stage 1: Training - Learning the Patterns
-
-The model is fed a colossal amount of data—text, images, audio, etc. The goal
-is for the model to learn the probability distribution of the data. In simpler
-terms, it learns what kinds of things are likely to appear together.
-
-* **For Text**: It learns that the word "sky" is often followed by "is,"
- "was," or "blue," and almost never by "photosynthesis."
-* **For Images**: It learns that in a face, eyes are usually above a nose,
- and a nose is usually above a mouth. It learns the texture of fur, the
- shape of a wheel, the reflection of light on water.
-
-#### Stage 2: The Model's Internal "World" (Latent Space)
-
-This is a crucial concept. The model doesn't just store a giant database of
-images or text. It compresses the information it learns into a much smaller,
-dense, and abstract mathematical representation called **latent space**.
-
-Think of latent space as a vast map of all possible concepts the model has
-learned.
-* For a face-generating AI, this map would have regions for "smiling faces,"
- "faces with glasses," "faces with long hair," etc.
-* Moving a little in one direction on this map might change a person's
- expression from neutral to smiling. Moving in another direction might add
- glasses.
-
-This space is continuous, meaning every point in it corresponds to a valid
-piece of data (e.g., a specific face that doesn't exist but *could* exist).
-
-#### Stage 3: Generation - Creating Something New
-
-When you give a generative AI a prompt (e.g., "a cat wearing a top hat"), it
-uses this learned model and latent space to create a response.
-
-**Interpretation**: The model converts your text prompt into a mathematical
-representation that points to a specific region in its latent space. In
-this case, it's the area where "cat" and "top hat" concepts overlap.
-
-**Sampling**: The model then picks a random point within that targeted
-region of the latent space. Because the space is continuous, this point is
-unique and haven't been seen before.
-
-**Decoding**: The model then works backwards, using its neural network as a
-"decoder." It takes this abstract point from latent space and gradually
-transforms it into a concrete output—pixel by pixel for an image, or word
-by word for text.
-
-This is why you can ask for "a photorealistic cat wearing a red top hat,
-sitting on a couch," and it will generate a unique image that fits that
-description. It's not copying and pasting; it's navigating its internal "map"
-of concepts to a new location and then rendering what that location looks like.
-
----
-
-### Two Key Architectural Approaches
-
-1. **Generative Adversarial Networks (GANs) - Common for Images**
-* **How it works**: A GAN pits two neural networks against each other in a
- game.
-* **The Generator**: Tries to create fake data (e.g., a fake human face).
-* **The Discriminator**: Tries to tell the difference between real data
- (from the training set) and fake data from the Generator.
-* **The "Adversarial" Process**: The Generator gets better at fooling the
- Discriminator, and the Discriminator gets better at catching fakes. This
- competition drives both to become incredibly sophisticated, resulting in
- highly realistic generated content.
-
-2. **Transformers & Autoregressive Models - Common for Text (e.g., ChatGPT)**
-* **How it works**: These models predict the next most likely piece of data
- in a sequence.
-* **For Text**: Given the prompt "The cat sat on the," the model calculates
- the probability for every possible next word. "Mat" might have a high
- probability, "cloud" a very low one. It then selects a word (sometimes
- introducing a bit of randomness for creativity) and repeats the process:
- "The cat sat on the mat. It then..." and so on, word by word, until it
- generates a complete response.
-* **For Images (like DALL-E)**: They can treat a n image as a sequence of
- patches and predict the next patch based on the previous ones and the text
- prompt.
-
-### Summary
-
-In a nutshell, Generative AI works by:
-
-1. **Training** on a massive dataset to learn the underlying patterns.
-2. **Compressing** this knowledge into an abstract mathematical space
- (latent space).
-3. **Generating** new content by navigating this space based on a user's
- prompt and then decoding that abstract point into a concrete output
- (text, image, sound, etc.).
-
-It's a powerful form of pattern recognition and probabilistic creation, not
-conscious thought or understanding.
-===============================================================================
-'''
-
-# Create ERNIE 4.5 VL model configuration
-model = ModelFactory.create(
- model_platform=ModelPlatformType.QIANFAN,
- model_type=ModelType.ERNIE_4_5_TURBO_VL,
- model_config_dict=QianfanConfig(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)
-
-image_list = [
- "https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png"
-]
-
-user_msg = BaseMessage.make_user_message(
- role_name="User",
- content="Please tell me what is in the image!",
- image_list=image_list,
-)
-
-# Get response information
-response = camel_agent.step(user_msg)
-print(response.msgs[0].reasoning_content)
-print("========================")
-print(response.msgs[0].content)
-
-"""
-===============================================================================
-Okay, the user has shared an image with the logo of "CAMEL-AI" which features
-a stylized camel and the text "CAMEL-AI" in purple.
-First, I need to recognize that this is a brand logo. The camel is a
-well-known symbol associated with endurance and reliability, often used by
-brands that want to convey strength or dependability. The text is in
-uppercase, bold, and purple - a color that can represent creativity, wisdom,
-or luxury. The user's request is straightforward: they want to know what's in
-the image. Since this is a simple identification task, I should respond
-clearly and directly. No need for complex analysis unless the user asks for
-deeper interpretation. I notice the user didn't provide any context about
-why they're asking. Maybe they encountered this logo somewhere and got
-curious, or perhaps they're testing my recognition abilities. Either way,
-keeping the response simple and factual is best. The logo appears clean and
-professional - likely from a tech or AI company given the "-AI" suffix. But
-since the user didn't ask for brand analysis, I'll stick to basic description.
-Hmm, should I mention possible interpretations of the camel symbolism? Probably
-not - that would be overcomplicating a simple identification request. If the
-user wants more, they'll ask.
-Final decision: Just state the obvious elements - camel icon and "CAMEL-AI"
-text in purple. Keep it concise.
-
-========================
-The image shows the **logo for "CAMEL-AI"**, featuring two primary elements:
-
-1. **Stylized Camel Icon**:
- On the left, a minimalist silhouette of a camel in **solid purple**.
- The design is abstract, emphasizing the camel's distinctive hump and
- profile with clean lines and no intricate details.
-
-2. **Text "CAMEL-AI"**:
- To the right of the camel icon, the words "**CAMEL-AI**" are written in
- **bold, uppercase purple letters**. The font is modern, sans-serif, and
- evenly spaced, matching the camel icon's color for visual unity.
-
-### Key Details:
-- **Color Scheme**: Uniform purple (#4A248E or similar) on a white background,
- creating high contrast and a professional aesthetic.
-- **Design Style**: Minimalist and geometric, prioritizing simplicity and
- clarity.
-- **Symbolism**: The camel likely represents endurance, reliability, or
-"carrying" intelligence, while "-AI" explicitly denotes a focus on artificial
-intelligence.
-
-This logo likely represents a tech company, platform, or initiative bridging
-the identity of "Camel" with AI innovation.
-===============================================================================
-"""
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index 2e6f7538e..d3955ae90 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -1442,3 +1442,315 @@ def test_memory_setter_preserves_system_message():
assert len(new_context) > 0
assert new_context[0]['role'] == 'system'
assert new_context[0]['content'] == system_content
+
+
+async def test_chat_agent_async_stream_with_async_generator():
+ r"""Test async streaming when model backend returns an async generator."""
+ from typing import AsyncGenerator
+
+ from openai.types.chat.chat_completion_chunk import (
+ ChatCompletionChunk,
+ ChoiceDelta,
+ )
+ from openai.types.chat.chat_completion_chunk import (
+ Choice as ChunkChoice,
+ )
+
+ system_msg = BaseMessage(
+ role_name="assistant",
+ role_type=RoleType.ASSISTANT,
+ meta_dict=None,
+ content="You are a helpful assistant.",
+ )
+
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_5_MINI,
+ model_config_dict={"stream": True},
+ )
+
+ agent = ChatAgent(system_message=system_msg, model=model)
+
+ # Create mock streaming chunks
+ chunks = [
+ ChatCompletionChunk(
+ id="chatcmpl-mock-id",
+ choices=[
+ ChunkChoice(
+ delta=ChoiceDelta(content="Hello", role="assistant"),
+ index=0,
+ finish_reason=None,
+ )
+ ],
+ created=1234567890,
+ model="gpt-5-mini",
+ object="chat.completion.chunk",
+ ),
+ ChatCompletionChunk(
+ id="chatcmpl-mock-id",
+ choices=[
+ ChunkChoice(
+ delta=ChoiceDelta(content=" world"),
+ index=0,
+ finish_reason=None,
+ )
+ ],
+ created=1234567890,
+ model="gpt-5-mini",
+ object="chat.completion.chunk",
+ ),
+ ChatCompletionChunk(
+ id="chatcmpl-mock-id",
+ choices=[
+ ChunkChoice(
+ delta=ChoiceDelta(content="!"),
+ index=0,
+ finish_reason="stop",
+ )
+ ],
+ created=1234567890,
+ model="gpt-5-mini",
+ object="chat.completion.chunk",
+ usage={
+ "completion_tokens": 3,
+ "prompt_tokens": 10,
+ "total_tokens": 13,
+ },
+ ),
+ ]
+
+ # Create an async generator that wraps the chunks
+ # This simulates what GeminiModel does with _wrap_async_stream_with_
+ # thought_preservation
+ async def mock_async_generator() -> (
+ AsyncGenerator[ChatCompletionChunk, None]
+ ):
+ for chunk in chunks:
+ yield chunk
+
+ # Mock the model_backend.arun method to return an async generator
+ # (not AsyncStream). This simulates GeminiModel behavior.
+ agent.model_backend.arun = AsyncMock(return_value=mock_async_generator())
+
+ user_msg = BaseMessage(
+ role_name="User",
+ role_type=RoleType.USER,
+ meta_dict=dict(),
+ content="Say hello",
+ )
+
+ # Use astep to test async streaming. astep returns
+ # AsyncStreamingChatAgentResponse which supports async iteration.
+ responses = []
+ streaming_response = await agent.astep(user_msg)
+ async for response in streaming_response:
+ responses.append(response)
+
+ # Verify we got streaming responses
+ assert len(responses) > 0, "Should receive streaming responses"
+
+ # Verify final response contains the accumulated content
+ final_response = responses[-1]
+ assert (
+ final_response.msg is not None
+ ), "Final response should have a message"
+ assert (
+ "Hello" in final_response.msg.content
+ ), "Final content should contain 'Hello'"
+
+
+async def test_chat_agent_async_stream_with_async_generator_tool_calls():
+ r"""Test async streaming with tool calls when model returns async
+ generator.
+ """
+ from typing import AsyncGenerator
+
+ from openai.types.chat.chat_completion_chunk import (
+ ChatCompletionChunk,
+ ChoiceDelta,
+ ChoiceDeltaToolCall,
+ ChoiceDeltaToolCallFunction,
+ )
+ from openai.types.chat.chat_completion_chunk import (
+ Choice as ChunkChoice,
+ )
+
+ system_msg = BaseMessage(
+ role_name="assistant",
+ role_type=RoleType.ASSISTANT,
+ meta_dict=None,
+ content="You are a helpful assistant.",
+ )
+
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_5_MINI,
+ model_config_dict={"stream": True},
+ )
+
+ # Define a simple test tool
+ def test_add(a: int, b: int) -> int:
+ r"""Add two numbers.
+
+ Args:
+ a (int): First number.
+ b (int): Second number.
+
+ Returns:
+ int: Sum of a and b.
+ """
+ return a + b
+
+ agent = ChatAgent(
+ system_message=system_msg,
+ model=model,
+ tools=[FunctionTool(test_add)],
+ )
+
+ # Create mock streaming chunks with tool calls
+ tool_call_chunks = [
+ ChatCompletionChunk(
+ id="chatcmpl-mock-id",
+ choices=[
+ ChunkChoice(
+ delta=ChoiceDelta(
+ role="assistant",
+ tool_calls=[
+ ChoiceDeltaToolCall(
+ index=0,
+ id="call_mock_123",
+ type="function",
+ function=ChoiceDeltaToolCallFunction(
+ name="test_add",
+ arguments='{"a":',
+ ),
+ )
+ ],
+ ),
+ index=0,
+ finish_reason=None,
+ )
+ ],
+ created=1234567890,
+ model="gpt-5-mini",
+ object="chat.completion.chunk",
+ ),
+ ChatCompletionChunk(
+ id="chatcmpl-mock-id",
+ choices=[
+ ChunkChoice(
+ delta=ChoiceDelta(
+ tool_calls=[
+ ChoiceDeltaToolCall(
+ index=0,
+ function=ChoiceDeltaToolCallFunction(
+ arguments='1,"b":2}',
+ ),
+ )
+ ],
+ ),
+ index=0,
+ finish_reason=None,
+ )
+ ],
+ created=1234567890,
+ model="gpt-5-mini",
+ object="chat.completion.chunk",
+ ),
+ ChatCompletionChunk(
+ id="chatcmpl-mock-id",
+ choices=[
+ ChunkChoice(
+ delta=ChoiceDelta(),
+ index=0,
+ finish_reason="tool_calls",
+ )
+ ],
+ created=1234567890,
+ model="gpt-5-mini",
+ object="chat.completion.chunk",
+ usage={
+ "completion_tokens": 10,
+ "prompt_tokens": 20,
+ "total_tokens": 30,
+ },
+ ),
+ ]
+
+ # Create final response chunks after tool execution
+ final_chunks = [
+ ChatCompletionChunk(
+ id="chatcmpl-mock-id-2",
+ choices=[
+ ChunkChoice(
+ delta=ChoiceDelta(
+ content="The result is 3.", role="assistant"
+ ),
+ index=0,
+ finish_reason="stop",
+ )
+ ],
+ created=1234567891,
+ model="gpt-5-mini",
+ object="chat.completion.chunk",
+ usage={
+ "completion_tokens": 5,
+ "prompt_tokens": 30,
+ "total_tokens": 35,
+ },
+ ),
+ ]
+
+ call_count = 0
+
+ async def mock_async_generator() -> (
+ AsyncGenerator[ChatCompletionChunk, None]
+ ):
+ nonlocal call_count
+ if call_count == 0:
+ call_count += 1
+ for chunk in tool_call_chunks:
+ yield chunk
+ else:
+ for chunk in final_chunks:
+ yield chunk
+
+ # Mock the model_backend.arun method to return an async generator
+ agent.model_backend.arun = AsyncMock(
+ side_effect=lambda *args, **kwargs: mock_async_generator()
+ )
+
+ user_msg = BaseMessage(
+ role_name="User",
+ role_type=RoleType.USER,
+ meta_dict=dict(),
+ content="Add 1 and 2",
+ )
+
+ # Use astep to test async streaming with tool calls
+ responses = []
+ streaming_response = await agent.astep(user_msg)
+ async for response in streaming_response:
+ responses.append(response)
+
+ # Verify we got responses
+ assert len(responses) > 0, "Should receive streaming responses"
+
+ # Verify tool was called
+ tool_calls_found = False
+ for response in responses:
+ if response.info and "tool_calls" in response.info:
+ tool_calls = response.info["tool_calls"]
+ if tool_calls:
+ tool_calls_found = True
+ # Verify the tool was executed correctly
+ for tc in tool_calls:
+ if tc.tool_name == "test_add":
+ assert tc.result == 3, "Tool should return 3"
+ break
+
+ assert tool_calls_found, "Tool calls should be found in responses"