-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve serialization/deserialization of callables (to hand…
…le class methods and static methods) (#8683) * progress * refinements * tidy up * release note
- Loading branch information
Showing
7 changed files
with
112 additions
and
76 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
6 changes: 6 additions & 0 deletions
6
releasenotes/notes/improve-callables-serde-6aa1e23408063247.yaml
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,6 @@ | ||
--- | ||
enhancements: | ||
- | | ||
Improve serialization and deserialization of callables. | ||
We now allow serialization of classmethods and staticmethods | ||
and explicitly prohibit serialization of instance methods, lambdas, and nested functions. |
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 |
---|---|---|
@@ -1,26 +1,24 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest.mock import MagicMock, patch | ||
from unittest.mock import patch | ||
import pytest | ||
|
||
from typing import Iterator | ||
|
||
import logging | ||
import os | ||
import json | ||
from datetime import datetime | ||
|
||
from openai import OpenAIError | ||
from openai.types.chat import ChatCompletion, ChatCompletionChunk, ChatCompletionMessage, ChatCompletionMessageToolCall | ||
from openai.types.chat.chat_completion import Choice | ||
from openai.types.chat.chat_completion_message_tool_call import Function | ||
from openai.types.chat import chat_completion_chunk | ||
from openai import Stream | ||
|
||
from haystack.components.generators.utils import print_streaming_chunk | ||
from haystack.dataclasses import StreamingChunk | ||
from haystack.utils.auth import Secret | ||
from haystack.dataclasses import ChatMessage, Tool, ToolCall, ChatRole, TextContent | ||
from haystack.dataclasses import ChatMessage, Tool, ToolCall | ||
from haystack.components.generators.chat.openai import OpenAIChatGenerator | ||
|
||
|
||
|
@@ -212,31 +210,6 @@ def test_to_dict_with_parameters(self, monkeypatch): | |
}, | ||
} | ||
|
||
def test_to_dict_with_lambda_streaming_callback(self, monkeypatch): | ||
monkeypatch.setenv("OPENAI_API_KEY", "test-api-key") | ||
component = OpenAIChatGenerator( | ||
model="gpt-4o-mini", | ||
streaming_callback=lambda x: x, | ||
api_base_url="test-base-url", | ||
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"}, | ||
) | ||
data = component.to_dict() | ||
assert data == { | ||
"type": "haystack.components.generators.chat.openai.OpenAIChatGenerator", | ||
"init_parameters": { | ||
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"}, | ||
"model": "gpt-4o-mini", | ||
"organization": None, | ||
"api_base_url": "test-base-url", | ||
"max_retries": None, | ||
"timeout": None, | ||
"streaming_callback": "chat.test_openai.<lambda>", | ||
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"}, | ||
"tools": None, | ||
"tools_strict": False, | ||
}, | ||
} | ||
|
||
def test_from_dict(self, monkeypatch): | ||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key") | ||
data = { | ||
|
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