From 270eb56ac8d32ba355a3dd4f9639bdb7f515c3f4 Mon Sep 17 00:00:00 2001 From: "Christoph K." Date: Wed, 25 Feb 2026 07:15:33 +0100 Subject: [PATCH] init --- ralphchat.go | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 ralphchat.go diff --git a/ralphchat.go b/ralphchat.go new file mode 100644 index 0000000..b01984b --- /dev/null +++ b/ralphchat.go @@ -0,0 +1,114 @@ +package main + +import ( + "bufio" + "context" + "fmt" + "log" + "os" + "strconv" + "strings" + + "github.com/openai/openai-go" + oaioption "github.com/openai/openai-go/option" +) + +const ( + baseURL = "http://127.0.0.1:12434/v1" +) + +func selectModel(client *openai.Client) string { + // Verfügbare Modelle über die API abrufen + modelsPage, err := client.Models.List(context.Background()) + if err != nil { + log.Fatalf("Fehler beim Abrufen der Modelle: %v", err) + } + + models := modelsPage.Data + if len(models) == 0 { + log.Fatal("Keine Modelle verfügbar!") + } + + fmt.Println("\n📦 Verfügbare Modelle:") + fmt.Println(strings.Repeat("─", 50)) + for i, m := range models { + fmt.Printf(" [%d] %s\n", i+1, m.ID) + } + fmt.Println(strings.Repeat("─", 50)) + + scanner := bufio.NewScanner(os.Stdin) + + for { + fmt.Printf("Wähle ein Modell (1-%d): ", len(models)) + if !scanner.Scan() { + log.Fatal("Eingabe fehlgeschlagen") + } + + input := strings.TrimSpace(scanner.Text()) + choice, err := strconv.Atoi(input) + if err != nil || choice < 1 || choice > len(models) { + fmt.Printf("❌ Ungültige Eingabe. Bitte eine Zahl zwischen 1 und %d eingeben.\n", len(models)) + continue + } + + selected := models[choice-1].ID + fmt.Printf("✅ Modell gewählt: %s\n", selected) + return selected + } +} + +func main() { + client := openai.NewClient( + oaioption.WithBaseURL(baseURL), + oaioption.WithAPIKey("ollama"), + ) + + fmt.Println("🤖 LLM Chat") + model := selectModel(&client) + + // Chat-History für Konversationsgedächtnis + messages := []openai.ChatCompletionMessageParamUnion{ + openai.SystemMessage("You are a helpful golang coding assistant. Answer concisely. If you don't know the answer, say you don't know."), + } + + scanner := bufio.NewScanner(os.Stdin) + + fmt.Printf("\n🚀 Chat gestartet mit Modell: %s\n", model) + fmt.Println("Tippe deine Nachricht und drücke Enter. Mit 'exit' beenden.") + fmt.Println(strings.Repeat("─", 50)) + + for { + fmt.Print("\nDu: ") + if !scanner.Scan() { + break + } + + input := strings.TrimSpace(scanner.Text()) + if input == "" { + continue + } + if input == "exit" || input == "quit" { + fmt.Println("Tschüss!") + break + } + + messages = append(messages, openai.UserMessage(input)) + + resp, err := client.Chat.Completions.New( + context.Background(), + openai.ChatCompletionNewParams{ + Model: model, + Messages: messages, + }, + ) + if err != nil { + log.Printf("Fehler bei API-Aufruf: %v\n", err) + continue + } + + assistantMsg := resp.Choices[0].Message.Content + messages = append(messages, openai.AssistantMessage(assistantMsg)) + + fmt.Printf("\n🤖 Assistent:\n %s\n", assistantMsg) + } +}