Utilities and services for OpenAI in .NET (streaming, structured, etc.).
$ dotnet add package kvandijk.AIA utilities and services package for OpenAI in .NET (streaming, structured, etc.).
Install the package using the following command:
dotnet add package kvandijk.AI
Create a deployment in Azure OpenAI and set the following environment variables in your development environment or hosting platform:
OPENAI_ENDPOINT: Your OpenAI endpoint URL.OPENAI_DEPLOYMENT: Your deployment name for the OpenAI model.OPENAI_EMBEDDING_DEPLOYMENT: Your deployment name for the OpenAI embedding model.OPENAI_KEY: Your OpenAI API key.Add the following line to your Program.cs file to register the completion service:
builder.Services.AddCompletionService();
await foreach (var chunk in service.GetCompletionStreamAsync("Write a short story of 100 words."))
{
Console.Write(chunk);
}
var typed = await service.GetStructuredCompletionAsync<ClassificationResult>(""""
Categorise: 'Invoice from Contoso for Q3 services'.
Categories: invoice, receipt, contract, other. Include 1-3 short reasons.Return as JSON.
"""");
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(typed, new System.Text.Json.JsonSerializerOptions { WriteIndented = true }));