File: sample_files_async.py

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (73 lines) | stat: -rw-r--r-- 2,634 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
# pylint: disable=line-too-long,useless-suppression
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
DESCRIPTION:
    Given an AIProjectClient, this sample demonstrates how to use the asynchronous
    file operations using the OpenAI client: create, wait_for_processing, retrieve, content, list, and delete.

USAGE:
    python sample_files_async.py

    Before running the sample:

    pip install azure-ai-projects>=2.0.0b1 azure-identity openai python-dotenv aiohttp

    Set these environment variables with your own values:
    1) AZURE_AI_PROJECT_ENDPOINT - Required. The Azure AI Project endpoint, as found in the overview page of your
       Microsoft Foundry portal.
    2) FILE_PATH - Optional. Path to the file to upload. Defaults to the `data` folder.
"""

import asyncio
import os
from dotenv import load_dotenv
from azure.identity.aio import DefaultAzureCredential
from azure.ai.projects.aio import AIProjectClient
from pathlib import Path

load_dotenv()

endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"]
script_dir = Path(__file__).parent
file_path = os.environ.get("FILE_PATH", os.path.join(script_dir, "data", "test_file.jsonl"))


async def main() -> None:
    async with (
        DefaultAzureCredential() as credential,
        AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
        project_client.get_openai_client() as openai_client,
    ):

        print("Uploading file")
        with open(file_path, "rb") as f:
            uploaded_file = await openai_client.files.create(file=f, purpose="fine-tune")
        print(uploaded_file)

        print("Waits for the given file to be processed, default timeout is 30 mins")
        processed_file = await openai_client.files.wait_for_processing(uploaded_file.id)
        print(processed_file)

        print(f"Retrieving file metadata with ID: {processed_file.id}")
        retrieved_file = await openai_client.files.retrieve(processed_file.id)
        print(retrieved_file)

        print(f"Retrieving file content with ID: {processed_file.id}")
        file_content = await openai_client.files.content(processed_file.id)
        print(file_content.content)

        print("Listing all files:")
        async for file in openai_client.files.list():
            print(file)

        print(f"Deleting file with ID: {processed_file.id}")
        deleted_file = await openai_client.files.delete(processed_file.id)
        print(f"Successfully deleted file: {deleted_file.id}")


if __name__ == "__main__":
    asyncio.run(main())