unique_toolkit 1.8.1__py3-none-any.whl → 1.23.0__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.

Potentially problematic release.


This version of unique_toolkit might be problematic. Click here for more details.

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