参考文档 | 更多示例 | 包 (NuGet) | 库源代码
借助本快速入门,使用 .NET 客户端库创建实体链接应用程序。 在以下示例中,你将创建一个 C# 应用程序,该应用程序可以识别和消除文本中实体的歧义。
提示
可以使用 Language Studio 来试用语言服务功能,而无需编写代码。
- Azure 订阅 - 创建试用订阅
- Visual Studio IDE
若要使用下面的代码示例,需要部署 Azure 资源。 此资源将包含一个密钥和终结点,用于对发送到语言服务的 API 调用进行身份验证。
使用以下链接通过 Azure 门户创建语言资源。 需要使用 Azure 订阅登录。
在显示的“选择其他功能”屏幕上,选择“继续创建资源”。
在“创建语言”屏幕中,提供以下信息:
详细信息 说明 订阅 资源将与之关联的订阅帐户。 从下拉菜单选择 Azure 订阅。 资源组 资源组是存储所创建资源的容器。 选择“新建”来创建一个新的资源组。 区域 语言资源的位置。 不同区域可能会导致延迟,具体取决于你的物理位置,但不会影响资源的运行时可用性。 对于本快速入门,请选择你附近的可用区域,或选择“中国东部 2”。 名称 语言资源的名称。 此名称还将用于创建应用程序用于发送 API 请求的终结点 URL。 定价层 语言资源的定价层。 可以使用免费 F0 层试用该服务,然后再升级到付费层进行生产。 确保选中“负责任 AI 通知”复选框。
在页面底部选择“查看 + 创建” 。
在显示的屏幕中,确保验证已通过,并且已正确输入信息。 然后选择“创建”。
然后需要从资源获取密钥和终结点,以便将应用程序连接到 API。 稍后需要在本快速入门中将密钥和终结点粘贴到代码中。
应用程序必须经过身份验证才能发送 API 请求。 对于生产,请使用安全的方式存储和访问凭据。 在此示例中,你将凭据写入运行应用程序的本地计算机上的环境变量。
若要为语言资源密钥设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
LANGUAGE_KEY
环境变量,请将your-key
替换为资源的其中一个密钥。 - 若要设置
LANGUAGE_ENDPOINT
环境变量,请将your-endpoint
替换为资源的终结点。
重要
如果使用 API 密钥,请将其安全地存储在某个其他位置,例如 Azure Key Vault 中。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。
有关 Azure AI 服务安全性的详细信息,请参阅对 Azure AI 服务的请求进行身份验证。
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
添加环境变量后,请从控制台窗口运行 source ~/.bashrc
,使更改生效。
使用 Visual Studio IDE 创建新的 .NET Core 控制台应用。 这会创建包含单个 C# 源文件的“Hello World”项目:program.cs。
右键单击解决方案资源管理器中的解决方案,然后选择“管理 NuGet 包”,以便安装客户端库。 在打开的包管理器中选择“浏览”,搜索 Azure.AI.TextAnalytics
。 选择版本 5.2.0
,然后选择“安装”。 也可使用包管理器控制台。
将以下代码复制到 program.cs 文件中,然后运行代码。
using Azure;
using System;
using System.Globalization;
using Azure.AI.TextAnalytics;
namespace EntityLinkingExample
{
class Program
{
// This example requires environment variables named "LANGUAGE_KEY" and "LANGUAGE_ENDPOINT"
static string languageKey = Environment.GetEnvironmentVariable("LANGUAGE_KEY");
static string languageEndpoint = Environment.GetEnvironmentVariable("LANGUAGE_ENDPOINT");
private static readonly AzureKeyCredential credentials = new AzureKeyCredential(languageKey);
private static readonly Uri endpoint = new Uri(languageEndpoint);
// Example method for recognizing entities and providing a link to an online data source.
static void EntityLinkingExample(TextAnalyticsClient client)
{
var response = client.RecognizeLinkedEntities(
"Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, " +
"to develop and sell BASIC interpreters for the Altair 8800. " +
"During his career at Microsoft, Gates held the positions of chairman, " +
"chief executive officer, president and chief software architect, " +
"while also being the largest individual shareholder until May 2014.");
Console.WriteLine("Linked Entities:");
foreach (var entity in response.Value)
{
Console.WriteLine($"\tName: {entity.Name},\tID: {entity.DataSourceEntityId},\tURL: {entity.Url}\tData Source: {entity.DataSource}");
Console.WriteLine("\tMatches:");
foreach (var match in entity.Matches)
{
Console.WriteLine($"\t\tText: {match.Text}");
Console.WriteLine($"\t\tScore: {match.ConfidenceScore:F2}\n");
}
}
}
static void Main(string[] args)
{
var client = new TextAnalyticsClient(endpoint, credentials);
EntityLinkingExample(client);
Console.Write("Press any key to exit.");
Console.ReadKey();
}
}
}
Linked Entities:
Name: Microsoft, ID: Microsoft, URL: https://en.wikipedia.org/wiki/Microsoft Data Source: Wikipedia
Matches:
Text: Microsoft
Score: 0.55
Text: Microsoft
Score: 0.55
Name: Bill Gates, ID: Bill Gates, URL: https://en.wikipedia.org/wiki/Bill_Gates Data Source: Wikipedia
Matches:
Text: Bill Gates
Score: 0.63
Text: Gates
Score: 0.63
Name: Paul Allen, ID: Paul Allen, URL: https://en.wikipedia.org/wiki/Paul_Allen Data Source: Wikipedia
Matches:
Text: Paul Allen
Score: 0.60
Name: April 4, ID: April 4, URL: https://en.wikipedia.org/wiki/April_4 Data Source: Wikipedia
Matches:
Text: April 4
Score: 0.32
Name: BASIC, ID: BASIC, URL: https://en.wikipedia.org/wiki/BASIC Data Source: Wikipedia
Matches:
Text: BASIC
Score: 0.33
Name: Altair 8800, ID: Altair 8800, URL: https://en.wikipedia.org/wiki/Altair_8800 Data Source: Wikipedia
Matches:
Text: Altair 8800
Score: 0.88
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
参考文档 | 更多示例 | 包 (Maven) | 库源代码
借助本快速入门,使用 Java 客户端库创建实体链接应用程序。 在以下示例中,你将创建一个 Java 应用程序,该应用程序可以识别和消除文本中实体的歧义。
- Azure 订阅 - 创建试用订阅
- Java 开发工具包 (JDK) 版本 8 或更高版本
若要使用下面的代码示例,需要部署 Azure 资源。 此资源将包含一个密钥和终结点,用于对发送到语言服务的 API 调用进行身份验证。
使用以下链接通过 Azure 门户创建语言资源。 需要使用 Azure 订阅登录。
在显示的“选择其他功能”屏幕上,选择“继续创建资源”。
在“创建语言”屏幕中,提供以下信息:
详细信息 说明 订阅 资源将与之关联的订阅帐户。 从下拉菜单选择 Azure 订阅。 资源组 资源组是存储所创建资源的容器。 选择“新建”来创建一个新的资源组。 区域 语言资源的位置。 不同区域可能会导致延迟,具体取决于你的物理位置,但不会影响资源的运行时可用性。 对于本快速入门,请选择你附近的可用区域,或选择“中国东部 2”。 名称 语言资源的名称。 此名称还将用于创建应用程序用于发送 API 请求的终结点 URL。 定价层 语言资源的定价层。 可以使用免费 F0 层试用该服务,然后再升级到付费层进行生产。 确保选中“负责任 AI 通知”复选框。
在页面底部选择“查看 + 创建” 。
在显示的屏幕中,确保验证已通过,并且已正确输入信息。 然后选择“创建”。
然后需要从资源获取密钥和终结点,以便将应用程序连接到 API。 稍后需要在本快速入门中将密钥和终结点粘贴到代码中。
应用程序必须经过身份验证才能发送 API 请求。 对于生产,请使用安全的方式存储和访问凭据。 在此示例中,你将凭据写入运行应用程序的本地计算机上的环境变量。
若要为语言资源密钥设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
LANGUAGE_KEY
环境变量,请将your-key
替换为资源的其中一个密钥。 - 若要设置
LANGUAGE_ENDPOINT
环境变量,请将your-endpoint
替换为资源的终结点。
重要
如果使用 API 密钥,请将其安全地存储在某个其他位置,例如 Azure Key Vault 中。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。
有关 Azure AI 服务安全性的详细信息,请参阅对 Azure AI 服务的请求进行身份验证。
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
添加环境变量后,请从控制台窗口运行 source ~/.bashrc
,使更改生效。
在首选 IDE 或开发环境中创建 Maven 项目。 然后在项目的 pom.xml 文件中,添加以下依赖项。 可联机找到用于其他生成工具的实现语法。
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
创建名为 Example.java
的 Java 文件。 打开该文件,并复制以下代码。 然后运行代码。
import com.azure.core.credential.AzureKeyCredential;
import com.azure.ai.textanalytics.models.*;
import com.azure.ai.textanalytics.TextAnalyticsClientBuilder;
import com.azure.ai.textanalytics.TextAnalyticsClient;
public class Example {
// This example requires environment variables named "LANGUAGE_KEY" and "LANGUAGE_ENDPOINT"
private static String languageKey = System.getenv("LANGUAGE_KEY");
private static String languageEndpoint = System.getenv("LANGUAGE_ENDPOINT");
public static void main(String[] args) {
TextAnalyticsClient client = authenticateClient(languageKey, languageEndpoint);
recognizeLinkedEntitiesExample(client);
}
// Method to authenticate the client object with your key and endpoint
static TextAnalyticsClient authenticateClient(String key, String endpoint) {
return new TextAnalyticsClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildClient();
}
// Example method for recognizing entities and providing a link to an online data source
static void recognizeLinkedEntitiesExample(TextAnalyticsClient client)
{
// The text that need be analyzed.
String text = "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, " +
"to develop and sell BASIC interpreters for the Altair 8800. " +
"During his career at Microsoft, Gates held the positions of chairman, " +
"chief executive officer, president and chief software architect, " +
"while also being the largest individual shareholder until May 2014.";
System.out.printf("Linked Entities:%n");
for (LinkedEntity linkedEntity : client.recognizeLinkedEntities(text)) {
System.out.printf("Name: %s, ID: %s, URL: %s, Data Source: %s.%n",
linkedEntity.getName(),
linkedEntity.getDataSourceEntityId(),
linkedEntity.getUrl(),
linkedEntity.getDataSource());
System.out.printf("Matches:%n");
for (LinkedEntityMatch linkedEntityMatch : linkedEntity.getMatches()) {
System.out.printf("Text: %s, Score: %.2f, Offset: %s, Length: %s%n",
linkedEntityMatch.getText(),
linkedEntityMatch.getConfidenceScore(),
linkedEntityMatch.getOffset(),
linkedEntityMatch.getLength());
}
}
}
}
Linked Entities:
Name: Microsoft, ID: Microsoft, URL: https://en.wikipedia.org/wiki/Microsoft, Data Source: Wikipedia.
Matches:
Text: Microsoft, Score: 0.55, Offset: 0, Length: 9
Text: Microsoft, Score: 0.55, Offset: 150, Length: 9
Name: Bill Gates, ID: Bill Gates, URL: https://en.wikipedia.org/wiki/Bill_Gates, Data Source: Wikipedia.
Matches:
Text: Bill Gates, Score: 0.63, Offset: 25, Length: 10
Text: Gates, Score: 0.63, Offset: 161, Length: 5
Name: Paul Allen, ID: Paul Allen, URL: https://en.wikipedia.org/wiki/Paul_Allen, Data Source: Wikipedia.
Matches:
Text: Paul Allen, Score: 0.60, Offset: 40, Length: 10
Name: April 4, ID: April 4, URL: https://en.wikipedia.org/wiki/April_4, Data Source: Wikipedia.
Matches:
Text: April 4, Score: 0.32, Offset: 54, Length: 7
Name: BASIC, ID: BASIC, URL: https://en.wikipedia.org/wiki/BASIC, Data Source: Wikipedia.
Matches:
Text: BASIC, Score: 0.33, Offset: 89, Length: 5
Name: Altair 8800, ID: Altair 8800, URL: https://en.wikipedia.org/wiki/Altair_8800, Data Source: Wikipedia.
Matches:
Text: Altair 8800, Score: 0.88, Offset: 116, Length: 11
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
借助本快速入门,使用 Node.js 客户端库创建实体链接应用程序。 在以下示例中,你将创建一个 JavaScript 应用程序,该应用程序可以识别和消除文本中实体的歧义。
若要使用下面的代码示例,需要部署 Azure 资源。 此资源将包含一个密钥和终结点,用于对发送到语言服务的 API 调用进行身份验证。
使用以下链接通过 Azure 门户创建语言资源。 需要使用 Azure 订阅登录。
在显示的“选择其他功能”屏幕上,选择“继续创建资源”。
在“创建语言”屏幕中,提供以下信息:
详细信息 说明 订阅 资源将与之关联的订阅帐户。 从下拉菜单选择 Azure 订阅。 资源组 资源组是存储所创建资源的容器。 选择“新建”来创建一个新的资源组。 区域 语言资源的位置。 不同区域可能会导致延迟,具体取决于你的物理位置,但不会影响资源的运行时可用性。 对于本快速入门,请选择你附近的可用区域,或选择“中国东部 2”。 名称 语言资源的名称。 此名称还将用于创建应用程序用于发送 API 请求的终结点 URL。 定价层 语言资源的定价层。 可以使用免费 F0 层试用该服务,然后再升级到付费层进行生产。 确保选中“负责任 AI 通知”复选框。
在页面底部选择“查看 + 创建” 。
在显示的屏幕中,确保验证已通过,并且已正确输入信息。 然后选择“创建”。
然后需要从资源获取密钥和终结点,以便将应用程序连接到 API。 稍后需要在本快速入门中将密钥和终结点粘贴到代码中。
应用程序必须经过身份验证才能发送 API 请求。 对于生产,请使用安全的方式存储和访问凭据。 在此示例中,你将凭据写入运行应用程序的本地计算机上的环境变量。
若要为语言资源密钥设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
LANGUAGE_KEY
环境变量,请将your-key
替换为资源的其中一个密钥。 - 若要设置
LANGUAGE_ENDPOINT
环境变量,请将your-endpoint
替换为资源的终结点。
重要
如果使用 API 密钥,请将其安全地存储在某个其他位置,例如 Azure Key Vault 中。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。
有关 Azure AI 服务安全性的详细信息,请参阅对 Azure AI 服务的请求进行身份验证。
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
添加环境变量后,请从控制台窗口运行 source ~/.bashrc
,使更改生效。
在控制台窗口(例如 cmd、PowerShell 或 Bash)中,为应用创建一个新目录并导航到该目录。
mkdir myapp
cd myapp
运行 npm init
命令以使用 package.json
文件创建一个 node 应用程序。
npm init
安装 npm 包:
npm install @azure/ai-language-text
打开该文件,并复制以下代码。 然后运行代码。
"use strict";
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");
// This example requires environment variables named "LANGUAGE_KEY" and "LANGUAGE_ENDPOINT"
const key = process.env.LANGUAGE_KEY;
const endpoint = process.env.LANGUAGE_ENDPOINT;
//example sentence for recognizing entities
const documents = ["Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975."];
//example of how to use the client to perform entity linking on a document
async function main() {
console.log("== Entity linking sample ==");
const client = new TextAnalysisClient(endpoint, new AzureKeyCredential(key));
const results = await client.analyze("EntityLinking", documents);
for (const result of results) {
console.log(`- Document ${result.id}`);
if (!result.error) {
console.log("\tEntities:");
for (const entity of result.entities) {
console.log(
`\t- Entity ${entity.name}; link ${entity.url}; datasource: ${entity.dataSource}`
);
console.log("\t\tMatches:");
for (const match of entity.matches) {
console.log(
`\t\t- Entity appears as "${match.text}" (confidence: ${match.confidenceScore}`
);
}
}
} else {
console.error(" Error:", result.error);
}
}
}
//call the main function
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
== Entity linking sample ==
- Document 0
Entities:
- Entity Microsoft; link https://en.wikipedia.org/wiki/Microsoft; datasource: Wikipedia
Matches:
- Entity appears as "Microsoft" (confidence: 0.48
- Entity Bill Gates; link https://en.wikipedia.org/wiki/Bill_Gates; datasource: Wikipedia
Matches:
- Entity appears as "Bill Gates" (confidence: 0.52
- Entity Paul Allen; link https://en.wikipedia.org/wiki/Paul_Allen; datasource: Wikipedia
Matches:
- Entity appears as "Paul Allen" (confidence: 0.54
- Entity April 4; link https://en.wikipedia.org/wiki/April_4; datasource: Wikipedia
Matches:
- Entity appears as "April 4" (confidence: 0.38
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
借助本快速入门,使用 Python 客户端库创建实体链接应用程序。 在以下示例中,你将创建一个 Python 应用程序,该应用程序可以识别和消除文本中实体的歧义。
- Azure 订阅 - 创建试用订阅
- Python 3.7 或更高版本
若要使用下面的代码示例,需要部署 Azure 资源。 此资源将包含一个密钥和终结点,用于对发送到语言服务的 API 调用进行身份验证。
使用以下链接通过 Azure 门户创建语言资源。 需要使用 Azure 订阅登录。
在显示的“选择其他功能”屏幕上,选择“继续创建资源”。
在“创建语言”屏幕中,提供以下信息:
详细信息 说明 订阅 资源将与之关联的订阅帐户。 从下拉菜单选择 Azure 订阅。 资源组 资源组是存储所创建资源的容器。 选择“新建”来创建一个新的资源组。 区域 语言资源的位置。 不同区域可能会导致延迟,具体取决于你的物理位置,但不会影响资源的运行时可用性。 对于本快速入门,请选择你附近的可用区域,或选择“中国东部 2”。 名称 语言资源的名称。 此名称还将用于创建应用程序用于发送 API 请求的终结点 URL。 定价层 语言资源的定价层。 可以使用免费 F0 层试用该服务,然后再升级到付费层进行生产。 确保选中“负责任 AI 通知”复选框。
在页面底部选择“查看 + 创建” 。
在显示的屏幕中,确保验证已通过,并且已正确输入信息。 然后选择“创建”。
然后需要从资源获取密钥和终结点,以便将应用程序连接到 API。 稍后需要在本快速入门中将密钥和终结点粘贴到代码中。
应用程序必须经过身份验证才能发送 API 请求。 对于生产,请使用安全的方式存储和访问凭据。 在此示例中,你将凭据写入运行应用程序的本地计算机上的环境变量。
若要为语言资源密钥设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
LANGUAGE_KEY
环境变量,请将your-key
替换为资源的其中一个密钥。 - 若要设置
LANGUAGE_ENDPOINT
环境变量,请将your-endpoint
替换为资源的终结点。
重要
如果使用 API 密钥,请将其安全地存储在某个其他位置,例如 Azure Key Vault 中。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。
有关 Azure AI 服务安全性的详细信息,请参阅对 Azure AI 服务的请求进行身份验证。
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
添加环境变量后,请从控制台窗口运行 source ~/.bashrc
,使更改生效。
在安装 Python 后,可以通过以下命令安装客户端库:
pip install azure-ai-textanalytics==5.2.0
创建新的 Python 文件,并复制以下代码。 然后运行代码。
# This example requires environment variables named "LANGUAGE_KEY" and "LANGUAGE_ENDPOINT"
language_key = os.environ.get('LANGUAGE_KEY')
language_endpoint = os.environ.get('LANGUAGE_ENDPOINT')
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
# Authenticate the client using your key and endpoint.
def authenticate_client():
ta_credential = AzureKeyCredential(language_key)
text_analytics_client = TextAnalyticsClient(
endpoint=language_endpoint,
credential=ta_credential)
return text_analytics_client
client = authenticate_client()
# Example function for recognizing entities and providing a link to an online data source.
def entity_linking_example(client):
try:
documents = ["""Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975,
to develop and sell BASIC interpreters for the Altair 8800.
During his career at Microsoft, Gates held the positions of chairman,
chief executive officer, president and chief software architect,
while also being the largest individual shareholder until May 2014."""]
result = client.recognize_linked_entities(documents = documents)[0]
print("Linked Entities:\n")
for entity in result.entities:
print("\tName: ", entity.name, "\tId: ", entity.data_source_entity_id, "\tUrl: ", entity.url,
"\n\tData Source: ", entity.data_source)
print("\tMatches:")
for match in entity.matches:
print("\t\tText:", match.text)
print("\t\tConfidence Score: {0:.2f}".format(match.confidence_score))
print("\t\tOffset: {}".format(match.offset))
print("\t\tLength: {}".format(match.length))
except Exception as err:
print("Encountered exception. {}".format(err))
entity_linking_example(client)
Linked Entities:
Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
Data Source: Wikipedia
Matches:
Text: Microsoft
Confidence Score: 0.55
Offset: 0
Length: 9
Text: Microsoft
Confidence Score: 0.55
Offset: 168
Length: 9
Name: Bill Gates Id: Bill Gates Url: https://en.wikipedia.org/wiki/Bill_Gates
Data Source: Wikipedia
Matches:
Text: Bill Gates
Confidence Score: 0.63
Offset: 25
Length: 10
Text: Gates
Confidence Score: 0.63
Offset: 179
Length: 5
Name: Paul Allen Id: Paul Allen Url: https://en.wikipedia.org/wiki/Paul_Allen
Data Source: Wikipedia
Matches:
Text: Paul Allen
Confidence Score: 0.60
Offset: 40
Length: 10
Name: April 4 Id: April 4 Url: https://en.wikipedia.org/wiki/April_4
Data Source: Wikipedia
Matches:
Text: BASIC
Confidence Score: 0.33
Offset: 98
Length: 5
Name: Altair 8800 Id: Altair 8800 Url: https://en.wikipedia.org/wiki/Altair_8800
Data Source: Wikipedia
Matches:
Text: Altair 8800
Confidence Score: 0.88
Offset: 125
Length: 11
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。
借助本快速入门,使用 REST API 发送实体链接请求。 在以下示例中,你将使用 cURL 来识别和消除文本中实体的歧义。
- Azure 订阅 - 创建试用订阅
若要使用下面的代码示例,需要部署 Azure 资源。 此资源将包含一个密钥和终结点,用于对发送到语言服务的 API 调用进行身份验证。
使用以下链接通过 Azure 门户创建语言资源。 需要使用 Azure 订阅登录。
在显示的“选择其他功能”屏幕上,选择“继续创建资源”。
在“创建语言”屏幕中,提供以下信息:
详细信息 说明 订阅 资源将与之关联的订阅帐户。 从下拉菜单选择 Azure 订阅。 资源组 资源组是存储所创建资源的容器。 选择“新建”来创建一个新的资源组。 区域 语言资源的位置。 不同区域可能会导致延迟,具体取决于你的物理位置,但不会影响资源的运行时可用性。 对于本快速入门,请选择你附近的可用区域,或选择“中国东部 2”。 名称 语言资源的名称。 此名称还将用于创建应用程序用于发送 API 请求的终结点 URL。 定价层 语言资源的定价层。 可以使用免费 F0 层试用该服务,然后再升级到付费层进行生产。 确保选中“负责任 AI 通知”复选框。
在页面底部选择“查看 + 创建” 。
在显示的屏幕中,确保验证已通过,并且已正确输入信息。 然后选择“创建”。
然后需要从资源获取密钥和终结点,以便将应用程序连接到 API。 稍后需要在本快速入门中将密钥和终结点粘贴到代码中。
应用程序必须经过身份验证才能发送 API 请求。 对于生产,请使用安全的方式存储和访问凭据。 在此示例中,你将凭据写入运行应用程序的本地计算机上的环境变量。
若要为语言资源密钥设置环境变量,请打开控制台窗口,并按照操作系统和开发环境的说明进行操作。
- 若要设置
LANGUAGE_KEY
环境变量,请将your-key
替换为资源的其中一个密钥。 - 若要设置
LANGUAGE_ENDPOINT
环境变量,请将your-endpoint
替换为资源的终结点。
重要
如果使用 API 密钥,请将其安全地存储在某个其他位置,例如 Azure Key Vault 中。 请不要直接在代码中包含 API 密钥,并且切勿公开发布该密钥。
有关 Azure AI 服务安全性的详细信息,请参阅对 Azure AI 服务的请求进行身份验证。
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
添加环境变量后,请从控制台窗口运行 source ~/.bashrc
,使更改生效。
在代码编辑器中,创建一个名为 test_entitylinking_payload.json
的新文件并复制以下 JSON 示例。 在下一步中,此示例请求将发送到 API。
{
"kind": "EntityLinking",
"parameters": {
"modelVersion": "latest"
},
"analysisInput":{
"documents":[
{
"id":"1",
"language":"en",
"text": "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975."
}
]
}
}
将 test_entitylinking_payload.json
保存在计算机上的某个位置。 例如,桌面。
使用以下命令通过所使用的程序发送 API 请求。 将命令复制到终端并运行。
参数 (parameter) | 说明 |
---|---|
-X POST <endpoint> |
指定用于访问 API 的终结点。 |
-H Content-Type: application/json |
用于发送 JSON 数据的内容类型。 |
-H "Ocp-Apim-Subscription-Key:<key> |
指定用于访问 API 的密钥。 |
-d <documents> |
包含要发送的文档的 JSON。 |
使用以下命令通过所使用的程序发送 API 请求。 将 /home/mydir/test_entitylinking_payload.json
替换为在上一步中创建的示例 JSON 请求文件的位置。
curl -X POST $LANGUAGE_ENDPOINT/language/:analyze-text?api-version=2022-05-01 \
-H "Content-Type: application/json" \
-H "Ocp-Apim-Subscription-Key: $LANGUAGE_KEY" \
-d "@/home/mydir/test_entitylinking_payload.json"
{
"kind": "EntityLinkingResults",
"results": {
"documents": [{
"id": "1",
"entities": [{
"bingId": "a093e9b9-90f5-a3d5-c4b8-5855e1b01f85",
"name": "Microsoft",
"matches": [{
"text": "Microsoft",
"offset": 0,
"length": 9,
"confidenceScore": 0.48
}],
"language": "en",
"id": "Microsoft",
"url": "https://en.wikipedia.org/wiki/Microsoft",
"dataSource": "Wikipedia"
}, {
"bingId": "0d47c987-0042-5576-15e8-97af601614fa",
"name": "Bill Gates",
"matches": [{
"text": "Bill Gates",
"offset": 25,
"length": 10,
"confidenceScore": 0.52
}],
"language": "en",
"id": "Bill Gates",
"url": "https://en.wikipedia.org/wiki/Bill_Gates",
"dataSource": "Wikipedia"
}, {
"bingId": "df2c4376-9923-6a54-893f-2ee5a5badbc7",
"name": "Paul Allen",
"matches": [{
"text": "Paul Allen",
"offset": 40,
"length": 10,
"confidenceScore": 0.54
}],
"language": "en",
"id": "Paul Allen",
"url": "https://en.wikipedia.org/wiki/Paul_Allen",
"dataSource": "Wikipedia"
}, {
"bingId": "52535f87-235e-b513-54fe-c03e4233ac6e",
"name": "April 4",
"matches": [{
"text": "April 4",
"offset": 54,
"length": 7,
"confidenceScore": 0.38
}],
"language": "en",
"id": "April 4",
"url": "https://en.wikipedia.org/wiki/April_4",
"dataSource": "Wikipedia"
}],
"warnings": []
}],
"errors": [],
"modelVersion": "2021-06-01"
}
}
如果想要清理并移除 Azure AI 服务订阅,可以删除资源或资源组。 删除资源组同时也会删除与之相关联的任何其他资源。