-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5690 Added samples @RogerBarreto --------- Co-authored-by: Roger Barreto <[email protected]>
- Loading branch information
1 parent
e96eb90
commit 28cadcc
Showing
10 changed files
with
288 additions
and
4 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
dotnet/samples/Concepts/ChatCompletion/Anthropic_ChatCompletion.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,68 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
|
||
namespace ChatCompletion; | ||
|
||
public sealed class Anthropic_ChatCompletion(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
[Fact] | ||
public async Task SampleAsync() | ||
{ | ||
Console.WriteLine("============= Anthropic - Claude Chat Completion ============="); | ||
|
||
string apiKey = TestConfiguration.AnthropicAI.ApiKey; | ||
string modelId = TestConfiguration.AnthropicAI.ModelId; | ||
|
||
Assert.NotNull(apiKey); | ||
Assert.NotNull(modelId); | ||
|
||
Kernel kernel = Kernel.CreateBuilder() | ||
.AddAnthropicChatCompletion( | ||
modelId: modelId, | ||
apiKey: apiKey) | ||
.Build(); | ||
|
||
await SimpleChatAsync(kernel); | ||
} | ||
|
||
private async Task SimpleChatAsync(Kernel kernel) | ||
{ | ||
Console.WriteLine("======== Simple Chat ========"); | ||
|
||
var chatHistory = new ChatHistory("You are an expert in the tool shop."); | ||
var chat = kernel.GetRequiredService<IChatCompletionService>(); | ||
|
||
// First user message | ||
chatHistory.AddUserMessage("Hi, I'm looking for new power tools, any suggestion?"); | ||
await MessageOutputAsync(chatHistory); | ||
|
||
// First bot assistant message | ||
var reply = await chat.GetChatMessageContentAsync(chatHistory); | ||
chatHistory.Add(reply); | ||
await MessageOutputAsync(chatHistory); | ||
|
||
// Second user message | ||
chatHistory.AddUserMessage("I'm looking for a drill, a screwdriver and a hammer."); | ||
await MessageOutputAsync(chatHistory); | ||
|
||
// Second bot assistant message | ||
reply = await chat.GetChatMessageContentAsync(chatHistory); | ||
chatHistory.Add(reply); | ||
await MessageOutputAsync(chatHistory); | ||
} | ||
|
||
/// <summary> | ||
/// Outputs the last message of the chat history | ||
/// </summary> | ||
private Task MessageOutputAsync(ChatHistory chatHistory) | ||
{ | ||
var message = chatHistory.Last(); | ||
|
||
Console.WriteLine($"{message.Role}: {message.Content}"); | ||
Console.WriteLine("------------------------"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
dotnet/samples/Concepts/ChatCompletion/Anthropic_ChatCompletionStreaming.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,90 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
|
||
namespace ChatCompletion; | ||
|
||
public sealed class Anthropic_ChatCompletionStreaming(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
[Fact] | ||
public async Task SampleAsync() | ||
{ | ||
Console.WriteLine("============= Anthropic - Claude Chat Streaming ============="); | ||
|
||
string apiKey = TestConfiguration.AnthropicAI.ApiKey; | ||
string modelId = TestConfiguration.AnthropicAI.ModelId; | ||
|
||
Assert.NotNull(apiKey); | ||
Assert.NotNull(modelId); | ||
|
||
Kernel kernel = Kernel.CreateBuilder() | ||
.AddAnthropicChatCompletion( | ||
modelId: modelId, | ||
apiKey: apiKey) | ||
.Build(); | ||
|
||
await this.StreamingChatAsync(kernel); | ||
} | ||
|
||
private async Task StreamingChatAsync(Kernel kernel) | ||
{ | ||
Console.WriteLine("======== Streaming Chat ========"); | ||
|
||
var chatHistory = new ChatHistory("You are an expert in the tool shop."); | ||
var chat = kernel.GetRequiredService<IChatCompletionService>(); | ||
|
||
// First user message | ||
chatHistory.AddUserMessage("Hi, I'm looking for alternative coffee brew methods, can you help me?"); | ||
await MessageOutputAsync(chatHistory); | ||
|
||
// First bot assistant message | ||
var streamingChat = chat.GetStreamingChatMessageContentsAsync(chatHistory); | ||
var reply = await MessageOutputAsync(streamingChat); | ||
chatHistory.Add(reply); | ||
|
||
// Second user message | ||
chatHistory.AddUserMessage("Give me the best speciality coffee roasters."); | ||
await MessageOutputAsync(chatHistory); | ||
|
||
// Second bot assistant message | ||
streamingChat = chat.GetStreamingChatMessageContentsAsync(chatHistory); | ||
reply = await MessageOutputAsync(streamingChat); | ||
chatHistory.Add(reply); | ||
} | ||
|
||
/// <summary> | ||
/// Outputs the last message of the chat history | ||
/// </summary> | ||
private Task MessageOutputAsync(ChatHistory chatHistory) | ||
{ | ||
var message = chatHistory.Last(); | ||
|
||
Console.WriteLine($"{message.Role}: {message.Content}"); | ||
Console.WriteLine("------------------------"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private async Task<ChatMessageContent> MessageOutputAsync(IAsyncEnumerable<StreamingChatMessageContent> streamingChat) | ||
{ | ||
bool first = true; | ||
StringBuilder messageBuilder = new(); | ||
await foreach (var chatMessage in streamingChat) | ||
{ | ||
if (first) | ||
{ | ||
Console.Write($"{chatMessage.Role}: "); | ||
first = false; | ||
} | ||
|
||
Console.Write(chatMessage.Content); | ||
messageBuilder.Append(chatMessage.Content); | ||
} | ||
|
||
Console.WriteLine(); | ||
Console.WriteLine("------------------------"); | ||
return new ChatMessageContent(AuthorRole.Assistant, messageBuilder.ToString()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
dotnet/samples/Concepts/ChatCompletion/Anthropic_ProvidersSetup.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,33 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel; | ||
|
||
namespace ChatCompletion; | ||
|
||
/// <summary> | ||
/// This sample shows how to setup different providers for anthropic. | ||
/// </summary> | ||
public sealed class Anthropic_ProvidersSetup(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
public void AnthropicProvider() | ||
{ | ||
var kernel = Kernel.CreateBuilder() | ||
.AddAnthropicChatCompletion( | ||
modelId: "modelId", | ||
apiKey: "apiKey") | ||
.Build(); | ||
} | ||
|
||
/// <summary> | ||
/// For more information on how to setup the Vertex AI provider, go to <see cref="Google_GeminiChatCompletion"/> sample. | ||
/// </summary> | ||
public void VertexAiProvider() | ||
{ | ||
var kernel = Kernel.CreateBuilder() | ||
.AddAnthropicVertextAIChatCompletion( | ||
modelId: "modelId", | ||
bearerTokenProvider: () => ValueTask.FromResult("bearer"), | ||
endpoint: new Uri("https://your-endpoint")) | ||
.Build(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
dotnet/samples/Concepts/ChatCompletion/Anthropic_Vision.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,52 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Resources; | ||
|
||
namespace ChatCompletion; | ||
|
||
public sealed class Anthropic_Vision(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
[Fact] | ||
public async Task SampleAsync() | ||
{ | ||
Console.WriteLine("============= Anthropic - Claude Chat Completion ============="); | ||
|
||
string apiKey = TestConfiguration.AnthropicAI.ApiKey; | ||
string modelId = TestConfiguration.AnthropicAI.ModelId; | ||
|
||
Assert.NotNull(apiKey); | ||
Assert.NotNull(modelId); | ||
|
||
Kernel kernel = Kernel.CreateBuilder() | ||
.AddAnthropicChatCompletion( | ||
modelId: modelId, | ||
apiKey: apiKey) | ||
.Build(); | ||
|
||
var chatHistory = new ChatHistory("Your job is describing images."); | ||
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>(); | ||
|
||
// Load the image from the resources | ||
await using var stream = EmbeddedResource.ReadStream("sample_image.jpg")!; | ||
using var binaryReader = new BinaryReader(stream); | ||
var bytes = binaryReader.ReadBytes((int)stream.Length); | ||
|
||
chatHistory.AddUserMessage( | ||
[ | ||
new TextContent("What’s in this image?"), | ||
// Vertex AI Gemini API supports both base64 and URI format | ||
// You have to always provide the mimeType for the image | ||
new ImageContent(bytes, "image/jpeg"), | ||
// The Cloud Storage URI of the image to include in the prompt. | ||
// The bucket that stores the file must be in the same Google Cloud project that's sending the request. | ||
// new ImageContent(new Uri("gs://generativeai-downloads/images/scones.jpg"), | ||
// metadata: new Dictionary<string, object?> { { "mimeType", "image/jpeg" } }) | ||
]); | ||
|
||
var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory); | ||
|
||
Console.WriteLine(reply.Content); | ||
} | ||
} |
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
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