-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added c# azure ai inference sample for chat w/ streaming and updated … (
#328) * added c# azure ai inference sample for chat w/ streaming and updated python sample a bit to use matching env vars * can't have .csproj's in the template directory... i should know better. * fixed for real * enable inferencing via azure ai inference sdk using `ai chat` after `ai init inference`... * updated with github capabilities w/ azure.ai.inference sdk and related
- Loading branch information
Showing
35 changed files
with
627 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
service.config.region=@region | ||
service.config.endpoint.uri=@endpoint | ||
[email protected] | ||
service.config.key=@key |
Empty file.
16 changes: 16 additions & 0 deletions
16
...ai/.x/templates/aml-chat-streaming-cs/AzureAIInferencingChatCompletionsStreaming.csproj._
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<EnableDefaultCompileItems>true</EnableDefaultCompileItems> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" Version="1.12.0" /> | ||
<PackageReference Include="Azure.AI.Inference" Version="1.0.0-beta.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
66 changes: 66 additions & 0 deletions
66
src/ai/.x/templates/aml-chat-streaming-cs/AzureAIInferencingChatCompletionsStreamingClass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. | ||
// | ||
|
||
using Azure; | ||
using Azure.Identity; | ||
using Azure.AI.Inference; | ||
using System; | ||
|
||
public class {ClassName} | ||
{ | ||
public {ClassName}(string aiChatEndpoint, string aiChatAPIKey, string? aiChatModel, string systemPrompt) | ||
{ | ||
_systemPrompt = systemPrompt; | ||
_aiChatModel = aiChatModel; | ||
|
||
_client = string.IsNullOrEmpty(aiChatAPIKey) | ||
? new ChatCompletionsClient(new Uri(aiChatEndpoint), new DefaultAzureCredential()) | ||
: new ChatCompletionsClient(new Uri(aiChatEndpoint), new AzureKeyCredential(aiChatAPIKey)); | ||
_messages = new List<ChatRequestMessage>(); | ||
|
||
ClearConversation(); | ||
} | ||
|
||
public void ClearConversation() | ||
{ | ||
_messages.Clear(); | ||
_messages.Add(new ChatRequestSystemMessage(_systemPrompt)); | ||
} | ||
|
||
public async Task<string> GetChatCompletionsStreamingAsync(string userPrompt, Action<StreamingChatCompletionsUpdate>? callback = null) | ||
{ | ||
_messages.Add(new ChatRequestUserMessage(userPrompt)); | ||
var options = new ChatCompletionsOptions(_messages); | ||
if (!string.IsNullOrEmpty(_aiChatModel)) | ||
{ | ||
options.Model = _aiChatModel; | ||
} | ||
|
||
var responseContent = string.Empty; | ||
var response = await _client.CompleteStreamingAsync(options); | ||
await foreach (var update in response) | ||
{ | ||
var content = update.ContentUpdate; | ||
|
||
if (update.FinishReason == CompletionsFinishReason.ContentFiltered) | ||
{ | ||
content = $"{content}\nWARNING: Content filtered!"; | ||
} | ||
|
||
if (string.IsNullOrEmpty(content)) continue; | ||
|
||
responseContent += content; | ||
if (callback != null) callback(update); | ||
} | ||
|
||
_messages.Add(new ChatRequestAssistantMessage() { Content = responseContent }); | ||
return responseContent; | ||
} | ||
|
||
private string _systemPrompt; | ||
private string? _aiChatModel; | ||
private ChatCompletionsClient _client; | ||
private List<ChatRequestMessage> _messages; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
|
||
public class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
var aiChatAPIKey = Environment.GetEnvironmentVariable("AZURE_AI_CHAT_API_KEY") ?? "<insert your OpenAI API key here>"; | ||
var aiChatEndpoint = Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT") ?? "<insert your OpenAI endpoint here>"; | ||
var aiChatModel = Environment.GetEnvironmentVariable("AZURE_AI_CHAT_MODEL"); // null is fine | ||
var systemPrompt = Environment.GetEnvironmentVariable("SYSTEM_PROMPT") ?? "You are a helpful AI assistant."; | ||
|
||
if (string.IsNullOrEmpty(aiChatAPIKey) || aiChatAPIKey.StartsWith("<insert") || | ||
string.IsNullOrEmpty(aiChatEndpoint) || aiChatEndpoint.StartsWith("<insert") || | ||
string.IsNullOrEmpty(systemPrompt) || systemPrompt.StartsWith("<insert")) | ||
{ | ||
Console.WriteLine("To use Azure AI Inference, set the following environment variables:"); | ||
Console.WriteLine("- AZURE_AI_CHAT_API_KEY\n- AZURE_AI_CHAT_ENDPOINT\n- AZURE_AI_CHAT_MODEL (optional)\n- SYSTEM_PROMPT (optional)"); | ||
Environment.Exit(1); | ||
} | ||
|
||
var chat = new {ClassName}(aiChatEndpoint, aiChatAPIKey, aiChatModel, systemPrompt); | ||
|
||
while (true) | ||
{ | ||
Console.Write("User: "); | ||
var userPrompt = Console.ReadLine(); | ||
if (string.IsNullOrEmpty(userPrompt) || userPrompt == "exit") break; | ||
|
||
Console.Write("\nAssistant: "); | ||
var response = await chat.GetChatCompletionsStreamingAsync(userPrompt, update => { | ||
var text = update.ContentUpdate; | ||
Console.Write(text); | ||
}); | ||
Console.WriteLine("\n"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"_LongName": "Azure AI Inference Chat Completions (Streaming)", | ||
"_ShortName": "az-inference-chat-streaming", | ||
"_Language": "C#", | ||
"ClassName": "AzureAIInferenceChatCompletionsStreaming" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"_LongName": "AzureML Chat Completions (Streaming)", | ||
"_ShortName": "aml-chat-streaming", | ||
"_LongName": "Azure AI Inference Chat Completions (Streaming)", | ||
"_ShortName": "az-inference-chat-streaming", | ||
"_Language": "Python", | ||
"ClassName": "AzureMLChatCompletionsStreaming" | ||
"ClassName": "AzureAIInferenceChatCompletionsStreaming" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.