unique_toolkit 0.5.55__py3-none-any.whl → 0.6.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.
Files changed (35) hide show
  1. unique_toolkit/_common/validate_required_values.py +21 -0
  2. unique_toolkit/app/__init__.py +20 -0
  3. unique_toolkit/app/schemas.py +73 -7
  4. unique_toolkit/chat/__init__.py +5 -4
  5. unique_toolkit/chat/constants.py +3 -0
  6. unique_toolkit/chat/functions.py +661 -0
  7. unique_toolkit/chat/schemas.py +11 -11
  8. unique_toolkit/chat/service.py +273 -430
  9. unique_toolkit/content/__init__.py +1 -0
  10. unique_toolkit/content/constants.py +2 -0
  11. unique_toolkit/content/functions.py +475 -0
  12. unique_toolkit/content/service.py +163 -315
  13. unique_toolkit/content/utils.py +32 -0
  14. unique_toolkit/embedding/__init__.py +3 -0
  15. unique_toolkit/embedding/constants.py +2 -0
  16. unique_toolkit/embedding/functions.py +79 -0
  17. unique_toolkit/embedding/service.py +47 -34
  18. unique_toolkit/evaluators/__init__.py +1 -0
  19. unique_toolkit/evaluators/constants.py +1 -0
  20. unique_toolkit/evaluators/context_relevancy/constants.py +3 -3
  21. unique_toolkit/evaluators/context_relevancy/utils.py +5 -2
  22. unique_toolkit/evaluators/hallucination/utils.py +2 -1
  23. unique_toolkit/language_model/__init__.py +1 -0
  24. unique_toolkit/language_model/constants.py +4 -0
  25. unique_toolkit/language_model/functions.py +362 -0
  26. unique_toolkit/language_model/service.py +246 -293
  27. unique_toolkit/short_term_memory/__init__.py +5 -0
  28. unique_toolkit/short_term_memory/constants.py +1 -0
  29. unique_toolkit/short_term_memory/functions.py +175 -0
  30. unique_toolkit/short_term_memory/service.py +153 -27
  31. {unique_toolkit-0.5.55.dist-info → unique_toolkit-0.6.0.dist-info}/METADATA +33 -7
  32. unique_toolkit-0.6.0.dist-info/RECORD +64 -0
  33. unique_toolkit-0.5.55.dist-info/RECORD +0 -50
  34. {unique_toolkit-0.5.55.dist-info → unique_toolkit-0.6.0.dist-info}/LICENSE +0 -0
  35. {unique_toolkit-0.5.55.dist-info → unique_toolkit-0.6.0.dist-info}/WHEEL +0 -0
@@ -1,12 +1,23 @@
1
1
  import logging
2
- from typing import Optional, Type, cast
2
+ from typing import Optional, Type
3
3
 
4
- import unique_sdk
5
4
  from pydantic import BaseModel
5
+ from typing_extensions import deprecated
6
6
 
7
- from unique_toolkit._common._base_service import BaseService
8
- from unique_toolkit.app.schemas import Event
7
+ from unique_toolkit._common.validate_required_values import validate_required_values
8
+ from unique_toolkit.app.schemas import BaseEvent, ChatEvent, Event
9
9
  from unique_toolkit.content.schemas import ContentChunk
10
+ from unique_toolkit.language_model.constants import (
11
+ DEFAULT_COMPLETE_TEMPERATURE,
12
+ DEFAULT_COMPLETE_TIMEOUT,
13
+ DOMAIN_NAME,
14
+ )
15
+ from unique_toolkit.language_model.functions import (
16
+ complete,
17
+ complete_async,
18
+ stream_complete_to_chat,
19
+ stream_complete_to_chat_async,
20
+ )
10
21
  from unique_toolkit.language_model.infos import LanguageModelName
11
22
  from unique_toolkit.language_model.schemas import (
12
23
  LanguageModelMessages,
@@ -15,21 +26,61 @@ from unique_toolkit.language_model.schemas import (
15
26
  LanguageModelTool,
16
27
  )
17
28
 
29
+ logger = logging.getLogger(f"toolkit.{DOMAIN_NAME}.{__name__}")
18
30
 
19
- class LanguageModelService(BaseService):
31
+
32
+ class LanguageModelService:
20
33
  """
21
34
  Provides methods to interact with the Language Model by generating responses.
22
35
 
23
- Attributes:
24
- event (Event): The Event object.
25
- logger (Optional[logging.Logger]): The logger object. Defaults to None.
36
+ Args:
37
+ company_id (str | None, optional): The company identifier. Defaults to None.
38
+ user_id (str | None, optional): The user identifier. Defaults to None.
39
+ assistant_message_id (str | None, optional): The assistant message identifier. Defaults to None.
40
+ chat_id (str | None, optional): The chat identifier. Defaults to None.
41
+ assistant_id (str | None, optional): The assistant identifier. Defaults to None.
42
+ user_message_id (str | None, optional): The user message identifier. Defaults to None.
26
43
  """
27
44
 
28
- def __init__(self, event: Event, logger: Optional[logging.Logger] = None):
29
- super().__init__(event, logger)
45
+ def __init__(
46
+ self,
47
+ event: Event | BaseEvent | None = None,
48
+ company_id: str | None = None,
49
+ user_id: str | None = None,
50
+ assistant_message_id: str | None = None,
51
+ chat_id: str | None = None,
52
+ assistant_id: str | None = None,
53
+ user_message_id: str | None = None,
54
+ ):
55
+ self._event = event
56
+ self.company_id = company_id
57
+ self.user_id = user_id
58
+ self.assistant_message_id = assistant_message_id
59
+ self.user_message_id = user_message_id
60
+ self.chat_id = chat_id
61
+ self.assistant_id = assistant_id
62
+
63
+ if event:
64
+ self.company_id = event.company_id
65
+ self.user_id = event.user_id
66
+ if isinstance(event, (ChatEvent, Event)):
67
+ self.assistant_message_id = event.payload.assistant_message.id
68
+ self.user_message_id = event.payload.user_message.id
69
+ self.chat_id = event.payload.chat_id
70
+ self.assistant_id = event.payload.assistant_id
71
+
72
+ @property
73
+ @deprecated(
74
+ "The event property is deprecated and will be removed in a future version."
75
+ )
76
+ def event(self) -> Event | BaseEvent | None:
77
+ """
78
+ Get the event object (deprecated).
30
79
 
31
- DEFAULT_COMPLETE_TIMEOUT = 240_000
32
- DEFAULT_COMPLETE_TEMPERATURE = 0.0
80
+ Returns:
81
+ Event | BaseEvent | None: The event object.
82
+ """
83
+ return self._event
33
84
 
34
85
  def complete(
35
86
  self,
@@ -41,116 +92,57 @@ class LanguageModelService(BaseService):
41
92
  structured_output_model: Optional[Type[BaseModel]] = None,
42
93
  structured_output_enforce_schema: bool = False,
43
94
  other_options: Optional[dict] = None,
44
- ):
95
+ ) -> LanguageModelResponse:
45
96
  """
46
97
  Calls the completion endpoint synchronously without streaming the response.
47
-
48
- Args:
49
- messages (LanguageModelMessages): The LanguageModelMessages obj to complete.
50
- model_name (LanguageModelName | str): The model name.
51
- temperature (float): The temperature value. Defaults to 0.
52
- timeout (int): The timeout value in milliseconds. Defaults to 240_000.
53
- tools (Optional[list[LanguageModelTool]]): The tools to use. Defaults to None.
54
- structured_output_model (Optional[Type[BaseModel]]): The structured output model. Defaults to None.
55
- structured_output_enforce_schema (bool): Whether to enforce the schema. Defaults to False.
56
- other_options (Optional[dict]): The other options to use. Defaults to None.
57
-
58
- Returns:
59
- LanguageModelResponse: The LanguageModelResponse object.
60
98
  """
61
- options, model, messages_dict, _ = self._prepare_completion_params_util(
99
+ [company_id] = validate_required_values([self.company_id])
100
+
101
+ return complete(
102
+ company_id=company_id,
62
103
  messages=messages,
63
104
  model_name=model_name,
64
105
  temperature=temperature,
106
+ timeout=timeout,
65
107
  tools=tools,
66
108
  other_options=other_options,
67
109
  structured_output_model=structured_output_model,
68
110
  structured_output_enforce_schema=structured_output_enforce_schema,
69
111
  )
70
112
 
71
- try:
72
- response = unique_sdk.ChatCompletion.create(
73
- company_id=self.event.company_id,
74
- model=model,
75
- messages=cast(
76
- list[unique_sdk.Integrated.ChatCompletionRequestMessage],
77
- messages_dict,
78
- ),
79
- timeout=timeout,
80
- options=options, # type: ignore
81
- )
82
- return LanguageModelResponse(**response)
83
- except Exception as e:
84
- self.logger.error(f"Error completing: {e}")
85
- raise e
86
-
87
- @classmethod
88
- async def complete_async_util(
89
- cls,
90
- company_id: str,
113
+ async def complete_async(
114
+ self,
91
115
  messages: LanguageModelMessages,
92
116
  model_name: LanguageModelName | str,
93
117
  temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
94
118
  timeout: int = DEFAULT_COMPLETE_TIMEOUT,
95
119
  tools: Optional[list[LanguageModelTool]] = None,
96
- other_options: Optional[dict] = None,
97
120
  structured_output_model: Optional[Type[BaseModel]] = None,
98
121
  structured_output_enforce_schema: bool = False,
99
- logger: Optional[logging.Logger] = logging.getLogger(__name__),
122
+ other_options: Optional[dict] = None,
100
123
  ) -> LanguageModelResponse:
101
124
  """
102
125
  Calls the completion endpoint asynchronously without streaming the response.
103
-
104
- This method sends a request to the completion endpoint using the provided messages, model name,
105
- temperature, timeout, and optional tools. It returns a `LanguageModelResponse` object containing
106
- the completed result.
107
-
108
- Args:
109
- company_id (str): The company ID associated with the request.
110
- messages (LanguageModelMessages): The messages to complete.
111
- model_name (LanguageModelName | str): The model name to use for the completion.
112
- temperature (float): The temperature setting for the completion. Defaults to 0.
113
- timeout (int): The timeout value in milliseconds for the request. Defaults to 240_000.
114
- tools (Optional[list[LanguageModelTool]]): Optional list of tools to include in the request.
115
- other_options (Optional[dict]): The other options to use. Defaults to None.
116
- structured_output_model (Optional[Type[BaseModel]]): The structured output model. Defaults to None.
117
- structured_output_enforce_schema (bool): Whether to enforce the schema. Defaults to False.
118
- logger (Optional[logging.Logger], optional): The logger used to log errors. Defaults to the logger for the current module.
119
-
120
- Returns:
121
- LanguageModelResponse: The response object containing the completed result.
122
-
123
- Raises:
124
- Exception: If an error occurs during the request, an exception is raised and logged.
125
126
  """
126
- options, model, messages_dict, _ = cls._prepare_completion_params_util(
127
+ [company_id] = validate_required_values([self.company_id])
128
+
129
+ return await complete_async(
130
+ company_id=company_id,
127
131
  messages=messages,
128
132
  model_name=model_name,
129
133
  temperature=temperature,
134
+ timeout=timeout,
130
135
  tools=tools,
131
136
  other_options=other_options,
132
137
  structured_output_model=structured_output_model,
133
138
  structured_output_enforce_schema=structured_output_enforce_schema,
134
139
  )
135
140
 
136
- try:
137
- response = await unique_sdk.ChatCompletion.create_async(
138
- company_id=company_id,
139
- model=model,
140
- messages=cast(
141
- list[unique_sdk.Integrated.ChatCompletionRequestMessage],
142
- messages_dict,
143
- ),
144
- timeout=timeout,
145
- options=options, # type: ignore
146
- )
147
- return LanguageModelResponse(**response)
148
- except Exception as e:
149
- logger.error(f"Error completing: {e}") # type: ignore
150
- raise e
151
-
152
- async def complete_async(
153
- self,
141
+ @classmethod
142
+ @deprecated("Use complete_async of language_model.functions instead")
143
+ async def complete_async_util(
144
+ cls,
145
+ company_id: str,
154
146
  messages: LanguageModelMessages,
155
147
  model_name: LanguageModelName | str,
156
148
  temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
@@ -162,39 +154,21 @@ class LanguageModelService(BaseService):
162
154
  ) -> LanguageModelResponse:
163
155
  """
164
156
  Calls the completion endpoint asynchronously without streaming the response.
165
-
166
- This method utilizes the class method `complete_async_util` to perform the asynchronous completion
167
- request using the provided messages, model name, temperature, timeout, and optional tools. It
168
- returns a `LanguageModelResponse` object containing the result of the completion.
169
-
170
- Args:
171
- messages (LanguageModelMessages): The messages to complete.
172
- model_name (LanguageModelName | str): The model name to use for the completion.
173
- temperature (float): The temperature setting for the completion. Defaults to 0.0.
174
- timeout (int): The timeout value in milliseconds for the request. Defaults to 240,000.
175
- tools (Optional[list[LanguageModelTool]]): Optional list of tools to include in the request.
176
- structured_output_model (Optional[Type[BaseModel]]): The structured output model. Defaults to None.
177
- structured_output_enforce_schema (bool): Whether to enforce the schema. Defaults to False.
178
- other_options (Optional[dict]): The other options to use. Defaults to None.
179
- Returns:
180
- LanguageModelResponse: The response object containing the completed result.
181
-
182
- Raises:
183
- Exception: If an error occurs during the completion request.
184
157
  """
185
- return await self.complete_async_util(
186
- company_id=self.event.company_id,
158
+
159
+ return await complete_async(
160
+ company_id=company_id,
187
161
  messages=messages,
188
162
  model_name=model_name,
189
163
  temperature=temperature,
190
164
  timeout=timeout,
191
165
  tools=tools,
192
166
  other_options=other_options,
193
- logger=self.logger,
194
167
  structured_output_model=structured_output_model,
195
168
  structured_output_enforce_schema=structured_output_enforce_schema,
196
169
  )
197
170
 
171
+ @deprecated("Use stream_complete_to_chat instead")
198
172
  def stream_complete(
199
173
  self,
200
174
  messages: LanguageModelMessages,
@@ -206,59 +180,47 @@ class LanguageModelService(BaseService):
206
180
  tools: Optional[list[LanguageModelTool]] = None,
207
181
  start_text: Optional[str] = None,
208
182
  other_options: Optional[dict] = None,
209
- ):
183
+ ) -> LanguageModelStreamResponse:
210
184
  """
211
185
  Streams a completion in the chat session synchronously.
212
-
213
- Args:
214
- messages (LanguageModelMessages): The LanguageModelMessages object to stream.
215
- content_chunks (list[ContentChunk]): The ContentChunks objects.
216
- model_name (LanguageModelName | str): The language model to use for the completion.
217
- debug_info (dict): The debug information. Defaults to {}.
218
- temperature (float): The temperature value. Defaults to 0.25.
219
- timeout (int): The timeout value in milliseconds. Defaults to 240_000.
220
- tools (Optional[list[LanguageModelTool]]): The tools to use. Defaults to None.
221
- start_text (Optional[str]): The start text. Defaults to None.
222
- other_options (Optional[dict]): The other options to use. Defaults to None.
223
- Returns:
224
- The LanguageModelStreamResponse object once the stream has finished.
225
186
  """
226
- options, model, messages_dict, search_context = (
227
- self._prepare_completion_params_util(
228
- messages=messages,
229
- model_name=model_name,
230
- temperature=temperature,
231
- tools=tools,
232
- other_options=other_options,
233
- content_chunks=content_chunks,
234
- )
187
+ [
188
+ company_id,
189
+ user_id,
190
+ assistant_message_id,
191
+ user_message_id,
192
+ chat_id,
193
+ assistant_id,
194
+ ] = validate_required_values(
195
+ [
196
+ self.company_id,
197
+ self.user_id,
198
+ self.assistant_message_id,
199
+ self.user_message_id,
200
+ self.chat_id,
201
+ self.assistant_id,
202
+ ]
235
203
  )
236
204
 
237
- try:
238
- response = unique_sdk.Integrated.chat_stream_completion(
239
- user_id=self.event.user_id,
240
- company_id=self.event.company_id,
241
- assistantMessageId=self.event.payload.assistant_message.id,
242
- userMessageId=self.event.payload.user_message.id,
243
- messages=cast(
244
- list[unique_sdk.Integrated.ChatCompletionRequestMessage],
245
- messages_dict,
246
- ),
247
- chatId=self.event.payload.chat_id,
248
- searchContext=search_context,
249
- model=model,
250
- timeout=timeout,
251
- assistantId=self.event.payload.assistant_id,
252
- debugInfo=debug_info,
253
- options=options, # type: ignore
254
- startText=start_text,
255
- )
256
- return LanguageModelStreamResponse(**response)
257
- except Exception as e:
258
- self.logger.error(f"Error streaming completion: {e}")
259
- raise e
205
+ return stream_complete_to_chat(
206
+ company_id=company_id,
207
+ user_id=user_id,
208
+ assistant_message_id=assistant_message_id,
209
+ user_message_id=user_message_id,
210
+ chat_id=chat_id,
211
+ assistant_id=assistant_id,
212
+ messages=messages,
213
+ model_name=model_name,
214
+ content_chunks=content_chunks,
215
+ debug_info=debug_info,
216
+ temperature=temperature,
217
+ timeout=timeout,
218
+ tools=tools,
219
+ start_text=start_text,
220
+ other_options=other_options,
221
+ )
260
222
 
261
- async def stream_complete_async(
223
+ def stream_complete_to_chat(
262
224
  self,
263
225
  messages: LanguageModelMessages,
264
226
  model_name: LanguageModelName | str,
@@ -269,156 +231,147 @@ class LanguageModelService(BaseService):
269
231
  tools: Optional[list[LanguageModelTool]] = None,
270
232
  start_text: Optional[str] = None,
271
233
  other_options: Optional[dict] = None,
272
- ):
234
+ ) -> LanguageModelStreamResponse:
273
235
  """
274
- Streams a completion in the chat session asynchronously.
275
-
276
- Args:
277
- messages (LanguageModelMessages): The LanguageModelMessages object to stream.
278
- content_chunks (list[ContentChunk]): The content chunks.
279
- model_name (LanguageModelName | str): The language model to use for the completion.
280
- debug_info (dict): The debug information. Defaults to {}.
281
- temperature (float): The temperature value. Defaults to 0.25.
282
- timeout (int): The timeout value in milliseconds. Defaults to 240_000.
283
- tools (Optional[list[LanguageModelTool]]): The tools to use. Defaults to None.
284
- start_text (Optional[str]): The start text. Defaults to None.
285
- other_options (Optional[dict]): The other options to use. Defaults to None.
286
- Returns:
287
- The LanguageModelStreamResponse object once the stream has finished.
236
+ Streams a completion in the chat session synchronously.
288
237
  """
289
- options, model, messages_dict, search_context = (
290
- self._prepare_completion_params_util(
291
- messages=messages,
292
- model_name=model_name,
293
- temperature=temperature,
294
- tools=tools,
295
- other_options=other_options,
296
- content_chunks=content_chunks,
297
- )
238
+ [
239
+ company_id,
240
+ user_id,
241
+ assistant_message_id,
242
+ user_message_id,
243
+ chat_id,
244
+ assistant_id,
245
+ ] = validate_required_values(
246
+ [
247
+ self.company_id,
248
+ self.user_id,
249
+ self.assistant_message_id,
250
+ self.user_message_id,
251
+ self.chat_id,
252
+ self.assistant_id,
253
+ ]
298
254
  )
299
255
 
300
- try:
301
- response = await unique_sdk.Integrated.chat_stream_completion_async(
302
- user_id=self.event.user_id,
303
- company_id=self.event.company_id,
304
- assistantMessageId=self.event.payload.assistant_message.id,
305
- userMessageId=self.event.payload.user_message.id,
306
- messages=cast(
307
- list[unique_sdk.Integrated.ChatCompletionRequestMessage],
308
- messages_dict,
309
- ),
310
- chatId=self.event.payload.chat_id,
311
- searchContext=search_context,
312
- model=model,
313
- timeout=timeout,
314
- assistantId=self.event.payload.assistant_id,
315
- debugInfo=debug_info,
316
- options=options, # type: ignore
317
- startText=start_text,
318
- )
319
- return LanguageModelStreamResponse(**response)
320
- except Exception as e:
321
- self.logger.error(f"Error streaming completion: {e}")
322
- raise e
256
+ return stream_complete_to_chat(
257
+ company_id=company_id,
258
+ user_id=user_id,
259
+ assistant_message_id=assistant_message_id,
260
+ user_message_id=user_message_id,
261
+ chat_id=chat_id,
262
+ assistant_id=assistant_id,
263
+ messages=messages,
264
+ model_name=model_name,
265
+ content_chunks=content_chunks,
266
+ debug_info=debug_info,
267
+ temperature=temperature,
268
+ timeout=timeout,
269
+ tools=tools,
270
+ start_text=start_text,
271
+ other_options=other_options,
272
+ )
323
273
 
324
- @staticmethod
325
- def _to_search_context(chunks: list[ContentChunk]) -> dict | None:
326
- if not chunks:
327
- return None
328
- return [
329
- unique_sdk.Integrated.SearchResult(
330
- id=chunk.id,
331
- chunkId=chunk.chunk_id,
332
- key=chunk.key,
333
- title=chunk.title,
334
- url=chunk.url,
335
- startPage=chunk.start_page,
336
- endPage=chunk.end_page,
337
- order=chunk.order,
338
- object=chunk.object,
339
- ) # type: ignore
340
- for chunk in chunks
341
- ]
274
+ @deprecated("Use stream_complete_to_chat_async instead")
275
+ async def stream_complete_async(
276
+ self,
277
+ messages: LanguageModelMessages,
278
+ model_name: LanguageModelName | str,
279
+ content_chunks: list[ContentChunk] = [],
280
+ debug_info: dict = {},
281
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
282
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
283
+ tools: Optional[list[LanguageModelTool]] = None,
284
+ start_text: Optional[str] = None,
285
+ other_options: Optional[dict] = None,
286
+ ) -> LanguageModelStreamResponse:
287
+ """
288
+ Streams a completion in the chat session asynchronously.
289
+ """
342
290
 
343
- @staticmethod
344
- def _add_tools_to_options(
345
- options: dict, tools: Optional[list[LanguageModelTool]]
346
- ) -> dict:
347
- if tools:
348
- options["tools"] = [
349
- {
350
- "type": "function",
351
- "function": tool.model_dump(exclude_none=True),
352
- }
353
- for tool in tools
291
+ [
292
+ company_id,
293
+ user_id,
294
+ assistant_message_id,
295
+ user_message_id,
296
+ chat_id,
297
+ assistant_id,
298
+ ] = validate_required_values(
299
+ [
300
+ self.company_id,
301
+ self.user_id,
302
+ self.assistant_message_id,
303
+ self.user_message_id,
304
+ self.chat_id,
305
+ self.assistant_id,
354
306
  ]
355
- return options
307
+ )
356
308
 
357
- @staticmethod
358
- def _add_response_format_to_options(
359
- options: dict,
360
- structured_output_model: Type[BaseModel],
361
- structured_output_enforce_schema: bool = False,
362
- ) -> dict:
363
- options["responseFormat"] = {
364
- "type": "json_schema",
365
- "json_schema": {
366
- "name": structured_output_model.__name__,
367
- "strict": structured_output_enforce_schema,
368
- "schema": structured_output_model.model_json_schema(),
369
- },
370
- }
371
- return options
309
+ return await stream_complete_to_chat_async(
310
+ company_id=company_id,
311
+ user_id=user_id,
312
+ assistant_message_id=assistant_message_id,
313
+ user_message_id=user_message_id,
314
+ chat_id=chat_id,
315
+ assistant_id=assistant_id,
316
+ messages=messages,
317
+ model_name=model_name,
318
+ content_chunks=content_chunks,
319
+ debug_info=debug_info,
320
+ temperature=temperature,
321
+ timeout=timeout,
322
+ tools=tools,
323
+ start_text=start_text,
324
+ other_options=other_options,
325
+ )
372
326
 
373
- @classmethod
374
- def _prepare_completion_params_util(
375
- cls,
327
+ async def stream_complete_to_chat_async(
328
+ self,
376
329
  messages: LanguageModelMessages,
377
330
  model_name: LanguageModelName | str,
378
- temperature: float,
331
+ content_chunks: list[ContentChunk] = [],
332
+ debug_info: dict = {},
333
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
334
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
379
335
  tools: Optional[list[LanguageModelTool]] = None,
336
+ start_text: Optional[str] = None,
380
337
  other_options: Optional[dict] = None,
381
- content_chunks: Optional[list[ContentChunk]] = None,
382
- structured_output_model: Optional[Type[BaseModel]] = None,
383
- structured_output_enforce_schema: bool = False,
384
- ) -> tuple[dict, str, dict, Optional[dict]]:
338
+ ) -> LanguageModelStreamResponse:
385
339
  """
386
- Prepares common parameters for completion requests.
387
-
388
- Returns:
389
- tuple containing:
390
- - options (dict): Combined options including tools and temperature
391
- - model (str): Resolved model name
392
- - messages_dict (dict): Processed messages
393
- - search_context (Optional[dict]): Processed content chunks if provided
340
+ Streams a completion in the chat session asynchronously.
394
341
  """
395
342
 
396
- options = cls._add_tools_to_options({}, tools)
397
-
398
- if structured_output_model:
399
- options = cls._add_response_format_to_options(
400
- options, structured_output_model, structured_output_enforce_schema
401
- )
402
-
403
- options["temperature"] = temperature
404
-
405
- if other_options:
406
- options.update(other_options)
407
-
408
- model = (
409
- model_name.name if isinstance(model_name, LanguageModelName) else model_name
410
- )
411
-
412
- # Different methods need different message dump parameters
413
- messages_dict = messages.model_dump(
414
- exclude_none=True,
415
- by_alias=content_chunks is not None, # Use by_alias for streaming methods
343
+ [
344
+ company_id,
345
+ user_id,
346
+ assistant_message_id,
347
+ user_message_id,
348
+ chat_id,
349
+ assistant_id,
350
+ ] = validate_required_values(
351
+ [
352
+ self.company_id,
353
+ self.user_id,
354
+ self.assistant_message_id,
355
+ self.user_message_id,
356
+ self.chat_id,
357
+ self.assistant_id,
358
+ ]
416
359
  )
417
360
 
418
- search_context = (
419
- LanguageModelService._to_search_context(content_chunks)
420
- if content_chunks is not None
421
- else None
361
+ return await stream_complete_to_chat_async(
362
+ company_id=company_id,
363
+ user_id=user_id,
364
+ assistant_message_id=assistant_message_id,
365
+ user_message_id=user_message_id,
366
+ chat_id=chat_id,
367
+ assistant_id=assistant_id,
368
+ messages=messages,
369
+ model_name=model_name,
370
+ content_chunks=content_chunks,
371
+ debug_info=debug_info,
372
+ temperature=temperature,
373
+ timeout=timeout,
374
+ tools=tools,
375
+ start_text=start_text,
376
+ other_options=other_options,
422
377
  )
423
-
424
- return options, model, messages_dict, search_context
@@ -0,0 +1,5 @@
1
+ from .constants import DOMAIN_NAME as DOMAIN_NAME
2
+ from .schemas import ShortTermMemory as ShortTermMemory
3
+ from .service import (
4
+ ShortTermMemoryService as ShortTermMemoryService,
5
+ )
@@ -0,0 +1 @@
1
+ DOMAIN_NAME = "short_term_memory"