-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
312 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
appsettings.json |
35 changes: 35 additions & 0 deletions
35
Codeblaze.SemanticKernel.Console/Codeblaze.SemanticKernel.Console.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,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="..\.dockerignore"> | ||
<Link>.dockerignore</Link> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
<PackageReference Include="Spectre.Console" Version="0.48.0" /> | ||
<PackageReference Include="Spectre.Console.Json" Version="0.48.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="*.json" | ||
CopyToOutputDirectory="Always" | ||
CopyToPublishDirectory="Always" | ||
/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Codeblaze.SemanticKernel.Connectors.AI.Ollama\Codeblaze.SemanticKernel.Connectors.AI.Ollama.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,21 @@ | ||
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base | ||
USER $APP_UID | ||
WORKDIR /app | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build | ||
ARG BUILD_CONFIGURATION=Release | ||
WORKDIR /src | ||
COPY ["Codeblaze.SemanticKernel.Console/Codeblaze.SemanticKernel.Console.csproj", "Codeblaze.SemanticKernel.Console/"] | ||
RUN dotnet restore "Codeblaze.SemanticKernel.Console/Codeblaze.SemanticKernel.Console.csproj" | ||
COPY . . | ||
WORKDIR "/src/Codeblaze.SemanticKernel.Console" | ||
RUN dotnet build "Codeblaze.SemanticKernel.Console.csproj" -c $BUILD_CONFIGURATION -o /app/build | ||
|
||
FROM build AS publish | ||
ARG BUILD_CONFIGURATION=Release | ||
RUN dotnet publish "Codeblaze.SemanticKernel.Console.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "Codeblaze.SemanticKernel.Console.dll"] |
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,74 @@ | ||
using Codeblaze.SemanticKernel.Console.Services; | ||
using Microsoft.Extensions.Configuration; | ||
using Spectre.Console; | ||
|
||
var config = new ConfigurationBuilder() | ||
.AddJsonFile("appsettings.json") | ||
.Build(); | ||
|
||
AnsiConsole.Write(new FigletText($"{config["Name"]!}").Color(Color.Green)); | ||
AnsiConsole.WriteLine(""); | ||
|
||
KernelService kernel = null; | ||
|
||
AnsiConsole.Status().Start("Initializing...", ctx => | ||
{ | ||
ctx.Spinner(Spinner.Known.Star); | ||
ctx.SpinnerStyle(Style.Parse("green")); | ||
kernel = new KernelService(config); | ||
ctx.Status("Initialized"); | ||
}); | ||
|
||
const string prompt = "1.\tPrompt kernel"; | ||
const string exit = "2.\tExit"; | ||
|
||
Run(); | ||
|
||
return; | ||
|
||
void Run() | ||
{ | ||
while (true) | ||
{ | ||
var option = AnsiConsole.Prompt( | ||
new SelectionPrompt<string>() | ||
.Title("Select an option") | ||
.PageSize(10) | ||
.MoreChoicesText("[grey](Move up and down to reveal more options)[/]") | ||
.AddChoices(prompt, exit) | ||
); | ||
|
||
switch (option) | ||
{ | ||
case prompt: | ||
Prompt().GetAwaiter().GetResult(); | ||
break; | ||
case exit: | ||
return; | ||
} | ||
} | ||
} | ||
|
||
async Task Prompt() | ||
{ | ||
var prompt = AnsiConsole.Prompt(new TextPrompt<string>("What are you looking to do today ?").PromptStyle("teal")); | ||
|
||
var result = string.Empty; | ||
|
||
await AnsiConsole.Status().StartAsync("Processing...", async ctx => | ||
{ | ||
ctx.Spinner(Spinner.Known.Star); | ||
ctx.SpinnerStyle(Style.Parse("green")); | ||
ctx.Status($"Processing input to generate Chat Response"); | ||
result = await kernel.BasicPrompt(prompt); | ||
}); | ||
|
||
AnsiConsole.WriteLine(""); | ||
AnsiConsole.Write(new Rule($"[silver]AI Assistant Response[/]") { Justification = Justify.Center }); | ||
AnsiConsole.WriteLine(result); | ||
AnsiConsole.Write(new Rule($"[yellow]****[/]") { Justification = Justify.Center }); | ||
AnsiConsole.WriteLine(""); | ||
} |
59 changes: 59 additions & 0 deletions
59
Codeblaze.SemanticKernel.Console/Services/KernelService.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,59 @@ | ||
using Codeblaze.SemanticKernel.Connectors.AI.Ollama; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.AI.ChatCompletion; | ||
using Microsoft.SemanticKernel.Memory; | ||
|
||
namespace Codeblaze.SemanticKernel.Console.Services; | ||
|
||
public class KernelService | ||
{ | ||
private readonly Kernel _Kernel; | ||
|
||
public KernelService(IConfiguration config) | ||
{ | ||
var builder = new KernelBuilder(); | ||
|
||
builder.Services.AddTransient<HttpClient>(); | ||
|
||
builder | ||
.AddOllamaTextGeneration(config["Ollama:Model"], config["Ollama:BaseUrlGeneration"]); | ||
Check warning on line 21 in Codeblaze.SemanticKernel.Console/Services/KernelService.cs GitHub Actions / create_nuget
Check warning on line 21 in Codeblaze.SemanticKernel.Console/Services/KernelService.cs GitHub Actions / create_nuget
|
||
|
||
_Kernel = builder.Build(); | ||
} | ||
|
||
public async Task<string> BasicPrompt(string input) | ||
{ | ||
const string prompt = """ | ||
Bot: How can I help you? | ||
User: {{$input}} | ||
|
||
--------------------------------------------- | ||
|
||
The intent of the user in 5 words or less: | ||
"""; | ||
|
||
var result = await _Kernel.InvokePromptAsync(prompt, new KernelArguments | ||
{ | ||
{"input", input} | ||
}); | ||
|
||
return result.GetValue<string>(); | ||
} | ||
|
||
public async Task<string> BasicChat(string input) | ||
{ | ||
var chat = _Kernel.GetRequiredService<IChatCompletionService>(); | ||
|
||
var history = new ChatHistory(); | ||
|
||
history.AddSystemMessage("..."); | ||
history.AddAssistantMessage("..."); | ||
history.AddUserMessage(input); | ||
|
||
var result = await chat.GetChatMessageContentsAsync(history); | ||
|
||
return result[^1].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