Skip to content

Commit

Permalink
feat: supported writable stages/choices (epam#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik authored Jul 9, 2024
1 parent ea6caa4 commit efd3725
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions aidial_sdk/chat_completion/choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from aidial_sdk.chat_completion.stage import Stage
from aidial_sdk.pydantic_v1 import ValidationError
from aidial_sdk.utils._attachment import create_attachment
from aidial_sdk.utils._content_stream import ContentStream
from aidial_sdk.utils.errors import runtime_error
from aidial_sdk.utils.logging import log_debug

Expand Down Expand Up @@ -91,6 +92,10 @@ def append_content(self, content: str) -> None:
self.send_chunk(ContentChunk(content, self._index))
self._last_finish_reason = FinishReason.STOP

@property
def content_stream(self) -> ContentStream:
return ContentStream(self)

def create_function_tool_call(
self, id: str, name: str, arguments: Optional[str] = None
) -> FunctionToolCall:
Expand Down
5 changes: 5 additions & 0 deletions aidial_sdk/chat_completion/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from aidial_sdk.chat_completion.request import Attachment
from aidial_sdk.pydantic_v1 import ValidationError
from aidial_sdk.utils._attachment import create_attachment
from aidial_sdk.utils._content_stream import ContentStream
from aidial_sdk.utils.errors import runtime_error


Expand Down Expand Up @@ -68,6 +69,10 @@ def append_content(self, content: str):
ContentStageChunk(self._choice_index, self._stage_index, content)
)

@property
def content_stream(self) -> ContentStream:
return ContentStream(self)

def append_name(self, name: str):
if not self._opened:
raise runtime_error("Trying to append name to an unopened stage")
Expand Down
40 changes: 40 additions & 0 deletions aidial_sdk/utils/_content_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Protocol


class ContentReceiver(Protocol):
def append_content(self, content: str) -> None: ...


class ContentStream:
"""
The ContentStream class allows using the receiver in contexts where typing.SupportsWrite[str] is expected.
For example:
1. Redirecting print statements:
print("Hello, world", file=content_stream)
2. Using with tqdm for progress bars:
import tqdm
for item in tqdm(items, file=content_stream):
process(item)
3. Redirecting logs to the content stream:
import logging
logging_handler = logging.StreamHandler(stream=content_stream)
4. Writing CSV data:
import csv
csv.writer(content_stream).writerows(data)
"""

_receiver: ContentReceiver

def __init__(self, receiver: ContentReceiver) -> None:
self._receiver = receiver

def write(self, s: str) -> None:
self._receiver.append_content(s)

0 comments on commit efd3725

Please sign in to comment.