unique_toolkit 0.7.9__py3-none-any.whl → 1.33.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unique_toolkit/__init__.py +36 -3
- unique_toolkit/_common/api_calling/human_verification_manager.py +357 -0
- unique_toolkit/_common/base_model_type_attribute.py +303 -0
- unique_toolkit/_common/chunk_relevancy_sorter/config.py +49 -0
- unique_toolkit/_common/chunk_relevancy_sorter/exception.py +5 -0
- unique_toolkit/_common/chunk_relevancy_sorter/schemas.py +46 -0
- unique_toolkit/_common/chunk_relevancy_sorter/service.py +374 -0
- unique_toolkit/_common/chunk_relevancy_sorter/tests/test_service.py +275 -0
- unique_toolkit/_common/default_language_model.py +12 -0
- unique_toolkit/_common/docx_generator/__init__.py +7 -0
- unique_toolkit/_common/docx_generator/config.py +12 -0
- unique_toolkit/_common/docx_generator/schemas.py +80 -0
- unique_toolkit/_common/docx_generator/service.py +225 -0
- unique_toolkit/_common/docx_generator/template/Doc Template.docx +0 -0
- unique_toolkit/_common/endpoint_builder.py +368 -0
- unique_toolkit/_common/endpoint_requestor.py +480 -0
- unique_toolkit/_common/exception.py +24 -0
- unique_toolkit/_common/experimental/endpoint_builder.py +368 -0
- unique_toolkit/_common/experimental/endpoint_requestor.py +488 -0
- unique_toolkit/_common/feature_flags/schema.py +9 -0
- unique_toolkit/_common/pydantic/rjsf_tags.py +936 -0
- unique_toolkit/_common/pydantic_helpers.py +174 -0
- unique_toolkit/_common/referencing.py +53 -0
- unique_toolkit/_common/string_utilities.py +140 -0
- unique_toolkit/_common/tests/test_referencing.py +521 -0
- unique_toolkit/_common/tests/test_string_utilities.py +506 -0
- unique_toolkit/_common/token/image_token_counting.py +67 -0
- unique_toolkit/_common/token/token_counting.py +204 -0
- unique_toolkit/_common/utils/__init__.py +1 -0
- unique_toolkit/_common/utils/files.py +43 -0
- unique_toolkit/_common/utils/image/encode.py +25 -0
- unique_toolkit/_common/utils/jinja/helpers.py +10 -0
- unique_toolkit/_common/utils/jinja/render.py +18 -0
- unique_toolkit/_common/utils/jinja/schema.py +65 -0
- unique_toolkit/_common/utils/jinja/utils.py +80 -0
- unique_toolkit/_common/utils/structured_output/__init__.py +1 -0
- unique_toolkit/_common/utils/structured_output/schema.py +5 -0
- unique_toolkit/_common/utils/write_configuration.py +51 -0
- unique_toolkit/_common/validators.py +101 -4
- unique_toolkit/agentic/__init__.py +1 -0
- unique_toolkit/agentic/debug_info_manager/debug_info_manager.py +28 -0
- unique_toolkit/agentic/debug_info_manager/test/test_debug_info_manager.py +278 -0
- unique_toolkit/agentic/evaluation/config.py +36 -0
- unique_toolkit/{evaluators → agentic/evaluation}/context_relevancy/prompts.py +25 -0
- unique_toolkit/agentic/evaluation/context_relevancy/schema.py +80 -0
- unique_toolkit/agentic/evaluation/context_relevancy/service.py +273 -0
- unique_toolkit/agentic/evaluation/evaluation_manager.py +218 -0
- unique_toolkit/agentic/evaluation/hallucination/constants.py +61 -0
- unique_toolkit/agentic/evaluation/hallucination/hallucination_evaluation.py +112 -0
- unique_toolkit/{evaluators → agentic/evaluation}/hallucination/prompts.py +1 -1
- unique_toolkit/{evaluators → agentic/evaluation}/hallucination/service.py +20 -16
- unique_toolkit/{evaluators → agentic/evaluation}/hallucination/utils.py +32 -21
- unique_toolkit/{evaluators → agentic/evaluation}/output_parser.py +20 -2
- unique_toolkit/{evaluators → agentic/evaluation}/schemas.py +27 -7
- unique_toolkit/agentic/evaluation/tests/test_context_relevancy_service.py +253 -0
- unique_toolkit/agentic/evaluation/tests/test_output_parser.py +87 -0
- unique_toolkit/agentic/history_manager/history_construction_with_contents.py +298 -0
- unique_toolkit/agentic/history_manager/history_manager.py +241 -0
- unique_toolkit/agentic/history_manager/loop_token_reducer.py +484 -0
- unique_toolkit/agentic/history_manager/utils.py +96 -0
- unique_toolkit/agentic/message_log_manager/__init__.py +5 -0
- unique_toolkit/agentic/message_log_manager/service.py +93 -0
- unique_toolkit/agentic/postprocessor/postprocessor_manager.py +212 -0
- unique_toolkit/agentic/reference_manager/reference_manager.py +103 -0
- unique_toolkit/agentic/responses_api/__init__.py +19 -0
- unique_toolkit/agentic/responses_api/postprocessors/code_display.py +71 -0
- unique_toolkit/agentic/responses_api/postprocessors/generated_files.py +297 -0
- unique_toolkit/agentic/responses_api/stream_handler.py +15 -0
- unique_toolkit/agentic/short_term_memory_manager/persistent_short_term_memory_manager.py +141 -0
- unique_toolkit/agentic/thinking_manager/thinking_manager.py +103 -0
- unique_toolkit/agentic/tools/__init__.py +1 -0
- unique_toolkit/agentic/tools/a2a/__init__.py +36 -0
- unique_toolkit/agentic/tools/a2a/config.py +17 -0
- unique_toolkit/agentic/tools/a2a/evaluation/__init__.py +15 -0
- unique_toolkit/agentic/tools/a2a/evaluation/_utils.py +66 -0
- unique_toolkit/agentic/tools/a2a/evaluation/config.py +55 -0
- unique_toolkit/agentic/tools/a2a/evaluation/evaluator.py +260 -0
- unique_toolkit/agentic/tools/a2a/evaluation/summarization_user_message.j2 +9 -0
- unique_toolkit/agentic/tools/a2a/manager.py +55 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/__init__.py +21 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/_display_utils.py +240 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/_ref_utils.py +84 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/config.py +78 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/display.py +264 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/references.py +101 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/test/test_display.py +421 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/test/test_display_utils.py +2103 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/test/test_ref_utils.py +603 -0
- unique_toolkit/agentic/tools/a2a/prompts.py +46 -0
- unique_toolkit/agentic/tools/a2a/response_watcher/__init__.py +6 -0
- unique_toolkit/agentic/tools/a2a/response_watcher/service.py +91 -0
- unique_toolkit/agentic/tools/a2a/tool/__init__.py +4 -0
- unique_toolkit/agentic/tools/a2a/tool/_memory.py +26 -0
- unique_toolkit/agentic/tools/a2a/tool/_schema.py +9 -0
- unique_toolkit/agentic/tools/a2a/tool/config.py +158 -0
- unique_toolkit/agentic/tools/a2a/tool/service.py +393 -0
- unique_toolkit/agentic/tools/agent_chunks_hanlder.py +65 -0
- unique_toolkit/agentic/tools/config.py +128 -0
- unique_toolkit/agentic/tools/factory.py +44 -0
- unique_toolkit/agentic/tools/mcp/__init__.py +4 -0
- unique_toolkit/agentic/tools/mcp/manager.py +71 -0
- unique_toolkit/agentic/tools/mcp/models.py +28 -0
- unique_toolkit/agentic/tools/mcp/tool_wrapper.py +234 -0
- unique_toolkit/agentic/tools/openai_builtin/__init__.py +11 -0
- unique_toolkit/agentic/tools/openai_builtin/base.py +46 -0
- unique_toolkit/agentic/tools/openai_builtin/code_interpreter/__init__.py +8 -0
- unique_toolkit/agentic/tools/openai_builtin/code_interpreter/config.py +88 -0
- unique_toolkit/agentic/tools/openai_builtin/code_interpreter/service.py +250 -0
- unique_toolkit/agentic/tools/openai_builtin/manager.py +79 -0
- unique_toolkit/agentic/tools/schemas.py +145 -0
- unique_toolkit/agentic/tools/test/test_mcp_manager.py +536 -0
- unique_toolkit/agentic/tools/test/test_tool_progress_reporter.py +445 -0
- unique_toolkit/agentic/tools/tool.py +187 -0
- unique_toolkit/agentic/tools/tool_manager.py +492 -0
- unique_toolkit/agentic/tools/tool_progress_reporter.py +285 -0
- unique_toolkit/agentic/tools/utils/__init__.py +19 -0
- unique_toolkit/agentic/tools/utils/execution/__init__.py +1 -0
- unique_toolkit/agentic/tools/utils/execution/execution.py +286 -0
- unique_toolkit/agentic/tools/utils/source_handling/__init__.py +0 -0
- unique_toolkit/agentic/tools/utils/source_handling/schema.py +21 -0
- unique_toolkit/agentic/tools/utils/source_handling/source_formatting.py +207 -0
- unique_toolkit/agentic/tools/utils/source_handling/tests/test_source_formatting.py +216 -0
- unique_toolkit/app/__init__.py +9 -0
- unique_toolkit/app/dev_util.py +180 -0
- unique_toolkit/app/fast_api_factory.py +131 -0
- unique_toolkit/app/init_sdk.py +32 -1
- unique_toolkit/app/schemas.py +206 -31
- unique_toolkit/app/unique_settings.py +367 -0
- unique_toolkit/app/webhook.py +77 -0
- unique_toolkit/chat/__init__.py +8 -1
- unique_toolkit/chat/deprecated/service.py +232 -0
- unique_toolkit/chat/functions.py +648 -78
- unique_toolkit/chat/rendering.py +34 -0
- unique_toolkit/chat/responses_api.py +461 -0
- unique_toolkit/chat/schemas.py +134 -2
- unique_toolkit/chat/service.py +115 -767
- unique_toolkit/content/functions.py +353 -8
- unique_toolkit/content/schemas.py +128 -15
- unique_toolkit/content/service.py +321 -45
- unique_toolkit/content/smart_rules.py +301 -0
- unique_toolkit/content/utils.py +10 -3
- unique_toolkit/data_extraction/README.md +96 -0
- unique_toolkit/data_extraction/__init__.py +11 -0
- unique_toolkit/data_extraction/augmented/__init__.py +5 -0
- unique_toolkit/data_extraction/augmented/service.py +93 -0
- unique_toolkit/data_extraction/base.py +25 -0
- unique_toolkit/data_extraction/basic/__init__.py +11 -0
- unique_toolkit/data_extraction/basic/config.py +18 -0
- unique_toolkit/data_extraction/basic/prompt.py +13 -0
- unique_toolkit/data_extraction/basic/service.py +55 -0
- unique_toolkit/embedding/service.py +103 -12
- unique_toolkit/framework_utilities/__init__.py +1 -0
- unique_toolkit/framework_utilities/langchain/__init__.py +10 -0
- unique_toolkit/framework_utilities/langchain/client.py +71 -0
- unique_toolkit/framework_utilities/langchain/history.py +19 -0
- unique_toolkit/framework_utilities/openai/__init__.py +6 -0
- unique_toolkit/framework_utilities/openai/client.py +84 -0
- unique_toolkit/framework_utilities/openai/message_builder.py +229 -0
- unique_toolkit/framework_utilities/utils.py +23 -0
- unique_toolkit/language_model/__init__.py +3 -0
- unique_toolkit/language_model/_responses_api_utils.py +93 -0
- unique_toolkit/language_model/builder.py +27 -11
- unique_toolkit/language_model/default_language_model.py +3 -0
- unique_toolkit/language_model/functions.py +345 -43
- unique_toolkit/language_model/infos.py +1288 -46
- unique_toolkit/language_model/reference.py +242 -0
- unique_toolkit/language_model/schemas.py +481 -49
- unique_toolkit/language_model/service.py +229 -28
- unique_toolkit/protocols/support.py +145 -0
- unique_toolkit/services/__init__.py +7 -0
- unique_toolkit/services/chat_service.py +1631 -0
- unique_toolkit/services/knowledge_base.py +1094 -0
- unique_toolkit/short_term_memory/service.py +178 -41
- unique_toolkit/smart_rules/__init__.py +0 -0
- unique_toolkit/smart_rules/compile.py +56 -0
- unique_toolkit/test_utilities/events.py +197 -0
- unique_toolkit-1.33.3.dist-info/METADATA +1145 -0
- unique_toolkit-1.33.3.dist-info/RECORD +205 -0
- unique_toolkit/evaluators/__init__.py +0 -1
- unique_toolkit/evaluators/config.py +0 -35
- unique_toolkit/evaluators/constants.py +0 -1
- unique_toolkit/evaluators/context_relevancy/constants.py +0 -32
- unique_toolkit/evaluators/context_relevancy/service.py +0 -53
- unique_toolkit/evaluators/context_relevancy/utils.py +0 -142
- unique_toolkit/evaluators/hallucination/constants.py +0 -41
- unique_toolkit-0.7.9.dist-info/METADATA +0 -413
- unique_toolkit-0.7.9.dist-info/RECORD +0 -64
- /unique_toolkit/{evaluators → agentic/evaluation}/exception.py +0 -0
- {unique_toolkit-0.7.9.dist-info → unique_toolkit-1.33.3.dist-info}/LICENSE +0 -0
- {unique_toolkit-0.7.9.dist-info → unique_toolkit-1.33.3.dist-info}/WHEEL +0 -0
unique_toolkit/__init__.py
CHANGED
|
@@ -1,17 +1,50 @@
|
|
|
1
1
|
# Re-export commonly used classes for easier imports
|
|
2
2
|
from unique_toolkit.chat import ChatService
|
|
3
3
|
from unique_toolkit.content import ContentService
|
|
4
|
+
from unique_toolkit.data_extraction import (
|
|
5
|
+
StructuredOutputDataExtractor,
|
|
6
|
+
StructuredOutputDataExtractorConfig,
|
|
7
|
+
)
|
|
4
8
|
from unique_toolkit.embedding import EmbeddingService
|
|
5
|
-
from unique_toolkit.
|
|
9
|
+
from unique_toolkit.framework_utilities.openai.client import (
|
|
10
|
+
get_async_openai_client,
|
|
11
|
+
get_openai_client,
|
|
12
|
+
)
|
|
13
|
+
from unique_toolkit.language_model import (
|
|
14
|
+
LanguageModelMessages,
|
|
15
|
+
LanguageModelName,
|
|
16
|
+
LanguageModelService,
|
|
17
|
+
LanguageModelToolDescription,
|
|
18
|
+
)
|
|
19
|
+
from unique_toolkit.services.knowledge_base import KnowledgeBaseService
|
|
6
20
|
from unique_toolkit.short_term_memory import ShortTermMemoryService
|
|
7
21
|
|
|
22
|
+
# Conditionally import langchain utilities if langchain is installed
|
|
23
|
+
try:
|
|
24
|
+
from unique_toolkit.framework_utilities.langchain.client import get_langchain_client # noqa: F401, I001
|
|
25
|
+
|
|
26
|
+
_LANGCHAIN_AVAILABLE = True
|
|
27
|
+
except ImportError:
|
|
28
|
+
_LANGCHAIN_AVAILABLE = False
|
|
29
|
+
|
|
8
30
|
# You can add other classes you frequently use here as well
|
|
9
31
|
|
|
10
32
|
__all__ = [
|
|
11
|
-
"LanguageModelService",
|
|
12
|
-
"LanguageModelMessages",
|
|
13
33
|
"ChatService",
|
|
14
34
|
"ContentService",
|
|
15
35
|
"EmbeddingService",
|
|
36
|
+
"get_openai_client",
|
|
37
|
+
"get_async_openai_client",
|
|
38
|
+
"KnowledgeBaseService",
|
|
39
|
+
"LanguageModelMessages",
|
|
40
|
+
"LanguageModelName",
|
|
41
|
+
"LanguageModelService",
|
|
42
|
+
"LanguageModelToolDescription",
|
|
16
43
|
"ShortTermMemoryService",
|
|
44
|
+
"StructuredOutputDataExtractor",
|
|
45
|
+
"StructuredOutputDataExtractorConfig",
|
|
17
46
|
]
|
|
47
|
+
|
|
48
|
+
# Add langchain-specific exports if available
|
|
49
|
+
if _LANGCHAIN_AVAILABLE:
|
|
50
|
+
__all__.append("get_langchain_client")
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from logging import Logger
|
|
4
|
+
from typing import Any, Generic
|
|
5
|
+
|
|
6
|
+
import jinja2
|
|
7
|
+
from pydantic import BaseModel
|
|
8
|
+
|
|
9
|
+
from unique_toolkit._common.endpoint_builder import (
|
|
10
|
+
ApiOperationProtocol,
|
|
11
|
+
EmptyModel,
|
|
12
|
+
PathParamsSpec,
|
|
13
|
+
PathParamsType,
|
|
14
|
+
PayloadParamSpec,
|
|
15
|
+
PayloadType,
|
|
16
|
+
QueryParamsSpec,
|
|
17
|
+
QueryParamsType,
|
|
18
|
+
ResponseType,
|
|
19
|
+
ResponseValidationException,
|
|
20
|
+
)
|
|
21
|
+
from unique_toolkit._common.endpoint_requestor import (
|
|
22
|
+
RequestContext,
|
|
23
|
+
RequestorType,
|
|
24
|
+
build_requestor,
|
|
25
|
+
)
|
|
26
|
+
from unique_toolkit._common.pydantic_helpers import (
|
|
27
|
+
create_complement_model,
|
|
28
|
+
create_union_model_from_models,
|
|
29
|
+
)
|
|
30
|
+
from unique_toolkit._common.string_utilities import (
|
|
31
|
+
dict_to_markdown_table,
|
|
32
|
+
extract_dicts_from_string,
|
|
33
|
+
)
|
|
34
|
+
from unique_toolkit.chat.schemas import ChatMessage, ChatMessageRole
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class HumanConfirmation(BaseModel):
|
|
38
|
+
payload_hash: str
|
|
39
|
+
time_stamp: datetime
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
NEXT_USER_MESSAGE_JINJA2_TEMPLATE = jinja2.Template("""I confirm the api call with the following data:
|
|
43
|
+
```json
|
|
44
|
+
{{ api_call_as_json }}
|
|
45
|
+
```""")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
ASSISTANT_CONFIRMATION_MESSAGE_JINJA2_TEMPLATE = jinja2.Template(
|
|
49
|
+
"""
|
|
50
|
+
\n
|
|
51
|
+
{{ api_call_as_markdown_table }}
|
|
52
|
+
\n\n
|
|
53
|
+
[{{ button_text }}](https://prompt={{ next_user_message | urlencode }})"""
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class HumanVerificationManagerForApiCalling(
|
|
58
|
+
Generic[
|
|
59
|
+
PathParamsSpec,
|
|
60
|
+
PathParamsType,
|
|
61
|
+
PayloadParamSpec,
|
|
62
|
+
PayloadType,
|
|
63
|
+
QueryParamsSpec,
|
|
64
|
+
QueryParamsType,
|
|
65
|
+
ResponseType,
|
|
66
|
+
]
|
|
67
|
+
):
|
|
68
|
+
"""
|
|
69
|
+
Manages human verification for api calling.
|
|
70
|
+
|
|
71
|
+
The idea is that the manager is able to produce the verification message to the user
|
|
72
|
+
and to detect an api call from the user message.
|
|
73
|
+
|
|
74
|
+
If it detects such a verification message in the user message, it will call the api
|
|
75
|
+
and incorporate the response into the user message.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def __init__(
|
|
79
|
+
self,
|
|
80
|
+
*,
|
|
81
|
+
logger: Logger,
|
|
82
|
+
operation: type[
|
|
83
|
+
ApiOperationProtocol[
|
|
84
|
+
PathParamsSpec,
|
|
85
|
+
PathParamsType,
|
|
86
|
+
PayloadParamSpec,
|
|
87
|
+
PayloadType,
|
|
88
|
+
QueryParamsSpec,
|
|
89
|
+
QueryParamsType,
|
|
90
|
+
ResponseType,
|
|
91
|
+
]
|
|
92
|
+
],
|
|
93
|
+
requestor_type: RequestorType = RequestorType.REQUESTS,
|
|
94
|
+
environment_payload_params: BaseModel | None = None,
|
|
95
|
+
modifiable_payload_params_model: type[BaseModel] | None = None,
|
|
96
|
+
**kwargs: dict[str, Any],
|
|
97
|
+
):
|
|
98
|
+
"""
|
|
99
|
+
Manages human verification for api calling.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
logger: The logger to use for logging.
|
|
103
|
+
operation: The operation to use for the api calling.
|
|
104
|
+
requestor_type: The requestor type to use for the api calling.
|
|
105
|
+
environment_payload_params: The environment payload params to use for the api calling.
|
|
106
|
+
If None, the modifiable params model will be the operation payload model.
|
|
107
|
+
This can be useful for parameters in the payload that should not be modified by the user.
|
|
108
|
+
modifiable_payload_params_model: The modifiable payload params model to use for the api calling.
|
|
109
|
+
If None, a complement model will be created using the operation payload model
|
|
110
|
+
and the environment payload params.
|
|
111
|
+
If provided, it will be used instead of the complement model.
|
|
112
|
+
This is necessary if the modifiable params model is required
|
|
113
|
+
to use custom validators or serializers.
|
|
114
|
+
**kwargs: Additional keyword arguments to pass to the requestor.
|
|
115
|
+
"""
|
|
116
|
+
self._logger = logger
|
|
117
|
+
self._operation = operation
|
|
118
|
+
self._environment_payload_params = environment_payload_params
|
|
119
|
+
# Create internal models for this manager instance
|
|
120
|
+
|
|
121
|
+
if self._environment_payload_params is None:
|
|
122
|
+
self._modifiable_payload_params_model = self._operation.payload_model()
|
|
123
|
+
else:
|
|
124
|
+
if modifiable_payload_params_model is None:
|
|
125
|
+
self._modifiable_payload_params_model = create_complement_model(
|
|
126
|
+
model_type_a=self._operation.payload_model(),
|
|
127
|
+
model_type_b=type(self._environment_payload_params),
|
|
128
|
+
)
|
|
129
|
+
else:
|
|
130
|
+
# This is necessary if the modifiable params model is required
|
|
131
|
+
# to use custom validators or serializers.
|
|
132
|
+
self._modifiable_payload_params_model = modifiable_payload_params_model
|
|
133
|
+
|
|
134
|
+
if self._environment_payload_params is not None:
|
|
135
|
+
combined_keys = set(
|
|
136
|
+
self._modifiable_payload_params_model.model_fields.keys()
|
|
137
|
+
) | set(type(self._environment_payload_params).model_fields.keys())
|
|
138
|
+
payload_keys = set(self._operation.payload_model().model_fields.keys())
|
|
139
|
+
if not payload_keys.issubset(combined_keys):
|
|
140
|
+
raise ValueError(
|
|
141
|
+
"The modifiable params model + the environment parameters do not have all the keys of the operation payload model."
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
class VerificationModel(BaseModel):
|
|
145
|
+
confirmation: HumanConfirmation
|
|
146
|
+
modifiable_params: self._modifiable_payload_params_model # type: ignore
|
|
147
|
+
|
|
148
|
+
self._verification_model = VerificationModel
|
|
149
|
+
|
|
150
|
+
self._requestor_type = requestor_type
|
|
151
|
+
|
|
152
|
+
self._combined_params_model = create_union_model_from_models(
|
|
153
|
+
model_types=[
|
|
154
|
+
self._operation.path_params_model(),
|
|
155
|
+
self._operation.payload_model(),
|
|
156
|
+
self._operation.query_params_model(),
|
|
157
|
+
],
|
|
158
|
+
)
|
|
159
|
+
self._requestor = build_requestor(
|
|
160
|
+
requestor_type=requestor_type,
|
|
161
|
+
operation_type=self._operation,
|
|
162
|
+
combined_model=self._combined_params_model,
|
|
163
|
+
**kwargs,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def detect_api_calls_from_user_message(
|
|
167
|
+
self,
|
|
168
|
+
*,
|
|
169
|
+
last_assistant_message: ChatMessage,
|
|
170
|
+
user_message: str,
|
|
171
|
+
) -> PayloadType | None:
|
|
172
|
+
user_message_dicts = extract_dicts_from_string(user_message)
|
|
173
|
+
if len(user_message_dicts) == 0:
|
|
174
|
+
return None
|
|
175
|
+
|
|
176
|
+
user_message_dicts.reverse()
|
|
177
|
+
for user_message_dict in user_message_dicts:
|
|
178
|
+
try:
|
|
179
|
+
# Convert dict to payload model first, then create payload
|
|
180
|
+
verfication_data = self._verification_model.model_validate(
|
|
181
|
+
user_message_dict, by_alias=True, by_name=True
|
|
182
|
+
)
|
|
183
|
+
if self._verify_human_verification(
|
|
184
|
+
verfication_data.confirmation, last_assistant_message
|
|
185
|
+
):
|
|
186
|
+
payload_dict = verfication_data.modifiable_params.model_dump()
|
|
187
|
+
if self._environment_payload_params is not None:
|
|
188
|
+
payload_dict.update(
|
|
189
|
+
self._environment_payload_params.model_dump()
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
return self._operation.payload_model().model_validate(
|
|
193
|
+
payload_dict, by_alias=True, by_name=True
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
except Exception as e:
|
|
197
|
+
self._logger.error(f"Error detecting api calls from user message: {e}")
|
|
198
|
+
|
|
199
|
+
return None
|
|
200
|
+
|
|
201
|
+
def _verify_human_verification(
|
|
202
|
+
self, confirmation: HumanConfirmation, last_assistant_message: ChatMessage
|
|
203
|
+
) -> bool:
|
|
204
|
+
if (
|
|
205
|
+
last_assistant_message.role != ChatMessageRole.ASSISTANT
|
|
206
|
+
or last_assistant_message.content is None
|
|
207
|
+
):
|
|
208
|
+
self._logger.error(
|
|
209
|
+
"Last assistant message is not an assistant message or content is empty."
|
|
210
|
+
)
|
|
211
|
+
return False
|
|
212
|
+
|
|
213
|
+
return confirmation.payload_hash in last_assistant_message.content
|
|
214
|
+
|
|
215
|
+
def _create_next_user_message(self, payload: PayloadType) -> str:
|
|
216
|
+
# Extract only the modifiable fields from the payload
|
|
217
|
+
payload_dict = payload.model_dump()
|
|
218
|
+
if self._environment_payload_params is not None:
|
|
219
|
+
# Remove environment params from payload to avoid validation errors
|
|
220
|
+
environment_fields = set(
|
|
221
|
+
type(self._environment_payload_params).model_fields.keys()
|
|
222
|
+
)
|
|
223
|
+
modifiable_dict = {
|
|
224
|
+
k: v for k, v in payload_dict.items() if k not in environment_fields
|
|
225
|
+
}
|
|
226
|
+
else:
|
|
227
|
+
modifiable_dict = payload_dict
|
|
228
|
+
|
|
229
|
+
modifiable_params = self._modifiable_payload_params_model.model_validate(
|
|
230
|
+
modifiable_dict,
|
|
231
|
+
by_alias=True,
|
|
232
|
+
by_name=True,
|
|
233
|
+
)
|
|
234
|
+
api_call = self._verification_model(
|
|
235
|
+
modifiable_params=modifiable_params,
|
|
236
|
+
confirmation=HumanConfirmation(
|
|
237
|
+
payload_hash=hashlib.sha256(
|
|
238
|
+
modifiable_params.model_dump_json().encode()
|
|
239
|
+
).hexdigest(),
|
|
240
|
+
time_stamp=datetime.now(),
|
|
241
|
+
),
|
|
242
|
+
)
|
|
243
|
+
return NEXT_USER_MESSAGE_JINJA2_TEMPLATE.render(
|
|
244
|
+
api_call_as_json=api_call.model_dump_json(indent=2)
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
def create_assistant_confirmation_message(
|
|
248
|
+
self, *, payload: PayloadType, button_text: str = "Confirm"
|
|
249
|
+
) -> str:
|
|
250
|
+
return ASSISTANT_CONFIRMATION_MESSAGE_JINJA2_TEMPLATE.render(
|
|
251
|
+
api_call_as_markdown_table=dict_to_markdown_table(payload.model_dump()),
|
|
252
|
+
button_text=button_text,
|
|
253
|
+
next_user_message=self._create_next_user_message(payload),
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
def call_api(
|
|
257
|
+
self,
|
|
258
|
+
*,
|
|
259
|
+
context: RequestContext,
|
|
260
|
+
path_params: PathParamsType | EmptyModel = EmptyModel(),
|
|
261
|
+
payload: PayloadType | EmptyModel = EmptyModel(),
|
|
262
|
+
query_params: QueryParamsType | EmptyModel = EmptyModel(),
|
|
263
|
+
) -> ResponseType:
|
|
264
|
+
"""
|
|
265
|
+
Call the api with the given path params, payload and secured payload params.
|
|
266
|
+
|
|
267
|
+
The `secured payload params` are params that are enforced by the application.
|
|
268
|
+
It should generally be not possible for the user to adapt those but here we
|
|
269
|
+
ensure that the application has the last word.
|
|
270
|
+
|
|
271
|
+
"""
|
|
272
|
+
params = path_params.model_dump()
|
|
273
|
+
params.update(payload.model_dump())
|
|
274
|
+
params.update(query_params.model_dump())
|
|
275
|
+
|
|
276
|
+
try:
|
|
277
|
+
response = self._requestor.request(
|
|
278
|
+
context=context,
|
|
279
|
+
**params,
|
|
280
|
+
)
|
|
281
|
+
return response
|
|
282
|
+
except ResponseValidationException as e:
|
|
283
|
+
self._logger.error(f"Error calling api: {e.response}.")
|
|
284
|
+
raise e
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
if __name__ == "__main__":
|
|
288
|
+
import logging
|
|
289
|
+
from string import Template
|
|
290
|
+
|
|
291
|
+
from unique_toolkit._common.endpoint_builder import (
|
|
292
|
+
EndpointMethods,
|
|
293
|
+
build_api_operation,
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
class GetUserPathParams(BaseModel):
|
|
297
|
+
user_id: int
|
|
298
|
+
|
|
299
|
+
class GetUserRequestBody(BaseModel):
|
|
300
|
+
include_profile: bool = False
|
|
301
|
+
|
|
302
|
+
class UserResponse(BaseModel):
|
|
303
|
+
id: int
|
|
304
|
+
name: str
|
|
305
|
+
|
|
306
|
+
class CombinedParams(GetUserPathParams, GetUserRequestBody):
|
|
307
|
+
pass
|
|
308
|
+
|
|
309
|
+
UserApiOperation = build_api_operation(
|
|
310
|
+
method=EndpointMethods.GET,
|
|
311
|
+
path_template=Template("/users/{user_id}"),
|
|
312
|
+
path_params_constructor=GetUserPathParams,
|
|
313
|
+
payload_constructor=GetUserRequestBody,
|
|
314
|
+
response_model_type=UserResponse,
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
human_verification_manager = HumanVerificationManagerForApiCalling(
|
|
318
|
+
logger=logging.getLogger(__name__),
|
|
319
|
+
operation=UserApiOperation,
|
|
320
|
+
requestor_type=RequestorType.FAKE,
|
|
321
|
+
return_value={"id": 100, "name": "John Doe"},
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
payload = GetUserRequestBody(include_profile=True)
|
|
325
|
+
|
|
326
|
+
api_call = human_verification_manager._verification_model(
|
|
327
|
+
modifiable_params=payload,
|
|
328
|
+
confirmation=HumanConfirmation(
|
|
329
|
+
payload_hash=hashlib.sha256(payload.model_dump_json().encode()).hexdigest(),
|
|
330
|
+
time_stamp=datetime.now(),
|
|
331
|
+
),
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
last_assistant_message = ChatMessage(
|
|
335
|
+
role=ChatMessageRole.ASSISTANT,
|
|
336
|
+
text=api_call.confirmation.payload_hash,
|
|
337
|
+
chat_id="123",
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
user_message_with_api_call = human_verification_manager._create_next_user_message(
|
|
341
|
+
payload=payload
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
print(user_message_with_api_call)
|
|
345
|
+
|
|
346
|
+
payload = human_verification_manager.detect_api_calls_from_user_message(
|
|
347
|
+
user_message=user_message_with_api_call,
|
|
348
|
+
last_assistant_message=last_assistant_message,
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
if payload is None:
|
|
352
|
+
print("❌ Detection failed - payload is None")
|
|
353
|
+
exit(1)
|
|
354
|
+
else:
|
|
355
|
+
print("✅ Detection successful!")
|
|
356
|
+
print(f"Payload: {payload.model_dump()}")
|
|
357
|
+
print("✅ Dict extraction from string works correctly!")
|