Skip to content

Commit b057888

Browse files
authored
Add docstrings for _assistants (#131)
1 parent b80de5a commit b057888

File tree

3 files changed

+188
-8
lines changed

3 files changed

+188
-8
lines changed

src/yandex_cloud_ml_sdk/_assistants/assistant.py

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from yandex_cloud_ml_sdk._types.misc import UNDEFINED, UndefinedOr, get_defined_value
2323
from yandex_cloud_ml_sdk._types.resource import ExpirableResource, safe_on_delete
2424
from yandex_cloud_ml_sdk._types.schemas import ResponseType
25+
from yandex_cloud_ml_sdk._utils.doc import doc_from
2526
from yandex_cloud_ml_sdk._utils.proto import proto_to_dict
2627
from yandex_cloud_ml_sdk._utils.sync import run_sync_generator_impl, run_sync_impl
2728

@@ -33,15 +34,24 @@
3334

3435
@dataclasses.dataclass(frozen=True)
3536
class BaseAssistant(ExpirableResource[ProtoAssistant], Generic[RunTypeT, ThreadTypeT]):
37+
#: Expiration configuration for the assistant.
3638
expiration_config: ExpirationConfig
39+
#: The GPT model used by the assistant.
3740
model: BaseGPTModel
41+
#: Instructions or guidelines that the assistant should follow. These instructions guide the assistant's behavior and responses.
3842
instruction: str | None
43+
#: Options for truncating thread messages. Controls how messages are truncated when forming the prompt.
3944
prompt_truncation_options: PromptTruncationOptions
45+
#: Tools available to the assistant. Can be a sequence or a single tool. Tools must implement BaseTool interface.
4046
tools: tuple[BaseTool, ...]
47+
#: A format of the response returned by the model. Could be a JsonSchema, a JSON string, or a pydantic model
4148
response_format: ResponseType | None
4249

4350
@property
4451
def max_prompt_tokens(self) -> int | None:
52+
"""
53+
Returns the maximum number of prompt tokens allowed for the assistant.
54+
"""
4555
return self.prompt_truncation_options.max_prompt_tokens
4656

4757
@classmethod
@@ -99,6 +109,30 @@ async def _update(
99109
response_format: UndefinedOr[ResponseType] = UNDEFINED,
100110
timeout: float = 60,
101111
) -> Self:
112+
"""
113+
Update the assistant's configuration with new parameters.
114+
115+
This method sends an update request to Yandex Cloud ML API to modify the assistant's
116+
configuration. Only specified parameters will be updated, others remain unchanged.
117+
118+
:param model: New model URI or BaseGPTModel instance to use
119+
:param temperature: A sampling temperature to use - higher values mean more random results. Should be a double number between 0 (inclusive) and 1 (inclusive).
120+
:param max_tokens: Maximum number of tokens to generate
121+
:param instruction: New instructions for the assistant
122+
:param max_prompt_tokens: Maximum tokens allowed in the prompt
123+
:param prompt_truncation_strategy: Strategy for truncating long prompts
124+
:param name: New name for the assistant
125+
:param description: New description for the assistant
126+
:param labels: New key-value labels for the assistant
127+
:param ttl_days: Time-to-live in days before automatic deletion
128+
:param tools: Tools to use for completion. Can be a sequence or a single tool.
129+
:param expiration_policy: Policy for handling expiration
130+
:param response_format: A format of the response returned by the model. Could be a JsonSchema, a JSON string, or a pydantic model.
131+
Read more about possible response formats in the
132+
`structured output documentation <https://yandex.cloud/docs/foundation-models/concepts/yandexgpt/#structured-output>`_.
133+
:param timeout: The timeout, or the maximum time to wait for the request to complete in seconds.
134+
Defaults to 60 seconds.
135+
"""
102136
# pylint: disable=too-many-locals
103137
prompt_truncation_options = PromptTruncationOptions._coerce(
104138
max_prompt_tokens=max_prompt_tokens,
@@ -161,6 +195,15 @@ async def _delete(
161195
*,
162196
timeout: float = 60,
163197
) -> None:
198+
"""
199+
Delete the assistant from Yandex Cloud ML.
200+
201+
Sends a delete request to the Yandex Cloud ML API to remove the assistant.
202+
After successful deletion, marks the assistant as deleted internally.
203+
204+
:param timeout: The timeout, or the maximum time to wait for the request to complete in seconds.
205+
Defaults to 60 seconds.
206+
"""
164207
request = DeleteAssistantRequest(assistant_id=self.id)
165208

166209
async with self._client.get_service_stub(AssistantServiceStub, timeout=timeout) as stub:
@@ -178,6 +221,16 @@ async def _list_versions(
178221
page_token: UndefinedOr[str] = UNDEFINED,
179222
timeout: float = 60
180223
) -> AsyncIterator[AssistantVersion]:
224+
"""
225+
List all versions of the assistant.
226+
227+
This method retrieves historical versions of the assistant in a paginated manner.
228+
229+
:param page_size: Maximum number of versions to return per page
230+
:param page_token: Token for pagination
231+
:param timeout: The timeout, or the maximum time to wait for the request to complete in seconds.
232+
Defaults to 60 seconds.
233+
"""
181234
page_token_ = get_defined_value(page_token, '')
182235
page_size_ = get_defined_value(page_size, 0)
183236

@@ -246,6 +299,18 @@ async def _run(
246299
custom_response_format: UndefinedOr[ResponseType] = UNDEFINED,
247300
timeout: float = 60,
248301
) -> RunTypeT:
302+
"""
303+
Execute a non-streaming run with the assistant on the given thread.
304+
305+
:param thread: Thread ID or Thread object to run on
306+
:param custom_temperature: Override for model temperature
307+
:param custom_max_tokens: Override for max tokens to generate
308+
:param custom_max_prompt_tokens: Override for max prompt tokens
309+
:param custom_prompt_truncation_strategy: Override for prompt truncation strategy
310+
:param custom_response_format: Override for response format
311+
:param timeout: The timeout, or the maximum time to wait for the request to complete in seconds.
312+
Defaults to 60 seconds.
313+
"""
249314
return await self._run_impl(
250315
thread=thread,
251316
stream=False,
@@ -268,6 +333,18 @@ async def _run_stream(
268333
custom_response_format: UndefinedOr[ResponseType] = UNDEFINED,
269334
timeout: float = 60,
270335
) -> RunTypeT:
336+
"""
337+
Execute a streaming run with the assistant on the given thread.
338+
339+
:param thread: Thread ID or Thread object to run on
340+
:param custom_temperature: Override for model temperature
341+
:param custom_max_tokens: Override for max tokens to generate
342+
:param custom_max_prompt_tokens: Override for max prompt tokens
343+
:param custom_prompt_truncation_strategy: Override for prompt truncation strategy
344+
:param custom_response_format: Override for response format
345+
:param timeout: The timeout, or the maximum time to wait for the request to complete in seconds.
346+
Defaults to 60 seconds.
347+
"""
271348
return await self._run_impl(
272349
thread=thread,
273350
stream=True,
@@ -283,24 +360,46 @@ async def _run_stream(
283360

284361
@dataclasses.dataclass(frozen=True)
285362
class ReadOnlyAssistant(BaseAssistant[RunTypeT, ThreadTypeT]):
363+
"""
364+
Base class providing read-only access to Yandex Cloud ML Assistant configuration and metadata.
365+
366+
This class implements the core interface for interacting with Yandex Cloud ML Assistant API
367+
in a read-only manner. It serves as the parent class for both synchronous (Assistant)
368+
and asynchronous (AsyncAssistant) implementations.
369+
"""
370+
371+
#: The name of the assistant.
286372
name: str | None
373+
#: The description of the assistant.
287374
description: str | None
375+
#: The identifier of the user who created the assistant.
288376
created_by: str
377+
#: The timestamp when the assistant was created.
289378
created_at: datetime
379+
#: The identifier of the user who last updated the assistant.
290380
updated_by: str
381+
#: The timestamp when the assistant was last updated.
291382
updated_at: datetime
383+
#: The timestamp when the assistant will expire.
292384
expires_at: datetime
385+
#: Additional labels associated with the assistant.
293386
labels: dict[str, str] | None
294387

295-
296388
@dataclasses.dataclass(frozen=True)
297389
class AssistantVersion:
390+
"""
391+
Represents a specific version of an Assistant.
392+
"""
393+
#: ID of the assistant version.
298394
id: str
395+
#: The assistant instance for this version.
299396
assistant: ReadOnlyAssistant
397+
#: Mask specifying which fields were updated in this version. Mask also have a custom JSON encoding
300398
update_mask: tuple[str, ...]
301399

302-
400+
@doc_from(ReadOnlyAssistant)
303401
class AsyncAssistant(ReadOnlyAssistant[AsyncRun, AsyncThread]):
402+
@doc_from(ReadOnlyAssistant._update)
304403
async def update(
305404
self,
306405
*,
@@ -336,13 +435,15 @@ async def update(
336435
timeout=timeout
337436
)
338437

438+
@doc_from(ReadOnlyAssistant._delete)
339439
async def delete(
340440
self,
341441
*,
342442
timeout: float = 60,
343443
) -> None:
344444
await self._delete(timeout=timeout)
345445

446+
@doc_from(ReadOnlyAssistant._list_versions)
346447
async def list_versions(
347448
self,
348449
page_size: UndefinedOr[int] = UNDEFINED,
@@ -356,6 +457,7 @@ async def list_versions(
356457
):
357458
yield version
358459

460+
@doc_from(ReadOnlyAssistant._run)
359461
async def run(
360462
self,
361463
thread: str | AsyncThread,
@@ -377,6 +479,7 @@ async def run(
377479
timeout=timeout
378480
)
379481

482+
@doc_from(ReadOnlyAssistant._run_stream)
380483
async def run_stream(
381484
self,
382485
thread: str | AsyncThread,
@@ -398,8 +501,9 @@ async def run_stream(
398501
timeout=timeout
399502
)
400503

401-
504+
@doc_from(ReadOnlyAssistant)
402505
class Assistant(ReadOnlyAssistant[Run, Thread]):
506+
@doc_from(ReadOnlyAssistant._update)
403507
def update(
404508
self,
405509
*,
@@ -435,13 +539,15 @@ def update(
435539
timeout=timeout
436540
), self._sdk)
437541

542+
@doc_from(ReadOnlyAssistant._delete)
438543
def delete(
439544
self,
440545
*,
441546
timeout: float = 60,
442547
) -> None:
443548
run_sync_impl(self._delete(timeout=timeout), self._sdk)
444549

550+
@doc_from(ReadOnlyAssistant._list_versions)
445551
def list_versions(
446552
self,
447553
page_size: UndefinedOr[int] = UNDEFINED,
@@ -457,6 +563,7 @@ def list_versions(
457563
self._sdk
458564
)
459565

566+
@doc_from(ReadOnlyAssistant._run)
460567
def run(
461568
self,
462569
thread: str | Thread,
@@ -478,6 +585,7 @@ def run(
478585
timeout=timeout
479586
), self._sdk)
480587

588+
@doc_from(ReadOnlyAssistant._run_stream)
481589
def run_stream(
482590
self,
483591
thread: str | Thread,

0 commit comments

Comments
 (0)