unique_toolkit 1.14.2__py3-none-any.whl → 1.14.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.
@@ -0,0 +1,1498 @@
1
+ import logging
2
+ from typing import Any
3
+
4
+ import unique_sdk
5
+ from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam
6
+ from typing_extensions import deprecated
7
+
8
+ from unique_toolkit._common.utils.files import is_file_content, is_image_content
9
+ from unique_toolkit.chat.constants import (
10
+ DEFAULT_MAX_MESSAGES,
11
+ DEFAULT_PERCENT_OF_MAX_TOKENS,
12
+ DOMAIN_NAME,
13
+ )
14
+ from unique_toolkit.chat.deprecated.service import ChatServiceDeprecated
15
+ from unique_toolkit.chat.functions import (
16
+ create_message,
17
+ create_message_assessment,
18
+ create_message_assessment_async,
19
+ create_message_async,
20
+ create_message_execution,
21
+ create_message_execution_async,
22
+ create_message_log,
23
+ create_message_log_async,
24
+ get_full_history,
25
+ get_full_history_async,
26
+ get_message_execution,
27
+ get_message_execution_async,
28
+ get_selection_from_history,
29
+ modify_message,
30
+ modify_message_assessment,
31
+ modify_message_assessment_async,
32
+ modify_message_async,
33
+ stream_complete_with_references,
34
+ stream_complete_with_references_async,
35
+ update_message_execution,
36
+ update_message_execution_async,
37
+ update_message_log,
38
+ update_message_log_async,
39
+ )
40
+ from unique_toolkit.chat.schemas import (
41
+ ChatMessage,
42
+ ChatMessageAssessment,
43
+ ChatMessageAssessmentLabel,
44
+ ChatMessageAssessmentStatus,
45
+ ChatMessageAssessmentType,
46
+ ChatMessageRole,
47
+ MessageExecution,
48
+ MessageExecutionType,
49
+ MessageExecutionUpdateStatus,
50
+ MessageLog,
51
+ MessageLogDetails,
52
+ MessageLogStatus,
53
+ MessageLogUncitedReferences,
54
+ )
55
+ from unique_toolkit.content.functions import (
56
+ download_content_to_bytes,
57
+ search_contents,
58
+ upload_content_from_bytes,
59
+ )
60
+ from unique_toolkit.content.schemas import (
61
+ Content,
62
+ ContentChunk,
63
+ ContentReference,
64
+ )
65
+ from unique_toolkit.language_model.constants import (
66
+ DEFAULT_COMPLETE_TEMPERATURE,
67
+ DEFAULT_COMPLETE_TIMEOUT,
68
+ )
69
+ from unique_toolkit.language_model.infos import (
70
+ LanguageModelName,
71
+ )
72
+ from unique_toolkit.language_model.schemas import (
73
+ LanguageModelMessages,
74
+ LanguageModelResponse,
75
+ LanguageModelStreamResponse,
76
+ LanguageModelTool,
77
+ LanguageModelToolDescription,
78
+ )
79
+
80
+ logger = logging.getLogger(f"toolkit.{DOMAIN_NAME}.{__name__}")
81
+
82
+
83
+ class ChatService(ChatServiceDeprecated):
84
+ """Provides all functionalities to manage the chat session."""
85
+
86
+ async def update_debug_info_async(self, debug_info: dict):
87
+ """Updates the debug information for the chat session.
88
+
89
+ Args:
90
+ debug_info (dict): The new debug information.
91
+
92
+ """
93
+ return await modify_message_async(
94
+ user_id=self._user_id,
95
+ company_id=self._company_id,
96
+ assistant_message_id=self._assistant_message_id,
97
+ chat_id=self._chat_id,
98
+ user_message_id=self._user_message_id,
99
+ user_message_text=self._user_message_text,
100
+ assistant=False,
101
+ debug_info=debug_info,
102
+ )
103
+
104
+ def replace_debug_info(self, debug_info: dict):
105
+ """Replace the debug information in the last user message
106
+
107
+ Args:
108
+ debug_info (dict): The new debug information.
109
+
110
+ """
111
+ return modify_message(
112
+ user_id=self._user_id,
113
+ company_id=self._company_id,
114
+ assistant_message_id=self._assistant_message_id,
115
+ chat_id=self._chat_id,
116
+ user_message_id=self._user_message_id,
117
+ user_message_text=self._user_message_text,
118
+ assistant=False,
119
+ debug_info=debug_info,
120
+ )
121
+
122
+ # Message Methods
123
+ ############################################################################
124
+
125
+ def modify_user_message(
126
+ self,
127
+ content: str,
128
+ references: list[ContentReference] | None = None,
129
+ debug_info: dict | None = None,
130
+ message_id: str | None = None,
131
+ set_completed_at: bool | None = False,
132
+ ) -> ChatMessage:
133
+ """Modifies a user message in the chat session synchronously.
134
+
135
+ Args:
136
+ content (str): The new content for the message.
137
+ references (list[ContentReference]): list of ContentReference objects.
138
+ debug_info (dict[str, Any]]]): Debug information.
139
+ message_id (str, optional): The message ID, if not specified the last user message is edited.
140
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
141
+
142
+ Returns:
143
+ ChatMessage: The modified message.
144
+
145
+ Raises:
146
+ Exception: If the modification fails.
147
+
148
+ """
149
+ return modify_message(
150
+ user_id=self._user_id,
151
+ company_id=self._company_id,
152
+ assistant_message_id=self._assistant_message_id,
153
+ chat_id=self._chat_id,
154
+ user_message_id=self._user_message_id,
155
+ user_message_text=self._user_message_text,
156
+ assistant=False,
157
+ content=content,
158
+ references=references,
159
+ debug_info=debug_info,
160
+ message_id=message_id,
161
+ set_completed_at=set_completed_at or False,
162
+ )
163
+
164
+ async def modify_user_message_async(
165
+ self,
166
+ content: str,
167
+ references: list[ContentReference] = [],
168
+ debug_info: dict = {},
169
+ message_id: str | None = None,
170
+ set_completed_at: bool | None = False,
171
+ ) -> ChatMessage:
172
+ """Modifies a message in the chat session asynchronously.
173
+
174
+ Args:
175
+ content (str): The new content for the message.
176
+ message_id (str, optional): The message ID. Defaults to None, then the ChatState user message id is used.
177
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
178
+ debug_info (dict[str, Any]]]): Debug information. Defaults to {}.
179
+ set_completed_at (bool, optional): Whether to set the completedAt field with the current date time. Defaults to False.
180
+
181
+ Returns:
182
+ ChatMessage: The modified message.
183
+
184
+ Raises:
185
+ Exception: If the modification fails.
186
+
187
+ """
188
+ return await modify_message_async(
189
+ user_id=self._user_id,
190
+ company_id=self._company_id,
191
+ assistant_message_id=self._assistant_message_id,
192
+ chat_id=self._chat_id,
193
+ user_message_id=self._user_message_id,
194
+ user_message_text=self._user_message_text,
195
+ assistant=False,
196
+ content=content,
197
+ references=references,
198
+ debug_info=debug_info,
199
+ message_id=message_id,
200
+ set_completed_at=set_completed_at or False,
201
+ )
202
+
203
+ def modify_assistant_message(
204
+ self,
205
+ content: str | None = None,
206
+ original_content: str | None = None,
207
+ references: list[ContentReference] | None = None,
208
+ debug_info: dict | None = None,
209
+ message_id: str | None = None,
210
+ set_completed_at: bool | None = None,
211
+ ) -> ChatMessage:
212
+ """Modifies a message in the chat session synchronously if parameter is not specified the corresponding field will remain as is.
213
+
214
+ Args:
215
+ content (str, optional): The new content for the message.
216
+ original_content (str, optional): The original content for the message.
217
+ references (list[ContentReference]): list of ContentReference objects. Defaults to [].
218
+ debug_info (dict[str, Any]]]): Debug information. Defaults to {}.
219
+ message_id (Optional[str]): The message ID. Defaults to None.
220
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
221
+
222
+ Returns:
223
+ ChatMessage: The modified message.
224
+
225
+ Raises:
226
+ Exception: If the modification fails.
227
+
228
+ """
229
+ return modify_message(
230
+ user_id=self._user_id,
231
+ company_id=self._company_id,
232
+ assistant_message_id=self._assistant_message_id,
233
+ chat_id=self._chat_id,
234
+ user_message_id=self._user_message_id,
235
+ user_message_text=self._user_message_text,
236
+ assistant=True,
237
+ content=content,
238
+ original_content=original_content,
239
+ references=references,
240
+ debug_info=debug_info,
241
+ message_id=message_id,
242
+ set_completed_at=set_completed_at or False,
243
+ )
244
+
245
+ async def modify_assistant_message_async(
246
+ self,
247
+ content: str | None = None,
248
+ original_content: str | None = None,
249
+ references: list[ContentReference] | None = None,
250
+ debug_info: dict | None = None,
251
+ message_id: str | None = None,
252
+ set_completed_at: bool | None = False,
253
+ ) -> ChatMessage:
254
+ """Modifies a message in the chat session asynchronously.
255
+
256
+ Args:
257
+ content (str, optional): The new content for the message.
258
+ original_content (str, optional): The original content for the message.
259
+ message_id (str, optional): The message ID. Defaults to None, then the ChatState assistant message id is used.
260
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
261
+ debug_info (dict[str, Any]], optional): Debug information. Defaults to None.
262
+ set_completed_at (bool, optional): Whether to set the completedAt field with the current date time. Defaults to False.
263
+
264
+ Returns:
265
+ ChatMessage: The modified message.
266
+
267
+ Raises:
268
+ Exception: If the modification fails.
269
+
270
+ """
271
+ return await modify_message_async(
272
+ user_id=self._user_id,
273
+ company_id=self._company_id,
274
+ assistant_message_id=self._assistant_message_id,
275
+ chat_id=self._chat_id,
276
+ user_message_id=self._user_message_id,
277
+ user_message_text=self._user_message_text,
278
+ assistant=True,
279
+ content=content,
280
+ original_content=original_content,
281
+ references=references,
282
+ debug_info=debug_info,
283
+ message_id=message_id,
284
+ set_completed_at=set_completed_at or False,
285
+ )
286
+
287
+ def create_assistant_message(
288
+ self,
289
+ content: str,
290
+ original_content: str | None = None,
291
+ references: list[ContentReference] | None = None,
292
+ debug_info: dict | None = None,
293
+ set_completed_at: bool | None = False,
294
+ ) -> ChatMessage:
295
+ """Creates a message in the chat session synchronously.
296
+
297
+ Args:
298
+ content (str): The content for the message.
299
+ original_content (str, optional): The original content for the message.
300
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
301
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
302
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
303
+
304
+ Returns:
305
+ ChatMessage: The created message.
306
+
307
+ Raises:
308
+ Exception: If the creation fails.
309
+
310
+ """
311
+ chat_message = create_message(
312
+ user_id=self._user_id,
313
+ company_id=self._company_id,
314
+ chat_id=self._chat_id,
315
+ assistant_id=self._assistant_id,
316
+ role=ChatMessageRole.ASSISTANT,
317
+ content=content,
318
+ original_content=original_content,
319
+ references=references,
320
+ debug_info=debug_info,
321
+ set_completed_at=set_completed_at,
322
+ )
323
+ # Update the assistant message id
324
+ self._assistant_message_id = chat_message.id or "unknown"
325
+ return chat_message
326
+
327
+ async def create_assistant_message_async(
328
+ self,
329
+ content: str,
330
+ original_content: str | None = None,
331
+ references: list[ContentReference] | None = None,
332
+ debug_info: dict | None = None,
333
+ set_completed_at: bool | None = False,
334
+ ) -> ChatMessage:
335
+ """Creates a message in the chat session asynchronously.
336
+
337
+ Args:
338
+ content (str): The content for the message.
339
+ original_content (str, optional): The original content for the message.
340
+ references (list[ContentReference]): list of references. Defaults to None.
341
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
342
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
343
+
344
+ Returns:
345
+ ChatMessage: The created message.
346
+
347
+ Raises:
348
+ Exception: If the creation fails.
349
+
350
+ """
351
+ chat_message = await create_message_async(
352
+ user_id=self._user_id,
353
+ company_id=self._company_id,
354
+ chat_id=self._chat_id,
355
+ assistant_id=self._assistant_id,
356
+ role=ChatMessageRole.ASSISTANT,
357
+ content=content,
358
+ original_content=original_content,
359
+ references=references,
360
+ debug_info=debug_info,
361
+ set_completed_at=set_completed_at,
362
+ )
363
+ # Update the assistant message id
364
+ self._assistant_message_id = chat_message.id or "unknown"
365
+ return chat_message
366
+
367
+ def create_user_message(
368
+ self,
369
+ content: str,
370
+ original_content: str | None = None,
371
+ references: list[ContentReference] | None = None,
372
+ debug_info: dict | None = None,
373
+ set_completed_at: bool | None = False,
374
+ ) -> ChatMessage:
375
+ """Creates a user message in the chat session synchronously.
376
+
377
+ Args:
378
+ content (str): The content for the message.
379
+ original_content (str, optional): The original content for the message.
380
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
381
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
382
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
383
+
384
+ Returns:
385
+ ChatMessage: The created message.
386
+
387
+ Raises:
388
+ Exception: If the creation fails.
389
+
390
+ """
391
+ chat_message = create_message(
392
+ user_id=self._user_id,
393
+ company_id=self._company_id,
394
+ chat_id=self._chat_id,
395
+ assistant_id=self._assistant_id,
396
+ role=ChatMessageRole.USER,
397
+ content=content,
398
+ original_content=original_content,
399
+ references=references,
400
+ debug_info=debug_info,
401
+ set_completed_at=set_completed_at,
402
+ )
403
+ # Update the user message id
404
+ self._user_message_id = chat_message.id or "unknown"
405
+ return chat_message
406
+
407
+ async def create_user_message_async(
408
+ self,
409
+ content: str,
410
+ original_content: str | None = None,
411
+ references: list[ContentReference] | None = None,
412
+ debug_info: dict | None = None,
413
+ set_completed_at: bool | None = False,
414
+ ) -> ChatMessage:
415
+ """Creates a user message in the chat session asynchronously.
416
+
417
+ Args:
418
+ content (str): The content for the message.
419
+ original_content (str, optional): The original content for the message.
420
+ references (list[ContentReference]): list of references. Defaults to None.
421
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
422
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
423
+
424
+ Returns:
425
+ ChatMessage: The created message.
426
+
427
+ Raises:
428
+ Exception: If the creation fails.
429
+
430
+ """
431
+ chat_message = await create_message_async(
432
+ user_id=self._user_id,
433
+ company_id=self._company_id,
434
+ chat_id=self._chat_id,
435
+ assistant_id=self._assistant_id,
436
+ role=ChatMessageRole.USER,
437
+ content=content,
438
+ original_content=original_content,
439
+ references=references,
440
+ debug_info=debug_info,
441
+ set_completed_at=set_completed_at,
442
+ )
443
+ # Update the user message id
444
+ self._user_message_id = chat_message.id or "unknown"
445
+ return chat_message
446
+
447
+ def free_user_input(self) -> None:
448
+ """Unblocks the next user input"""
449
+ self.modify_assistant_message(set_completed_at=True)
450
+
451
+ # History Methods
452
+ ############################################################################
453
+
454
+ def get_full_history(self) -> list[ChatMessage]:
455
+ """Loads the full chat history for the chat session synchronously.
456
+
457
+ Returns:
458
+ list[ChatMessage]: The full chat history.
459
+
460
+ Raises:
461
+ Exception: If the loading fails.
462
+
463
+ """
464
+ return get_full_history(
465
+ event_user_id=self._user_id,
466
+ event_company_id=self._company_id,
467
+ event_payload_chat_id=self._chat_id,
468
+ )
469
+
470
+ async def get_full_history_async(self) -> list[ChatMessage]:
471
+ """Loads the full chat history for the chat session asynchronously.
472
+
473
+ Returns:
474
+ list[ChatMessage]: The full chat history.
475
+
476
+ Raises:
477
+ Exception: If the loading fails.
478
+
479
+ """
480
+ return await get_full_history_async(
481
+ event_user_id=self._user_id,
482
+ event_company_id=self._company_id,
483
+ event_payload_chat_id=self._chat_id,
484
+ )
485
+
486
+ def get_full_and_selected_history(
487
+ self,
488
+ token_limit: int,
489
+ percent_of_max_tokens: float = DEFAULT_PERCENT_OF_MAX_TOKENS,
490
+ max_messages: int = DEFAULT_MAX_MESSAGES,
491
+ ) -> tuple[list[ChatMessage], list[ChatMessage]]:
492
+ """Loads the chat history for the chat session synchronously.
493
+
494
+ Args:
495
+ token_limit (int): The maximum number of tokens to load.
496
+ percent_of_max_tokens (float): The percentage of the maximum tokens to load. Defaults to 0.15.
497
+ max_messages (int): The maximum number of messages to load. Defaults to 4.
498
+
499
+ Returns:
500
+ tuple[list[ChatMessage], list[ChatMessage]]: The selected and full chat history.
501
+
502
+ Raises:
503
+ Exception: If the loading fails.
504
+
505
+ """
506
+ full_history = get_full_history(
507
+ event_user_id=self._user_id,
508
+ event_company_id=self._company_id,
509
+ event_payload_chat_id=self._chat_id,
510
+ )
511
+ selected_history = get_selection_from_history(
512
+ full_history=full_history,
513
+ max_tokens=int(round(token_limit * percent_of_max_tokens)),
514
+ max_messages=max_messages,
515
+ )
516
+
517
+ return full_history, selected_history
518
+
519
+ async def get_full_and_selected_history_async(
520
+ self,
521
+ token_limit: int,
522
+ percent_of_max_tokens: float = DEFAULT_PERCENT_OF_MAX_TOKENS,
523
+ max_messages: int = DEFAULT_MAX_MESSAGES,
524
+ ) -> tuple[list[ChatMessage], list[ChatMessage]]:
525
+ """Loads the chat history for the chat session asynchronously.
526
+
527
+ Args:
528
+ token_limit (int): The maximum number of tokens to load.
529
+ percent_of_max_tokens (float): The percentage of the maximum tokens to load. Defaults to 0.15.
530
+ max_messages (int): The maximum number of messages to load. Defaults to 4.
531
+
532
+ Returns:
533
+ tuple[list[ChatMessage], list[ChatMessage]]: The selected and full chat history.
534
+
535
+ Raises:
536
+ Exception: If the loading fails.
537
+
538
+ """
539
+ full_history = await get_full_history_async(
540
+ event_user_id=self._user_id,
541
+ event_company_id=self._company_id,
542
+ event_payload_chat_id=self._chat_id,
543
+ )
544
+ selected_history = get_selection_from_history(
545
+ full_history=full_history,
546
+ max_tokens=int(round(token_limit * percent_of_max_tokens)),
547
+ max_messages=max_messages,
548
+ )
549
+
550
+ return full_history, selected_history
551
+
552
+ # Message Assessment Methods
553
+ ############################################################################
554
+
555
+ def create_message_assessment(
556
+ self,
557
+ assistant_message_id: str,
558
+ status: ChatMessageAssessmentStatus,
559
+ type: ChatMessageAssessmentType,
560
+ title: str | None = None,
561
+ explanation: str | None = None,
562
+ label: ChatMessageAssessmentLabel | None = None,
563
+ is_visible: bool = True,
564
+ ) -> ChatMessageAssessment:
565
+ """Creates a message assessment for an assistant message synchronously.
566
+
567
+ Args:
568
+ assistant_message_id (str): The ID of the assistant message to assess
569
+ status (MessageAssessmentStatus): The status of the assessment (e.g. "DONE")
570
+ type (MessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
571
+ title (str | None): The title of the assessment
572
+ explanation (str | None): Explanation of the assessment
573
+ label (MessageAssessmentLabel | None): The assessment label (e.g. "RED")
574
+ is_visible (bool): Whether the assessment is visible to users. Defaults to True.
575
+
576
+ Returns:
577
+ ChatMessageAssessment: The created message assessment
578
+
579
+ Raises:
580
+ Exception: If the creation fails
581
+
582
+ """
583
+ return create_message_assessment(
584
+ user_id=self._user_id,
585
+ company_id=self._company_id,
586
+ assistant_message_id=assistant_message_id,
587
+ status=status,
588
+ type=type,
589
+ title=title,
590
+ explanation=explanation,
591
+ label=label,
592
+ is_visible=is_visible,
593
+ )
594
+
595
+ async def create_message_assessment_async(
596
+ self,
597
+ assistant_message_id: str,
598
+ status: ChatMessageAssessmentStatus,
599
+ type: ChatMessageAssessmentType,
600
+ title: str | None = None,
601
+ explanation: str | None = None,
602
+ label: ChatMessageAssessmentLabel | None = None,
603
+ is_visible: bool = True,
604
+ ) -> ChatMessageAssessment:
605
+ """Creates a message assessment for an assistant message asynchronously.
606
+
607
+ Args:
608
+ assistant_message_id (str): The ID of the assistant message to assess
609
+ status (ChatMessageAssessmentStatus): The status of the assessment (e.g. "DONE")
610
+ type (ChatMessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
611
+ title (str | None): The title of the assessment
612
+ explanation (str | None): Explanation of the assessment
613
+ label (ChatMessageAssessmentLabel | None): The assessment label (e.g. "RED")
614
+ is_visible (bool): Whether the assessment is visible to users. Defaults to True.
615
+
616
+ Returns:
617
+ ChatMessageAssessment: The created message assessment
618
+
619
+ Raises:
620
+ Exception: If the creation fails
621
+
622
+ """
623
+ return await create_message_assessment_async(
624
+ user_id=self._user_id,
625
+ company_id=self._company_id,
626
+ assistant_message_id=assistant_message_id,
627
+ status=status,
628
+ type=type,
629
+ title=title,
630
+ explanation=explanation,
631
+ label=label,
632
+ is_visible=is_visible,
633
+ )
634
+
635
+ def modify_message_assessment(
636
+ self,
637
+ assistant_message_id: str,
638
+ status: ChatMessageAssessmentStatus,
639
+ type: ChatMessageAssessmentType,
640
+ title: str | None = None,
641
+ explanation: str | None = None,
642
+ label: ChatMessageAssessmentLabel | None = None,
643
+ ) -> ChatMessageAssessment:
644
+ """Modifies a message assessment for an assistant message synchronously.
645
+
646
+ Args:
647
+ assistant_message_id (str): The ID of the assistant message to assess
648
+ status (MessageAssessmentStatus): The status of the assessment (e.g. "DONE")
649
+ title (str | None): The title of the assessment
650
+ explanation (str | None): Explanation of the assessment
651
+ label (ChatMessageAssessmentLabel | None): The assessment label (e.g. "RED")
652
+ type (ChatMessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
653
+
654
+ Returns:
655
+ dict: The modified message assessment
656
+
657
+ Raises:
658
+ Exception: If the modification fails
659
+
660
+ """
661
+ return modify_message_assessment(
662
+ user_id=self._user_id,
663
+ company_id=self._company_id,
664
+ assistant_message_id=assistant_message_id,
665
+ status=status,
666
+ type=type,
667
+ title=title,
668
+ explanation=explanation,
669
+ label=label,
670
+ )
671
+
672
+ async def modify_message_assessment_async(
673
+ self,
674
+ assistant_message_id: str,
675
+ type: ChatMessageAssessmentType,
676
+ title: str | None = None,
677
+ status: ChatMessageAssessmentStatus | None = None,
678
+ explanation: str | None = None,
679
+ label: ChatMessageAssessmentLabel | None = None,
680
+ ) -> ChatMessageAssessment:
681
+ """Modifies a message assessment for an assistant message asynchronously.
682
+
683
+ Args:
684
+ assistant_message_id (str): The ID of the assistant message to assess
685
+ status (ChatMessageAssessmentStatus): The status of the assessment (e.g. "DONE")
686
+ title (str | None): The title of the assessment
687
+ explanation (str | None): Explanation of the assessment
688
+ label (ChatMessageAssessmentLabel | None): The assessment label (e.g. "RED")
689
+ type (ChatMessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
690
+
691
+ Returns:
692
+ ChatMessageAssessment: The modified message assessment
693
+
694
+ Raises:
695
+ Exception: If the modification fails
696
+
697
+ """
698
+ return await modify_message_assessment_async(
699
+ user_id=self._user_id,
700
+ company_id=self._company_id,
701
+ assistant_message_id=assistant_message_id,
702
+ status=status,
703
+ type=type,
704
+ title=title,
705
+ explanation=explanation,
706
+ label=label,
707
+ )
708
+
709
+ # Message Log Methods
710
+ ############################################################################
711
+
712
+ def create_message_log(
713
+ self,
714
+ *,
715
+ message_id: str,
716
+ text: str,
717
+ status: MessageLogStatus,
718
+ order: int,
719
+ details: MessageLogDetails | None = None,
720
+ uncited_references: MessageLogUncitedReferences | None = None,
721
+ references: list[ContentReference] | None = None,
722
+ ) -> MessageLog:
723
+ """Creates a message log for tracking execution steps synchronously.
724
+
725
+ Args:
726
+ message_id (str): The ID of the message this log belongs to
727
+ text (str): The log text content
728
+ status (MessageLogStatus): The status of this log entry
729
+ order (int): The order/sequence number of this log entry
730
+ details (MessageLogDetails | None): Additional details about this log entry
731
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
732
+ references (list[ContentReference] | None): List of references for this log
733
+
734
+ Returns:
735
+ MessageLog: The created message log
736
+
737
+ Raises:
738
+ Exception: If the creation fails
739
+
740
+ """
741
+ return create_message_log(
742
+ user_id=self._user_id,
743
+ company_id=self._company_id,
744
+ message_id=message_id,
745
+ text=text,
746
+ status=status,
747
+ order=order,
748
+ details=details,
749
+ uncited_references=uncited_references,
750
+ references=references,
751
+ )
752
+
753
+ async def create_message_log_async(
754
+ self,
755
+ *,
756
+ message_id: str,
757
+ text: str,
758
+ status: MessageLogStatus,
759
+ order: int,
760
+ details: MessageLogDetails | None = None,
761
+ uncited_references: MessageLogUncitedReferences | None = None,
762
+ references: list[ContentReference] | None = None,
763
+ ) -> MessageLog:
764
+ """Creates a message log for tracking execution steps asynchronously.
765
+
766
+ Args:
767
+ message_id (str): The ID of the message this log belongs to
768
+ text (str): The log text content
769
+ status (MessageLogStatus): The status of this log entry
770
+ order (int): The order/sequence number of this log entry
771
+ details (MessageLogDetails | None): Additional details about this log entry
772
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
773
+ references (list[ContentReference] | None): List of references for this log
774
+
775
+ Returns:
776
+ MessageLog: The created message log
777
+
778
+ Raises:
779
+ Exception: If the creation fails
780
+
781
+ """
782
+ return await create_message_log_async(
783
+ user_id=self._user_id,
784
+ company_id=self._company_id,
785
+ message_id=message_id,
786
+ text=text,
787
+ status=status,
788
+ order=order,
789
+ details=details,
790
+ uncited_references=uncited_references,
791
+ references=references,
792
+ )
793
+
794
+ def update_message_log(
795
+ self,
796
+ *,
797
+ message_log_id: str,
798
+ order: int,
799
+ text: str | None = None,
800
+ status: MessageLogStatus | None = None,
801
+ details: MessageLogDetails | None = None,
802
+ uncited_references: MessageLogUncitedReferences | None = None,
803
+ references: list[ContentReference] | None = None,
804
+ ) -> MessageLog:
805
+ """Updates a message log synchronously.
806
+
807
+ Args:
808
+ message_log_id (str): The ID of the message log to update
809
+ order (int): The order/sequence number (required)
810
+ text (str | None): The updated log text content
811
+ status (MessageLogStatus | None): The updated status
812
+ details (MessageLogDetails | None): Updated additional details
813
+ uncited_references (MessageLogUncitedReferences | None): Updated uncited references
814
+ references (list[ContentReference] | None): Updated list of references
815
+
816
+ Returns:
817
+ MessageLog: The updated message log
818
+
819
+ Raises:
820
+ Exception: If the update fails
821
+
822
+ """
823
+ return update_message_log(
824
+ user_id=self._user_id,
825
+ company_id=self._company_id,
826
+ message_log_id=message_log_id,
827
+ order=order,
828
+ text=text,
829
+ status=status,
830
+ details=details,
831
+ uncited_references=uncited_references,
832
+ references=references,
833
+ )
834
+
835
+ async def update_message_log_async(
836
+ self,
837
+ *,
838
+ message_log_id: str,
839
+ order: int,
840
+ text: str | None = None,
841
+ status: MessageLogStatus | None = None,
842
+ details: MessageLogDetails | None = None,
843
+ uncited_references: MessageLogUncitedReferences | None = None,
844
+ references: list[ContentReference] | None = None,
845
+ ) -> MessageLog:
846
+ """Updates a message log asynchronously.
847
+
848
+ Args:
849
+ message_log_id (str): The ID of the message log to update
850
+ order (int): The order/sequence number (required)
851
+ text (str | None): The updated log text content
852
+ status (MessageLogStatus | None): The updated status
853
+ details (MessageLogDetails | None): Updated additional details
854
+ uncited_references (MessageLogUncitedReferences | None): Updated uncited references
855
+ references (list[ContentReference] | None): Updated list of references
856
+
857
+ Returns:
858
+ MessageLog: The updated message log
859
+
860
+ Raises:
861
+ Exception: If the update fails
862
+
863
+ """
864
+ return await update_message_log_async(
865
+ user_id=self._user_id,
866
+ company_id=self._company_id,
867
+ message_log_id=message_log_id,
868
+ order=order,
869
+ text=text,
870
+ status=status,
871
+ details=details,
872
+ uncited_references=uncited_references,
873
+ references=references,
874
+ )
875
+
876
+ def create_assistant_message_log(
877
+ self,
878
+ *,
879
+ text: str,
880
+ status: MessageLogStatus,
881
+ order: int,
882
+ details: MessageLogDetails | None = None,
883
+ uncited_references: MessageLogUncitedReferences | None = None,
884
+ references: list[ContentReference] | None = None,
885
+ ) -> MessageLog:
886
+ """Creates a message log for the current assistant message synchronously.
887
+
888
+ This is a convenience method that uses the current assistant message ID.
889
+
890
+ Args:
891
+ text (str): The log text content
892
+ status (MessageLogStatus): The status of this log entry
893
+ order (int): The order/sequence number of this log entry
894
+ details (MessageLogDetails | None): Additional details about this log entry
895
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
896
+ references (list[ContentReference] | None): List of references for this log
897
+
898
+ Returns:
899
+ MessageLog: The created message log
900
+
901
+ Raises:
902
+ Exception: If the creation fails
903
+
904
+ """
905
+ return self.create_message_log(
906
+ message_id=self._assistant_message_id,
907
+ text=text,
908
+ status=status,
909
+ order=order,
910
+ details=details,
911
+ uncited_references=uncited_references,
912
+ references=references,
913
+ )
914
+
915
+ async def create_assistant_message_log_async(
916
+ self,
917
+ *,
918
+ text: str,
919
+ status: MessageLogStatus,
920
+ order: int,
921
+ details: MessageLogDetails | None = None,
922
+ uncited_references: MessageLogUncitedReferences | None = None,
923
+ references: list[ContentReference] | None = None,
924
+ ) -> MessageLog:
925
+ """Creates a message log for the current assistant message asynchronously.
926
+
927
+ This is a convenience method that uses the current assistant message ID.
928
+
929
+ Args:
930
+ text (str): The log text content
931
+ status (MessageLogStatus): The status of this log entry
932
+ order (int): The order/sequence number of this log entry
933
+ details (MessageLogDetails | None): Additional details about this log entry
934
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
935
+ references (list[ContentReference] | None): List of references for this log
936
+
937
+ Returns:
938
+ MessageLog: The created message log
939
+
940
+ Raises:
941
+ Exception: If the creation fails
942
+
943
+ """
944
+ return await self.create_message_log_async(
945
+ message_id=self._assistant_message_id,
946
+ text=text,
947
+ status=status,
948
+ order=order,
949
+ details=details,
950
+ uncited_references=uncited_references,
951
+ references=references,
952
+ )
953
+
954
+ # Message Execution Methods
955
+ ############################################################################
956
+
957
+ def create_message_execution(
958
+ self,
959
+ *,
960
+ message_id: str,
961
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
962
+ seconds_remaining: int | None = None,
963
+ percentage_completed: int | None = None,
964
+ ) -> MessageExecution:
965
+ """Creates a message execution for tracking long-running operations synchronously.
966
+
967
+ Args:
968
+ message_id (str): The ID of the message this execution belongs to
969
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
970
+ seconds_remaining (int | None): Estimated seconds remaining for completion
971
+ percentage_completed (int | None): Percentage of completion (0-100)
972
+
973
+ Returns:
974
+ MessageExecution: The created message execution
975
+
976
+ Raises:
977
+ Exception: If the creation fails
978
+
979
+ """
980
+ return create_message_execution(
981
+ user_id=self._user_id,
982
+ company_id=self._company_id,
983
+ message_id=message_id,
984
+ chat_id=self._chat_id,
985
+ type=type,
986
+ seconds_remaining=seconds_remaining,
987
+ percentage_completed=percentage_completed,
988
+ )
989
+
990
+ async def create_message_execution_async(
991
+ self,
992
+ *,
993
+ message_id: str,
994
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
995
+ seconds_remaining: int | None = None,
996
+ percentage_completed: int | None = None,
997
+ ) -> MessageExecution:
998
+ """Creates a message execution for tracking long-running operations asynchronously.
999
+
1000
+ Args:
1001
+ message_id (str): The ID of the message this execution belongs to
1002
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
1003
+ seconds_remaining (int | None): Estimated seconds remaining for completion
1004
+ percentage_completed (int | None): Percentage of completion (0-100)
1005
+
1006
+ Returns:
1007
+ MessageExecution: The created message execution
1008
+
1009
+ Raises:
1010
+ Exception: If the creation fails
1011
+
1012
+ """
1013
+ return await create_message_execution_async(
1014
+ user_id=self._user_id,
1015
+ company_id=self._company_id,
1016
+ message_id=message_id,
1017
+ chat_id=self._chat_id,
1018
+ type=type,
1019
+ seconds_remaining=seconds_remaining,
1020
+ percentage_completed=percentage_completed,
1021
+ )
1022
+
1023
+ def get_message_execution(
1024
+ self,
1025
+ *,
1026
+ message_id: str,
1027
+ ) -> MessageExecution:
1028
+ """Gets a message execution by message ID synchronously.
1029
+
1030
+ Args:
1031
+ message_id (str): The ID of the message to get execution for
1032
+
1033
+ Returns:
1034
+ MessageExecution: The message execution
1035
+
1036
+ Raises:
1037
+ Exception: If the retrieval fails
1038
+
1039
+ """
1040
+ return get_message_execution(
1041
+ user_id=self._user_id,
1042
+ company_id=self._company_id,
1043
+ message_id=message_id,
1044
+ )
1045
+
1046
+ async def get_message_execution_async(
1047
+ self,
1048
+ *,
1049
+ message_id: str,
1050
+ ) -> MessageExecution:
1051
+ """Gets a message execution by message ID asynchronously.
1052
+
1053
+ Args:
1054
+ message_id (str): The ID of the message to get execution for
1055
+
1056
+ Returns:
1057
+ MessageExecution: The message execution
1058
+
1059
+ Raises:
1060
+ Exception: If the retrieval fails
1061
+
1062
+ """
1063
+ return await get_message_execution_async(
1064
+ user_id=self._user_id,
1065
+ company_id=self._company_id,
1066
+ message_id=message_id,
1067
+ )
1068
+
1069
+ def update_message_execution(
1070
+ self,
1071
+ *,
1072
+ message_id: str,
1073
+ status: MessageExecutionUpdateStatus,
1074
+ seconds_remaining: int | None = None,
1075
+ percentage_completed: int | None = None,
1076
+ ) -> MessageExecution:
1077
+ """Updates a message execution synchronously.
1078
+
1079
+ Args:
1080
+ message_id (str): The ID of the message to update execution for
1081
+ status (MessageExecutionUpdateStatus): The updated status (COMPLETED or FAILED)
1082
+ seconds_remaining (int | None): Updated estimated seconds remaining
1083
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1084
+
1085
+ Returns:
1086
+ MessageExecution: The updated message execution
1087
+
1088
+ Raises:
1089
+ Exception: If the update fails
1090
+
1091
+ """
1092
+ return update_message_execution(
1093
+ user_id=self._user_id,
1094
+ company_id=self._company_id,
1095
+ message_id=message_id,
1096
+ status=status,
1097
+ seconds_remaining=seconds_remaining,
1098
+ percentage_completed=percentage_completed,
1099
+ )
1100
+
1101
+ async def update_message_execution_async(
1102
+ self,
1103
+ *,
1104
+ message_id: str,
1105
+ status: MessageExecutionUpdateStatus,
1106
+ seconds_remaining: int | None = None,
1107
+ percentage_completed: int | None = None,
1108
+ ) -> MessageExecution:
1109
+ """Updates a message execution asynchronously.
1110
+
1111
+ Args:
1112
+ message_id (str): The ID of the message to update execution for
1113
+ status (MessageExecutionUpdateStatus): The updated status (COMPLETED or FAILED)
1114
+ seconds_remaining (int | None): Updated estimated seconds remaining
1115
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1116
+
1117
+ Returns:
1118
+ MessageExecution: The updated message execution
1119
+
1120
+ Raises:
1121
+ Exception: If the update fails
1122
+
1123
+ """
1124
+ return await update_message_execution_async(
1125
+ user_id=self._user_id,
1126
+ company_id=self._company_id,
1127
+ message_id=message_id,
1128
+ status=status,
1129
+ seconds_remaining=seconds_remaining,
1130
+ percentage_completed=percentage_completed,
1131
+ )
1132
+
1133
+ def create_assistant_message_execution(
1134
+ self,
1135
+ *,
1136
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
1137
+ seconds_remaining: int | None = None,
1138
+ percentage_completed: int | None = None,
1139
+ ) -> MessageExecution:
1140
+ """Creates a message execution for the current assistant message synchronously.
1141
+
1142
+ This is a convenience method that uses the current assistant message ID.
1143
+
1144
+ Args:
1145
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
1146
+ seconds_remaining (int | None): Estimated seconds remaining for completion
1147
+ percentage_completed (int | None): Percentage of completion (0-100)
1148
+
1149
+ Returns:
1150
+ MessageExecution: The created message execution
1151
+
1152
+ Raises:
1153
+ Exception: If the creation fails
1154
+
1155
+ """
1156
+ return self.create_message_execution(
1157
+ message_id=self._assistant_message_id,
1158
+ type=type,
1159
+ seconds_remaining=seconds_remaining,
1160
+ percentage_completed=percentage_completed,
1161
+ )
1162
+
1163
+ async def create_assistant_message_execution_async(
1164
+ self,
1165
+ *,
1166
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
1167
+ seconds_remaining: int | None = None,
1168
+ percentage_completed: int | None = None,
1169
+ ) -> MessageExecution:
1170
+ """Creates a message execution for the current assistant message asynchronously.
1171
+
1172
+ This is a convenience method that uses the current assistant message ID.
1173
+
1174
+ Args:
1175
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
1176
+ seconds_remaining (int | None): Estimated seconds remaining for completion
1177
+ percentage_completed (int | None): Percentage of completion (0-100)
1178
+
1179
+ Returns:
1180
+ MessageExecution: The created message execution
1181
+
1182
+ Raises:
1183
+ Exception: If the creation fails
1184
+
1185
+ """
1186
+ return await self.create_message_execution_async(
1187
+ message_id=self._assistant_message_id,
1188
+ type=type,
1189
+ seconds_remaining=seconds_remaining,
1190
+ percentage_completed=percentage_completed,
1191
+ )
1192
+
1193
+ def get_assistant_message_execution(self) -> MessageExecution:
1194
+ """Gets the message execution for the current assistant message synchronously.
1195
+
1196
+ This is a convenience method that uses the current assistant message ID.
1197
+
1198
+ Returns:
1199
+ MessageExecution: The message execution
1200
+
1201
+ Raises:
1202
+ Exception: If the retrieval fails
1203
+
1204
+ """
1205
+ return self.get_message_execution(message_id=self._assistant_message_id)
1206
+
1207
+ async def get_assistant_message_execution_async(self) -> MessageExecution:
1208
+ """Gets the message execution for the current assistant message asynchronously.
1209
+
1210
+ This is a convenience method that uses the current assistant message ID.
1211
+
1212
+ Returns:
1213
+ MessageExecution: The message execution
1214
+
1215
+ Raises:
1216
+ Exception: If the retrieval fails
1217
+
1218
+ """
1219
+ return await self.get_message_execution_async(
1220
+ message_id=self._assistant_message_id
1221
+ )
1222
+
1223
+ def update_assistant_message_execution(
1224
+ self,
1225
+ *,
1226
+ status: MessageExecutionUpdateStatus,
1227
+ seconds_remaining: int | None = None,
1228
+ percentage_completed: int | None = None,
1229
+ ) -> MessageExecution:
1230
+ """Updates the message execution for the current assistant message synchronously.
1231
+
1232
+ This is a convenience method that uses the current assistant message ID.
1233
+
1234
+ Args:
1235
+ status (MessageExecutionUpdateStatus): The updated status (COMPLETED or FAILED)
1236
+ seconds_remaining (int | None): Updated estimated seconds remaining
1237
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1238
+
1239
+ Returns:
1240
+ MessageExecution: The updated message execution
1241
+
1242
+ Raises:
1243
+ Exception: If the update fails
1244
+
1245
+ """
1246
+ return self.update_message_execution(
1247
+ message_id=self._assistant_message_id,
1248
+ status=status,
1249
+ seconds_remaining=seconds_remaining,
1250
+ percentage_completed=percentage_completed,
1251
+ )
1252
+
1253
+ async def update_assistant_message_execution_async(
1254
+ self,
1255
+ *,
1256
+ status: MessageExecutionUpdateStatus,
1257
+ seconds_remaining: int | None = None,
1258
+ percentage_completed: int | None = None,
1259
+ ) -> MessageExecution:
1260
+ """Updates the message execution for the current assistant message asynchronously.
1261
+
1262
+ This is a convenience method that uses the current assistant message ID.
1263
+
1264
+ Args:
1265
+ status (MessageExecutionUpdateStatus): The updated status (COMPLETED or FAILED)
1266
+ seconds_remaining (int | None): Updated estimated seconds remaining
1267
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1268
+
1269
+ Returns:
1270
+ MessageExecution: The updated message execution
1271
+
1272
+ Raises:
1273
+ Exception: If the update fails
1274
+
1275
+ """
1276
+ return await self.update_message_execution_async(
1277
+ message_id=self._assistant_message_id,
1278
+ status=status,
1279
+ seconds_remaining=seconds_remaining,
1280
+ percentage_completed=percentage_completed,
1281
+ )
1282
+
1283
+ # Language Model Methods
1284
+ ############################################################################
1285
+
1286
+ @deprecated("Use complete_with_references instead")
1287
+ def stream_complete(
1288
+ self,
1289
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1290
+ model_name: LanguageModelName | str,
1291
+ content_chunks: list[ContentChunk] | None = None,
1292
+ debug_info: dict = {},
1293
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1294
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1295
+ tools: list[LanguageModelTool | LanguageModelToolDescription] | None = None,
1296
+ start_text: str | None = None,
1297
+ other_options: dict | None = None,
1298
+ ) -> LanguageModelStreamResponse:
1299
+ return self.complete_with_references(
1300
+ messages=messages,
1301
+ model_name=model_name,
1302
+ content_chunks=content_chunks,
1303
+ debug_info=debug_info,
1304
+ temperature=temperature,
1305
+ timeout=timeout,
1306
+ tools=tools,
1307
+ start_text=start_text,
1308
+ other_options=other_options,
1309
+ )
1310
+
1311
+ def complete_with_references(
1312
+ self,
1313
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1314
+ model_name: LanguageModelName | str,
1315
+ content_chunks: list[ContentChunk] | None = None,
1316
+ debug_info: dict | None = None,
1317
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1318
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1319
+ tools: list[LanguageModelTool | LanguageModelToolDescription] | None = None,
1320
+ start_text: str | None = None,
1321
+ other_options: dict | None = None,
1322
+ ) -> LanguageModelStreamResponse:
1323
+ """Streams a completion in the chat session synchronously."""
1324
+ return stream_complete_with_references(
1325
+ company_id=self._company_id,
1326
+ user_id=self._user_id,
1327
+ assistant_message_id=self._assistant_message_id,
1328
+ user_message_id=self._user_message_id,
1329
+ chat_id=self._chat_id,
1330
+ assistant_id=self._assistant_id,
1331
+ messages=messages,
1332
+ model_name=model_name,
1333
+ content_chunks=content_chunks,
1334
+ debug_info=debug_info,
1335
+ temperature=temperature,
1336
+ timeout=timeout,
1337
+ tools=tools,
1338
+ start_text=start_text,
1339
+ other_options=other_options,
1340
+ )
1341
+
1342
+ def complete(
1343
+ self,
1344
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1345
+ model_name: LanguageModelName | str,
1346
+ content_chunks: list[ContentChunk] | None = None,
1347
+ debug_info: dict | None = None,
1348
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1349
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1350
+ tools: list[LanguageModelTool | LanguageModelToolDescription] | None = None,
1351
+ start_text: str | None = None,
1352
+ other_options: dict | None = None,
1353
+ ) -> LanguageModelResponse:
1354
+ response = self.complete_with_references(
1355
+ messages=messages,
1356
+ model_name=model_name,
1357
+ content_chunks=content_chunks,
1358
+ debug_info=debug_info,
1359
+ temperature=temperature,
1360
+ timeout=timeout,
1361
+ tools=tools,
1362
+ start_text=start_text,
1363
+ other_options=other_options,
1364
+ )
1365
+
1366
+ return LanguageModelResponse.from_stream_response(response)
1367
+
1368
+ @deprecated("use complete_with_references_async instead.")
1369
+ async def stream_complete_async(
1370
+ self,
1371
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1372
+ model_name: LanguageModelName | str,
1373
+ content_chunks: list[ContentChunk] | None = None,
1374
+ debug_info: dict | None = None,
1375
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1376
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1377
+ tools: list[LanguageModelTool | LanguageModelToolDescription] | None = None,
1378
+ start_text: str | None = None,
1379
+ other_options: dict | None = None,
1380
+ ) -> LanguageModelStreamResponse:
1381
+ """Stream a completion in the chat session asynchronously."""
1382
+ return await self.complete_with_references_async(
1383
+ messages=messages,
1384
+ model_name=model_name,
1385
+ content_chunks=content_chunks,
1386
+ debug_info=debug_info,
1387
+ temperature=temperature,
1388
+ timeout=timeout,
1389
+ tools=tools,
1390
+ start_text=start_text,
1391
+ other_options=other_options,
1392
+ )
1393
+
1394
+ async def complete_with_references_async(
1395
+ self,
1396
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1397
+ model_name: LanguageModelName | str,
1398
+ content_chunks: list[ContentChunk] | None = None,
1399
+ debug_info: dict | None = None,
1400
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1401
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1402
+ tools: list[LanguageModelTool | LanguageModelToolDescription] | None = None,
1403
+ start_text: str | None = None,
1404
+ other_options: dict | None = None,
1405
+ ) -> LanguageModelStreamResponse:
1406
+ return await stream_complete_with_references_async(
1407
+ company_id=self._company_id,
1408
+ user_id=self._user_id,
1409
+ assistant_message_id=self._assistant_message_id,
1410
+ user_message_id=self._user_message_id,
1411
+ chat_id=self._chat_id,
1412
+ assistant_id=self._assistant_id,
1413
+ messages=messages,
1414
+ model_name=model_name,
1415
+ content_chunks=content_chunks,
1416
+ debug_info=debug_info,
1417
+ temperature=temperature,
1418
+ timeout=timeout,
1419
+ tools=tools,
1420
+ start_text=start_text,
1421
+ other_options=other_options,
1422
+ )
1423
+
1424
+ async def complete_async(
1425
+ self,
1426
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1427
+ model_name: LanguageModelName | str,
1428
+ content_chunks: list[ContentChunk] | None,
1429
+ debug_info: dict | None = None,
1430
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1431
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1432
+ tools: list[LanguageModelTool | LanguageModelToolDescription] | None = None,
1433
+ start_text: str | None = None,
1434
+ other_options: dict | None = None,
1435
+ ) -> LanguageModelResponse:
1436
+ response = self.complete_with_references_async(
1437
+ messages=messages,
1438
+ model_name=model_name,
1439
+ content_chunks=content_chunks,
1440
+ debug_info=debug_info,
1441
+ temperature=temperature,
1442
+ timeout=timeout,
1443
+ tools=tools,
1444
+ start_text=start_text,
1445
+ other_options=other_options,
1446
+ )
1447
+
1448
+ return LanguageModelResponse.from_stream_response(await response)
1449
+
1450
+ # Chat Content Methods
1451
+ ############################################################################
1452
+
1453
+ def upload_to_chat_from_bytes(
1454
+ self,
1455
+ *,
1456
+ content: bytes,
1457
+ content_name: str,
1458
+ mime_type: str,
1459
+ scope_id: str | None = None,
1460
+ skip_ingestion: bool = False,
1461
+ ingestion_config: unique_sdk.Content.IngestionConfig | None = None,
1462
+ metadata: dict[str, Any] | None = None,
1463
+ ) -> Content:
1464
+ return upload_content_from_bytes(
1465
+ user_id=self._user_id,
1466
+ company_id=self._company_id,
1467
+ content=content,
1468
+ content_name=content_name,
1469
+ mime_type=mime_type,
1470
+ scope_id=scope_id,
1471
+ chat_id=self._chat_id,
1472
+ skip_ingestion=skip_ingestion,
1473
+ ingestion_config=ingestion_config,
1474
+ metadata=metadata,
1475
+ )
1476
+
1477
+ def download_chat_content_to_bytes(self, *, content_id: str) -> bytes:
1478
+ return download_content_to_bytes(
1479
+ user_id=self._user_id,
1480
+ company_id=self._company_id,
1481
+ content_id=content_id,
1482
+ chat_id=self._chat_id,
1483
+ )
1484
+
1485
+ def download_chat_images_and_documents(self) -> tuple[list[Content], list[Content]]:
1486
+ images: list[Content] = []
1487
+ files: list[Content] = []
1488
+ for c in search_contents(
1489
+ user_id=self._user_id,
1490
+ company_id=self._company_id,
1491
+ chat_id=self._chat_id,
1492
+ where={"ownerId": {"equals": self._chat_id}},
1493
+ ):
1494
+ if is_file_content(filename=c.key):
1495
+ files.append(c)
1496
+ if is_image_content(filename=c.key):
1497
+ images.append(c)
1498
+ return images, files