unique_toolkit 0.6.0__py3-none-any.whl → 0.6.2__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/chat/functions.py +146 -2
- unique_toolkit/chat/service.py +215 -7
- unique_toolkit/evaluators/config.py +1 -1
- unique_toolkit/evaluators/context_relevancy/constants.py +1 -1
- unique_toolkit/language_model/functions.py +18 -151
- unique_toolkit/language_model/infos.py +146 -207
- unique_toolkit/language_model/schemas.py +46 -24
- unique_toolkit/language_model/service.py +0 -220
- {unique_toolkit-0.6.0.dist-info → unique_toolkit-0.6.2.dist-info}/METADATA +13 -3
- {unique_toolkit-0.6.0.dist-info → unique_toolkit-0.6.2.dist-info}/RECORD +12 -12
- {unique_toolkit-0.6.0.dist-info → unique_toolkit-0.6.2.dist-info}/LICENSE +0 -0
- {unique_toolkit-0.6.0.dist-info → unique_toolkit-0.6.2.dist-info}/WHEEL +0 -0
unique_toolkit/chat/functions.py
CHANGED
@@ -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
|
unique_toolkit/chat/service.py
CHANGED
@@ -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,
|
@@ -1,20 +1,20 @@
|
|
1
1
|
import logging
|
2
|
-
from typing import
|
2
|
+
from typing import Type, cast
|
3
3
|
|
4
4
|
import unique_sdk
|
5
5
|
from pydantic import BaseModel
|
6
6
|
|
7
7
|
from unique_toolkit.content.schemas import ContentChunk
|
8
8
|
from unique_toolkit.evaluators import DOMAIN_NAME
|
9
|
-
|
9
|
+
|
10
|
+
from .constants import (
|
10
11
|
DEFAULT_COMPLETE_TEMPERATURE,
|
11
12
|
DEFAULT_COMPLETE_TIMEOUT,
|
12
13
|
)
|
13
|
-
from
|
14
|
-
from
|
14
|
+
from .infos import LanguageModelName
|
15
|
+
from .schemas import (
|
15
16
|
LanguageModelMessages,
|
16
17
|
LanguageModelResponse,
|
17
|
-
LanguageModelStreamResponse,
|
18
18
|
LanguageModelTool,
|
19
19
|
)
|
20
20
|
|
@@ -27,9 +27,9 @@ def complete(
|
|
27
27
|
model_name: LanguageModelName | str,
|
28
28
|
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
29
29
|
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
30
|
-
tools:
|
31
|
-
other_options:
|
32
|
-
structured_output_model:
|
30
|
+
tools: list[LanguageModelTool] | None = None,
|
31
|
+
other_options: dict | None = None,
|
32
|
+
structured_output_model: Type[BaseModel] | None = None,
|
33
33
|
structured_output_enforce_schema: bool = False,
|
34
34
|
) -> LanguageModelResponse:
|
35
35
|
"""
|
@@ -80,9 +80,9 @@ async def complete_async(
|
|
80
80
|
model_name: LanguageModelName | str,
|
81
81
|
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
82
82
|
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
83
|
-
tools:
|
84
|
-
other_options:
|
85
|
-
structured_output_model:
|
83
|
+
tools: list[LanguageModelTool] | None = None,
|
84
|
+
other_options: dict | None = None,
|
85
|
+
structured_output_model: Type[BaseModel] | None = None,
|
86
86
|
structured_output_enforce_schema: bool = False,
|
87
87
|
) -> LanguageModelResponse:
|
88
88
|
"""
|
@@ -134,142 +134,9 @@ async def complete_async(
|
|
134
134
|
raise e
|
135
135
|
|
136
136
|
|
137
|
-
def stream_complete_to_chat(
|
138
|
-
company_id: str,
|
139
|
-
user_id: str,
|
140
|
-
assistant_message_id: str,
|
141
|
-
user_message_id: str,
|
142
|
-
chat_id: str,
|
143
|
-
assistant_id: str,
|
144
|
-
messages: LanguageModelMessages,
|
145
|
-
model_name: LanguageModelName | str,
|
146
|
-
content_chunks: list[ContentChunk] = [],
|
147
|
-
debug_info: dict = {},
|
148
|
-
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
149
|
-
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
150
|
-
tools: Optional[list[LanguageModelTool]] = None,
|
151
|
-
start_text: Optional[str] = None,
|
152
|
-
other_options: Optional[dict] = None,
|
153
|
-
) -> LanguageModelStreamResponse:
|
154
|
-
"""
|
155
|
-
Streams a completion synchronously.
|
156
|
-
|
157
|
-
Args:
|
158
|
-
company_id (str): The company ID associated with the request.
|
159
|
-
user_id (str): The user ID for the request.
|
160
|
-
assistant_message_id (str): The assistant message ID.
|
161
|
-
user_message_id (str): The user message ID.
|
162
|
-
chat_id (str): The chat ID.
|
163
|
-
assistant_id (str): The assistant ID.
|
164
|
-
messages (LanguageModelMessages): The messages to complete.
|
165
|
-
model_name (LanguageModelName | str): The model name.
|
166
|
-
content_chunks (list[ContentChunk]): Content chunks for context.
|
167
|
-
debug_info (dict): Debug information.
|
168
|
-
temperature (float): Temperature setting.
|
169
|
-
timeout (int): Timeout in milliseconds.
|
170
|
-
tools (Optional[list[LanguageModelTool]]): Optional tools.
|
171
|
-
start_text (Optional[str]): Starting text.
|
172
|
-
other_options (Optional[dict]): Additional options.
|
173
|
-
|
174
|
-
Returns:
|
175
|
-
LanguageModelStreamResponse: The streaming response object.
|
176
|
-
"""
|
177
|
-
options, model, messages_dict, search_context = _prepare_completion_params_util(
|
178
|
-
messages=messages,
|
179
|
-
model_name=model_name,
|
180
|
-
temperature=temperature,
|
181
|
-
tools=tools,
|
182
|
-
other_options=other_options,
|
183
|
-
content_chunks=content_chunks,
|
184
|
-
)
|
185
|
-
|
186
|
-
try:
|
187
|
-
response = unique_sdk.Integrated.chat_stream_completion(
|
188
|
-
user_id=user_id,
|
189
|
-
company_id=company_id,
|
190
|
-
assistantMessageId=assistant_message_id,
|
191
|
-
userMessageId=user_message_id,
|
192
|
-
messages=cast(
|
193
|
-
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
194
|
-
messages_dict,
|
195
|
-
),
|
196
|
-
chatId=chat_id,
|
197
|
-
searchContext=search_context,
|
198
|
-
model=model,
|
199
|
-
timeout=timeout,
|
200
|
-
assistantId=assistant_id,
|
201
|
-
debugInfo=debug_info,
|
202
|
-
options=options, # type: ignore
|
203
|
-
startText=start_text,
|
204
|
-
)
|
205
|
-
return LanguageModelStreamResponse(**response)
|
206
|
-
except Exception as e:
|
207
|
-
logger.error(f"Error streaming completion: {e}")
|
208
|
-
raise e
|
209
|
-
|
210
|
-
|
211
|
-
async def stream_complete_to_chat_async(
|
212
|
-
company_id: str,
|
213
|
-
user_id: str,
|
214
|
-
assistant_message_id: str,
|
215
|
-
user_message_id: str,
|
216
|
-
chat_id: str,
|
217
|
-
assistant_id: str,
|
218
|
-
messages: LanguageModelMessages,
|
219
|
-
model_name: LanguageModelName | str,
|
220
|
-
content_chunks: list[ContentChunk] = [],
|
221
|
-
debug_info: dict = {},
|
222
|
-
temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
|
223
|
-
timeout: int = DEFAULT_COMPLETE_TIMEOUT,
|
224
|
-
tools: Optional[list[LanguageModelTool]] = None,
|
225
|
-
start_text: Optional[str] = None,
|
226
|
-
other_options: Optional[dict] = None,
|
227
|
-
) -> LanguageModelStreamResponse:
|
228
|
-
"""
|
229
|
-
Streams a completion asynchronously.
|
230
|
-
|
231
|
-
Args: [same as stream_complete]
|
232
|
-
|
233
|
-
Returns:
|
234
|
-
LanguageModelStreamResponse: The streaming response object.
|
235
|
-
"""
|
236
|
-
options, model, messages_dict, search_context = _prepare_completion_params_util(
|
237
|
-
messages=messages,
|
238
|
-
model_name=model_name,
|
239
|
-
temperature=temperature,
|
240
|
-
tools=tools,
|
241
|
-
other_options=other_options,
|
242
|
-
content_chunks=content_chunks,
|
243
|
-
)
|
244
|
-
|
245
|
-
try:
|
246
|
-
response = await unique_sdk.Integrated.chat_stream_completion_async(
|
247
|
-
user_id=user_id,
|
248
|
-
company_id=company_id,
|
249
|
-
assistantMessageId=assistant_message_id,
|
250
|
-
userMessageId=user_message_id,
|
251
|
-
messages=cast(
|
252
|
-
list[unique_sdk.Integrated.ChatCompletionRequestMessage],
|
253
|
-
messages_dict,
|
254
|
-
),
|
255
|
-
chatId=chat_id,
|
256
|
-
searchContext=search_context,
|
257
|
-
model=model,
|
258
|
-
timeout=timeout,
|
259
|
-
assistantId=assistant_id,
|
260
|
-
debugInfo=debug_info,
|
261
|
-
options=options, # type: ignore
|
262
|
-
startText=start_text,
|
263
|
-
)
|
264
|
-
return LanguageModelStreamResponse(**response)
|
265
|
-
except Exception as e:
|
266
|
-
logger.error(f"Error streaming completion: {e}")
|
267
|
-
raise e
|
268
|
-
|
269
|
-
|
270
137
|
def _add_tools_to_options(
|
271
138
|
options: dict,
|
272
|
-
tools:
|
139
|
+
tools: list[LanguageModelTool] | None,
|
273
140
|
) -> dict:
|
274
141
|
if tools:
|
275
142
|
options["tools"] = [
|
@@ -321,12 +188,12 @@ def _prepare_completion_params_util(
|
|
321
188
|
messages: LanguageModelMessages,
|
322
189
|
model_name: LanguageModelName | str,
|
323
190
|
temperature: float,
|
324
|
-
tools:
|
325
|
-
other_options:
|
326
|
-
content_chunks:
|
327
|
-
structured_output_model:
|
191
|
+
tools: list[LanguageModelTool] | None = None,
|
192
|
+
other_options: dict | None = None,
|
193
|
+
content_chunks: list[ContentChunk] | None = None,
|
194
|
+
structured_output_model: Type[BaseModel] | None = None,
|
328
195
|
structured_output_enforce_schema: bool = False,
|
329
|
-
) -> tuple[dict, str, dict,
|
196
|
+
) -> tuple[dict, str, dict, dict | None]:
|
330
197
|
"""
|
331
198
|
Prepares common parameters for completion requests.
|
332
199
|
|
@@ -335,7 +202,7 @@ def _prepare_completion_params_util(
|
|
335
202
|
- options (dict): Combined options including tools and temperature
|
336
203
|
- model (str): Resolved model name
|
337
204
|
- messages_dict (dict): Processed messages
|
338
|
-
- search_context (
|
205
|
+
- search_context (dict | None): Processed content chunks if provided
|
339
206
|
"""
|
340
207
|
|
341
208
|
options = _add_tools_to_options({}, tools)
|