test: Move pipeline test to behavorials (#8377)

This commit is contained in:
Sebastian Husch Lee 2024-09-19 16:59:35 +02:00 committed by GitHub
parent 151bd531a5
commit 2235ce673f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 41 deletions

View File

@ -1,17 +1,11 @@
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
from haystack.components.builders import AnswerBuilder
from haystack import Document, Pipeline
from haystack import Document
from haystack.dataclasses.answer import ExtractedAnswer, GeneratedAnswer, ExtractedTableAnswer
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.joiners.answer_joiner import AnswerJoiner, JoinMode
from haystack.dataclasses import ChatMessage
class TestAnswerJoiner:
@ -106,36 +100,3 @@ class TestAnswerJoiner:
unsupported_mode = "unsupported_mode"
with pytest.raises(ValueError):
AnswerJoiner(join_mode=unsupported_mode)
@pytest.mark.skipif(not os.environ.get("OPENAI_API_KEY", ""), reason="Needs OPENAI_API_KEY to run this test.")
@pytest.mark.integration
def test_with_pipeline(self):
query = "What's Natural Language Processing?"
messages = [
ChatMessage.from_system("You are a helpful, respectful and honest assistant. Be super concise."),
ChatMessage.from_user(query),
]
pipe = Pipeline()
pipe.add_component("gpt-4o", OpenAIChatGenerator(model="gpt-4o"))
pipe.add_component("llama", OpenAIChatGenerator(model="gpt-3.5-turbo"))
pipe.add_component("aba", AnswerBuilder())
pipe.add_component("abb", AnswerBuilder())
pipe.add_component("joiner", AnswerJoiner())
pipe.connect("gpt-4o.replies", "aba")
pipe.connect("llama.replies", "abb")
pipe.connect("aba.answers", "joiner")
pipe.connect("abb.answers", "joiner")
results = pipe.run(
data={
"gpt-4o": {"messages": messages},
"llama": {"messages": messages},
"aba": {"query": query},
"abb": {"query": query},
}
)
assert "joiner" in results
assert len(results["joiner"]["answers"]) == 2

View File

@ -40,6 +40,7 @@ Feature: Pipeline running
| that has multiple components with only default inputs and are added in a different order from the order of execution |
| that is linear with conditional branching and multiple joins |
| that has a variadic component that receives partial inputs |
| that has an answer joiner variadic component |
Scenario Outline: Running a bad Pipeline
Given a pipeline <kind>

View File

@ -9,7 +9,7 @@ from haystack.components.routers import ConditionalRouter
from haystack.components.builders import PromptBuilder, AnswerBuilder
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.joiners import BranchJoiner, DocumentJoiner
from haystack.components.joiners import BranchJoiner, DocumentJoiner, AnswerJoiner
from haystack.testing.sample_components import (
Accumulate,
AddFixedValue,
@ -1635,3 +1635,47 @@ def that_has_a_variadic_component_that_receives_partial_inputs():
),
],
)
@given("a pipeline that has an answer joiner variadic component", target_fixture="pipeline_data")
def that_has_an_answer_joiner_variadic_component():
query = "What's Natural Language Processing?"
pipeline = Pipeline()
pipeline.add_component("answer_builder_1", AnswerBuilder())
pipeline.add_component("answer_builder_2", AnswerBuilder())
pipeline.add_component("answer_joiner", AnswerJoiner())
pipeline.connect("answer_builder_1.answers", "answer_joiner")
pipeline.connect("answer_builder_2.answers", "answer_joiner")
return (
pipeline,
[
PipelineRunData(
inputs={
"answer_builder_1": {"query": query, "replies": ["This is a test answer"]},
"answer_builder_2": {"query": query, "replies": ["This is a second test answer"]},
},
expected_outputs={
"answer_joiner": {
"answers": [
GeneratedAnswer(
data="This is a test answer",
query="What's Natural Language Processing?",
documents=[],
meta={},
),
GeneratedAnswer(
data="This is a second test answer",
query="What's Natural Language Processing?",
documents=[],
meta={},
),
]
}
},
expected_run_order=["answer_builder_1", "answer_builder_2", "answer_joiner"],
)
],
)