|
| 1 | +# pylint: disable=no-name-in-module |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import abc |
| 6 | +import dataclasses |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +from yandex.cloud.ai.assistants.v1.threads.message_pb2 import Citation as ProtoCitation |
| 10 | +from yandex.cloud.ai.assistants.v1.threads.message_pb2 import Source as ProtoSource |
| 11 | + |
| 12 | +from yandex_cloud_ml_sdk._files.file import BaseFile |
| 13 | +from yandex_cloud_ml_sdk._search_indexes.search_index import BaseSearchIndex |
| 14 | +from yandex_cloud_ml_sdk._types.result import BaseResult |
| 15 | + |
| 16 | +from .base import BaseMessage |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from yandex_cloud_ml_sdk._sdk import BaseSDK |
| 20 | + |
| 21 | + |
| 22 | +@dataclasses.dataclass(frozen=True) |
| 23 | +class Citation(BaseResult): |
| 24 | + sources: tuple[Source, ...] |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def _from_proto(cls, proto: ProtoCitation, sdk: BaseSDK) -> Citation: # type: ignore[override] |
| 28 | + return cls( |
| 29 | + sources=tuple( |
| 30 | + Source._from_proto(proto=source, sdk=sdk) |
| 31 | + for source in proto.sources |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | +class Source(BaseResult): |
| 36 | + @property |
| 37 | + @abc.abstractmethod |
| 38 | + def type(self) -> str: |
| 39 | + pass |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def _from_proto(cls, proto: ProtoSource, sdk: BaseSDK) -> Source: # type: ignore[override] |
| 43 | + if proto.HasField('chunk'): |
| 44 | + return FileChunk._from_proto(proto=proto, sdk=sdk) |
| 45 | + |
| 46 | + return UnknownSource._from_proto(proto=proto, sdk=sdk) |
| 47 | + |
| 48 | + |
| 49 | +@dataclasses.dataclass(frozen=True) |
| 50 | +class FileChunk(Source, BaseMessage): |
| 51 | + search_index: BaseSearchIndex |
| 52 | + file: BaseFile | None |
| 53 | + |
| 54 | + @property |
| 55 | + def type(self) -> str: |
| 56 | + return 'filechunk' |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def _from_proto(cls, proto: ProtoSource, sdk: BaseSDK) -> FileChunk | UnknownSource: # type: ignore[override] |
| 60 | + # pylint: disable=protected-access |
| 61 | + chunk = proto.chunk |
| 62 | + assert chunk |
| 63 | + |
| 64 | + raw_parts = (part.text.content for part in chunk.content.content) |
| 65 | + parts = tuple(part for part in raw_parts if part) |
| 66 | + |
| 67 | + search_index = sdk.search_indexes._impl._from_proto(proto=chunk.search_index, sdk=sdk) |
| 68 | + file: BaseFile | None = None |
| 69 | + |
| 70 | + # NB: at the moment backend always returns non-empty source_file field |
| 71 | + # but in case it deleted, source_file will content empty File structure |
| 72 | + if ( |
| 73 | + chunk.HasField('source_file') and |
| 74 | + chunk.source_file and |
| 75 | + chunk.source_file.id |
| 76 | + ): |
| 77 | + file = sdk.files._file_impl._from_proto(proto=chunk.source_file, sdk=sdk) |
| 78 | + |
| 79 | + return cls( |
| 80 | + search_index=search_index, |
| 81 | + file=file, |
| 82 | + parts=parts, |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +@dataclasses.dataclass(frozen=True) |
| 87 | +class UnknownSource(Source): |
| 88 | + text: str |
| 89 | + |
| 90 | + @property |
| 91 | + def type(self) -> str: |
| 92 | + return 'unknown' |
| 93 | + |
| 94 | + @classmethod |
| 95 | + def _from_proto(cls, proto: ProtoSource, sdk: BaseSDK) -> UnknownSource: # type: ignore[override] |
| 96 | + return cls( |
| 97 | + text="Source's protobuf have unknown fields; try to update yandex-cloud-ml-sdk" |
| 98 | + ) |
0 commit comments