Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assistants #15

Merged
merged 13 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
55 changes: 55 additions & 0 deletions examples/async/assistants/assistant_with_search_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

from __future__ import annotations

import asyncio
import pathlib

from yandex_cloud_ml_sdk import AsyncYCloudML


def local_path(path: str) -> pathlib.Path:
return pathlib.Path(__file__).parent / path


async def main() -> None:
sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64')

file_coros = (
sdk.files.upload(
local_path(path),
ttl_days=5,
expiration_policy="static",
)
for path in ['turkey_example.txt', 'maldives_example.txt']
)

files = await asyncio.gather(*file_coros)
operation = await sdk.search_indexes.create_deferred(files)
search_index = await operation

tool = sdk.tools.search_index(search_index)

assistant = await sdk.assistants.create('yandexgpt', tools=[tool])
thread = await sdk.threads.create()

for search_query in (
local_path('search_query.txt').read_text().splitlines()[0],
"Cколько пошлина в Анталье"
):
await thread.write(search_query)
run = await assistant.run(thread)
result = await run
print('Question', search_query)
print('Answer:', result.text)

await search_index.delete()
await thread.delete()
await assistant.delete()

for file in files:
await file.delete()


if __name__ == '__main__':
asyncio.run(main())
39 changes: 39 additions & 0 deletions examples/async/assistants/assistants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3

from __future__ import annotations

import asyncio

from yandex_cloud_ml_sdk import AsyncYCloudML


async def main() -> None:
sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64')

assistant = await sdk.assistants.create(
'yandexgpt',
ttl_days=1,
expiration_policy='static',
temperature=0.5,
max_prompt_tokens=50,
)
print(f"{assistant=}")

assistant2 = await sdk.assistants.get(assistant.id)
print(f"same {assistant2=}")

await assistant2.update(model='yandexgpt-lite', name='foo', max_tokens=5)
print(f"updated {assistant2=}")

async for version in assistant.list_versions():
print(f"assistant {version=}")

async for assistant in sdk.assistants.list():
print(f"deleting {assistant=}")

await assistant.delete()



if __name__ == '__main__':
asyncio.run(main())
1 change: 1 addition & 0 deletions examples/async/assistants/example_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some example
34 changes: 34 additions & 0 deletions examples/async/assistants/files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

from __future__ import annotations

import asyncio
import pathlib

from yandex_cloud_ml_sdk import AsyncYCloudML


async def main() -> None:
sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64')

path = pathlib.Path(__file__).parent / 'example_file'
file = await sdk.files.upload(path, ttl_days=5, expiration_policy="static")
print(f"created {file=}")

await file.update(name='foo', ttl_days=9)
print(f"updated {file=}")

second = await sdk.files.get(file.id)
print(f"just as file {second=}")
await second.update(name='foo', expiration_policy='since_last_active')
print(f"it keeps update from first instance, {second=}")

print(f"url for downloading: {await file.get_url()=}")
print(f"getting content {await file.download_as_bytes()=}")

async for file in sdk.files.list():
print(f"deleting {file=}")
await file.delete()

if __name__ == '__main__':
asyncio.run(main())
1 change: 1 addition & 0 deletions examples/async/assistants/maldives_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Стоимость въезда/выезда с Мальдив - 10 тысяч долларов в обе стороны
57 changes: 57 additions & 0 deletions examples/async/assistants/runs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3

from __future__ import annotations

import asyncio

from yandex_cloud_ml_sdk import AsyncYCloudML


async def main() -> None:
sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64')

assistant = await sdk.assistants.create(
'yandexgpt',
temperature=0.5,
max_prompt_tokens=50,
ttl_days=6,
expiration_policy='static',
)
print(f'new {assistant=}')

thread = await sdk.threads.create(
name='foo',
ttl_days=6,
expiration_policy='static',
)
message = await thread.write("hi! how are you")
print(f'new {thread=} with {message=}')

run = await assistant.run_stream(thread)
print(f'new stream {run=} on this thread and assistant')
async for event in run:
print(f'from stream {event=}')

message = await thread.write("how is your name?")
print(f'second {message=}')

run = await assistant.run(thread)
print(f'second {run=}')
result = await run
print(f'run {result=}')

run = await sdk.runs.get_last_by_thread(thread)
print(f'last run in thread, same as last one: {run}')

# NB: it doesn't work at the moment at the backend
# async for run in sdk.runs.list(page_size=10):
# print('run:', run)

async for assistant in sdk.assistants.list():
await assistant.delete()

await thread.delete()


if __name__ == '__main__':
asyncio.run(main())
62 changes: 62 additions & 0 deletions examples/async/assistants/search_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3

from __future__ import annotations

import asyncio
import pathlib

from yandex_cloud_ml_sdk import AsyncYCloudML
from yandex_cloud_ml_sdk.search_indexes import StaticIndexChunkingStrategy, TextSearchIndexType


def local_path(path: str) -> pathlib.Path:
return pathlib.Path(__file__).parent / path


async def main() -> None:
sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64')

file_coros = (
sdk.files.upload(
local_path(path),
ttl_days=5,
expiration_policy="static",
)
for path in ['turkey_example.txt', 'maldives_example.txt']
)
files = await asyncio.gather(*file_coros)

operation = await sdk.search_indexes.create_deferred(
files,
index_type=TextSearchIndexType(
chunking_strategy=StaticIndexChunkingStrategy(
max_chunk_size_tokens=700,
chunk_overlap_tokens=300,
)
)
)
search_index = await operation.wait()
print(f"new {search_index=}")

search_index2 = await sdk.search_indexes.get(search_index.id)
print(f"same as first, {search_index2=}")

await search_index.update(name="foo")
print(f"now with a name {search_index=}")

# NB: it doesn't work at the moment
# index_files = [file async for file in search_index.list_files()]
# print(f"search index files: {index_files}")
# index_file = await search_index.get_file(index_files[0].id)
# print(f"search index file: {index_file}")

for file in files:
await file.delete()

async for search_index in sdk.search_indexes.list():
print(f"delete {search_index=}")
await search_index.delete()


if __name__ == '__main__':
asyncio.run(main())
1 change: 1 addition & 0 deletions examples/async/assistants/search_query.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Какова стоимость выездной пошлины для путешествия в чарующие и солнечные МАЛЬДИВЫ, где беззаботный отдых на восхитительных пляжах, утопающих в лучах мягкого средиземноморского солнца, соединяется с открытием величественных исторических памятников и наслаждением изысканными ароматами местной кухни?
36 changes: 36 additions & 0 deletions examples/async/assistants/threads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3

from __future__ import annotations

import asyncio

from yandex_cloud_ml_sdk import AsyncYCloudML


async def main() -> None:
sdk = AsyncYCloudML(folder_id='b1ghsjum2v37c2un8h64')

thread = await sdk.threads.create(name='foo', ttl_days=5, expiration_policy="static")
print(f"new {thread=}")

second = await sdk.threads.get(thread.id)
print(f"same as first, {second=}")
await second.update(ttl_days=9)
print(f"with updated epiration config, {second=}")

message = await thread.write("content")
message2 = await second.write("content2")
print(f"hey, we just writed {message=} and {message2} into the thread")

print("and now we could read it:")
async for message in thread:
print(f" {message=}")
print(f" {message.text=}\n")

async for thread in sdk.threads.list():
print(f"deleting thread {thread=}")
await thread.delete()


if __name__ == '__main__':
asyncio.run(main())
Loading
Loading