Skip to content

Commit

Permalink
Supporting more llms and multple commands and improved prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
wisamidris77 committed Dec 29, 2024
1 parent 75b5e33 commit 77cc217
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 149 deletions.
2 changes: 1 addition & 1 deletion AiTerminal/Abstraction/IAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace AiTerminal.Abstraction
{
public interface IAiClient
{
Task<string> GenerateContent(ChatContents contents, ContentModel systemInstructions);
Task<string?> GenerateContent(ChatContents contents, ContentModel systemInstructions);
}
}
1 change: 1 addition & 0 deletions AiTerminal/AiTerminal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LlmTornado" Version="3.1.29" />
<PackageReference Include="Markdig" Version="0.39.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>
Expand Down
47 changes: 29 additions & 18 deletions AiTerminal/ChatApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AiTerminal.Abstraction;
using AiTerminal.Helpers;
Expand All @@ -22,36 +23,44 @@ public ChatApplication(IAiClient aiClient)
_systemInstruction = SystemInstructions.Initialize();
}

public async Task Run()
public async Task<bool> Run()
{
Console.WriteLine("Gemini Terminal Assistant");
Console.WriteLine("Ai Terminal Assistant");
Console.WriteLine("---------------------");

while (true)
{
var prompt = GetUserInput();
if (prompt == "clear_data")
return true;
if (prompt == "exit")
return false;
if (string.IsNullOrWhiteSpace(prompt)) continue;
if (prompt == "exit") break;
_chatContents.AddContent(ContentModel.User([prompt]));
var content = await _aiClient.GenerateContent(_chatContents, _systemInstruction);
_chatContents.AddContent(ContentModel.Model([content]));
var (terminalCommand, explanation) = ParseContent(content);
var (terminalCommands, explanation) = ParseContent(content);

DisplayExplanation(explanation);

if (terminalCommand != null)
if (terminalCommands?.Any() ?? false)
{
DisplayCommand(terminalCommand);
if (GetUserConfirmation())
foreach (var terminalCommand in terminalCommands)
{
TerminalCommandRunner.ExecuteCommand(terminalCommand);
}
else
{
Console.WriteLine("Command execution skipped.");
DisplayCommand(terminalCommand);
if (GetUserConfirmation())
{
TerminalCommandRunner.ExecuteCommand(terminalCommand);
}
else
{
Console.WriteLine("Command execution skipped.");
}
}
}
}
return false;
}

private string? GetUserInput()
Expand All @@ -60,24 +69,26 @@ public async Task Run()
return Console.ReadLine();
}

private (string? terminalCommand, StringBuilder explanation) ParseContent(string content)
private (List<string>? terminalCommand, StringBuilder explanation) ParseContent(string content)
{
string? terminalCommand = null;
List<string>? terminalCommands = [];
StringBuilder explanation = new StringBuilder();

foreach (var item in content.Split('\n'))
{
if (terminalCommand == null && item.StartsWith("Terminal:", StringComparison.OrdinalIgnoreCase))
string pattern = @"^([""'`])|([""'`])$";
var parsedLine = Regex.Replace(item, pattern, "");
if (parsedLine.StartsWith("Terminal:", StringComparison.OrdinalIgnoreCase))
{
terminalCommand = item.Substring("Terminal:".Length).Trim();
terminalCommands.Add(parsedLine.Substring("Terminal:".Length).Trim());
}
else if (terminalCommand == null)
else
{
explanation.AppendLine(item);
}
}

return (terminalCommand, explanation);
return (terminalCommands, explanation);
}

private void DisplayExplanation(StringBuilder explanation)
Expand All @@ -89,7 +100,7 @@ private void DisplayCommand(string terminalCommand)
{
CliHelper.WriteColored("> ", ConsoleColor.Yellow);
CliHelper.WriteLineColored(terminalCommand, ConsoleColor.White);
CliHelper.WriteLineColored("Warning: Gemini might make mistakes with commands. Review carefully.", ConsoleColor.DarkYellow);
CliHelper.WriteLineColored("Warning: Ai might make mistakes with commands. Review carefully.", ConsoleColor.DarkYellow);
}

private bool GetUserConfirmation()
Expand Down
86 changes: 0 additions & 86 deletions AiTerminal/Clients/GeminiClient.cs

This file was deleted.

39 changes: 39 additions & 0 deletions AiTerminal/Clients/LlmTornadoClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiTerminal.Abstraction;
using AiTerminal.Models;
using AiTerminal.Providers;
using LlmTornado;
using LlmTornado.Chat.Models;
using LlmTornado.Models;
namespace AiTerminal.Clients
{
public class LlmTornadoClient(TornadoApi tornadoApi, ChatModel chatModel) : IAiClient
{
public async Task<string?> GenerateContent(ChatContents contents, ContentModel systemInstructions)
{
var conversion = tornadoApi.Chat.CreateConversation(chatModel);
foreach (var systemInstructionPart in systemInstructions.Parts)
{
conversion.AppendSystemMessage(systemInstructionPart.Text);
}
if (conversion != null)
{
foreach (var contentModel in contents.ContentModels)
{
foreach (var part in contentModel.Parts)
{
if (contentModel.Role == "user")
conversion.AppendUserInput(part.Text);
else
conversion.AppendExampleChatbotOutput(part.Text);
}
}
}
return await (conversion?.GetResponse() ?? Task.FromResult(default(string)));
}
}
}
45 changes: 34 additions & 11 deletions AiTerminal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,45 @@
using AiTerminal;
using AiTerminal.Abstraction;
using AiTerminal.Clients;
using LlmTornado.Chat.Models;
using LlmTornado.Code;
using LlmTornado;

public class Program
{

public static async Task Main(string[] args)
{
await ApiKeyProvider.InitApiKey();
IAiClient geminiClient = new GeminiClient(ApiKeyProvider.ApiKey);
var chatApp = new ChatApplication(geminiClient);
//if (args.Any())
//{
// await chatApp.RunCommand(args[0]); // Future plans
//}
//else
//{
await chatApp.Run();
//}
while (true)
{
//ApiKeyProvider.SelectedModel = ChatModel.Groq.Meta.Llama38B; // Custom models
await ApiKeyProvider.InitApiKeyAndModel();
TornadoApi api = new TornadoApi(new List<ProviderAuthentication>
{
new ProviderAuthentication(LLmProviders.OpenAi, ApiKeyProvider.ApiKey),
new ProviderAuthentication(LLmProviders.Anthropic, ApiKeyProvider.ApiKey),
new ProviderAuthentication(LLmProviders.Cohere, ApiKeyProvider.ApiKey),
new ProviderAuthentication(LLmProviders.Google, ApiKeyProvider.ApiKey),
new ProviderAuthentication(LLmProviders.Groq, ApiKeyProvider.ApiKey)
});


IAiClient aiClient = new LlmTornadoClient(api, ApiKeyProvider.SelectedModel);
var chatApp = new ChatApplication(aiClient);
//if (args.Any())
//{
// await chatApp.RunCommand(args[0]); // Future plans
//}
//else
//{
var response = await chatApp.Run();
if (response)
{
ApiKeyProvider.RemoveAiData();
continue;
}
break;
//}
}
}
}
Loading

0 comments on commit 77cc217

Please sign in to comment.