Den här sidan innehåller information om autentiseringsmetoder och klienter som stöds, tillsammans med exempelkod för att ansluta dina appar till Azure AI-tjänster med hjälp av Service Connector. På den här sidan visas även standardnamn och värden för miljövariabler som hämtas när du skapar tjänstanslutningen.
Beräkningstjänster som stöds
Service Connector kan användas för att ansluta följande beräkningstjänster till Azure AI Services:
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
Autentiseringstyper och klienttyper som stöds
Tabellen nedan anger vilka kombinationer av autentiseringsmetoder och klienter som stöds för att ansluta din beräkningstjänst till enskilda Azure AI-tjänster med hjälp av Service Connector. Ett "Ja" anger att kombinationen stöds, medan ett "Nej" anger att den inte stöds.
| Klienttyp |
Systemtilldelad hanterad identitet |
Användartilldelad hanterad identitet |
Hemlighet/anslutningssträng |
Service Principal |
| .NET |
Ja |
Ja |
Ja |
Ja |
| Java |
Ja |
Ja |
Ja |
Ja |
| Node.js |
Ja |
Ja |
Ja |
Ja |
| Python |
Ja |
Ja |
Ja |
Ja |
| None |
Ja |
Ja |
Ja |
Ja |
Den här tabellen anger att alla kombinationer av klienttyper och autentiseringsmetoder i tabellen stöds. Alla klienttyper kan använda någon av autentiseringsmetoderna för att ansluta till Azure AI Services med hjälp av Service Connector.
Standardnamn för miljövariabler eller programegenskaper och exempelkod
Använd anslutningsinformationen nedan för att ansluta beräkningstjänster till Azure AI Services. Mer information om namngivningskonventioner finns i artikeln Service Connector internals.
Systemtilldelad hanterad identitet (rekommenderas)
| Standardnamn för miljövariabel |
beskrivning |
Exempelvärde |
| AZURE_AISERVICES_OPENAI_BASE |
Azure OpenAI-slutpunkt |
https://<your-Azure-AI-Services-endpoint>.openai.azure.com/ |
| AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT |
Azure Cognitive Services-tokenprovidertjänst |
https://<your-Azure-AI-Services-endpoint>.cognitiveservices.azure.com/ |
| AZURE_AISERVICES_SPEECH_ENDPOINT |
Api-slutpunkt för tal till text (standard) |
https://<location>.stt.speech.microsoft.com |
Exempelkod
Se stegen och koden nedan för att ansluta till Azure AI Services med hjälp av en systemtilldelad hanterad identitet.
Du kan använda Azure-klientbiblioteket för att få åtkomst till olika kognitiva API:er som Azure AI Services stöder. Vi använder Azure AI-Textanalys som exempel i det här exemplet.
Se Autentisera begäranden till Azure AI-tjänster för att anropa de kognitiva API:erna direkt.
Installera följande beroenden. Vi använder Azure.AI.TextAnalytics som exempel.
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Identity
Autentisera med hjälp av Azure Identity-biblioteket och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
using Azure.AI.TextAnalytics;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
credential);
Lägg till följande beroenden i din pom.xml-fil . Vi använder azure-ai-textanalytics som exempel.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
Autentisera med hjälp av azure-identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_AISERVICES_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_AISERVICES_CLIENTID"))
// .clientSecret(System.getenv("AZURE_AISERVICES_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_AISERVICES_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
- Installera följande beroenden. Vi använder
azure-ai-textanalytics som exempel.
pip install azure-ai-textanalytics==5.1.0
pip install azure-identity
- Autentisera med hjälp av
azure-identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_AISERVICES_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_AISERVICES_TENANTID')
# client_id = os.getenv('AZURE_AISERVICES_CLIENTID')
# client_secret = os.getenv('AZURE_AISERVICES_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv('AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT')
language_service_client = TextAnalyticsClient(
endpoint=endpoint,
credential=cred)
Installera följande beroenden. Vi använder ai-text-analytics som exempel.
npm install @azure/ai-text-analytics@5.1.0
npm install @azure/identity
Autentisera med hjälp av @azure/identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { TextAnalyticsClient } = require("@azure/ai-text-analytics");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_AISERVICES_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_AISERVICES_TENANTID;
// const clientId = process.env.AZURE_AISERVICES_CLIENTID;
// const clientSecret = process.env.AZURE_AISERVICES_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT;
const languageClient = new TextAnalyticsClient(endpoint, credential);
Användartilldelad hanterad identitet
| Standardnamn för miljövariabel |
beskrivning |
Exempelvärde |
| AZURE_AISERVICES_OPENAI_BASE |
Azure OpenAI-slutpunkt |
https://<your-Azure-AI-Services-endpoint>.openai.azure.com/ |
| AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT |
Azure Cognitive Services-tokenprovidertjänst |
https://<your-Azure-AI-Services-endpoint>.cognitiveservices.azure.com/ |
| AZURE_AISERVICES_SPEECH_ENDPOINT |
Api-slutpunkt för tal till text (standard) |
https://<location>.stt.speech.microsoft.com |
| AZURE_AISERVICES_CLIENTID |
Ditt klient-ID |
<client-ID> |
Exempelkod
Se stegen och koden nedan för att ansluta till Azure AI Services med hjälp av en användartilldelad hanterad identitet.
Du kan använda Azure-klientbiblioteket för att få åtkomst till olika kognitiva API:er som Azure AI Services stöder. Vi använder Azure AI-Textanalys som exempel i det här exemplet.
Se Autentisera begäranden till Azure AI-tjänster för att anropa de kognitiva API:erna direkt.
Installera följande beroenden. Vi använder Azure.AI.TextAnalytics som exempel.
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Identity
Autentisera med hjälp av Azure Identity-biblioteket och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
using Azure.AI.TextAnalytics;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
credential);
Lägg till följande beroenden i din pom.xml-fil . Vi använder azure-ai-textanalytics som exempel.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
Autentisera med hjälp av azure-identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_AISERVICES_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_AISERVICES_CLIENTID"))
// .clientSecret(System.getenv("AZURE_AISERVICES_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_AISERVICES_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
- Installera följande beroenden. Vi använder
azure-ai-textanalytics som exempel.
pip install azure-ai-textanalytics==5.1.0
pip install azure-identity
- Autentisera med hjälp av
azure-identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_AISERVICES_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_AISERVICES_TENANTID')
# client_id = os.getenv('AZURE_AISERVICES_CLIENTID')
# client_secret = os.getenv('AZURE_AISERVICES_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv('AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT')
language_service_client = TextAnalyticsClient(
endpoint=endpoint,
credential=cred)
Installera följande beroenden. Vi använder ai-text-analytics som exempel.
npm install @azure/ai-text-analytics@5.1.0
npm install @azure/identity
Autentisera med hjälp av @azure/identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { TextAnalyticsClient } = require("@azure/ai-text-analytics");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_AISERVICES_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_AISERVICES_TENANTID;
// const clientId = process.env.AZURE_AISERVICES_CLIENTID;
// const clientSecret = process.env.AZURE_AISERVICES_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT;
const languageClient = new TextAnalyticsClient(endpoint, credential);
Anslutningssträng
| Standardnamn för miljövariabel |
beskrivning |
Exempelvärde |
| AZURE_AISERVICES_OPENAI_BASE |
Azure OpenAI-slutpunkt |
https://<your-Azure-AI-Services-endpoint>.openai.azure.com/ |
| AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT |
Azure Cognitive Services-tokenprovidertjänst |
https://<your-Azure-AI-Services-endpoint>.cognitiveservices.azure.com/ |
| AZURE_AISERVICES_SPEECH_ENDPOINT |
Api-slutpunkt för tal till text (standard) |
https://<location>.stt.speech.microsoft.com |
| AZURE_AISERVICES_KEY |
Api-nyckel för Azure AI Services |
<api-key> |
Exempelkod
Se stegen och koden nedan för att ansluta till Azure AI Services med hjälp av en anslutningssträng.
Du kan använda Azure-klientbiblioteket för att få åtkomst till olika kognitiva API:er som Azure AI Services stöder. Vi använder Azure AI-Textanalys som exempel i det här exemplet.
Se Autentisera begäranden till Azure AI-tjänster för att anropa de kognitiva API:erna direkt.
Installera följande beroenden. Vi använder Azure.AI.TextAnalytics som exempel.
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Core --version 1.40.0
Hämta Azure AI Services-slutpunkten och nyckeln från miljövariablerna som lagts till av Service Connector.
using Azure.AI.TextAnalytics;
string endpoint = Environment.GetEnvironmentVariable("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT")
string key = Environment.GetEnvironmentVariable("AZURE_AISERVICES_KEY");
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
new AzureKeyCredential(key));
- Lägg till följande beroenden i din pom.xml-fil . Vi använder
azure-ai-textanalytics som exempel.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.49.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
- Hämta Azure AI Services-slutpunkten och nyckeln från miljövariablerna som lagts till av Service Connector.
String endpoint = System.getenv("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
String key = System.getenv("AZURE_AISERVICES_KEY");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildClient();
- Installera följande beroenden. Vi använder
azure-ai-textanalytics som exempel.
pip install azure-ai-textanalytics==5.1.0
pip install azure-core
- Hämta Azure AI Services-slutpunkten och nyckeln från miljövariablerna som lagts till av Service Connector.
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
key = os.environ['AZURE_AISERVICES_KEY']
endpoint = os.environ['AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT']
language_service_client = TextAnalyticsClient(
endpoint=retrieved_endpoint,
credential=AzureKeyCredential(key))
Installera följande beroende. Vi använder ai-text-analytics som exempel.
npm install @azure/ai-text-analytics@5.1.0
Hämta Azure AI Services-slutpunkten och nyckeln från miljövariablerna som lagts till av Service Connector.
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");
const endpoint = process.env.AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT;
const credential = new AzureKeyCredential(process.env.AZURE_AISERVICES_KEY);
const languageClient = new TextAnalyticsClient(endpoint, credential);
Service Principal
| Standardnamn för miljövariabel |
beskrivning |
Exempelvärde |
| AZURE_AISERVICES_OPENAI_BASE |
Azure OpenAI-slutpunkt |
https://<your-Azure-AI-Services-endpoint>.openai.azure.com/ |
| AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT |
Azure Cognitive Services-tokenprovidertjänst |
https://<your-Azure-AI-Services-endpoint>.cognitiveservices.azure.com/ |
| AZURE_AISERVICES_SPEECH_ENDPOINT |
Api-slutpunkt för tal till text (standard) |
https://<location>.stt.speech.microsoft.com |
| AZURE_AISERVICES_CLIENTID |
Ditt klient-ID |
<client-ID> |
| AZURE_AISERVICES_CLIENTSECRET |
Din klienthemlighet |
<client-secret> |
| AZURE_AISERVICES_TENANTID |
Ditt tenant-ID |
<tenant-ID> |
Exempelkod
Se stegen och koden nedan för att ansluta till Azure AI Services med hjälp av en tjänst principaL.
Du kan använda Azure-klientbiblioteket för att få åtkomst till olika kognitiva API:er som Azure AI Services stöder. Vi använder Azure AI-Textanalys som exempel i det här exemplet.
Se Autentisera begäranden till Azure AI-tjänster för att anropa de kognitiva API:erna direkt.
Installera följande beroenden. Vi använder Azure.AI.TextAnalytics som exempel.
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Identity
Autentisera med hjälp av Azure Identity-biblioteket och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
using Azure.AI.TextAnalytics;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
credential);
Lägg till följande beroenden i din pom.xml-fil . Vi använder azure-ai-textanalytics som exempel.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
Autentisera med hjälp av azure-identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_AISERVICES_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_AISERVICES_CLIENTID"))
// .clientSecret(System.getenv("AZURE_AISERVICES_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_AISERVICES_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
- Installera följande beroenden. Vi använder
azure-ai-textanalytics som exempel.
pip install azure-ai-textanalytics==5.1.0
pip install azure-identity
- Autentisera med hjälp av
azure-identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_AISERVICES_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_AISERVICES_TENANTID')
# client_id = os.getenv('AZURE_AISERVICES_CLIENTID')
# client_secret = os.getenv('AZURE_AISERVICES_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv('AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT')
language_service_client = TextAnalyticsClient(
endpoint=endpoint,
credential=cred)
Installera följande beroenden. Vi använder ai-text-analytics som exempel.
npm install @azure/ai-text-analytics@5.1.0
npm install @azure/identity
Autentisera med hjälp av @azure/identity och hämta Azure AI Services-slutpunkten från miljövariablerna som lagts till av Service Connector. När du använder koden nedan avkommentarer du delen av kodfragmentet för den autentiseringstyp som du vill använda.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { TextAnalyticsClient } = require("@azure/ai-text-analytics");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_AISERVICES_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_AISERVICES_TENANTID;
// const clientId = process.env.AZURE_AISERVICES_CLIENTID;
// const clientSecret = process.env.AZURE_AISERVICES_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT;
const languageClient = new TextAnalyticsClient(endpoint, credential);
Relaterat innehåll