|
| 1 | +--- |
| 2 | +author: wchigit |
| 3 | +ms.service: service-connector |
| 4 | +ms.topic: include |
| 5 | +ms.date: 10/20/2023 |
| 6 | +ms.author: wchi |
| 7 | +--- |
| 8 | + |
| 9 | +### [.NET](#tab/dotnet) |
| 10 | + |
| 11 | +1. Install dependencies. |
| 12 | + ```bash |
| 13 | + dotnet add package Azure.AI.OpenAI --prerelease |
| 14 | + dotnet add package Azure.Identity |
| 15 | + ``` |
| 16 | +1. Authenticate using Azure Identity library and get the Azure OpenAI endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 17 | + |
| 18 | + ```csharp |
| 19 | + using Azure.AI.OpenAI; |
| 20 | + using Azure.Identity; |
| 21 | + |
| 22 | + string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_BASE"); |
| 23 | + |
| 24 | + // Uncomment the following lines according to the authentication type. |
| 25 | + // system-assigned managed identity |
| 26 | + // var credential = new DefaultAzureCredential(); |
| 27 | + |
| 28 | + // user-assigned managed identity |
| 29 | + // var credential = new DefaultAzureCredential( |
| 30 | + // new DefaultAzureCredentialOptions |
| 31 | + // { |
| 32 | + // ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID"); |
| 33 | + // }); |
| 34 | + |
| 35 | + // service principal |
| 36 | + // var tenantId = Environment.GetEnvironmentVariable("AZURE_OPENAI_TENANTID"); |
| 37 | + // var clientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID"); |
| 38 | + // var clientSecret = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTSECRET"); |
| 39 | + // var credential = new ClientSecretCredential(tenantId, clientId, clientSecret); |
| 40 | + |
| 41 | + AzureOpenAIClient openAIClient = new( |
| 42 | + new Uri(endpoint), |
| 43 | + credential |
| 44 | + ); |
| 45 | + ``` |
| 46 | + |
| 47 | +### [Java](#tab/java) |
| 48 | +
|
| 49 | +1. Add the following dependencies in your *pom.xml* file: |
| 50 | + ```xml |
| 51 | + <dependency> |
| 52 | + <groupId>com.azure</groupId> |
| 53 | + <artifactId>azure-ai-openai</artifactId> |
| 54 | + <version>1.0.0-beta.6</version> |
| 55 | + </dependency> |
| 56 | + <dependency> |
| 57 | + <groupId>com.azure</groupId> |
| 58 | + <artifactId>azure-identity</artifactId> |
| 59 | + <version>1.11.4</version> |
| 60 | + </dependency> |
| 61 | + ``` |
| 62 | +1. Authenticate using `azure-identity` and get the Azure OpenAI endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 63 | +
|
| 64 | + ```java |
| 65 | + // Uncomment the following lines according to the authentication type. |
| 66 | + // for system-managed identity |
| 67 | + // DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build(); |
| 68 | +
|
| 69 | + // for user-assigned managed identity |
| 70 | + // DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder() |
| 71 | + // .managedIdentityClientId(System.getenv("AZURE_OPENAI_CLIENTID")) |
| 72 | + // .build(); |
| 73 | +
|
| 74 | + // for service principal |
| 75 | + // ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder() |
| 76 | + // .clientId(System.getenv("AZURE_OPENAI_CLIENTID")) |
| 77 | + // .clientSecret(System.getenv("AZURE_OPENAI_CLIENTSECRET")) |
| 78 | + // .tenantId(System.getenv("AZURE_OPENAI_TENANTID")) |
| 79 | + // .build(); |
| 80 | + |
| 81 | + String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT"); |
| 82 | + |
| 83 | + OpenAIClient client = new OpenAIClientBuilder() |
| 84 | + .credential(credential) |
| 85 | + .endpoint(endpoint) |
| 86 | + .buildClient(); |
| 87 | + ``` |
| 88 | +
|
| 89 | +### [Python](#tab/python) |
| 90 | +
|
| 91 | +1. Install dependencies. |
| 92 | + ```bash |
| 93 | + pip install azure-OPENAI |
| 94 | + pip install azure-identity |
| 95 | + ``` |
| 96 | +1. Authenticate using `azure-identity` and get the Azure App Configuration endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 97 | + ```python |
| 98 | + import os |
| 99 | + from azure.OPENAI import AzureOPENAIClient |
| 100 | + from azure.identity import ManagedIdentityCredential, ClientSecretCredential |
| 101 | + |
| 102 | + # Uncomment the following lines according to the authentication type. |
| 103 | + # system-assigned managed identity |
| 104 | + # cred = ManagedIdentityCredential() |
| 105 | + |
| 106 | + # user-assigned managed identity |
| 107 | + # managed_identity_client_id = os.getenv('AZURE_OPENAI_CLIENTID') |
| 108 | + # cred = ManagedIdentityCredential(client_id=managed_identity_client_id) |
| 109 | + |
| 110 | + # service principal |
| 111 | + # tenant_id = os.getenv('AZURE_OPENAI_TENANTID') |
| 112 | + # client_id = os.getenv('AZURE_OPENAI_CLIENTID') |
| 113 | + # client_secret = os.getenv('AZURE_OPENAI_CLIENTSECRET') |
| 114 | + # cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret) |
| 115 | +
|
| 116 | + endpoint_url = os.getenv('AZURE_OPENAI_ENDPOINT') |
| 117 | +
|
| 118 | + client = AzureOPENAIClient(base_url="your_endpoint_url", credential=credential) |
| 119 | + ``` |
| 120 | +
|
| 121 | +### [NodeJS](#tab/nodejs) |
| 122 | +
|
| 123 | +1. Install dependencies. |
| 124 | + ```bash |
| 125 | + npm install --save @azure/identity |
| 126 | + npm install @azure/app-configuration |
| 127 | + ``` |
| 128 | +1. Authenticate using `@azure/identity` and get the Azure App Configuration endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 129 | + |
| 130 | + ```javascript |
| 131 | + import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity"; |
| 132 | + const appConfig = require("@azure/app-configuration"); |
| 133 | + |
| 134 | + // Uncomment the following lines according to the authentication type. |
| 135 | + // for system-assigned managed identity |
| 136 | + // const credential = new DefaultAzureCredential(); |
| 137 | + |
| 138 | + // for user-assigned managed identity |
| 139 | + // const clientId = process.env.AZURE_OPENAI_CLIENTID; |
| 140 | + // const credential = new DefaultAzureCredential({ |
| 141 | + // managedIdentityClientId: clientId |
| 142 | + // }); |
| 143 | + |
| 144 | + // for service principal |
| 145 | + // const tenantId = process.env.AZURE_OPENAI_TENANTID; |
| 146 | + // const clientId = process.env.AZURE_OPENAI_CLIENTID; |
| 147 | + // const clientSecret = process.env.AZURE_OPENAI_CLIENTSECRET; |
| 148 | + // const credential = new ClientSecretCredential(tenantId, clientId, clientSecret); |
| 149 | + |
| 150 | + const endpoint = process.env.AZURE_OPENAI_ENDPOINT; |
| 151 | +
|
| 152 | + const client = new appConfig.OPENAIClient( |
| 153 | + endpoint, |
| 154 | + credential |
| 155 | + ); |
| 156 | + ``` |
| 157 | +
|
| 158 | +### [Other](#tab/none) |
| 159 | +For other languages, you can use the connection information that Service Connector sets to the environment variables to connect to Azure OpenAI. For environment variable details, see [Integrate Azure OpenAI with Service Connector](../how-to-integrate-openai.md). |
0 commit comments