> For the complete documentation index, see [llms.txt](https://docs.flexbase.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flexbase.in/data-and-providers/providers/ai-providers/azure-open-ai.md).

# Azure Open AI

## Description

The Azure OpenAI provider integrates Azure OpenAI chat + embeddings behind `IFlexAIProvider`.

Provider capabilities (based on the implementation):

* Chat completions: `ChatAsync(...)`
* Streaming chat: `ChatStreamAsync(...)`
* Embeddings: `EmbedAsync(...)`

## Important concepts

* In Azure OpenAI, “model” identifiers are typically **deployment names**.
* The implementation supports API key auth and also includes a Managed Identity–based constructor.

## Configuration in DI

```csharp
// Registers IFlexAIProvider / IFlexAIProviderBridge
services.AddFlexAzureOpenAI(configuration);
```

## appsettings.json

Configuration section: `FlexBase:AI:AzureOpenAI`

```json
{
  "FlexBase": {
	"AI": {
	  "AzureOpenAI": {
		"Endpoint": "https://your-resource.openai.azure.com/",
		"ApiKey": "<store-in-secrets>",
		"DeploymentName": "gpt-4o",
		"DefaultChatModel": "gpt-4o",
		"DefaultEmbeddingModel": "text-embedding-3-small",
		"ApiVersion": "2024-02-01"
	  }
	}
  }
}
```

## Examples (template-based)

These examples mirror the generated Query and PostBus handler templates. You do **not** register these types manually.

### Query: generate a completion

```csharp
using Microsoft.Extensions.Logging;
using Sumeru.Flex;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace {YourApplication}.Queries.AI;

public class GenerateChatCompletionGetSingle : FlexiQueryBridgeAsync<ChatCompletionDto>
{
	protected readonly ILogger<GenerateChatCompletionGetSingle> _logger;
	protected readonly IFlexHost _flexHost;
	protected readonly IFlexAIProvider _aiProvider;
	protected FlexAppContextBridge _flexAppContext;
	protected GenerateChatCompletionParams _params;

	public GenerateChatCompletionGetSingle(
		ILogger<GenerateChatCompletionGetSingle> logger,
		IFlexHost flexHost,
		IFlexAIProvider aiProvider)
	{
		_logger = logger;
		_flexHost = flexHost;
		_aiProvider = aiProvider;
	}

	public virtual GenerateChatCompletionGetSingle AssignParameters(GenerateChatCompletionParams @params)
	{
		_params = @params;
		return this;
	}

	public virtual async Task<ChatCompletionDto?> Fetch()
	{
		_flexAppContext = _params.GetAppContext();

		var response = await _aiProvider.ChatAsync(new FlexAIChatRequest
		{
			// Typically an Azure deployment name
			Model = _params.Model,
			Messages = _params.Messages,
			Temperature = _params.Temperature,
			MaxTokens = _params.MaxTokens
		});

		return new ChatCompletionDto { Response = response.Message.Content };
	}
}

public class GenerateChatCompletionParams : DtoBridge
{
	public string? Model { get; set; }
	public List<FlexAIMessage> Messages { get; set; } = new();
	public float? Temperature { get; set; }
	public int? MaxTokens { get; set; }
}

public class ChatCompletionDto : DtoBridge
{
	public string Response { get; set; }
}
```

### PostBus handler: generate a completion

```csharp
using Microsoft.Extensions.Logging;
using Sumeru.Flex;
using System.Threading.Tasks;

namespace {YourApplication}.PostBusHandlers.AI;

public partial class GenerateChatCompletionHandler : IGenerateChatCompletionHandler
{
	protected string EventCondition = string.Empty;
	protected readonly ILogger<GenerateChatCompletionHandler> _logger;
	protected readonly IFlexHost _flexHost;
	protected readonly IFlexAIProvider _aiProvider;

	protected FlexAppContextBridge? _flexAppContext;

	public GenerateChatCompletionHandler(
		ILogger<GenerateChatCompletionHandler> logger,
		IFlexHost flexHost,
		IFlexAIProvider aiProvider)
	{
		_logger = logger;
		_flexHost = flexHost;
		_aiProvider = aiProvider;
	}

	public virtual async Task Execute(GenerateChatCompletionCommand cmd, IFlexServiceBusContext serviceBusContext)
	{
		_flexAppContext = cmd.Dto.GetAppContext();  //do not remove this line

		var response = await _aiProvider.ChatAsync(new FlexAIChatRequest
		{
			Model = cmd.Dto.Model,
			Messages = cmd.Dto.Messages,
			Temperature = cmd.Dto.Temperature,
			MaxTokens = cmd.Dto.MaxTokens
		});

		cmd.Dto.Response = response.Message.Content;
		await this.Fire(EventCondition, serviceBusContext);
	}
}
```

## Provider considerations

* **Deployments**: set `DeploymentName` / defaults to match the deployment names you configured in Azure.
* **Auth**: store `ApiKey` securely; if your generated infrastructure is configured for Managed Identity, you can avoid keys.
* **Observability**: provider logs duration and usage when available.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flexbase.in/data-and-providers/providers/ai-providers/azure-open-ai.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
