CrestApps.Core OpenAI
The CrestApps.Core.AI.OpenAI package connects CrestApps.Core to api.openai.com and to any server that speaks the OpenAI API. The connection holds credentials and the endpoint; each deployment names a model and declares what that model can do.
Guidelines
- Register only the providers the host uses. OpenAI is the simplest first provider because it needs only an API key.
- Put the API key, endpoint and
ChatApion the connection. Put the model name and its capabilities on the deployment. - Every configured deployment must declare its model capabilities (
Features). A deployment entry with no features and no legacyPurpose/Typeis skipped at startup with the warning "declares no model capabilities". - Pick deployments through slots (
AIDeploymentSlotNames) andIAIDeploymentManager.ResolveSlotAsync(...). Never hard-code a provider client in business code. - Use
crestapps-core-ai-runtimefor the provider-agnostic runtime,crestapps-core-chatfor completion loops, andcrestapps-core-realtime-voicefor voice sessions.
Registration
using CrestApps.Core.AI.OpenAI;
builder.Services.AddCrestAppsCore(crestApps => crestApps
.AddAISuite(ai => ai
.AddOpenAI()
)
);
AddAISuite(...) already calls AddCoreServices(), AddCoreAIServices() and AddCoreAIOrchestration(). Without the builder, register them yourself:
builder.Services
.AddCoreAIServices()
.AddCoreAIOrchestration()
.AddCoreAIOpenAI();
AddCoreAIOpenAI() registers:
| Service | Implementation |
|---|---|
| IAIClientProvider | OpenAIClientProvider (chat, embeddings, images, speech-to-text, text-to-speech, realtime) |
| IAICompletionClient for "OpenAI" | ProviderAICompletionClient<OpenAIClientMarker> |
| IAIProviderConnectionHandler | OpenAIConnectionHandler (unprotects a stored OpenAIConnectionMetadata.ApiKey) |
| Connection source and deployment provider | Client name OpenAIConstants.ClientName ("OpenAI") |
Connection and Deployments
Connections are read from CrestApps:AI:Connections (or CrestApps:AI:Providers:OpenAI:Connections) and deployments from CrestApps:AI:Deployments. A configured deployment is read-only in the admin UI.
{
"CrestApps": {
"AI": {
"Connections": [
{
"Name": "openai",
"ClientName": "OpenAI",
"ApiKey": "sk-..."
}
],
"Deployments": [
{
"ClientName": "OpenAI",
"ConnectionName": "openai",
"Name": "gpt-4.1",
"ModelName": "gpt-4.1",
"Features": [ "textGeneration", "toolCalling", "streaming", "imageInput" ]
},
{
"ClientName": "OpenAI",
"ConnectionName": "openai",
"Name": "gpt-5-reasoning",
"ModelName": "gpt-5",
"Features": [ "textGeneration", "toolCalling", "streaming", "reasoning" ],
"Parameters": {
"reasoningEffort": {
"AllowedValues": [ "None", "Low", "Medium", "High" ],
"DefaultValue": "Low"
}
}
},
{
"ClientName": "OpenAI",
"ConnectionName": "openai",
"Name": "text-embedding-3-small",
"Features": [ "textEmbedding" ]
},
{
"ClientName": "OpenAI",
"ConnectionName": "openai",
"Name": "gpt-4o-transcribe",
"Features": [ "speechToText" ]
},
{
"ClientName": "OpenAI",
"ConnectionName": "openai",
"Name": "gpt-4o-mini-tts",
"Features": [ "textToSpeech" ]
},
{
"ClientName": "OpenAI",
"ConnectionName": "openai",
"Name": "gpt-realtime",
"Features": [ "realtime" ]
}
]
}
}
}
Nameis the lookup name;ModelNameis the model sent to OpenAI and falls back toName.FeaturesandParameterson the entry are shorthand forProperties:AIDeploymentMetadata:FeaturesandProperties:AIDeploymentMetadata:Parameters. Both forms merge, and the shorthand keeps environment variable names short enough for Azure App Service (CrestApps__AI__Deployments__0__Features__0=textGeneration).textGenerationis opt-out only for a deployment with no metadata at all. OnceFeaturesis declared, listtextGenerationfor every text model. A speech-to-speech-only model declaresrealtimeand omitstextGeneration.- Declare
toolCallingandstreamingon chat models. WithouttoolCallingevery tool is stripped from the request; withoutstreaminga streaming call is completed as one response. - Feature names are the
AIDeploymentFeatureNamesconstants:textGeneration,toolCalling,structuredOutputs,streaming,reasoning,imageInput,imageOutput,audioInput,audioOutput,videoInput,videoOutput,realtime,textEmbedding,speechToText,textToSpeech.
Deployments declared on the connection
A connection can name its models directly instead. Each name becomes a read-only deployment with fixed capabilities:
| Connection key | Capabilities declared |
|---|---|
| ChatDeploymentName (or DeploymentName) | textGeneration, toolCalling, streaming |
| UtilityDeploymentName | textGeneration, streaming |
| EmbeddingDeploymentName | textEmbedding |
| ImagesDeploymentName | imageOutput |
| SpeechToTextDeploymentName | speechToText |
| TextToSpeechDeploymentName | textToSpeech |
Use the explicit Deployments list when a model needs reasoning, imageInput, realtime, or parameters.
Slots and Site Defaults
A slot is what the installation uses a deployment for. The built-in slots are chat, utility, embedding, image, vision, speechToText, textToSpeech and realtime. Fill a slot default through DefaultAIDeploymentSettings, then resolve it:
using CrestApps.Core.AI.Models;
builder.Services.Configure<DefaultAIDeploymentSettings>(settings =>
{
settings.DefaultChatDeploymentName = "gpt-4.1";
settings.DefaultUtilityDeploymentName = "gpt-4.1";
settings.DefaultEmbeddingDeploymentName = "text-embedding-3-small";
settings.DefaultSpeechToTextDeploymentName = "gpt-4o-transcribe";
settings.DefaultTextToSpeechDeploymentName = "gpt-4o-mini-tts";
settings.DefaultRealtimeDeploymentName = "gpt-realtime";
});
using CrestApps.Core.AI.Clients;
using CrestApps.Core.AI.Deployments;
using CrestApps.Core.AI.Models;
using Microsoft.Extensions.AI;
namespace MyApp.Services;
public sealed class EmbeddingService
{
private readonly IAIDeploymentManager _deploymentManager;
private readonly IAIClientFactory _clientFactory;
public EmbeddingService(IAIDeploymentManager deploymentManager, IAIClientFactory clientFactory)
{
_deploymentManager = deploymentManager;
_clientFactory = clientFactory;
}
public async Task<ReadOnlyMemory<float>> EmbedAsync(string text, CancellationToken cancellationToken = default)
{
var deployment = await _deploymentManager.ResolveSlotAsync(
AIDeploymentSlotNames.Embedding,
cancellationToken: cancellationToken)
?? throw new InvalidOperationException("No deployment declares the textEmbedding capability.");
var generator = await _clientFactory.CreateEmbeddingGeneratorAsync(deployment);
var embeddings = await generator.GenerateAsync([text], cancellationToken: cancellationToken);
return embeddings[0].Vector;
}
}
Resolution order is the explicit name, the slot's site default, the fallback slot (only utility falls back to chat), and finally the first deployment that declares the slot's capability. GetAllBySlotAsync(slotName) lists every candidate, which is what settings pickers use. ResolveSlotOrThrowAsync(...) throws instead of returning null.
Chat API and Reasoning Models
Reasoning models such as gpt-6-luna reject function tools combined with a reasoning effort on /v1/chat/completions. The connection's ChatApi setting (OpenAIChatApis) decides where such a request goes:
| ChatApi | Behavior |
|---|---|
| Auto (default) | On OpenAI's own endpoint (no Endpoint, api.openai.com, or a regional host such as eu.api.openai.com), a request with function tools and a reasoning effort other than None goes to the Responses API. Any other endpoint uses Chat Completions only. |
| ChatCompletions | Every request uses Chat Completions. |
| Responses | Tools plus reasoning go to /v1/responses on any endpoint. Use it for compatible servers such as OpenRouter or xAI. |
Values are case-insensitive; an unknown value logs a warning and acts as Auto. Responses requests are sent with store: false. A reasoning effort reaches the provider only when the deployment declares reasoning and the reasoningEffort parameter; without the parameter no effort is sent and Chat Completions rejects the tools. A host configured only from environment variables can set Parameters:reasoningEffort:DefaultValue=None for models that refuse tools unless the effort is none.
{
"CrestApps": {
"AI": {
"Connections": [
{
"Name": "openrouter",
"ClientName": "OpenAI",
"Endpoint": "https://openrouter.ai/api/v1",
"ApiKey": "...",
"ChatApi": "Responses"
}
]
}
}
}
Speech and Realtime
- Text-to-speech (
IAIClientFactory.CreateTextToSpeechClientAsync) streams forgpt-4o-mini-tts;tts-1andtts-1-hdanswer in one piece.ISpeechVoiceResolverreturnsOpenAITextToSpeechVoices.For(model): thirteen voices forgpt-4o-mini-tts, nine fortts-1/tts-1-hd. - OpenAI produces raw PCM only at 24 kHz. Ask for
pcmorPcm<rate>;Pcm24000passes through and any other rate (Pcm8000,Pcm16000) is requested at 24 kHz and resampled while streaming, so OpenAI can be the speaking leg of a cascaded realtime deployment at any output rate. TextToSpeechOptions.Speedis clamped to 0.25 to 4. A speaking style passed as theTextToSpeechOptionsProperties.Instructionsadditional property is sent asinstructions, except totts-1andtts-1-hd.- The text-to-speech client is wrapped in
SpeechMarkupTextToSpeechClient, so neutral speech markup (<break time="500ms"/>,<spell>AB12</spell>) is rendered as plain punctuation and spaced letters instead of being read aloud. Seecrestapps-core-speech-voices. - Realtime uses
OpenAIRealtimeClientfromMicrosoft.Extensions.AI.OpenAI. Declare therealtimefeature so the deployment fills therealtimeslot. Voices come fromIRealtimeVoiceResolverand areOpenAIRealtimeVoices.All(alloy,ash,ballad,cedar,coral,echo,marin,sage,shimmer,verse). With no voice chosen the provider's own default voice is used. - Realtime sessions transcribe the user with a speech-to-text deployment on the same provider (for example
gpt-4o-transcribe); add one or the user's turns are not transcribed. Seecrestapps-core-realtime-voice.
OpenAI-Compatible Servers
Set Endpoint to the server's base URL (for example Groq, Mistral, Together AI, Fireworks, DeepSeek, OpenRouter, vLLM, LocalAI). Declare only the features the hosted model really has; many servers lack tool calling, structured outputs, or /v1/responses. For Anthropic's native API prefer crestapps-core-claude-orchestrator.
Troubleshooting
- Deployment missing from pickers or
ResolveSlotAsyncreturnsnull: the entry has noFeatures, itsClientNameis missing or unknown, or it declares a capability other than the slot requires. - The model describes a tool call but never calls it: the deployment does not declare
toolCalling; capability enforcement removed the tools (a warning is logged). - HTTP 400 about tools and reasoning effort: declare
reasoningandreasoningEffort, or leaveChatApiatAuto/Responsesso the request uses the Responses API. - Two connections whose names differ only by case: the first definition wins and the second is logged as a warning. Rename one.
Upgrading from 1.x
- Replace
"Purpose": "Chat"or"Type": "Chat"with"Features": [ "textGeneration", "toolCalling", "streaming" ]. LegacyPurpose,CapabilityandTypevalues still load, but the projection is narrow:ChatorUtilitybecomestextGenerationonly (not whenrealtimeis declared), andEmbedding,Image,Vision,SpeechToTextandTextToSpeechbecometextEmbedding,imageOutput,imageInput,speechToTextandtextToSpeech. A 1.x chat entry therefore loses tool calling and streaming untiltoolCallingandstreamingare declared. - Replace
ResolveOrDefaultAsync(AIDeploymentPurpose.X, ...)withResolveSlotAsync(AIDeploymentSlotNames.X, ...), andGetByPurposeAsync/GetAllByPurposeAsyncwithGetAllBySlotAsync.AIDeploymentPurpose,AIDeploymentType,SupportsPurposeandCanServeTextCompletion()are removed. - A realtime model is now a deployment with the
realtimefeature.ChatMode.RealtimeandAIProfile.RealtimeDeploymentNameare gone. - The OpenAI provider now supports text-to-speech and voice listing; earlier builds failed with "The provider 'OpenAI' does not support text-to-speech".