unique_toolkit 0.6.0__tar.gz → 0.6.2__tar.gz
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-0.6.0 → unique_toolkit-0.6.2}/CHANGELOG.md +10 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/PKG-INFO +13 -3
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/README.md +2 -2
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/pyproject.toml +1 -1
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/chat/functions.py +146 -2
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/chat/service.py +215 -7
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/config.py +1 -1
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/context_relevancy/constants.py +1 -1
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/language_model/functions.py +18 -151
- unique_toolkit-0.6.2/unique_toolkit/language_model/infos.py +352 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/language_model/schemas.py +46 -24
- unique_toolkit-0.6.2/unique_toolkit/language_model/service.py +157 -0
- unique_toolkit-0.6.0/unique_toolkit/language_model/infos.py +0 -413
- unique_toolkit-0.6.0/unique_toolkit/language_model/service.py +0 -377
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/LICENSE +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/_common/_base_service.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/_common/_time_utils.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/_common/exception.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/_common/validate_required_values.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/_common/validators.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/app/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/app/init_logging.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/app/init_sdk.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/app/performance/async_tasks.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/app/performance/async_wrapper.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/app/schemas.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/app/verification.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/chat/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/chat/constants.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/chat/schemas.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/chat/state.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/chat/utils.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/content/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/content/constants.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/content/functions.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/content/schemas.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/content/service.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/content/utils.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/embedding/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/embedding/constants.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/embedding/functions.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/embedding/schemas.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/embedding/service.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/embedding/utils.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/constants.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/context_relevancy/prompts.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/context_relevancy/service.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/context_relevancy/utils.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/exception.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/hallucination/constants.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/hallucination/prompts.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/hallucination/service.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/hallucination/utils.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/output_parser.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/evaluators/schemas.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/language_model/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/language_model/builder.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/language_model/constants.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/language_model/prompt.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/language_model/utils.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/short_term_memory/__init__.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/short_term_memory/constants.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/short_term_memory/functions.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/short_term_memory/schemas.py +0 -0
- {unique_toolkit-0.6.0 → unique_toolkit-0.6.2}/unique_toolkit/short_term_memory/service.py +0 -0
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.6.2] - 2025-02-25
|
9
|
+
- Deprecate `LanguageModel` in favor of `LanguageModelInfo`
|
10
|
+
- `LanguageModelTokenLimits` properties become mandatory, initialization allows
|
11
|
+
- init with `token_limit` and `fraction_input` or `input_token_limit` and `output_token_limit`
|
12
|
+
- only `input_token_limit` and `output_token_limit` are members of model
|
13
|
+
|
14
|
+
## [0.6.1] - 2025-02-25
|
15
|
+
- [BREAKING] `LanguageModelService.stream_complete` and `LanguageModelService.stream_complete_async` are now moved to `ChatService.stream_complete` and `ChatService.stream_complete_async`. Correspondingly `assistant_message_id` and `user_message_id` are removed from `LanguageModelService`.
|
16
|
+
- Add `create_user_message` and `create_user_message_async` to `ChatService` (similar to `create_assistant_message` and `create_assistant_message_async`)
|
17
|
+
|
8
18
|
## [0.6.0] - 2025-02-21
|
9
19
|
- make for each domain, its base functionality accessible from `functions.py`
|
10
20
|
- make it possible to instantiate the domain services directly from different event types, inhereted from common `BaseEvent`
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: unique_toolkit
|
3
|
-
Version: 0.6.
|
3
|
+
Version: 0.6.2
|
4
4
|
Summary:
|
5
5
|
License: Proprietary
|
6
6
|
Author: Martin Fadler
|
@@ -55,7 +55,7 @@ The `unique_toolkit.app` module encompasses functions for initializing and secur
|
|
55
55
|
The `unique_toolkit.chat` module encompasses all chat related functionality.
|
56
56
|
|
57
57
|
- `functions.py` comprises the functions to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message.
|
58
|
-
- `service.py` comprises the ChatService and provides an interface to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message.
|
58
|
+
- `service.py` comprises the ChatService and provides an interface to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message and stream complete.
|
59
59
|
- `schemas.py` comprises all relevant schemas, e.g., ChatMessage, used in the ChatService.
|
60
60
|
- `utils.py` comprises utility functions to use and convert ChatMessage objects in assistants, e.g., convert_chat_history_to_injectable_string converts the chat history to a string that can be injected into a prompt.
|
61
61
|
|
@@ -83,7 +83,7 @@ Unique platform.
|
|
83
83
|
|
84
84
|
- `infos.py` comprises the information on all language models deployed through the Unique platform. We recommend to use the LanguageModel class, initialized with the LanguageModelName, e.g., LanguageModel(LanguageModelName.AZURE_GPT_35_TURBO_0125) to get the information on the specific language model like the name, version, token limits or retirement date.
|
85
85
|
- `functions.py` comprises the functions to complete and stream complete to chat.
|
86
|
-
- `service.py` comprises the LanguageModelService and provides an interface to interact with the language models, e.g., complete
|
86
|
+
- `service.py` comprises the LanguageModelService and provides an interface to interact with the language models, e.g., complete.
|
87
87
|
- `schemas.py` comprises all relevant schemas, e.g., LanguageModelResponse, used in the LanguageModelService.
|
88
88
|
- `utils.py` comprises utility functions to parse the output of the language model, e.g., convert_string_to_json finds and parses the last json object in a string.
|
89
89
|
|
@@ -111,6 +111,16 @@ All notable changes to this project will be documented in this file.
|
|
111
111
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
112
112
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
113
113
|
|
114
|
+
## [0.6.2] - 2025-02-25
|
115
|
+
- Deprecate `LanguageModel` in favor of `LanguageModelInfo`
|
116
|
+
- `LanguageModelTokenLimits` properties become mandatory, initialization allows
|
117
|
+
- init with `token_limit` and `fraction_input` or `input_token_limit` and `output_token_limit`
|
118
|
+
- only `input_token_limit` and `output_token_limit` are members of model
|
119
|
+
|
120
|
+
## [0.6.1] - 2025-02-25
|
121
|
+
- [BREAKING] `LanguageModelService.stream_complete` and `LanguageModelService.stream_complete_async` are now moved to `ChatService.stream_complete` and `ChatService.stream_complete_async`. Correspondingly `assistant_message_id` and `user_message_id` are removed from `LanguageModelService`.
|
122
|
+
- Add `create_user_message` and `create_user_message_async` to `ChatService` (similar to `create_assistant_message` and `create_assistant_message_async`)
|
123
|
+
|
114
124
|
## [0.6.0] - 2025-02-21
|
115
125
|
- make for each domain, its base functionality accessible from `functions.py`
|
116
126
|
- make it possible to instantiate the domain services directly from different event types, inhereted from common `BaseEvent`
|
@@ -33,7 +33,7 @@ The `unique_toolkit.app` module encompasses functions for initializing and secur
|
|
33
33
|
The `unique_toolkit.chat` module encompasses all chat related functionality.
|
34
34
|
|
35
35
|
- `functions.py` comprises the functions to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message.
|
36
|
-
- `service.py` comprises the ChatService and provides an interface to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message.
|
36
|
+
- `service.py` comprises the ChatService and provides an interface to manage and load the chat history and interact with the chat ui, e.g., creating a new assistant message and stream complete.
|
37
37
|
- `schemas.py` comprises all relevant schemas, e.g., ChatMessage, used in the ChatService.
|
38
38
|
- `utils.py` comprises utility functions to use and convert ChatMessage objects in assistants, e.g., convert_chat_history_to_injectable_string converts the chat history to a string that can be injected into a prompt.
|
39
39
|
|
@@ -61,7 +61,7 @@ Unique platform.
|
|
61
61
|
|
62
62
|
- `infos.py` comprises the information on all language models deployed through the Unique platform. We recommend to use the LanguageModel class, initialized with the LanguageModelName, e.g., LanguageModel(LanguageModelName.AZURE_GPT_35_TURBO_0125) to get the information on the specific language model like the name, version, token limits or retirement date.
|
63
63
|
- `functions.py` comprises the functions to complete and stream complete to chat.
|
64
|
-
- `service.py` comprises the LanguageModelService and provides an interface to interact with the language models, e.g., complete
|
64
|
+
- `service.py` comprises the LanguageModelService and provides an interface to interact with the language models, e.g., complete.
|
65
65
|
- `schemas.py` comprises all relevant schemas, e.g., LanguageModelResponse, used in the LanguageModelService.
|
66
66
|
- `utils.py` comprises utility functions to parse the output of the language model, e.g., convert_string_to_json finds and parses the last json object in a string.
|
67
67
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import logging
|
2
2
|
import re
|
3
|
-
from typing import Any, Dict, List
|
3
|
+
from typing import Any, Dict, List, cast
|
4
4
|
|
5
5
|
import unique_sdk
|
6
6
|
from unique_sdk._list_object import ListObject
|
@@ -15,8 +15,19 @@ from unique_toolkit.chat.schemas import (
|
|
15
15
|
ChatMessageAssessmentType,
|
16
16
|
ChatMessageRole,
|
17
17
|
)
|
18
|
-
from unique_toolkit.content.schemas import ContentReference
|
18
|
+
from unique_toolkit.content.schemas import ContentChunk, ContentReference
|
19
19
|
from unique_toolkit.content.utils import count_tokens
|
20
|
+
from unique_toolkit.language_model.constants import (
|
21
|
+
DEFAULT_COMPLETE_TEMPERATURE,
|
22
|
+
DEFAULT_COMPLETE_TIMEOUT,
|
23
|
+
)
|
24
|
+
from unique_toolkit.language_model.functions import _prepare_completion_params_util
|
25
|
+
from unique_toolkit.language_model.infos import LanguageModelName
|
26
|
+
from unique_toolkit.language_model.schemas import (
|
27
|
+
LanguageModelMessages,
|
28
|
+
LanguageModelStreamResponse,
|
29
|
+
LanguageModelTool,
|
30
|
+
)
|
20
31
|
|
21
32
|
logger = logging.getLogger(__name__)
|
22
33
|
|
@@ -659,3 +670,136 @@ async def modify_message_assessment_async(
|
|
659
670
|
except Exception as e:
|
660
671
|
logger.error(f"Failed to modify message assessment: {e}")
|
661
672
|
raise e
|
673
|
+
|
674
|
+
|
675
|
+
def stream_complete_to_chat(
|
676
|
+
company_id: str,
|
677
|
+
user_id: str,
|
678
|
+
assistant_message_id: str,
|
679
|
+
user_message_id: str,
|
680
|
+
chat_id: str,
|
681
|
+
assistant_id: str,
|
682
|
+
messages: LanguageModelMessages,
|
683
|
+
model_name: LanguageModelName | str,
|
684
|
+
content_chunks: list[ContentChunk] = [],
|
685
|
+
debug_info: dict = {},
|
686
|
+
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
687
|
+
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
688
|
+
tools: list[LanguageModelTool] | None = None,
|
689
|
+
start_text: str | None = None,
|
690
|
+
other_options: dict | None = None,
|
691
|
+
) -> LanguageModelStreamResponse:
|
692
|
+
"""
|
693
|
+
Streams a completion synchronously.
|
694
|
+
|
695
|
+
Args:
|
696
|
+
company_id (str): The company ID associated with the request.
|
697
|
+
user_id (str): The user ID for the request.
|
698
|
+
assistant_message_id (str): The assistant message ID.
|
699
|
+
user_message_id (str): The user message ID.
|
700
|
+
chat_id (str): The chat ID.
|
701
|
+
assistant_id (str): The assistant ID.
|
702
|
+
messages (LanguageModelMessages): The messages to complete.
|
703
|
+
model_name (LanguageModelName | str): The model name.
|
704
|
+
content_chunks (list[ContentChunk]): Content chunks for context.
|
705
|
+
debug_info (dict): Debug information.
|
706
|
+
temperature (float): Temperature setting.
|
707
|
+
timeout (int): Timeout in milliseconds.
|
708
|
+
tools (Optional[list[LanguageModelTool]]): Optional tools.
|
709
|
+
start_text (Optional[str]): Starting text.
|
710
|
+
other_options (Optional[dict]): Additional options.
|
711
|
+
|
712
|
+
Returns:
|
713
|
+
LanguageModelStreamResponse: The streaming response object.
|
714
|
+
"""
|
715
|
+
options, model, messages_dict, search_context = _prepare_completion_params_util(
|
716
|
+
messages=messages,
|
717
|
+
model_name=model_name,
|
718
|
+
temperature=temperature,
|
719
|
+
tools=tools,
|
720
|
+
other_options=other_options,
|
721
|
+
content_chunks=content_chunks,
|
722
|
+
)
|
723
|
+
|
724
|
+
try:
|
725
|
+
response = unique_sdk.Integrated.chat_stream_completion(
|
726
|
+
user_id=user_id,
|
727
|
+
company_id=company_id,
|
728
|
+
assistantMessageId=assistant_message_id,
|
729
|
+
userMessageId=user_message_id,
|
730
|
+
messages=cast(
|
731
|
+
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
732
|
+
messages_dict,
|
733
|
+
),
|
734
|
+
chatId=chat_id,
|
735
|
+
searchContext=search_context,
|
736
|
+
model=model,
|
737
|
+
timeout=timeout,
|
738
|
+
assistantId=assistant_id,
|
739
|
+
debugInfo=debug_info,
|
740
|
+
options=options, # type: ignore
|
741
|
+
startText=start_text,
|
742
|
+
)
|
743
|
+
return LanguageModelStreamResponse(**response)
|
744
|
+
except Exception as e:
|
745
|
+
logger.error(f"Error streaming completion: {e}")
|
746
|
+
raise e
|
747
|
+
|
748
|
+
|
749
|
+
async def stream_complete_to_chat_async(
|
750
|
+
company_id: str,
|
751
|
+
user_id: str,
|
752
|
+
assistant_message_id: str,
|
753
|
+
user_message_id: str,
|
754
|
+
chat_id: str,
|
755
|
+
assistant_id: str,
|
756
|
+
messages: LanguageModelMessages,
|
757
|
+
model_name: LanguageModelName | str,
|
758
|
+
content_chunks: list[ContentChunk] = [],
|
759
|
+
debug_info: dict = {},
|
760
|
+
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
761
|
+
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
762
|
+
tools: list[LanguageModelTool] | None = None,
|
763
|
+
start_text: str | None = None,
|
764
|
+
other_options: dict | None = None,
|
765
|
+
) -> LanguageModelStreamResponse:
|
766
|
+
"""
|
767
|
+
Streams a completion asynchronously.
|
768
|
+
|
769
|
+
Args: [same as stream_complete]
|
770
|
+
|
771
|
+
Returns:
|
772
|
+
LanguageModelStreamResponse: The streaming response object.
|
773
|
+
"""
|
774
|
+
options, model, messages_dict, search_context = _prepare_completion_params_util(
|
775
|
+
messages=messages,
|
776
|
+
model_name=model_name,
|
777
|
+
temperature=temperature,
|
778
|
+
tools=tools,
|
779
|
+
other_options=other_options,
|
780
|
+
content_chunks=content_chunks,
|
781
|
+
)
|
782
|
+
|
783
|
+
try:
|
784
|
+
response = await unique_sdk.Integrated.chat_stream_completion_async(
|
785
|
+
user_id=user_id,
|
786
|
+
company_id=company_id,
|
787
|
+
assistantMessageId=assistant_message_id,
|
788
|
+
userMessageId=user_message_id,
|
789
|
+
messages=cast(
|
790
|
+
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
791
|
+
messages_dict,
|
792
|
+
),
|
793
|
+
chatId=chat_id,
|
794
|
+
searchContext=search_context,
|
795
|
+
model=model,
|
796
|
+
timeout=timeout,
|
797
|
+
assistantId=assistant_id,
|
798
|
+
debugInfo=debug_info,
|
799
|
+
options=options, # type: ignore
|
800
|
+
startText=start_text,
|
801
|
+
)
|
802
|
+
return LanguageModelStreamResponse(**response)
|
803
|
+
except Exception as e:
|
804
|
+
logger.error(f"Error streaming completion: {e}")
|
805
|
+
raise e
|
@@ -3,6 +3,7 @@ from typing import Optional
|
|
3
3
|
|
4
4
|
from typing_extensions import deprecated
|
5
5
|
|
6
|
+
from unique_toolkit._common.validate_required_values import validate_required_values
|
6
7
|
from unique_toolkit.app.schemas import ChatEvent, Event
|
7
8
|
from unique_toolkit.chat.constants import (
|
8
9
|
DEFAULT_MAX_MESSAGES,
|
@@ -30,7 +31,24 @@ from unique_toolkit.chat.schemas import (
|
|
30
31
|
ChatMessageAssessmentType,
|
31
32
|
ChatMessageRole,
|
32
33
|
)
|
33
|
-
from unique_toolkit.content.schemas import ContentReference
|
34
|
+
from unique_toolkit.content.schemas import ContentChunk, ContentReference
|
35
|
+
from unique_toolkit.language_model.constants import (
|
36
|
+
DEFAULT_COMPLETE_TEMPERATURE,
|
37
|
+
DEFAULT_COMPLETE_TIMEOUT,
|
38
|
+
)
|
39
|
+
from unique_toolkit.language_model.infos import (
|
40
|
+
LanguageModelName,
|
41
|
+
)
|
42
|
+
from unique_toolkit.language_model.schemas import (
|
43
|
+
LanguageModelMessages,
|
44
|
+
LanguageModelStreamResponse,
|
45
|
+
LanguageModelTool,
|
46
|
+
)
|
47
|
+
|
48
|
+
from .functions import (
|
49
|
+
stream_complete_to_chat,
|
50
|
+
stream_complete_to_chat_async,
|
51
|
+
)
|
34
52
|
|
35
53
|
logger = logging.getLogger(f"toolkit.{DOMAIN_NAME}.{__name__}")
|
36
54
|
|
@@ -377,8 +395,8 @@ class ChatService:
|
|
377
395
|
original_content: str | None = None,
|
378
396
|
references: list[ContentReference] = [],
|
379
397
|
debug_info: dict = {},
|
380
|
-
set_completed_at:
|
381
|
-
):
|
398
|
+
set_completed_at: bool | None = False,
|
399
|
+
) -> ChatMessage:
|
382
400
|
"""
|
383
401
|
Creates a message in the chat session synchronously.
|
384
402
|
|
@@ -395,7 +413,7 @@ class ChatService:
|
|
395
413
|
Raises:
|
396
414
|
Exception: If the creation fails.
|
397
415
|
"""
|
398
|
-
|
416
|
+
chat_message = create_message(
|
399
417
|
user_id=self.user_id,
|
400
418
|
company_id=self.company_id,
|
401
419
|
chat_id=self.chat_id,
|
@@ -407,6 +425,9 @@ class ChatService:
|
|
407
425
|
debug_info=debug_info,
|
408
426
|
set_completed_at=set_completed_at,
|
409
427
|
)
|
428
|
+
# Update the assistant message id
|
429
|
+
self.assistant_message_id = chat_message.id
|
430
|
+
return chat_message
|
410
431
|
|
411
432
|
async def create_assistant_message_async(
|
412
433
|
self,
|
@@ -414,8 +435,8 @@ class ChatService:
|
|
414
435
|
original_content: str | None = None,
|
415
436
|
references: list[ContentReference] = [],
|
416
437
|
debug_info: dict = {},
|
417
|
-
set_completed_at:
|
418
|
-
):
|
438
|
+
set_completed_at: bool | None = False,
|
439
|
+
) -> ChatMessage:
|
419
440
|
"""
|
420
441
|
Creates a message in the chat session asynchronously.
|
421
442
|
|
@@ -433,7 +454,7 @@ class ChatService:
|
|
433
454
|
Exception: If the creation fails.
|
434
455
|
"""
|
435
456
|
|
436
|
-
|
457
|
+
chat_message = await create_message_async(
|
437
458
|
user_id=self.user_id,
|
438
459
|
company_id=self.company_id,
|
439
460
|
chat_id=self.chat_id,
|
@@ -445,6 +466,90 @@ class ChatService:
|
|
445
466
|
debug_info=debug_info,
|
446
467
|
set_completed_at=set_completed_at,
|
447
468
|
)
|
469
|
+
# Update the assistant message id
|
470
|
+
self.assistant_message_id = chat_message.id
|
471
|
+
return chat_message
|
472
|
+
|
473
|
+
def create_user_message(
|
474
|
+
self,
|
475
|
+
content: str,
|
476
|
+
original_content: str | None = None,
|
477
|
+
references: list[ContentReference] = [],
|
478
|
+
debug_info: dict = {},
|
479
|
+
set_completed_at: bool | None = False,
|
480
|
+
) -> ChatMessage:
|
481
|
+
"""
|
482
|
+
Creates a user message in the chat session synchronously.
|
483
|
+
|
484
|
+
Args:
|
485
|
+
content (str): The content for the message.
|
486
|
+
original_content (str, optional): The original content for the message.
|
487
|
+
references (list[ContentReference]): list of ContentReference objects. Defaults to None.
|
488
|
+
debug_info (dict[str, Any]]): Debug information. Defaults to None.
|
489
|
+
set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
|
490
|
+
|
491
|
+
Returns:
|
492
|
+
ChatMessage: The created message.
|
493
|
+
|
494
|
+
Raises:
|
495
|
+
Exception: If the creation fails.
|
496
|
+
"""
|
497
|
+
chat_message = create_message(
|
498
|
+
user_id=self.user_id,
|
499
|
+
company_id=self.company_id,
|
500
|
+
chat_id=self.chat_id,
|
501
|
+
assistant_id=self.assistant_id,
|
502
|
+
role=ChatMessageRole.USER,
|
503
|
+
content=content,
|
504
|
+
original_content=original_content,
|
505
|
+
references=references,
|
506
|
+
debug_info=debug_info,
|
507
|
+
set_completed_at=set_completed_at,
|
508
|
+
)
|
509
|
+
# Update the user message id
|
510
|
+
self.user_message_id = chat_message.id
|
511
|
+
return chat_message
|
512
|
+
|
513
|
+
async def create_user_message_async(
|
514
|
+
self,
|
515
|
+
content: str,
|
516
|
+
original_content: str | None = None,
|
517
|
+
references: list[ContentReference] = [],
|
518
|
+
debug_info: dict = {},
|
519
|
+
set_completed_at: bool | None = False,
|
520
|
+
) -> ChatMessage:
|
521
|
+
"""
|
522
|
+
Creates a user message in the chat session asynchronously.
|
523
|
+
|
524
|
+
Args:
|
525
|
+
content (str): The content for the message.
|
526
|
+
original_content (str, optional): The original content for the message.
|
527
|
+
references (list[ContentReference]): list of references. Defaults to None.
|
528
|
+
debug_info (dict[str, Any]]): Debug information. Defaults to None.
|
529
|
+
set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
|
530
|
+
|
531
|
+
Returns:
|
532
|
+
ChatMessage: The created message.
|
533
|
+
|
534
|
+
Raises:
|
535
|
+
Exception: If the creation fails.
|
536
|
+
"""
|
537
|
+
|
538
|
+
chat_message = await create_message_async(
|
539
|
+
user_id=self.user_id,
|
540
|
+
company_id=self.company_id,
|
541
|
+
chat_id=self.chat_id,
|
542
|
+
assistant_id=self.assistant_id,
|
543
|
+
role=ChatMessageRole.USER,
|
544
|
+
content=content,
|
545
|
+
original_content=original_content,
|
546
|
+
references=references,
|
547
|
+
debug_info=debug_info,
|
548
|
+
set_completed_at=set_completed_at,
|
549
|
+
)
|
550
|
+
# Update the user message id
|
551
|
+
self.user_message_id = chat_message.id
|
552
|
+
return chat_message
|
448
553
|
|
449
554
|
def create_message_assessment(
|
450
555
|
self,
|
@@ -599,3 +704,106 @@ class ChatService:
|
|
599
704
|
explanation=explanation,
|
600
705
|
label=label,
|
601
706
|
)
|
707
|
+
|
708
|
+
def stream_complete(
|
709
|
+
self,
|
710
|
+
messages: LanguageModelMessages,
|
711
|
+
model_name: LanguageModelName | str,
|
712
|
+
content_chunks: list[ContentChunk] = [],
|
713
|
+
debug_info: dict = {},
|
714
|
+
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
715
|
+
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
716
|
+
tools: Optional[list[LanguageModelTool]] = None,
|
717
|
+
start_text: Optional[str] = None,
|
718
|
+
other_options: Optional[dict] = None,
|
719
|
+
) -> LanguageModelStreamResponse:
|
720
|
+
"""
|
721
|
+
Streams a completion in the chat session synchronously.
|
722
|
+
"""
|
723
|
+
[
|
724
|
+
company_id,
|
725
|
+
user_id,
|
726
|
+
assistant_message_id,
|
727
|
+
user_message_id,
|
728
|
+
chat_id,
|
729
|
+
assistant_id,
|
730
|
+
] = validate_required_values(
|
731
|
+
[
|
732
|
+
self.company_id,
|
733
|
+
self.user_id,
|
734
|
+
self.assistant_message_id,
|
735
|
+
self.user_message_id,
|
736
|
+
self.chat_id,
|
737
|
+
self.assistant_id,
|
738
|
+
]
|
739
|
+
)
|
740
|
+
|
741
|
+
return stream_complete_to_chat(
|
742
|
+
company_id=company_id,
|
743
|
+
user_id=user_id,
|
744
|
+
assistant_message_id=assistant_message_id,
|
745
|
+
user_message_id=user_message_id,
|
746
|
+
chat_id=chat_id,
|
747
|
+
assistant_id=assistant_id,
|
748
|
+
messages=messages,
|
749
|
+
model_name=model_name,
|
750
|
+
content_chunks=content_chunks,
|
751
|
+
debug_info=debug_info,
|
752
|
+
temperature=temperature,
|
753
|
+
timeout=timeout,
|
754
|
+
tools=tools,
|
755
|
+
start_text=start_text,
|
756
|
+
other_options=other_options,
|
757
|
+
)
|
758
|
+
|
759
|
+
async def stream_complete_async(
|
760
|
+
self,
|
761
|
+
messages: LanguageModelMessages,
|
762
|
+
model_name: LanguageModelName | str,
|
763
|
+
content_chunks: list[ContentChunk] = [],
|
764
|
+
debug_info: dict = {},
|
765
|
+
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
766
|
+
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
767
|
+
tools: Optional[list[LanguageModelTool]] = None,
|
768
|
+
start_text: Optional[str] = None,
|
769
|
+
other_options: Optional[dict] = None,
|
770
|
+
) -> LanguageModelStreamResponse:
|
771
|
+
"""
|
772
|
+
Streams a completion in the chat session asynchronously.
|
773
|
+
"""
|
774
|
+
|
775
|
+
[
|
776
|
+
company_id,
|
777
|
+
user_id,
|
778
|
+
assistant_message_id,
|
779
|
+
user_message_id,
|
780
|
+
chat_id,
|
781
|
+
assistant_id,
|
782
|
+
] = validate_required_values(
|
783
|
+
[
|
784
|
+
self.company_id,
|
785
|
+
self.user_id,
|
786
|
+
self.assistant_message_id,
|
787
|
+
self.user_message_id,
|
788
|
+
self.chat_id,
|
789
|
+
self.assistant_id,
|
790
|
+
]
|
791
|
+
)
|
792
|
+
|
793
|
+
return await stream_complete_to_chat_async(
|
794
|
+
company_id=company_id,
|
795
|
+
user_id=user_id,
|
796
|
+
assistant_message_id=assistant_message_id,
|
797
|
+
user_message_id=user_message_id,
|
798
|
+
chat_id=chat_id,
|
799
|
+
assistant_id=assistant_id,
|
800
|
+
messages=messages,
|
801
|
+
model_name=model_name,
|
802
|
+
content_chunks=content_chunks,
|
803
|
+
debug_info=debug_info,
|
804
|
+
temperature=temperature,
|
805
|
+
timeout=timeout,
|
806
|
+
tools=tools,
|
807
|
+
start_text=start_text,
|
808
|
+
other_options=other_options,
|
809
|
+
)
|
@@ -25,7 +25,7 @@ class EvaluationMetricConfig(BaseModel):
|
|
25
25
|
enabled: bool = False
|
26
26
|
name: EvaluationMetricName
|
27
27
|
language_model: LanguageModel = LanguageModel(
|
28
|
-
LanguageModelName.
|
28
|
+
LanguageModelName.AZURE_GPT_35_TURBO_0125
|
29
29
|
)
|
30
30
|
custom_prompts: dict[str, str] = {}
|
31
31
|
score_to_emoji: dict[str, str] = {}
|
@@ -23,7 +23,7 @@ context_relevancy_required_input_fields = [
|
|
23
23
|
default_config = EvaluationMetricConfig(
|
24
24
|
enabled=False,
|
25
25
|
name=EvaluationMetricName.CONTEXT_RELEVANCY,
|
26
|
-
language_model=LanguageModel(LanguageModelName.
|
26
|
+
language_model=LanguageModel(LanguageModelName.AZURE_GPT_35_TURBO_0125),
|
27
27
|
score_to_emoji={"LOW": "🟢", "MEDIUM": "🟡", "HIGH": "🔴"},
|
28
28
|
custom_prompts={
|
29
29
|
SYSTEM_MSG_KEY: CONTEXT_RELEVANCY_METRIC_SYSTEM_MSG,
|