File: sample_connections_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 (72 lines) | stat: -rw-r--r-- 2,748 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
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
DESCRIPTION:
    Given an AIProjectClient, this sample demonstrates how to use the asynchronous
    `.connections` methods to enumerate the properties of all connections
    and get the properties of a connection by its name.

USAGE:
    python sample_connections_async.py

    Before running the sample:

    pip install azure-ai-projects azure-identity aiohttp

    Set these environment variables with your own values:
    1) PROJECT_ENDPOINT - Required. The Azure AI Project endpoint, as found in the overview page of your
       Azure AI Foundry project.
    2) CONNECTION_NAME - The name of a connection, as found in the "Connected resources" tab
       in the Management Center of your AI Foundry project.
"""

import asyncio
import os
from azure.identity.aio import DefaultAzureCredential
from azure.ai.projects.aio import AIProjectClient
from azure.ai.projects.models import ConnectionType


async def main() -> None:

    endpoint = os.environ["PROJECT_ENDPOINT"]
    connection_name = os.environ["CONNECTION_NAME"]

    async with DefaultAzureCredential() as credential:

        async with AIProjectClient(endpoint=endpoint, credential=credential) as project_client:

            print("List all connections:")
            async for connection in project_client.connections.list():
                print(connection)

            print("List all connections of a particular type:")
            async for connection in project_client.connections.list(
                connection_type=ConnectionType.AZURE_OPEN_AI,
            ):
                print(connection)

            print("Get the default connection of a particular type, without its credentials:")
            connection = await project_client.connections.get_default(connection_type=ConnectionType.AZURE_OPEN_AI)
            print(connection)

            print("Get the default connection of a particular type, with its credentials:")
            connection = await project_client.connections.get_default(
                connection_type=ConnectionType.AZURE_OPEN_AI, include_credentials=True
            )
            print(connection)

            print(f"Get the connection named `{connection_name}`, without its credentials:")
            connection = await project_client.connections.get(connection_name)
            print(connection)

            print(f"Get the connection named `{connection_name}`, with its credentials:")
            connection = await project_client.connections.get(connection_name, include_credentials=True)
            print(connection)


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