Files
goralphy/ralphchat.go
Christoph K. 270eb56ac8 init
2026-02-25 07:15:33 +01:00

115 lines
2.6 KiB
Go

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)
}
}