1111from yandex_cloud_ml_sdk ._types .misc import UNDEFINED , UndefinedOr
1212from yandex_cloud_ml_sdk ._types .model import ModelSyncMixin
1313from yandex_cloud_ml_sdk ._types .string import SmartStringSequence
14+ from yandex_cloud_ml_sdk ._utils .doc import doc_from
1415from yandex_cloud_ml_sdk ._utils .sync import run_sync
1516
1617from .config import GenerativeSearchConfig , SmartFilterSequence , format_to_proto
2122
2223
2324class BaseGenerativeSearch (ModelSyncMixin [GenerativeSearchConfig , GenerativeSearchResult ]):
25+ """Generative search class which provides concrete methods for working with Search API
26+ and incapsulates search setting.
27+ """
28+
2429 _config_type = GenerativeSearchConfig
2530 _result_type = GenerativeSearchResult
2631
@@ -36,6 +41,32 @@ def configure( # type: ignore[override]
3641 enable_nrfm_docs : UndefinedOr [bool ] | None = UNDEFINED ,
3742 search_filters : UndefinedOr [SmartFilterSequence ] | None = UNDEFINED
3843 ) -> Self :
44+ """
45+ Returns the new object with config fields overrode by passed values.
46+
47+ To learn more about parameters and their formats and possible values,
48+ refer to
49+ `generative search documentation <https://yandex.cloud/docs/search-api/concepts/generative-response#body>`_
50+
51+ NB: All of the ``site``, ``host``, ``url`` parameters are mutually exclusive
52+ and using one of them is mandatory.
53+
54+ :param site: parameter for limiting search to specific location or list of sites.
55+ :param host: parameter for limiting search to specific location or list of hosts.
56+ :param url: parameter for limiting search to specific location or list of URLs.
57+ :param fix_misspell: tells to backend to fix or not to fix misspels in queries.
58+ :param enable_nrfm_docs: tells to backend to include or not to include pages,
59+ which are not available via direct clicks from given sites/hosts/urls
60+ to search result.
61+ :param search_filters: allows to limit search results with additional filters.
62+
63+ >>> date_filter = {'date': '<20250101'}
64+ >>> format_filter = {'format': 'doc'}
65+ >>> lang_filter = {'lang': 'ru'}
66+ >>> search = sdk.search_api.generative(search_filters=[date_filter, format_filter, lang_filter])
67+
68+ """
69+
3970 return super ().configure (
4071 site = site ,
4172 host = host ,
@@ -53,6 +84,35 @@ def __repr__(self) -> str:
5384
5485 @override
5586 async def _run (self , request : MessageInputType , * , timeout : float = 60 ) -> GenerativeSearchResult :
87+ """Run a search query with given ``request`` and search settings of this generative search
88+ object.
89+
90+ To change initial search settings use ``.configure`` method:
91+
92+ >>> search = sdk.search_api.generative(site="site")
93+ >>> search = search.configure(site="other_site")
94+
95+ :param request: search request, which could be either standalone request (message) or
96+ a list of messages, which represents a context of conversation with a model.
97+
98+ Also message could be one of the data formats:
99+
100+ * ``"string"`` -- in case of string input message will be passed to a model with a ``role="user"``;
101+
102+ * ``{"text": "text", "role": "user"}`` -- in case of dict input, it will be passed
103+ with corresponding ``"text"`` and ``"role"`` dict keys;
104+
105+ * ``MessageObject`` -- you could also pass any object which have a
106+ ``text: str`` and ``role: str`` attributes, allowing to reuse various
107+ result object, for example object you getting from compltions model run
108+ or result object from generative search itself;
109+
110+ * ``["string"/dict/object]`` -- list or any other sequence of any above described
111+ formats.
112+
113+ :param timeout: timeout, or the maximum time to wait for the request to complete in seconds.
114+
115+ """
56116 self .config ._validate_run ()
57117 messages = messages_to_proto (request )
58118
@@ -98,14 +158,18 @@ async def _run(self, request: MessageInputType, *, timeout: float = 60) -> Gener
98158 raise RuntimeError ("call returned less then one result" )
99159
100160
161+ @doc_from (BaseGenerativeSearch )
101162class AsyncGenerativeSearch (BaseGenerativeSearch ):
163+ @doc_from (BaseGenerativeSearch ._run )
102164 async def run (self , request : MessageInputType , * , timeout : float = 60 ) -> GenerativeSearchResult :
103165 return await self ._run (request = request , timeout = timeout )
104166
105167
168+ @doc_from (BaseGenerativeSearch )
106169class GenerativeSearch (BaseGenerativeSearch ):
107170 __run = run_sync (BaseGenerativeSearch ._run )
108171
172+ @doc_from (BaseGenerativeSearch ._run )
109173 def run (self , request : MessageInputType , * , timeout : float = 60 ) -> GenerativeSearchResult :
110174 return self .__run (request = request , timeout = timeout )
111175
0 commit comments