|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import json |
| 7 | + |
| 8 | +import pydantic |
| 9 | + |
| 10 | +from yandex_cloud_ml_sdk import AsyncYCloudML |
| 11 | + |
| 12 | + |
| 13 | +class Venue(pydantic.BaseModel): |
| 14 | + date: str |
| 15 | + place: str |
| 16 | + |
| 17 | + |
| 18 | +@pydantic.dataclasses.dataclass |
| 19 | +class VenueDataclass: |
| 20 | + date: str |
| 21 | + place: str |
| 22 | + name: str |
| 23 | + |
| 24 | + |
| 25 | +async def main() -> None: |
| 26 | + sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64') |
| 27 | + sdk.setup_default_logging() |
| 28 | + |
| 29 | + # NB: for now (24.02.2025) structured output is supported only at release candidate model version. |
| 30 | + model = sdk.models.completions('yandexgpt', model_version='rc') |
| 31 | + text = ( |
| 32 | + 'The conference will take place from May 10th to 12th, 2023, ' |
| 33 | + 'at 30 Avenue Corentin Cariou in Paris, France.' |
| 34 | + ) |
| 35 | + |
| 36 | + # We could as model to return data just with json format, model will |
| 37 | + # figure out format by itself: |
| 38 | + model = model.configure(response_format='json') |
| 39 | + result = await model.run([ |
| 40 | + {'role': 'system', 'text': 'Extract the date and venue information'}, |
| 41 | + {'role': 'user', 'text': text}, |
| 42 | + ]) |
| 43 | + print('Any JSON:', result[0].text) |
| 44 | + |
| 45 | + # Now, if you need not just JSON, but a parsed Python structure, you will need to parse it. |
| 46 | + # Be aware that you may need to handle parsing exceptions in case the model returns incorrect json. |
| 47 | + # This could happen, for example, if you exceed the token limit. |
| 48 | + try: |
| 49 | + data = json.loads(result.text) |
| 50 | + print("Parsed JSON:", data) |
| 51 | + |
| 52 | + bad_text = result.text[:5] |
| 53 | + json.loads(bad_text) |
| 54 | + except json.JSONDecodeError as e: |
| 55 | + print("JSON parsing error:", e) |
| 56 | + |
| 57 | + # You could use not only .run, but .run_stream as well as other methods too: |
| 58 | + print('Any JSON in streaming:') |
| 59 | + async for partial_result in model.run_stream([ |
| 60 | + {'role': 'system', 'text': 'Extract the date and venue information'}, |
| 61 | + {'role': 'user', 'text': text}, |
| 62 | + ]): |
| 63 | + print(f" {partial_result.text}") |
| 64 | + |
| 65 | + # NB: For each example, I am trying to make slightly different format to show a difference at print results. |
| 66 | + # We could pass a raw json schema: |
| 67 | + model = model.configure(response_format={ |
| 68 | + "json_schema": { |
| 69 | + "properties": { |
| 70 | + "DATE": { |
| 71 | + "title": "Date", |
| 72 | + "type": "string" |
| 73 | + }, |
| 74 | + "PLACE": { |
| 75 | + "title": "Place", |
| 76 | + "type": "string" |
| 77 | + } |
| 78 | + }, |
| 79 | + "required": ["DATE", "PLACE"], |
| 80 | + "title": "Venue", |
| 81 | + "type": "object" |
| 82 | + } |
| 83 | + }) |
| 84 | + result = await model.run([ |
| 85 | + {'role': 'system', 'text': 'Extract the date and venue information'}, |
| 86 | + {'role': 'user', 'text': text}, |
| 87 | + ]) |
| 88 | + print('JSONSchema from raw jsonschema:', result[0].text) |
| 89 | + |
| 90 | + # Also we could use pydantic.BaseModel descendant to describe JSONSchema for |
| 91 | + # structured output: |
| 92 | + model = model.configure(response_format=Venue) |
| 93 | + result = await model.run([ |
| 94 | + {'role': 'system', 'text': 'Extract the date and venue information'}, |
| 95 | + {'role': 'user', 'text': text}, |
| 96 | + ]) |
| 97 | + print('JSONSchema from Pydantic model:', result[0].text) |
| 98 | + |
| 99 | + # Lastly we could pass pydantic-dataclass: |
| 100 | + assert pydantic.__version__ > "2" |
| 101 | + model = model.configure(response_format=VenueDataclass) |
| 102 | + result = await model.run([ |
| 103 | + {'role': 'system', 'text': 'Extract the date and venue information'}, |
| 104 | + {'role': 'user', 'text': text}, |
| 105 | + ]) |
| 106 | + print('JSONSchema from Pydantic dataclass:', result[0].text) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + asyncio.run(main()) |
0 commit comments