Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Reference documentation | More samples | Package (NuGet) | Library source code
Use this quickstart to create a Named Entity Recognition (NER) application with the client library for .NET. In the following example, you will create a C# application that can identify recognized entities in text.
Tip
You can use Azure AI Foundry to try summarization without needing to write code.
To use the code sample below, you'll need to deploy an Azure resource. This resource will contain a key and endpoint you'll use to authenticate the API calls you send to the Language service.
Use the following link to create a language resource using the Azure portal. You will need to sign in using your Azure subscription.
On the Select additional features screen that appears, select Continue to create your resource.
In the Create language screen, provide the following information:
Detail | Description |
---|---|
Subscription | The subscription account that your resource will be associated with. Select your Azure subscription from the drop-down menu. |
Resource group | A resource group is a container that stores the resources you create. Select Create new to create a new resource group. |
Region | The location of your Language resource. Different regions may introduce latency depending on your physical location, but have no impact on the runtime availability of your resource. For this quickstart, either select an available region near you, or choose East US. |
Name | The name for your Language resource. This name will also be used to create an endpoint URL that your applications will use to send API requests. |
Pricing tier | The pricing tier for your Language resource. You can use the Free F0 tier to try the service and upgrade later to a paid tier for production. |
Make sure the Responsible AI Notice checkbox is checked.
Select Review + Create at the bottom of the page.
In the screen that appears, make sure the validation has passed, and that you entered your information correctly. Then select Create.
Next you will need the key and endpoint from the resource to connect your application to the API. You'll paste your key and endpoint into the code later in the quickstart.
After the Language resource deploys successfully, click the Go to Resource button under Next Steps.
On the screen for your resource, select Keys and endpoint on the left navigation menu. You will use one of your keys and your endpoint in the steps below.
Your application must be authenticated to send API requests. For production, use a secure way of storing and accessing your credentials. In this example, you will write your credentials to environment variables on the local machine running the application.
To set the environment variable for your Language resource key, open a console window, and follow the instructions for your operating system and development environment.
LANGUAGE_KEY
environment variable, replace your-key
with one of the keys for your resource.LANGUAGE_ENDPOINT
environment variable, replace your-endpoint
with the endpoint for your resource.Important
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
After you add the environment variables, run source ~/.bashrc
from your console window to make the changes effective.
Using the Visual Studio IDE, create a new .NET Core console app. This creates a "Hello World" project with a single C# source file: program.cs.
Install the client library by right-clicking on the solution in the Solution Explorer and selecting Manage NuGet Packages. In the package manager that opens select Browse and search for Azure.AI.TextAnalytics
. Select version 5.2.0
, and then Install. You can also use the Package Manager Console.
Copy the following code into your program.cs file and run the code.
using Azure;
using System;
using Azure.AI.TextAnalytics;
namespace Example
{
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 extracting named entities from text
static void EntityRecognitionExample(TextAnalyticsClient client)
{
var response = client.RecognizeEntities("I had a wonderful trip to Seattle last week.");
Console.WriteLine("Named Entities:");
foreach (var entity in response.Value)
{
Console.WriteLine($"\tText: {entity.Text},\tCategory: {entity.Category},\tSub-Category: {entity.SubCategory}");
Console.WriteLine($"\t\tScore: {entity.ConfidenceScore:F2},\tLength: {entity.Length},\tOffset: {entity.Offset}\n");
}
}
static void Main(string[] args)
{
var client = new TextAnalyticsClient(endpoint, credentials);
EntityRecognitionExample(client);
Console.Write("Press any key to exit.");
Console.ReadKey();
}
}
}
Named Entities:
Text: trip, Category: Event, Sub-Category:
Score: 0.74, Length: 4, Offset: 18
Text: Seattle, Category: Location, Sub-Category: GPE
Score: 1.00, Length: 7, Offset: 26
Text: last week, Category: DateTime, Sub-Category: DateRange
Score: 0.80, Length: 9, Offset: 34
Reference documentation | More samples | Package (Maven) | Library source code
Use this quickstart to create a Named Entity Recognition (NER) application with the client library for Java. In the following example, you will create a Java application that can identify recognized entities in text.
To use the code sample below, you'll need to deploy an Azure resource. This resource will contain a key and endpoint you'll use to authenticate the API calls you send to the Language service.
Use the following link to create a language resource using the Azure portal. You will need to sign in using your Azure subscription.
On the Select additional features screen that appears, select Continue to create your resource.
In the Create language screen, provide the following information:
Detail | Description |
---|---|
Subscription | The subscription account that your resource will be associated with. Select your Azure subscription from the drop-down menu. |
Resource group | A resource group is a container that stores the resources you create. Select Create new to create a new resource group. |
Region | The location of your Language resource. Different regions may introduce latency depending on your physical location, but have no impact on the runtime availability of your resource. For this quickstart, either select an available region near you, or choose East US. |
Name | The name for your Language resource. This name will also be used to create an endpoint URL that your applications will use to send API requests. |
Pricing tier | The pricing tier for your Language resource. You can use the Free F0 tier to try the service and upgrade later to a paid tier for production. |
Make sure the Responsible AI Notice checkbox is checked.
Select Review + Create at the bottom of the page.
In the screen that appears, make sure the validation has passed, and that you entered your information correctly. Then select Create.
Next you will need the key and endpoint from the resource to connect your application to the API. You'll paste your key and endpoint into the code later in the quickstart.
After the Language resource deploys successfully, click the Go to Resource button under Next Steps.
On the screen for your resource, select Keys and endpoint on the left navigation menu. You will use one of your keys and your endpoint in the steps below.
Your application must be authenticated to send API requests. For production, use a secure way of storing and accessing your credentials. In this example, you will write your credentials to environment variables on the local machine running the application.
To set the environment variable for your Language resource key, open a console window, and follow the instructions for your operating system and development environment.
LANGUAGE_KEY
environment variable, replace your-key
with one of the keys for your resource.LANGUAGE_ENDPOINT
environment variable, replace your-endpoint
with the endpoint for your resource.Important
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
After you add the environment variables, run source ~/.bashrc
from your console window to make the changes effective.
Create a Maven project in your preferred IDE or development environment. Then add the following dependency to your project's pom.xml file. You can find the implementation syntax for other build tools online.
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
Create a Java file named Example.java
. Open the file and copy the below code. Then run the code.
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);
recognizeEntitiesExample(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 in text
static void recognizeEntitiesExample(TextAnalyticsClient client)
{
// The text that needs to be analyzed.
String text = "I had a wonderful trip to Seattle last week.";
for (CategorizedEntity entity : client.recognizeEntities(text)) {
System.out.printf(
"Recognized entity: %s, entity category: %s, entity sub-category: %s, score: %s, offset: %s, length: %s.%n",
entity.getText(),
entity.getCategory(),
entity.getSubcategory(),
entity.getConfidenceScore(),
entity.getOffset(),
entity.getLength());
}
}
}
Recognized entity: trip, entity category: Event, entity sub-category: null, score: 0.74, offset: 18, length: 4.
Recognized entity: Seattle, entity category: Location, entity sub-category: GPE, score: 1.0, offset: 26, length: 7.
Recognized entity: last week, entity category: DateTime, entity sub-category: DateRange, score: 0.8, offset: 34, length: 9.
Reference documentation | More samples | Package (npm) | Library source code
Use this quickstart to create a Named Entity Recognition (NER) application with the client library for Node.js. In the following example, you create a JavaScript application that can identify recognized entities in text.
To use the code sample below, you'll need to deploy an Azure resource. This resource will contain a key and endpoint you'll use to authenticate the API calls you send to the Language service.
Use the following link to create a language resource using the Azure portal. You will need to sign in using your Azure subscription.
On the Select additional features screen that appears, select Continue to create your resource.
In the Create language screen, provide the following information:
Detail | Description |
---|---|
Subscription | The subscription account that your resource will be associated with. Select your Azure subscription from the drop-down menu. |
Resource group | A resource group is a container that stores the resources you create. Select Create new to create a new resource group. |
Region | The location of your Language resource. Different regions may introduce latency depending on your physical location, but have no impact on the runtime availability of your resource. For this quickstart, either select an available region near you, or choose East US. |
Name | The name for your Language resource. This name will also be used to create an endpoint URL that your applications will use to send API requests. |
Pricing tier | The pricing tier for your Language resource. You can use the Free F0 tier to try the service and upgrade later to a paid tier for production. |
Make sure the Responsible AI Notice checkbox is checked.
Select Review + Create at the bottom of the page.
In the screen that appears, make sure the validation has passed, and that you entered your information correctly. Then select Create.
Next you will need the key and endpoint from the resource to connect your application to the API. You'll paste your key and endpoint into the code later in the quickstart.
After the Language resource deploys successfully, click the Go to Resource button under Next Steps.
On the screen for your resource, select Keys and endpoint on the left navigation menu. You will use one of your keys and your endpoint in the steps below.
Your application must be authenticated to send API requests. For production, use a secure way of storing and accessing your credentials. In this example, you will write your credentials to environment variables on the local machine running the application.
To set the environment variable for your Language resource key, open a console window, and follow the instructions for your operating system and development environment.
LANGUAGE_KEY
environment variable, replace your-key
with one of the keys for your resource.LANGUAGE_ENDPOINT
environment variable, replace your-endpoint
with the endpoint for your resource.Important
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
After you add the environment variables, run source ~/.bashrc
from your console window to make the changes effective.
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
mkdir myapp
cd myapp
Run the npm init
command to create a node application with a package.json
file.
npm init
Install the npm package:
npm install @azure/ai-language-text
Open the file and copy the below code. Then run the code.
"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;
//an example document for entity recognition
const documents = [ "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800"];
//example of how to use the client library to recognize entities in a document.
async function main() {
console.log("== NER sample ==");
const client = new TextAnalysisClient(endpoint, new AzureKeyCredential(key));
const results = await client.analyze("EntityRecognition", documents);
for (const result of results) {
console.log(`- Document ${result.id}`);
if (!result.error) {
console.log("\tRecognized Entities:");
for (const entity of result.entities) {
console.log(`\t- Entity ${entity.text} of type ${entity.category}`);
}
} else console.error("\tError:", result.error);
}
}
//call the main function
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Document ID: 0
Name: Microsoft Category: Organization Subcategory: N/A
Score: 0.29
Name: Bill Gates Category: Person Subcategory: N/A
Score: 0.78
Name: Paul Allen Category: Person Subcategory: N/A
Score: 0.82
Name: April 4, 1975 Category: DateTime Subcategory: Date
Score: 0.8
Name: 8800 Category: Quantity Subcategory: Number
Score: 0.8
Document ID: 1
Name: 21 Category: Quantity Subcategory: Number
Score: 0.8
Name: Seattle Category: Location Subcategory: GPE
Score: 0.25
Reference documentation | More samples | Package (PyPi) | Library source code
Use this quickstart to create a Named Entity Recognition (NER) application with the client library for Python. In the following example, you create a Python application that can identify recognized entities in text.
After installing Python, you can install the client library with:
pip install azure-ai-textanalytics==5.2.0
Create a new Python file and copy the below code. Then run the code.
# 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 from text
def entity_recognition_example(client):
try:
documents = ["I had a wonderful trip to Seattle last week."]
result = client.recognize_entities(documents = documents)[0]
print("Named Entities:\n")
for entity in result.entities:
print("\tText: \t", entity.text, "\tCategory: \t", entity.category, "\tSubCategory: \t", entity.subcategory,
"\n\tConfidence Score: \t", round(entity.confidence_score, 2), "\tLength: \t", entity.length, "\tOffset: \t", entity.offset, "\n")
except Exception as err:
print("Encountered exception. {}".format(err))
entity_recognition_example(client)
Named Entities:
Text: trip Category: Event SubCategory: None
Confidence Score: 0.74 Length: 4 Offset: 18
Text: Seattle Category: Location SubCategory: GPE
Confidence Score: 1.0 Length: 7 Offset: 26
Text: last week Category: DateTime SubCategory: DateRange
Confidence Score: 0.8 Length: 9 Offset: 34
Use this quickstart to send Named Entity Recognition (NER) requests using the REST API. In the following example, you'll use cURL to identify recognized entities in text.
To use the code sample below, you'll need to deploy an Azure resource. This resource will contain a key and endpoint you'll use to authenticate the API calls you send to the Language service.
Use the following link to create a language resource using the Azure portal. You will need to sign in using your Azure subscription.
On the Select additional features screen that appears, select Continue to create your resource.
In the Create language screen, provide the following information:
Detail | Description |
---|---|
Subscription | The subscription account that your resource will be associated with. Select your Azure subscription from the drop-down menu. |
Resource group | A resource group is a container that stores the resources you create. Select Create new to create a new resource group. |
Region | The location of your Language resource. Different regions may introduce latency depending on your physical location, but have no impact on the runtime availability of your resource. For this quickstart, either select an available region near you, or choose East US. |
Name | The name for your Language resource. This name will also be used to create an endpoint URL that your applications will use to send API requests. |
Pricing tier | The pricing tier for your Language resource. You can use the Free F0 tier to try the service and upgrade later to a paid tier for production. |
Make sure the Responsible AI Notice checkbox is checked.
Select Review + Create at the bottom of the page.
In the screen that appears, make sure the validation has passed, and that you entered your information correctly. Then select Create.
Next you will need the key and endpoint from the resource to connect your application to the API. You'll paste your key and endpoint into the code later in the quickstart.
After the Language resource deploys successfully, click the Go to Resource button under Next Steps.
On the screen for your resource, select Keys and endpoint on the left navigation menu. You will use one of your keys and your endpoint in the steps below.
Your application must be authenticated to send API requests. For production, use a secure way of storing and accessing your credentials. In this example, you will write your credentials to environment variables on the local machine running the application.
To set the environment variable for your Language resource key, open a console window, and follow the instructions for your operating system and development environment.
LANGUAGE_KEY
environment variable, replace your-key
with one of the keys for your resource.LANGUAGE_ENDPOINT
environment variable, replace your-endpoint
with the endpoint for your resource.Important
We recommend Microsoft Entra ID authentication with managed identities for Azure resources to avoid storing credentials with your applications that run in the cloud.
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If using API keys, store them securely in Azure Key Vault, rotate the keys regularly, and restrict access to Azure Key Vault using role based access control and network access restrictions. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
export LANGUAGE_KEY=your-key
export LANGUAGE_ENDPOINT=your-endpoint
After you add the environment variables, run source ~/.bashrc
from your console window to make the changes effective.
In a code editor, create a new file named test_ner_payload.json
and copy the following JSON example. This example request will be sent to the API in the next step.
{
"kind": "EntityRecognition",
"parameters": {
"modelVersion": "latest"
},
"analysisInput":{
"documents":[
{
"id":"1",
"language": "en",
"text": "I had a wonderful trip to Seattle last week."
}
]
}
}
Save test_ner_payload.json
somewhere on your computer. For example, your desktop.
Use the following commands to send the API request using the program you're using. Copy the command into your terminal, and run it.
parameter | Description |
---|---|
-X POST <endpoint> |
Specifies your endpoint for accessing the API. |
-H Content-Type: application/json |
The content type for sending JSON data. |
-H "Ocp-Apim-Subscription-Key:<key> |
Specifies the key for accessing the API. |
-d <documents> |
The JSON containing the documents you want to send. |
Use the following commands to send the API request using the program you're using. Replace /home/mydir/test_ner_payload.json
with the location of the example JSON request file you created in the previous step.
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_ner_payload.json"
Note
2023-04-15-preview
.{
"kind": "EntityRecognitionResults",
"results": {
"documents": [{
"id": "1",
"entities": [{
"text": "trip",
"category": "Event",
"offset": 18,
"length": 4,
"confidenceScore": 0.74
}, {
"text": "Seattle",
"category": "Location",
"subcategory": "GPE",
"offset": 26,
"length": 7,
"confidenceScore": 1.0
}, {
"text": "last week",
"category": "DateTime",
"subcategory": "DateRange",
"offset": 34,
"length": 9,
"confidenceScore": 0.8
}],
"warnings": []
}],
"errors": [],
"modelVersion": "2021-06-01"
}
}
Using the left side pane, select Playgrounds. Then select the Try the Language Playground button.
The Language Playground consists of four sections:
Here you can select the Named Entity Recognition capability by choosing the top banner tile, Extract Named Entities.
Extract Named Entities is designed to identify named entities in text.
In Configuration there are the following options:
Option | Description |
---|---|
Select API version | Select which version of the API to use. |
Select model version | Select which version of the model to use. |
Select text language | Select which language the language is input in. |
Select types to include | Select they types of information you want to extract. |
Overlap policy | Select the policy for overlapping entities. |
Inference options | Additional options to customize the return of the processed data. |
After your operation is completed, the type of entity is displayed beneath each entity in the center pane and the Details section contains the following fields for each entity:
Field | Description |
---|---|
Entity | The detected entity. |
Category | The type of entity that was detected. |
Offset | The number of characters that the entity was detected from the beginning of the line. |
Length | The character length of the entity. |
Confidence | How confident the model is in the correctness of identification of entity's type. |
If you want to clean up and remove an Azure AI services subscription, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Certification
Microsoft Certified: Azure AI Fundamentals - Certifications
Demonstrate fundamental AI concepts related to the development of software and services of Microsoft Azure to create AI solutions.