Share via


Azure Cognitive Language Services Question Answering (Inference) client library for .NET - version 1.0.0-beta.1

The Question Answering service lets you build a conversational question–answering layer over your data. It can extract questions and answers from semi‑structured content (FAQs, manuals, documents) and improve relevance over time.

Source code | Samples | Product documentation | [REST API documentation][questionanswering_rest_docs]

This package provides runtime inference (querying) only.
To create/update/deploy projects (authoring operations), use the Authoring package Azure.AI.Language.QuestionAnswering.Authoring (namespace: Azure.AI.Language.QuestionAnswering.Authoring).

Getting started

Service API version targeted: 2025-05-15-preview
Package version: 1.0.0-beta.1 (preview).

Install the package

dotnet add package Azure.AI.Language.QuestionAnswering.Inference --prerelease

Prerequisites

  • An Azure subscription
  • A Cognitive Services Language resource with a deployed Question Answering project (created via Authoring SDK or Language Studio)
  • Endpoint (e.g. https://<resource>.cognitiveservices.azure.com/)
  • API key or AAD credential (Reader role)

Language Studio remains the preferred experience for creating and managing projects; the Authoring SDK enables CI/CD automation.

Authenticate the client

You need an endpoint plus either an API key or AAD credential.

API key

using Azure.Core;
using Azure.AI.Language.QuestionAnswering;
Uri endpoint = new Uri("https://myaccount.cognitiveservices.azure.com/");
AzureKeyCredential credential = new AzureKeyCredential("{api-key}");

QuestionAnsweringClient client = new QuestionAnsweringClient(endpoint, credential);

Azure Active Directory (AAD)

using Azure.Identity;
Uri endpoint = new Uri("https://myaccount.cognitiveservices.azure.com");
DefaultAzureCredential credential = new DefaultAzureCredential();

QuestionAnsweringClient client = new QuestionAnsweringClient(endpoint, credential);

Regional endpoints do not support AAD directly; configure a custom domain to use AAD authentication.

Key concepts

  • QuestionAnsweringClient: asks questions against a deployed project (knowledge base).
  • QuestionAnsweringProject: identifies the project and deployment.
  • AnswersOptions: optional tuning (top answers, confidence threshold, follow‑up context).
  • AnswersResult / KnowledgeBaseAnswer: structured answers (confidence, source, answer text, optional short answer).
  • Follow‑up (chit‑chat) uses KnowledgeBaseAnswerContext(previousAnswer.QnaId.Value) for dialog continuity.
  • This package does not include project creation, source ingestion, QnA editing, deployment operations.

Thread safety

Client instances are thread‑safe; reuse them across threads and requests.

Additional concepts

Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime

Examples

Ask a question

string projectName = "{ProjectName}";
string deploymentName = "{DeploymentName}";
QuestionAnsweringProject project = new QuestionAnsweringProject(projectName, deploymentName);
Response<AnswersResult> response = client.GetAnswers("How long should my Surface battery last?", project);

foreach (KnowledgeBaseAnswer answer in response.Value.Answers)
{
    Console.WriteLine($"({answer.Confidence:P2}) {answer.Answer}");
    Console.WriteLine($"Source: {answer.Source}");
    Console.WriteLine();
}

Ask a follow‑up question (chit‑chat)

string projectName = "{ProjectName}";
string deploymentName = "{DeploymentName}";
// Answers are ordered by their ConfidenceScore so assume the user choose the first answer below:
KnowledgeBaseAnswer previousAnswer = answers.Answers.First();
QuestionAnsweringProject project = new QuestionAnsweringProject(projectName, deploymentName);
AnswersOptions options = new AnswersOptions
{
    AnswerContext = new KnowledgeBaseAnswerContext(previousAnswer.QnaId.Value)
};

Response<AnswersResult> response = client.GetAnswers("How long should charging take?", project, options);

foreach (KnowledgeBaseAnswer answer in response.Value.Answers)
{
    Console.WriteLine($"({answer.Confidence:P2}) {answer.Answer}");
    Console.WriteLine($"Source: {answer.Source}");
    Console.WriteLine();
}

Troubleshooting

Common HTTP status codes map directly from service responses (e.g. 400 invalid project/deployment, 404 not found).

Bad request example:

try
{
    QuestionAnsweringProject project = new QuestionAnsweringProject("invalid-knowledgebase", "test");
    Response<AnswersResult> response = client.GetAnswers("Does this knowledge base exist?", project);
}
catch (RequestFailedException ex)
{
    Console.WriteLine(ex.ToString());
}

Enable console diagnostics:

using Azure.Core.Diagnostics;
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();

Next steps

  • Explore samples.
  • Use Authoring SDK or Language Studio to evolve your knowledge base.
  • Review the migration guide

Contributing

See CONTRIBUTING.md. This project follows the Microsoft Open Source Code of Conduct.