|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import pathlib |
| 7 | + |
| 8 | +from yandex_cloud_ml_sdk import AsyncYCloudML |
| 9 | + |
| 10 | +LABEL_KEY = 'yc-ml-sdk-example' |
| 11 | +PATH = pathlib.Path(__file__) |
| 12 | +NAME = f'example-{PATH.parent.name}-{PATH.name}' |
| 13 | +LABELS = {LABEL_KEY: NAME} |
| 14 | + |
| 15 | + |
| 16 | +def local_path(path: str) -> pathlib.Path: |
| 17 | + return pathlib.Path(__file__).parent / path |
| 18 | + |
| 19 | + |
| 20 | +async def get_search_index(sdk): |
| 21 | + """ |
| 22 | + This function represents getting or creating demo search_index object. |
| 23 | +
|
| 24 | + In real life you will get it any other way that would suit your case. |
| 25 | + """ |
| 26 | + |
| 27 | + async for search_index in sdk.search_indexes.list(): |
| 28 | + if search_index.labels and search_index.labels.get(LABEL_KEY) == NAME: |
| 29 | + print(f'using {search_index=}') |
| 30 | + break |
| 31 | + else: |
| 32 | + print('no search indexes found, creating new one') |
| 33 | + file_coros = ( |
| 34 | + sdk.files.upload( |
| 35 | + local_path(path), |
| 36 | + ttl_days=5, |
| 37 | + expiration_policy="static", |
| 38 | + ) |
| 39 | + for path in ['turkey_example.txt', 'maldives_example.txt'] |
| 40 | + ) |
| 41 | + files = await asyncio.gather(*file_coros) |
| 42 | + operation = await sdk.search_indexes.create_deferred(files, labels=LABELS) |
| 43 | + search_index = await operation |
| 44 | + print(f'new {search_index=}') |
| 45 | + |
| 46 | + for file in files: |
| 47 | + await file.delete() |
| 48 | + |
| 49 | + return search_index |
| 50 | + |
| 51 | + |
| 52 | +async def delete_labeled_entities(iterator): |
| 53 | + """ |
| 54 | + Deletes any entities from given iterator which have .labels attribute |
| 55 | + with `labels[LABEL_KEY] == NAME` |
| 56 | + """ |
| 57 | + |
| 58 | + async for entity in iterator: |
| 59 | + if entity.labels and entity.labels.get(LABEL_KEY) == NAME: |
| 60 | + print(f'deleting {entity.__class__.__name__} with id={entity.id!r}') |
| 61 | + await entity.delete() |
| 62 | + |
| 63 | + |
| 64 | +async def main() -> None: |
| 65 | + sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64') |
| 66 | + sdk.setup_default_logging(log_level='WARNING') |
| 67 | + |
| 68 | + search_index = await get_search_index(sdk) |
| 69 | + thread = await sdk.threads.create(labels=LABELS) |
| 70 | + |
| 71 | + tool = sdk.tools.search_index(search_index) |
| 72 | + assistant = await sdk.assistants.create('yandexgpt', tools=[tool], labels=LABELS) |
| 73 | + |
| 74 | + # Look, if you don't pass a call strategy to a SearchIndex, it is 'always' use by-default |
| 75 | + assert tool.call_strategy is None |
| 76 | + assert assistant.tools[0].call_strategy.value == 'always' # type: ignore[attr-defined] |
| 77 | + |
| 78 | + # First of all we are using request which will definitely find something |
| 79 | + search_query = local_path('search_query.txt').read_text().splitlines()[0] |
| 80 | + await thread.write(search_query) |
| 81 | + run = await assistant.run(thread) |
| 82 | + result = await run.wait() |
| 83 | + # NB: citations says if index were used or not |
| 84 | + assert len(result.citations) > 0 |
| 85 | + print(f'If you are using "always" call_strategy, it returns {len(result.citations)>0=} citations from search index') |
| 86 | + |
| 87 | + # Now we will use a search index, which will be used only if it asked to |
| 88 | + tool_with_call_strategy = sdk.tools.search_index( |
| 89 | + search_index, |
| 90 | + call_strategy={ |
| 91 | + 'type': 'function', |
| 92 | + 'function': {'name': 'guide', 'instruction': 'use this only if you are asked to look in the guide'} |
| 93 | + } |
| 94 | + ) |
| 95 | + assistant_with_call_strategy = await sdk.assistants.create( |
| 96 | + sdk.models.completions('yandexgpt', model_version='rc'), |
| 97 | + tools=[tool_with_call_strategy], |
| 98 | + labels=LABELS |
| 99 | + ) |
| 100 | + |
| 101 | + await thread.write(search_query) |
| 102 | + run = await assistant_with_call_strategy.run(thread) |
| 103 | + result = await run.wait() |
| 104 | + # NB: citations says if index were used or not |
| 105 | + assert len(result.citations) == 0 |
| 106 | + print( |
| 107 | + 'When you are using special call_strategy and model decides not to use search index according ' |
| 108 | + f'to call_strategy instruction, it returns {len(result.citations)>0=} citations from search index' |
| 109 | + ) |
| 110 | + |
| 111 | + await thread.write(f"Look at the guide, please: {search_query}") |
| 112 | + run = await assistant_with_call_strategy.run(thread) |
| 113 | + result = await run.wait() |
| 114 | + # NB: citations says if index were used or not |
| 115 | + assert len(result.citations) > 0 |
| 116 | + print( |
| 117 | + 'When you are using special call_strategy and model decides to use search index according ' |
| 118 | + f'to call_strategy instruction, it returns {len(result.citations)>0=} from search index' |
| 119 | + ) |
| 120 | + |
| 121 | + # we will delete all assistant and threads created in this example |
| 122 | + # to not to increase chaos level, but not the search index, because |
| 123 | + # index creation is a slow operation and could be re-used in this |
| 124 | + # example next run |
| 125 | + await delete_labeled_entities(sdk.assistants.list()) |
| 126 | + await delete_labeled_entities(sdk.threads.list()) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + asyncio.run(main()) |
0 commit comments