OpenAIClient client = new (new Uri(azureOpenAiEndpoint), new AzureKeyCredential(azureOpenAiKey));
var imageGenerationOptions = new ImageGenerationOptions
{
Prompt = prompt,
DeploymentName = deploymentName,
Size = new ImageSize("1792x1024"),
ImageCount = 1
};
var response = await client.GetImageGenerationsAsync(imageGenerationOptions);
Console.WriteLine(response.Value.Data[0].Url.AbsoluteUri);
await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
{
if (chatUpdate.Role.HasValue)
{
await Console.Out.WriteAsync(($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
}
if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
{
await Console.Out.WriteAsync(chatUpdate.ContentUpdate);
}
}
async Task AiSearchAsync(string input)
{
var options = new SearchOptions()
{
QueryType = SearchQueryType.Semantic,
SemanticSearch = new SemanticSearchOptions
{
SemanticConfigurationName = "semantic-config",
QueryCaption = new QueryCaption(QueryCaptionType.Extractive)
},
IncludeTotalCount = true,
Size = 5,
};
options.Select.Add(nameof(Article.Title));
options.Select.Add(nameof(Article.LegacyUrl));
SearchResults<Article> response = await searchClient.SearchAsync<Article>(input, options);
foreach(SearchResult<Article> result in response.GetResults())
{
await Console.Out.WriteLineAsync($"Title: {result.Document.Title}");
await Console.Out.WriteLineAsync($"LegacyUrl: {result.Document.LegacyUrl}");
await Console.Out.WriteLineAsync();
}
}
With Azure SDK and Azure.AI.OpenAI Nuget package, it makes .NET developers easy to work on GenAI software development. Cheers!!
Prerequisite: To setup required credentials with below commands or with secret manager in Visual Studio 2022
# For Azure OpenAI related sample code
setx AZURE_OPENAI_ENDPOINT REPLACE_WITH_YOUR_AOAI_ENDPOINT_VALUE_HERE
setx AZURE_OPENAI_API_KEY REPLACE_WITH_YOUR_AOAI_KEY_VALUE_HERE
setx AZURE_OPENAI_DEPLOYMENT_ID REPLACE_WITH_YOUR_AOAI_DEPLOYMENT_VALUE_HERE
# For Azure AI Search related sample code
setx AZURE_AI_SEARCH_ENDPOINT REPLACE_WITH_YOUR_AZURE_SEARCH_RESOURCE_VALUE_HERE
setx AZURE_AI_SEARCH_API_KEY REPLACE_WITH_YOUR_AZURE_SEARCH_RESOURCE_KEY_VALUE_HERE
setx AZURE_AI_SEARCH_INDEX REPLACE_WITH_YOUR_INDEX_NAME_HERE