-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: Add new class variable to chat generators for generation kwarg conversion #12328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
39baf35
Add new class variable for generation kwarg conversion and utils to u…
sjrl facd5f8
Merge branch 'main' into codex/chat-generator-parameter-mapping
sjrl d92b982
PR comments
sjrl 6882115
Merge branch 'codex/chat-generator-parameter-mapping' of github.com:d…
sjrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from typing import Any | ||
|
|
||
| from haystack.components.generators.chat.types import ChatGenerator | ||
|
|
||
| # The provider-neutral generation parameters that Haystack components can request from Chat Generators. | ||
| # The chosen name is based on OpenAI's Responses API. | ||
| _HAYSTACK_GENERATION_PARAMETERS = frozenset({"max_output_tokens"}) | ||
|
|
||
|
|
||
| def _convert_haystack_generation_kwargs( | ||
| chat_generator: ChatGenerator, haystack_generation_kwargs: dict[str, Any] | ||
| ) -> dict[str, Any]: | ||
| """ | ||
| Convert provider-neutral Haystack generation parameters for a Chat Generator. | ||
|
|
||
| Chat Generators advertise supported parameters through a private class-level mapping from the canonical Haystack | ||
| name to the provider-specific name. Parameters not advertised by the generator are omitted, allowing callers to | ||
| provide a fallback for generators that do not expose this optional capability. | ||
|
|
||
| :param chat_generator: The Chat Generator that will receive the converted parameters. | ||
| :param haystack_generation_kwargs: Generation parameters using Haystack's canonical names. | ||
| :returns: The supported parameters converted to their provider-specific names. | ||
| :raises ValueError: If a parameter is not part of Haystack's canonical vocabulary. | ||
| """ | ||
| unknown_parameters = haystack_generation_kwargs.keys() - _HAYSTACK_GENERATION_PARAMETERS | ||
| if unknown_parameters: | ||
| unknown = ", ".join(sorted(unknown_parameters)) | ||
| msg = f"Unknown Haystack generation parameter(s): {unknown}" | ||
| raise ValueError(msg) | ||
|
|
||
| parameter_mapping = getattr(chat_generator, "_HAYSTACK_TO_PROVIDER_GENERATION_KWARGS", {}) | ||
|
|
||
| return { | ||
| provider_name: haystack_generation_kwargs[haystack_name] | ||
| for haystack_name, provider_name in parameter_mapping.items() | ||
| if haystack_name in haystack_generation_kwargs | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,33 @@ | ||
| # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from typing import ClassVar | ||
|
|
||
| import pytest | ||
|
|
||
| from haystack.components.generators.chat import MockChatGenerator | ||
| from haystack.components.generators.chat.utils import ( | ||
| _HAYSTACK_GENERATION_PARAMETERS, | ||
| _convert_haystack_generation_kwargs, | ||
| ) | ||
|
|
||
|
|
||
| class MappedMockChatGenerator(MockChatGenerator): | ||
| _HAYSTACK_TO_PROVIDER_GENERATION_KWARGS: ClassVar[dict[str, str]] = {"max_output_tokens": "provider_max_tokens"} | ||
|
|
||
|
|
||
| class TestConvertHaystackGenerationKwargs: | ||
| def test_haystack_generation_parameters(self) -> None: | ||
| assert {"max_output_tokens"} == _HAYSTACK_GENERATION_PARAMETERS | ||
|
|
||
| def test_conversion(self) -> None: | ||
| converted = _convert_haystack_generation_kwargs(MappedMockChatGenerator(), {"max_output_tokens": 100}) | ||
| assert converted == {"provider_max_tokens": 100} | ||
|
|
||
| def test_no_mapping(self) -> None: | ||
| assert _convert_haystack_generation_kwargs(MockChatGenerator(), {"max_output_tokens": 100}) == {} | ||
|
|
||
| def test_invalid_parameter(self) -> None: | ||
| with pytest.raises(ValueError, match="Unknown Haystack generation parameter\\(s\\): max_tokens"): | ||
| _convert_haystack_generation_kwargs(MockChatGenerator(), {"max_tokens": 100}) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.