File: test_vector_stores_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (181 lines) | stat: -rw-r--r-- 7,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from __future__ import annotations

import os
import pytest
import pathlib
import uuid
import openai
from devtools_testutils import AzureRecordedTestCase
from conftest import ASST_AZURE, PREVIEW, GPT_4_OPENAI, configure_async


@pytest.mark.live_test_only
class TestVectorStoresAsync(AzureRecordedTestCase):

    @configure_async
    @pytest.mark.asyncio
    @pytest.mark.parametrize("api_type, api_version", [(ASST_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")])
    async def test_vector_stores_crud(self, client_async: openai.AsyncAzureOpenAI | openai.AsyncOpenAI, api_type, api_version, **kwargs):
        file_name = f"test{uuid.uuid4()}.txt"
        with open(file_name, "w") as f:
            f.write("Contoso company policy requires that all employees take at least 10 vacation days a year.")

        path = pathlib.Path(file_name)

        file = await client_async.files.create(
            file=open(path, "rb"),
            purpose="assistants"
        )

        try:
            vector_store = await client_async.vector_stores.create(
                name="Support FAQ"
            )
            assert vector_store.name == "Support FAQ"
            assert vector_store.id
            assert vector_store.object == "vector_store"
            assert vector_store.created_at
            assert vector_store.file_counts.total == 0

            vectors = client_async.vector_stores.list()
            async for vector in vectors:
                assert vector.id
                assert vector_store.object == "vector_store"
                assert vector_store.created_at

            vector_store = await client_async.vector_stores.update(
                vector_store_id=vector_store.id,
                name="Support FAQ and more",
                metadata={"Q": "A"}
            )
            assert vector_store.name == "Support FAQ and more"
            assert vector_store.metadata == {"Q": "A"}
            retrieved_vector = await client_async.vector_stores.retrieve(
                vector_store_id=vector_store.id
            )
            assert retrieved_vector.id == vector_store.id

            vector_store_file = await client_async.vector_stores.files.create(
                vector_store_id=vector_store.id,
                file_id=file.id
            )
            assert vector_store_file.id
            assert vector_store_file.object == "vector_store.file"
            assert vector_store_file.created_at
            assert vector_store_file.vector_store_id == vector_store.id

            vector_store_files = client_async.vector_stores.files.list(
                vector_store_id=vector_store.id
            )
            async for vector_file in vector_store_files:
                assert vector_file.id
                assert vector_file.object == "vector_store.file"
                assert vector_store_file.created_at
                assert vector_store_file.vector_store_id == vector_store.id

            vector_store_file_2 = await client_async.vector_stores.files.retrieve(
                vector_store_id=vector_store.id,
                file_id=vector_store_file.id
            )
            assert vector_store_file_2.id == vector_store_file.id
            assert vector_store_file.vector_store_id == vector_store.id

            # TODO Not supported by Azure yet
            # vector_store_file_updated = await client_async.vector_stores.files.update(
            #     file_id=vector_store_file.id,
            #     vector_store_id=vector_store.id,
            #     attributes={"Q": "A"}
            # )
            # assert vector_store_file_updated.attributes == {"Q": "A"}

            # file_content = await client_async.vector_stores.files.content(
            #     vector_store_id=vector_store.id,
            #     file_id=vector_store_file.id
            # )
            # assert file_content

            # search_response = await client_async.vector_stores.search(
            #     vector_store_id=vector_store.id,
            #     query="vacation days",
            # )
            # async for s in search_response:
            #     assert s

        finally:
            os.remove(path)
            deleted_vector_store_file = await client_async.vector_stores.files.delete(
                vector_store_id=vector_store.id,
                file_id=vector_store_file.id
            )
            assert deleted_vector_store_file.deleted is True
            deleted_vector_store = await client_async.vector_stores.delete(
                vector_store_id=vector_store.id
            )
            assert deleted_vector_store.deleted is True

    @configure_async
    @pytest.mark.asyncio
    @pytest.mark.parametrize("api_type, api_version", [(ASST_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")])
    async def test_vector_stores_batch_crud(self, client_async: openai.AsyncAzureOpenAI | openai.AsyncOpenAI, api_type, api_version, **kwargs):
        file_name = f"test{uuid.uuid4()}.txt"
        file_name_2 = f"test{uuid.uuid4()}.txt"
        with open(file_name, "w") as f:
            f.write("test")

        path = pathlib.Path(file_name)

        file = await client_async.files.create(
            file=open(path, "rb"),
            purpose="assistants"
        )
        with open(file_name_2, "w") as f:
            f.write("test")
        path_2 = pathlib.Path(file_name_2)

        file_2 = await client_async.files.create(
            file=open(path_2, "rb"),
            purpose="assistants"
        )
        try:
            vector_store = await client_async.vector_stores.create(
                name="Support FAQ"
            )
            vector_store_file_batch = await client_async.vector_stores.file_batches.create(
                vector_store_id=vector_store.id,
                file_ids=[file.id, file_2.id]
            )
            assert vector_store_file_batch.id
            assert vector_store_file_batch.object == "vector_store.file_batch"
            assert vector_store_file_batch.created_at is not None
            assert vector_store_file_batch.status

            vectors = await client_async.vector_stores.file_batches.list_files(
                vector_store_id=vector_store.id,
                batch_id=vector_store_file_batch.id
            )
            async for vector in vectors:
                assert vector.id
                assert vector.object == "vector_store.file"
                assert vector.created_at is not None

            retrieved_vector_store_file_batch = await client_async.vector_stores.file_batches.retrieve(
                vector_store_id=vector_store.id,
                batch_id=vector_store_file_batch.id
            )
            assert retrieved_vector_store_file_batch.id == vector_store_file_batch.id

        finally:
            os.remove(path)
            os.remove(path_2)
            delete_file = await client_async.files.delete(file.id)
            assert delete_file.deleted is True
            delete_file = await client_async.files.delete(file_2.id)
            assert delete_file.deleted is True
            deleted_vector_store = await client_async.vector_stores.delete(
                vector_store_id=vector_store.id
            )
            assert deleted_vector_store.deleted is True