44from functools import cached_property
55from typing import Generic
66
7- from yandex_ai_studio_sdk ._search_api .generative .config import SmartFilterSequence
8- from yandex_ai_studio_sdk ._search_indexes .search_index import BaseSearchIndex
97from yandex_ai_studio_sdk ._types .domain import BaseDomain
10- from yandex_ai_studio_sdk ._types .misc import UNDEFINED , UndefinedOr , get_defined_value , is_defined
11- from yandex_ai_studio_sdk ._types .string import SmartStringSequence
12- from yandex_ai_studio_sdk ._utils .coerce import ResourceType , coerce_resource_ids
138from yandex_ai_studio_sdk ._utils .doc import doc_from
149
1510from .function import AsyncFunctionTools , FunctionTools , FunctionToolsTypeT
16- from .generative_search import GenerativeSearchTool
17- from .search_index .call_strategy import CallStrategy , CallStrategyInputType
18- from .search_index .rephraser .function import RephraserFunction , RephraserInputType
19- from .search_index .tool import SearchIndexTool
2011
2112
2213class BaseTools (BaseDomain , Generic [FunctionToolsTypeT ]):
@@ -34,8 +25,6 @@ class BaseTools(BaseDomain, Generic[FunctionToolsTypeT]):
3425
3526 Tools are particularly useful in:
3627
37- - **AI Assistants**: Extending conversational agents with external capabilities like web search,
38- database queries, or API calls
3928 - **Completions**: Enabling language models to invoke functions during text generation for
4029 dynamic content creation and problem-solving
4130
@@ -54,121 +43,6 @@ def function(self) -> FunctionToolsTypeT:
5443 sdk = self ._sdk
5544 )
5645
57- @cached_property
58- def rephraser (self ) -> RephraserFunction :
59- """
60- Get the rephraser for creating query transformation models.
61-
62- The rephraser provides access to specialized language models designed to intelligently
63- rewrite and enhance user search queries by incorporating conversational context. This is
64- particularly useful in multi-turn conversations where the latest user message may lack context
65- from previous exchanges.
66-
67- The rephraser works by:
68- - Analyzing the conversation history and current user query
69- - Reformulating the query to be more specific and contextually complete
70- - Improving search relevance by expanding abbreviated or ambiguous terms
71- - Maintaining semantic intent while adding necessary context
72-
73- The rephraser returns a factory that can create Rephraser model instances with different
74- configurations, supporting various model types including the default 'rephraser' model or
75- custom rephrasing models.
76- """
77- return RephraserFunction (
78- name = 'tools.rehraser' ,
79- sdk = self ._sdk ,
80- parent_resource = self
81- )
82-
83- def search_index (
84- self ,
85- indexes : ResourceType [BaseSearchIndex ],
86- * ,
87- max_num_results : UndefinedOr [int ] = UNDEFINED ,
88- rephraser : UndefinedOr [RephraserInputType ] = UNDEFINED ,
89- call_strategy : UndefinedOr [CallStrategyInputType ] = UNDEFINED ,
90- ) -> SearchIndexTool :
91- """
92- Creates SearchIndexTool (not to be confused with :py:class:`~.SearchIndex`/:py:class:`~.AsyncSearchIndex`).
93-
94- :param indexes: parameter takes :py:class:`~.BaseSearchIndex`, string with search index id,
95- or a list of this values in any combination.
96- :param max_num_results: the maximum number of results to return from the search.
97- Fewer results may be returned if necessary to fit within the prompt's token limit.
98- This ensures that the combined prompt and search results do not exceed the token constraints.
99- :param rephraser: setting for rephrasing user queries; refer to :py:class:`~.Rephraser` documentation
100- for details.
101- """
102-
103- index_ids = coerce_resource_ids (indexes , BaseSearchIndex )
104- max_num_results_ = get_defined_value (max_num_results , None )
105-
106- rephraser_ = None
107- if is_defined (rephraser ):
108- # this is coercing any RephraserInputType to Rephraser
109- rephraser_ = self .rephraser (rephraser ) # type: ignore[arg-type]
110-
111- call_strategy_ = None
112- if is_defined (call_strategy ):
113- call_strategy_ = CallStrategy ._coerce (call_strategy )
114-
115- return SearchIndexTool (
116- search_index_ids = tuple (index_ids ),
117- max_num_results = max_num_results_ ,
118- rephraser = rephraser_ ,
119- call_strategy = call_strategy_ ,
120- )
121-
122- def generative_search (
123- self ,
124- * ,
125- description : str ,
126- site : UndefinedOr [SmartStringSequence ] = UNDEFINED ,
127- host : UndefinedOr [SmartStringSequence ] = UNDEFINED ,
128- url : UndefinedOr [SmartStringSequence ] = UNDEFINED ,
129- enable_nrfm_docs : UndefinedOr [bool ] = UNDEFINED ,
130- search_filters : UndefinedOr [SmartFilterSequence ] = UNDEFINED ,
131- ) -> GenerativeSearchTool :
132- """Creates GenerativeSearch tool which provide access to
133- generative search by `Yandex Search API <https://aistudio.yandex.ru/docs/search-api/concepts/index>`_ for LLMs.
134-
135- Not to be confused with ``sdk.search_api.generative``.
136- Tools domain is for creating tools for using in LLMs/Assistants and search_api domain
137- is for using Yandex Search API directly.
138-
139- To learn more about parameters, their formats, and possible values,
140- refer to
141- `generative search documentation <https://aistudio.yandex.ru/docs/search-api/concepts/generative-response#body>`_
142-
143- NB: All of the ``site``, ``host``, ``url`` parameters are mutually exclusive.
144-
145- :param site: parameter for limiting search to specific location or list of sites.
146- :param host: parameter for limiting search to specific location or list of hosts.
147- :param url: parameter for limiting search to specific location or list of URLs.
148- :param enable_nrfm_docs: tells to backend to include or not to include pages,
149- which are not available via direct clicks from given sites/hosts/urls
150- to search result.
151- :param search_filters: allows to limit search results with additional filters.
152-
153- >>> date_filter = {'date': '<20250101'}
154- >>> format_filter = {'format': 'doc'}
155- >>> lang_filter = {'lang': 'ru'}
156- >>> tool = sdk.tools.generative_search(
157- ... search_filters=[date_filter, format_filter, lang_filter],
158- ... description="description when model should call a tool"
159- ... )
160-
161- """
162-
163- search_api = self ._sdk .search_api .generative (
164- site = site ,
165- host = host ,
166- url = url ,
167- enable_nrfm_docs = enable_nrfm_docs ,
168- search_filters = search_filters
169- )
170- return search_api .as_tool (description = description )
171-
17246
17347@doc_from (BaseTools )
17448class AsyncTools (BaseTools [AsyncFunctionTools ]):
0 commit comments