-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1321 from dagardner-nv/david-fea-sherlock-llm-gen…
…erate-node Add docstrings & tests for LLMGenerateNode
- Loading branch information
Showing
9 changed files
with
194 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# 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. | ||
|
||
from unittest import mock | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(name="mock_llm_client") | ||
def mock_llm_client_fixture(): | ||
from morpheus.llm.services.llm_service import LLMClient | ||
mock_client = mock.MagicMock(LLMClient) | ||
mock_client.return_value = mock_client | ||
mock_client.generate_batch_async = mock.AsyncMock() | ||
return mock_client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# 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. | ||
|
||
from unittest import mock | ||
|
||
from _utils.llm import execute_node | ||
from morpheus.llm import LLMNodeBase | ||
from morpheus.llm.nodes.llm_generate_node import LLMGenerateNode | ||
|
||
|
||
def test_constructor(mock_llm_client: mock.MagicMock): | ||
node = LLMGenerateNode(llm_client=mock_llm_client) | ||
assert isinstance(node, LLMNodeBase) | ||
|
||
|
||
def test_get_input_names(mock_llm_client: mock.MagicMock): | ||
node = LLMGenerateNode(llm_client=mock_llm_client) | ||
assert node.get_input_names() == ["prompt"] | ||
|
||
|
||
def test_execute(mock_llm_client: mock.MagicMock): | ||
expected_output = ["response1", "response2"] | ||
mock_llm_client.generate_batch_async.return_value = expected_output.copy() | ||
|
||
node = LLMGenerateNode(llm_client=mock_llm_client) | ||
assert execute_node(node, prompt=["prompt1", "prompt2"]) == expected_output | ||
mock_llm_client.generate_batch_async.assert_called_once_with(["prompt1", "prompt2"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# 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. | ||
|
||
from unittest import mock | ||
|
||
import pytest | ||
|
||
import cudf | ||
|
||
from _utils import assert_results | ||
from morpheus.config import Config | ||
from morpheus.llm import LLMEngine | ||
from morpheus.llm.llm_engine_stage import LLMEngineStage | ||
from morpheus.llm.nodes.extracter_node import ExtracterNode | ||
from morpheus.llm.nodes.llm_generate_node import LLMGenerateNode | ||
from morpheus.llm.task_handlers.simple_task_handler import SimpleTaskHandler | ||
from morpheus.messages import ControlMessage | ||
from morpheus.pipeline.linear_pipeline import LinearPipeline | ||
from morpheus.stages.input.in_memory_source_stage import InMemorySourceStage | ||
from morpheus.stages.output.compare_dataframe_stage import CompareDataFrameStage | ||
from morpheus.stages.preprocess.deserialize_stage import DeserializeStage | ||
|
||
|
||
def _build_engine(mock_llm_client: mock.MagicMock) -> LLMEngine: | ||
engine = LLMEngine() | ||
engine.add_node("extracter", node=ExtracterNode()) | ||
engine.add_node("generate", inputs=["/extracter"], node=LLMGenerateNode(llm_client=mock_llm_client)) | ||
engine.add_task_handler(inputs=["/generate"], handler=SimpleTaskHandler()) | ||
|
||
return engine | ||
|
||
|
||
@pytest.mark.use_python | ||
def test_pipeline(config: Config, mock_llm_client: mock.MagicMock): | ||
expected_output = ["response1", "response2"] | ||
mock_llm_client.generate_batch_async.return_value = expected_output.copy() | ||
|
||
values = {'prompt': ["prompt1", "prompt2"]} | ||
input_df = cudf.DataFrame(values) | ||
expected_df = input_df.copy(deep=True) | ||
expected_df["response"] = expected_output | ||
|
||
task_payload = {"task_type": "llm_engine", "task_dict": {"input_keys": sorted(values.keys())}} | ||
|
||
pipe = LinearPipeline(config) | ||
pipe.set_source(InMemorySourceStage(config, dataframes=[input_df])) | ||
pipe.add_stage( | ||
DeserializeStage(config, message_type=ControlMessage, task_type="llm_engine", task_payload=task_payload)) | ||
pipe.add_stage(LLMEngineStage(config, engine=_build_engine(mock_llm_client=mock_llm_client))) | ||
sink = pipe.add_stage(CompareDataFrameStage(config, compare_df=expected_df)) | ||
|
||
pipe.run() | ||
|
||
assert_results(sink.get_results()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters