Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Generation and Chat Completion #1

Merged
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
6362c1e
initial chat completion start charyeh
charyeh Jul 1, 2024
ce24af6
created diff clients per service
charyeh Jul 2, 2024
394ebed
working on generatechatmessageasync, converse req and res bodies needed
charyeh Jul 3, 2024
d32a026
rough text and chat completion with titan written out, need to write …
charyeh Jul 3, 2024
909d59b
sample converse call gets a response
charyeh Jul 3, 2024
7ebee0b
titan chat completion connected, titan text gen request obj still buggy
charyeh Jul 4, 2024
924b585
got titan text gen connected (rough untemplated)
charyeh Jul 5, 2024
9c97764
titan text generation and chat completion connected
charyeh Jul 5, 2024
66be7e4
mistral request body built
charyeh Jul 5, 2024
ff102d3
mistral chat connected too, this one accounts for prompt execution se…
charyeh Jul 8, 2024
76a042c
mistral chat iterative works, chat history retained
charyeh Jul 8, 2024
cc5adfe
claude chat completion connected, tool calling enabled (didn't test t…
charyeh Jul 8, 2024
cdd249c
ai21 chat completion connected
charyeh Jul 8, 2024
ab567eb
cohere chat completion connected, tool config still buggy
charyeh Jul 9, 2024
bfc7d29
llama chat completion connected
charyeh Jul 9, 2024
d16d533
BedrockTest sample moved to Demos
charyeh Jul 9, 2024
78035c3
mistral text gen connected
charyeh Jul 9, 2024
4617cb2
ai21 jamba text gen connected
charyeh Jul 9, 2024
9059a1d
claude text gen connected
charyeh Jul 9, 2024
93abebf
command text gen connected (command r for chat, command for text
charyeh Jul 9, 2024
19eb570
llama text gen connected
charyeh Jul 9, 2024
0bc7928
stream text gen added for titan, claude, mistral, and command (not co…
charyeh Jul 9, 2024
4bd51cc
chat streaming connected for mistral and titan
charyeh Jul 10, 2024
a0ca077
command r and meta chat completion connected
charyeh Jul 10, 2024
2a31ae2
connected command r text gen. differentiated btwn command and command…
charyeh Jul 10, 2024
6b26c1d
cohere command r chat history bug fix
charyeh Jul 11, 2024
5f4c170
fixed IO naming conventions
charyeh Jul 11, 2024
09e8a9a
connected text gen/invoke for ai21 jurassic. streaming & converse not…
charyeh Jul 11, 2024
6e9da83
Code clean up. Removed/replaced all initial hard-coded settings. All …
charyeh Jul 11, 2024
1749a1a
basic chat completion service unit test added. minor client and servi…
charyeh Jul 12, 2024
c264906
basic text gen unit test written. streaming fails
charyeh Jul 13, 2024
8ff2fcb
still working on streaming unit test
charyeh Jul 15, 2024
64a8487
added promp exec settings and streaming unit tests to chat and text
charyeh Jul 16, 2024
c196378
added unit tests for role assigning and chat history configurations
charyeh Jul 16, 2024
c5f72e0
added kernel builder extension unit tests
charyeh Jul 16, 2024
01d657f
added some more unit tests and started adding XML descriptions
charyeh Jul 17, 2024
2009f0d
added XML descriptions, fixed like 600 build warnings
charyeh Jul 17, 2024
1460cae
fixed breaking changes on llama and command r. readded xml and fixed …
charyeh Jul 17, 2024
6b09044
added all user input options to authenticate AmazonBedrockRuntimeClie…
charyeh Jul 18, 2024
c35f048
empty chat message error handling
charyeh Jul 18, 2024
bebbf4c
corrected chat completion unit tests (no new added)
charyeh Jul 18, 2024
a625737
program lets user choose model from list now, fixed all sample progra…
charyeh Jul 19, 2024
3ef0559
resolved all comments from first batch: string.Split, modelName.Start…
charyeh Jul 19, 2024
9b2852d
added prompt exec settings tests for all models for chat and text. la…
charyeh Jul 19, 2024
1bf86b4
basic integration tests for chat completion and text generation (with…
charyeh Jul 19, 2024
71c701b
integration tests added for all models for chat completion and text g…
charyeh Jul 20, 2024
968e235
version dependent execution defaults within model types set (anthropi…
charyeh Jul 22, 2024
eae2216
comment clean ups. unit test clean up
charyeh Jul 23, 2024
2d4ef9b
utilities file for maprole and getextensiondatavalue
charyeh Jul 23, 2024
a1bd68d
creates service once in test run file (BedrockTest/Program.cs)
charyeh Jul 23, 2024
80908da
moved ioService constructor for service specific clients to utility c…
charyeh Jul 24, 2024
ebcd65b
set magic numbers to defaults, corrected llama and command r prompt e…
charyeh Jul 24, 2024
79789cc
removed intermediary request objects in invoke and converse for all m…
charyeh Jul 24, 2024
a62e0ca
BedrockTest program.cs lets user pick service to run, cleaned up unit…
charyeh Jul 24, 2024
49e3067
deleted unneeeded unit test program.cs file
charyeh Jul 24, 2024
3ca3a34
arrange act assert kernel builder extension unit tests
charyeh Jul 24, 2024
3c66c16
ran dotnet format for unit tests, moved CreateSampleChatHistory to top
charyeh Jul 24, 2024
e1e85c3
dotnet format ran for BedrockTest (demo file)
charyeh Jul 24, 2024
11d4b67
fixed commandr converse request param bug, moved converseRequest inst…
charyeh Jul 24, 2024
5c2c418
request object passed into DetermineServiceOperationEndpoint, adds IA…
charyeh Jul 24, 2024
6b26ab1
kernel builder only has two methods which takes IAmazonBedrockRuntime…
charyeh Jul 24, 2024
05ad681
unit test cs proj file update
charyeh Jul 24, 2024
2f88fc2
kernel clean up
charyeh Jul 24, 2024
1406b53
llama chat request clean up. user will follow prompt template
charyeh Jul 25, 2024
44cd9d4
chat stream unit test endpoint set
charyeh Jul 25, 2024
34e0d27
changed BedrockUtilities to BedrockModelUtilities
charyeh Jul 25, 2024
d4ab615
dotnet format Connectors.Amazon and Connectors.Amazon.UnitTests
charyeh Jul 25, 2024
b0f0cc5
configured chat history system messages for all models
charyeh Jul 25, 2024
420a35a
cleaned up bedrocktextgenerationservice client configuration options
charyeh Jul 25, 2024
1b04d03
added error messages for models that don't support certain features (…
charyeh Jul 25, 2024
406dd47
let user input aws credentials or access key + token using NETCore DI
charyeh Jul 26, 2024
fcba613
uncommented experimental tag + added ignore to csproj, made bedrockut…
charyeh Jul 27, 2024
3a107e1
made configure await false for client classes
charyeh Jul 27, 2024
33fdcb8
fixed some warning (not done)
charyeh Jul 27, 2024
963c81c
fixed all build warnings
charyeh Jul 29, 2024
b1983ae
remove extra kernel builder option (just modelid, runtime obj, or aws…
charyeh Jul 29, 2024
a029e35
removed all request class objects (except mistral which has diff stru…
charyeh Jul 29, 2024
4585cd2
removed interfaces for chat and text request and response
charyeh Jul 29, 2024
3dbc8f7
clean up mistral. no tool use
charyeh Jul 30, 2024
05edcfb
removed taking in aws credentials
charyeh Jul 30, 2024
5b2e6b1
removed system.json in csproj, dependency on sk core
charyeh Jul 30, 2024
656f2ad
remove SKEXP0010 warn in csproj
charyeh Jul 30, 2024
4ba3722
client class newed up in service (not base class), took out chat stre…
charyeh Jul 30, 2024
0225974
condense ToList in chat client
charyeh Jul 30, 2024
f422673
add completion activity to text gen, client utilities class added (ma…
charyeh Jul 30, 2024
57e6544
catch all exceptions and log bedrock runtime error in clients
charyeh Jul 30, 2024
d386d70
jamba supports streaming (jurassic doesnt)
charyeh Jul 30, 2024
e94ee7f
moved last chat history message null/whitespace check to buildmessage…
charyeh Jul 30, 2024
37554bb
dotnet format ran for Connectors.Amazon
charyeh Jul 30, 2024
bd9e9a8
set endpoint for text gen unit test, check null message chat history
charyeh Jul 30, 2024
a94980f
new up clientIOservice once only
charyeh Jul 31, 2024
a40f65e
clearer error message for jurassic
charyeh Jul 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="3.7.306.1" />
<PackageVersion Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.301" />
<PackageVersion Include="Azure.AI.ContentSafety" Version="1.0.0" />
<PackageVersion Include="Azure.AI.OpenAI" Version="1.0.0-beta.17" />
<PackageVersion Include="Azure.AI.OpenAI.Assistants" Version="1.0.0-beta.3" />
Expand All @@ -30,6 +32,7 @@
<PackageVersion Include="Microsoft.Identity.Client" Version="4.61.3" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.18.0" />
<PackageVersion Include="FastBertTokenizer" Version="1.0.28" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.15.1" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Memory.Data" Version="8.0.0" />
Expand Down
27 changes: 27 additions & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimePlugin", "samples\Demos
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Memory.AzureCosmosDBNoSQL", "src\Connectors\Connectors.Memory.AzureCosmosDBNoSQL\Connectors.Memory.AzureCosmosDBNoSQL.csproj", "{B0B3901E-AF56-432B-8FAA-858468E5D0DF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.Amazon", "src\Connectors\Connectors.Amazon\Connectors.Amazon.csproj", "{E059E9B0-1302-474D-B1B5-10A6E0F1A769}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BedrockTest", "samples\Demos\BedrockTest\BedrockTest.csproj", "{ABEAACCD-CF63-4850-8ED5-E01379DBFC46}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.Amazon.UnitTests", "src\Connectors\Connectors.Amazon.UnitTests\Connectors.Amazon.UnitTests.csproj", "{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -771,6 +777,24 @@ Global
{B0B3901E-AF56-432B-8FAA-858468E5D0DF}.Publish|Any CPU.Build.0 = Publish|Any CPU
{B0B3901E-AF56-432B-8FAA-858468E5D0DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0B3901E-AF56-432B-8FAA-858468E5D0DF}.Release|Any CPU.Build.0 = Release|Any CPU
{E059E9B0-1302-474D-B1B5-10A6E0F1A769}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E059E9B0-1302-474D-B1B5-10A6E0F1A769}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E059E9B0-1302-474D-B1B5-10A6E0F1A769}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
{E059E9B0-1302-474D-B1B5-10A6E0F1A769}.Publish|Any CPU.Build.0 = Debug|Any CPU
{E059E9B0-1302-474D-B1B5-10A6E0F1A769}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E059E9B0-1302-474D-B1B5-10A6E0F1A769}.Release|Any CPU.Build.0 = Release|Any CPU
{ABEAACCD-CF63-4850-8ED5-E01379DBFC46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABEAACCD-CF63-4850-8ED5-E01379DBFC46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABEAACCD-CF63-4850-8ED5-E01379DBFC46}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
{ABEAACCD-CF63-4850-8ED5-E01379DBFC46}.Publish|Any CPU.Build.0 = Debug|Any CPU
{ABEAACCD-CF63-4850-8ED5-E01379DBFC46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABEAACCD-CF63-4850-8ED5-E01379DBFC46}.Release|Any CPU.Build.0 = Release|Any CPU
{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E}.Publish|Any CPU.Build.0 = Debug|Any CPU
{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -877,6 +901,9 @@ Global
{1D3EEB5B-0E06-4700-80D5-164956E43D0A} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
{F312FCE1-12D7-4DEF-BC29-2FF6618509F3} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
{B0B3901E-AF56-432B-8FAA-858468E5D0DF} = {24503383-A8C4-4255-9998-28D70FE8E99A}
{E059E9B0-1302-474D-B1B5-10A6E0F1A769} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
{ABEAACCD-CF63-4850-8ED5-E01379DBFC46} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
{CCC6DC57-2AC1-4C8E-A448-2CC0537A288E} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down
18 changes: 18 additions & 0 deletions dotnet/samples/Demos/BedrockTest/BedrockTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.BedrockRuntime" />
<PackageReference Include="Microsoft.SemanticKernel"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Connectors\Connectors.Amazon\Connectors.Amazon.csproj"/>
</ItemGroup>

</Project>
233 changes: 233 additions & 0 deletions dotnet/samples/Demos/BedrockTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// Copyright (c) Microsoft. All rights reserved.

using Connectors.Amazon.Extensions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.TextGeneration;

// Display the available options
Console.WriteLine("Choose an option:");
Console.WriteLine("1. Chat Completion");
Console.WriteLine("2. Text Generation");
Console.WriteLine("3. Stream Chat Completion");
Console.WriteLine("4. Stream Text Generation");

Console.Write("Enter your choice (1-4): ");
int choice;
while (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 4)
{
Console.WriteLine("Invalid input. Please enter a valid number from the list.");
Console.Write("Enter your choice (1-4): ");
}

switch (choice)
{
case 1:
// ----------------------------CHAT COMPLETION----------------------------
string userInput;
ChatHistory chatHistory = new();

// List of available models
Dictionary<int, string> modelOptions = new()
{
{ 1, "amazon.titan-text-premier-v1:0" },
{ 2, "anthropic.claude-3-sonnet-20240229-v1:0" },
{ 3, "anthropic.claude-3-haiku-20240307-v1:0" },
{ 4, "anthropic.claude-v2:1" },
{ 5, "ai21.jamba-instruct-v1:0" },
{ 6, "cohere.command-r-plus-v1:0" },
{ 7, "meta.llama3-8b-instruct-v1:0" },
{ 8, "mistral.mistral-7b-instruct-v0:2" }
};

// Display the model options
Console.WriteLine("Available models:");
foreach (var option in modelOptions)
{
Console.WriteLine($"{option.Key}. {option.Value}");
}

Console.Write("Enter the number of the model you want to use for chat completion: ");
int chosenModel;
while (!int.TryParse(Console.ReadLine(), out chosenModel) || !modelOptions.ContainsKey(chosenModel))
{
Console.WriteLine("Invalid input. Please enter a valid number from the list.");
Console.Write("Enter the number of the model you want to use: ");
}

var kernel = Kernel.CreateBuilder().AddBedrockChatCompletionService(modelOptions[chosenModel]).Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

do
{
Console.Write("Enter a prompt (or 'exit' to quit): ");
userInput = Console.ReadLine() ?? "exit";

if (!string.Equals(userInput, "exit", StringComparison.OrdinalIgnoreCase))
{
chatHistory.AddMessage(AuthorRole.User, userInput);
var result = await chatCompletionService.GetChatMessageContentsAsync(chatHistory).ConfigureAwait(false);
string output = "";
foreach (var message in result)
{
output += message.Content;
Console.WriteLine($"Chat Completion Answer: {message.Content}");
Console.WriteLine();
}
chatHistory.AddMessage(AuthorRole.Assistant, output);
}
} while (!string.Equals(userInput, "exit", StringComparison.OrdinalIgnoreCase));
break;
case 2:
// ----------------------------TEXT GENERATION----------------------------
// List of available text generation models
Dictionary<int, string> textGenerationModelOptions = new()
{
{ 1, "amazon.titan-text-premier-v1:0" },
{ 2, "mistral.mistral-7b-instruct-v0:2" },
{ 3, "ai21.jamba-instruct-v1:0" },
{ 4, "anthropic.claude-v2:1" },
{ 5, "cohere.command-text-v14" },
{ 6, "meta.llama3-8b-instruct-v1:0" },
{ 7, "cohere.command-r-plus-v1:0" },
{ 8, "ai21.j2-ultra-v1" }
};

// Display the text generation model options
Console.WriteLine("Available text generation models:");
foreach (var option in textGenerationModelOptions)
{
Console.WriteLine($"{option.Key}. {option.Value}");
}

Console.Write("Enter the number of the text generation model you want to use: ");
int chosenTextGenerationModel;
while (!int.TryParse(Console.ReadLine(), out chosenTextGenerationModel) || !textGenerationModelOptions.ContainsKey(chosenTextGenerationModel))
{
Console.WriteLine("Invalid input. Please enter a valid number from the list.");
Console.Write("Enter the number of the text generation model you want to use: ");
}

Console.Write("Text Generation Prompt: ");
string UserPrompt2 = Console.ReadLine() ?? "";

var kernel2 = Kernel.CreateBuilder().AddBedrockTextGenerationService(textGenerationModelOptions[chosenTextGenerationModel]).Build();

var textGenerationService = kernel2.GetRequiredService<ITextGenerationService>();
var textGeneration = await textGenerationService.GetTextContentsAsync(UserPrompt2).ConfigureAwait(false);
if (textGeneration.Count > 0)
{
var firstTextContent = textGeneration[0];
if (firstTextContent != null)
{
Console.WriteLine("Text Generation Answer: " + firstTextContent.Text);
}
else
{
Console.WriteLine("Text Generation Answer: (none)");
}
}
else
{
Console.WriteLine("Text Generation Answer: (No output text)");
}
break;
case 3:
// ----------------------------STREAM CHAT COMPLETION----------------------------
string userInput2;
ChatHistory chatHistory2 = new();

// List of available stream chat completion models
Dictionary<int, string> streamChatCompletionModelOptions = new()
{
{ 1, "mistral.mistral-7b-instruct-v0:2" },
{ 2, "amazon.titan-text-premier-v1:0" },
{ 3, "anthropic.claude-v2" },
{ 4, "anthropic.claude-3-sonnet-20240229-v1:0" },
{ 5, "cohere.command-r-plus-v1:0" },
{ 6, "meta.llama3-8b-instruct-v1:0" }
};

// Display the stream chat completion model options
Console.WriteLine("Available stream chat completion models:");
foreach (var option in streamChatCompletionModelOptions)
{
Console.WriteLine($"{option.Key}. {option.Value}");
}

Console.Write("Enter the number of the stream chat completion model you want to use: ");
int chosenStreamChatCompletionModel;
while (!int.TryParse(Console.ReadLine(), out chosenStreamChatCompletionModel) || !streamChatCompletionModelOptions.ContainsKey(chosenStreamChatCompletionModel))
{
Console.WriteLine("Invalid input. Please enter a valid number from the list.");
Console.Write("Enter the number of the stream chat completion model you want to use: ");
}

var kernel4 = Kernel.CreateBuilder().AddBedrockChatCompletionService(streamChatCompletionModelOptions[chosenStreamChatCompletionModel]).Build();
var chatStreamCompletionService = kernel4.GetRequiredService<IChatCompletionService>();

do
{
Console.Write("Enter a prompt (or 'exit' to quit): ");
userInput2 = Console.ReadLine() ?? "exit";

if (!string.Equals(userInput2, "exit", StringComparison.OrdinalIgnoreCase))
{
chatHistory2.AddMessage(AuthorRole.User, userInput2);
var result = chatStreamCompletionService.GetStreamingChatMessageContentsAsync(chatHistory2).ConfigureAwait(false);
string output = "";
await foreach (var message in result)
{
Console.Write($"{message.Content}");
Thread.Sleep(50);
output += message.Content;
}
Console.WriteLine();
chatHistory2.AddMessage(AuthorRole.Assistant, output);
}
} while (!string.Equals(userInput2, "exit", StringComparison.OrdinalIgnoreCase));
break;
case 4:
// ----------------------------STREAM TEXT GENERATION----------------------------
// List of available stream text generation models
Dictionary<int, string> streamTextGenerationModelOptions = new()
{
{ 1, "amazon.titan-text-premier-v1:0" },
{ 2, "anthropic.claude-v2" },
{ 3, "mistral.mistral-7b-instruct-v0:2" },
{ 4, "cohere.command-text-v14" },
{ 5, "cohere.command-r-plus-v1:0" },
{ 6, "meta.llama3-8b-instruct-v1:0" }
};

// Display the stream text generation model options
Console.WriteLine("Available stream text generation models:");
foreach (var option in streamTextGenerationModelOptions)
{
Console.WriteLine($"{option.Key}. {option.Value}");
}

Console.Write("Enter the number of the stream text generation model you want to use: ");
int chosenStreamTextGenerationModel;
while (!int.TryParse(Console.ReadLine(), out chosenStreamTextGenerationModel) || !streamTextGenerationModelOptions.ContainsKey(chosenStreamTextGenerationModel))
{
Console.WriteLine("Invalid input. Please enter a valid number from the list.");
Console.Write("Enter the number of the stream text generation model you want to use: ");
}

Console.Write("Stream Text Generation Prompt: ");
string UserPrompt3 = Console.ReadLine() ?? "";

var kernel3 = Kernel.CreateBuilder().AddBedrockTextGenerationService(streamTextGenerationModelOptions[chosenStreamTextGenerationModel]).Build();

var streamTextGenerationService = kernel3.GetRequiredService<ITextGenerationService>();
var streamTextGeneration = streamTextGenerationService.GetStreamingTextContentsAsync(UserPrompt3).ConfigureAwait(true);
await foreach (var textContent in streamTextGeneration)
{
Console.Write(textContent.Text);
Thread.Sleep(50);
}

Console.WriteLine();
break;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft. All rights reserved.

using Connectors.Amazon.Extensions;
using Connectors.Amazon.Services;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Amazon.Services;
using Microsoft.SemanticKernel.TextGeneration;
using Xunit;

namespace Connectors.Amazon.UnitTests;

/// <summary>
/// Kernel Builder Extension Tests for Bedrock.
/// </summary>
public class BedrockKernelBuilderExtensionTests
{
/// <summary>
/// Checks that AddBedrockTextGenerationService builds a proper kernel.
/// </summary>
[Fact]
public void AddBedrockTextGenerationCreatesService()
{
// Arrange
var builder = Kernel.CreateBuilder();
builder.AddBedrockTextGenerationService("amazon.titan-text-premier-v1:0");

// Act
var kernel = builder.Build();
var service = kernel.GetRequiredService<ITextGenerationService>();

// Assert
Assert.NotNull(kernel);
charlizeyeh marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotNull(service);
Assert.IsType<BedrockTextGenerationService>(service);
}
/// <summary>
/// Checks that AddBedrockChatCompletionService builds a proper kernel.
/// </summary>
[Fact]
public void AddBedrockChatCompletionCreatesService()
{
// Arrange
var builder = Kernel.CreateBuilder();
builder.AddBedrockChatCompletionService("amazon.titan-text-premier-v1:0");

// Act
var kernel = builder.Build();
var service = kernel.GetRequiredService<IChatCompletionService>();

// Assert
Assert.NotNull(kernel);
Assert.NotNull(service);
Assert.IsType<BedrockChatCompletionService>(service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.BedrockRuntime" />
<PackageReference Include="Microsoft.SemanticKernel" />
<PackageReference Include="Moq"/>
<PackageReference Include="xunit"/>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj"/>
<ProjectReference Include="..\Connectors.Amazon\Connectors.Amazon.csproj"/>
</ItemGroup>

</Project>
Loading