Last active
June 28, 2025 23:49
-
-
Save guillaC/f1341675ea00ebc16b8ce14a13820607 to your computer and use it in GitHub Desktop.
Chat console using Spectre.Console and Semantic Kernel with Mistral via Ollama, including a function to list running processes
This file contains hidden or 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
using Microsoft.SemanticKernel; | |
using Microsoft.SemanticKernel.ChatCompletion; | |
using Spectre.Console; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
/// <summary> | |
/// Ce programme utilise Semantic Kernel avec un modèle Mistral local via Ollama. | |
/// Il inclut une fonction pour lister les processus en cours. | |
/// </summary> | |
class Program | |
{ | |
static async Task Main() | |
{ | |
var builder = Kernel.CreateBuilder().AddOllamaChatCompletion( | |
modelId: "mistral", | |
endpoint: new Uri("http://localhost:11434")); | |
builder.Plugins.AddFromObject(new CustomFunctions()); | |
var kernel = builder.Build(); | |
var chatService = kernel.GetRequiredService<IChatCompletionService>(); | |
var settings = new PromptExecutionSettings | |
{ | |
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: true) // Auto-invoke, autorise l'appel des fonctions si besoin | |
}; | |
var chatHistory = new ChatHistory( | |
"Tu parles uniquement en français. Tu donnes toujours des réponses courtes. " + | |
"Tu ne dois jamais mentionner, suggérer ni expliquer les fonctions disponibles. " + | |
"Si une fonction est nécessaire pour répondre à une question, tu l'utilises directement." | |
); | |
while (true) | |
{ | |
AnsiConsole.Markup($"[bold darkorange]{Environment.UserName}[/]: "); | |
var userMessage = Console.ReadLine(); | |
chatHistory.AddUserMessage(userMessage); | |
var result = await AnsiConsole.Status() | |
.Spinner(Spinner.Known.Triangle) | |
.SpinnerStyle(Style.Parse("lightpink1")) | |
.StartAsync("En attente de Mistral...", async ctx => | |
{ | |
return await chatService.GetChatMessageContentAsync( | |
chatHistory, | |
kernel: kernel, | |
executionSettings: settings); | |
}); | |
if (result.Content is not null) | |
{ | |
AnsiConsole.Markup($"[bold lightpink1]Mistral[/]: "); | |
AnsiConsole.WriteLine(result.Content); | |
chatHistory.AddAssistantMessage(result.Content); | |
} | |
} | |
} | |
} | |
public class CustomFunctions | |
{ | |
[KernelFunction, Description("Recherche les processus dont le nom contient une chaîne donnée et retourne leurs informations")] | |
public string ObtenirInfosProcessus([Description("Nom du processus à rechercher")] string nomProcessus) | |
{ | |
var processus = Process.GetProcesses() | |
.Where(p => p.ProcessName.Contains(nomProcessus, StringComparison.CurrentCultureIgnoreCase)) | |
.ToList(); | |
if (!processus.Any()) return $"Aucun processus contenant '{nomProcessus}' dans son nom n'est en cours d'exécution."; | |
var infos = processus.Select(p => | |
{ | |
return $"- Nom : {p.ProcessName}, PID : {p.Id}"; | |
}); | |
return $"Processus trouvés contenant '{nomProcessus}' :\n" + string.Join("\n", infos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment