|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | + |
| 7 | +from yandex_cloud_ml_sdk import AsyncYCloudML |
| 8 | + |
| 9 | + |
| 10 | +async def get_model(sdk: AsyncYCloudML): |
| 11 | + models = await sdk.chat.completions.list() |
| 12 | + i = 0 |
| 13 | + print('You have access to the following models:') |
| 14 | + for i, model in enumerate(models): |
| 15 | + print(f" [{i:2}] {model.uri}") |
| 16 | + |
| 17 | + raw_number = input(f"Please, input model number from 0 to {i}: ") |
| 18 | + number = int(raw_number) |
| 19 | + return models[number] |
| 20 | + |
| 21 | + |
| 22 | +async def main() -> None: |
| 23 | + sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64') |
| 24 | + sdk.setup_default_logging() |
| 25 | + |
| 26 | + model = await get_model(sdk) |
| 27 | + |
| 28 | + # You could pass any extra query parameters to the model |
| 29 | + # via extra_query configuration parameter |
| 30 | + model = model.configure(temperature=0.5, extra_query={'top_p': 0.2}) |
| 31 | + |
| 32 | + # Note that reconfiguring extra_query will rewrite it's value entirely |
| 33 | + # without any merging |
| 34 | + model = model.configure(extra_query={'top_k': 2}) |
| 35 | + print(f"{model.config.extra_query=} {model.config.temperature=}") |
| 36 | + |
| 37 | + request = 'Say random number from 0 to 10' |
| 38 | + for title, extra_query in ( |
| 39 | + ('deterministic', {'top_k': 2, 'top_p': 0.1}), |
| 40 | + ('another deterministic', {'top_k': 2, 'top_p': 0.1}), |
| 41 | + ('more random', {'top_k': 5, 'top_p': 1}), |
| 42 | + ('another more random', {'top_k': 5, 'top_p': 1}), |
| 43 | + ): |
| 44 | + model = model.configure(extra_query=extra_query) |
| 45 | + result = await model.run(request) |
| 46 | + print(f"{title} result: {result.text}") |
| 47 | + |
| 48 | + # Also note that there is no client validation about extra query value at all: |
| 49 | + model = model.configure(extra_query={'foo': 2}) |
| 50 | + # This will not fail: |
| 51 | + await model.run(request) |
| 52 | + # So, refer to models documentation to find out about extra model parameters |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + asyncio.run(main()) |
0 commit comments