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

2.3 KB
Raw Blame
<a id="camel.utils.stream_utils"></a>

Utilities to detect and normalize streaming chat responses.

<a id="camel.utils.stream_utils._is_cumulative"></a>

## _is_cumulative

```python
def _is_cumulative(contents: list[str]):
```

Return True if each string is a prefix of the next one.

Streaming heuristic:
- Gather non-empty `__INLINE_CODE_0__` pieces.
- If each later piece strictly grows in length and starts with the
previous, treat as cumulative (covers `__INLINE_CODE_1__`) and
use the last piece.
- Otherwise concatenate pieces in order (covers delta streaming).

<a id="camel.utils.stream_utils._finalize_stream"></a>

## _finalize_stream

```python
def _finalize_stream(
    contents: list[str],
    final_response: Optional[ChatAgentResponse]
):
```

Normalize collected chunks into `__INLINE_CODE_0__`.

<a id="camel.utils.stream_utils._collect_sync_stream"></a>

## _collect_sync_stream

```python
def _collect_sync_stream(
    response: Any,
    stream_callback: Optional[Callable[[ChatAgentResponse], None]] = None
):
```

Collect chunks from a sync streaming response.

<a id="camel.utils.stream_utils.is_streaming_response"></a>

## is_streaming_response

```python
def is_streaming_response(response: Any):
```

Return True when the object represents a streaming sync response.

**Parameters:**

- **response**: Any object that might represent a chat response. Typically a `__INLINE_CODE_0____INLINE_CODE_1____INLINE_CODE_2__` attribute.

**Returns:**

  bool: `__INLINE_CODE_0__` if the object is detected as streaming according
to the heuristics above, otherwise `__INLINE_CODE_1__`.

<a id="camel.utils.stream_utils.consume_response_content"></a>

## consume_response_content

```python
def consume_response_content(
    response: Any,
    stream_callback: Optional[Callable[[ChatAgentResponse], None]] = None
):
```

Consume a sync response (streaming or not) into final_response, content.

**Parameters:**

- **response**: A complete `__INLINE_CODE_0__` or a streaming iterable of partial responses/chunks.
- **stream_callback**: Optional callable executed for each chunk as it is produced. The callback receives the current chunk and is intended for side effects such as logging or UI updates.

**Returns:**

  Tuple[Any, str]: The last chunk observed (or an empty
`__INLINE_CODE_0__` placeholder) and the assembled message
content string derived from the streamed pieces.