2024-05-09 15:40:36 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-05-26 17:22:51 +01:00
|
|
|
|
2025-06-06 10:17:02 +02:00
|
|
|
import pytest
|
2023-10-26 17:42:52 +02:00
|
|
|
|
2025-06-06 10:17:02 +02:00
|
|
|
from haystack.dataclasses import StreamingChunk, ComponentInfo, ToolCallDelta, ToolCallResult, ToolCall
|
2025-05-27 12:23:40 +02:00
|
|
|
from haystack import component
|
|
|
|
from haystack import Pipeline
|
|
|
|
|
|
|
|
|
|
|
|
@component
|
2025-06-06 10:17:02 +02:00
|
|
|
class ExampleComponent:
|
2025-05-27 12:23:40 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.name = "test_component"
|
|
|
|
|
|
|
|
def run(self) -> str:
|
|
|
|
return "Test content"
|
2023-10-26 17:42:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_chunk_with_content_and_metadata():
|
2023-12-21 17:09:58 +05:30
|
|
|
chunk = StreamingChunk(content="Test content", meta={"key": "value"})
|
2023-10-26 17:42:52 +02:00
|
|
|
|
|
|
|
assert chunk.content == "Test content"
|
2023-12-21 17:09:58 +05:30
|
|
|
assert chunk.meta == {"key": "value"}
|
2023-10-26 17:42:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_chunk_with_only_content():
|
|
|
|
chunk = StreamingChunk(content="Test content")
|
|
|
|
|
|
|
|
assert chunk.content == "Test content"
|
2023-12-21 17:09:58 +05:30
|
|
|
assert chunk.meta == {}
|
2023-10-26 17:42:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_access_content():
|
2023-12-21 17:09:58 +05:30
|
|
|
chunk = StreamingChunk(content="Test content", meta={"key": "value"})
|
2023-10-26 17:42:52 +02:00
|
|
|
assert chunk.content == "Test content"
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_chunk_with_empty_content():
|
|
|
|
chunk = StreamingChunk(content="")
|
|
|
|
assert chunk.content == ""
|
2023-12-21 17:09:58 +05:30
|
|
|
assert chunk.meta == {}
|
2025-05-27 12:23:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_chunk_with_all_fields():
|
|
|
|
component_info = ComponentInfo(type="test.component", name="test_component")
|
|
|
|
chunk = StreamingChunk(content="Test content", meta={"key": "value"}, component_info=component_info)
|
|
|
|
|
|
|
|
assert chunk.content == "Test content"
|
|
|
|
assert chunk.meta == {"key": "value"}
|
|
|
|
assert chunk.component_info == component_info
|
|
|
|
|
|
|
|
|
2025-06-06 10:17:02 +02:00
|
|
|
def test_create_chunk_with_content_and_tool_call():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
# Can't have content + tool_call at the same time
|
|
|
|
StreamingChunk(
|
|
|
|
content="Test content",
|
|
|
|
meta={"key": "value"},
|
2025-06-23 10:14:25 +02:00
|
|
|
tool_calls=[ToolCallDelta(id="123", tool_name="test_tool", arguments='{"arg1": "value1"}', index=0)],
|
2025-06-06 10:17:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_chunk_with_content_and_tool_call_result():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
# Can't have content + tool_call_result at the same time
|
|
|
|
StreamingChunk(
|
|
|
|
content="Test content",
|
|
|
|
meta={"key": "value"},
|
|
|
|
tool_call_result=ToolCallResult(
|
|
|
|
result="output",
|
|
|
|
origin=ToolCall(id="123", tool_name="test_tool", arguments={"arg1": "value1"}),
|
|
|
|
error=False,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2025-05-27 12:23:40 +02:00
|
|
|
def test_component_info_from_component():
|
2025-06-06 10:17:02 +02:00
|
|
|
component = ExampleComponent()
|
2025-05-27 12:23:40 +02:00
|
|
|
component_info = ComponentInfo.from_component(component)
|
2025-06-06 10:17:02 +02:00
|
|
|
assert component_info.type == "test_streaming_chunk.ExampleComponent"
|
2025-05-27 12:23:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_component_info_from_component_with_name_from_pipeline():
|
|
|
|
pipeline = Pipeline()
|
2025-06-06 10:17:02 +02:00
|
|
|
component = ExampleComponent()
|
2025-05-27 12:23:40 +02:00
|
|
|
pipeline.add_component("pipeline_component", component)
|
|
|
|
component_info = ComponentInfo.from_component(component)
|
2025-06-06 10:17:02 +02:00
|
|
|
assert component_info.type == "test_streaming_chunk.ExampleComponent"
|
2025-05-27 12:23:40 +02:00
|
|
|
assert component_info.name == "pipeline_component"
|
2025-06-06 10:17:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_tool_call_delta():
|
2025-06-23 10:14:25 +02:00
|
|
|
tool_call = ToolCallDelta(id="123", tool_name="test_tool", arguments='{"arg1": "value1"}', index=0)
|
2025-06-06 10:17:02 +02:00
|
|
|
assert tool_call.id == "123"
|
|
|
|
assert tool_call.tool_name == "test_tool"
|
|
|
|
assert tool_call.arguments == '{"arg1": "value1"}'
|
2025-06-23 10:14:25 +02:00
|
|
|
assert tool_call.index == 0
|
2025-06-06 10:17:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_tool_call_delta_with_missing_fields():
|
|
|
|
with pytest.raises(ValueError):
|
2025-06-23 10:14:25 +02:00
|
|
|
_ = ToolCallDelta(id="123", index=0)
|