feat: localchat, a Go + Templ + HTMX chat app for local Gemma models

Streams replies over SSE from any OpenAI-compatible server (oMLX,
llama.cpp, Ollama, ...). Single binary that can install itself as an
OS service; docker compose bundles llama.cpp + Gemma 4 E2B.
This commit is contained in:
bdeb1337 committed 2026-10-05 07:25:42 +02:00
commit bb71b49fde
28 files changed
+2459

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
// Package config loads localchat settings from the environment and an
// optional .env file.
package config
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
// DefaultSystemPrompt is used when SYSTEM_PROMPT is not set.
const DefaultSystemPrompt = "You are a friendly, concise assistant running fully offline on the user's own machine. Answer clearly. Use Markdown when it helps."
// Config holds all runtime settings.
type Config struct {
Addr string // LOCALCHAT_ADDR: where the web UI listens
BaseURL string // LLM_BASE_URL: OpenAI-compatible API root, e.g. http://127.0.0.1:8000/v1
APIKey string // LLM_API_KEY: optional bearer token for the model server
Model string // LLM_MODEL: model id; empty means "first model the server lists"
SystemPrompt string // SYSTEM_PROMPT
Title string // LOCALCHAT_TITLE: name shown in the UI
MaxHistory int // MAX_HISTORY: messages sent back to the model as context
Timeout time.Duration // LLM_TIMEOUT: upper bound for a single reply
}
// Load reads the configuration from environment variables, applying defaults.
func Load() (Config, error) {
c := Config{
Addr: env("LOCALCHAT_ADDR", "127.0.0.1:3000"),
BaseURL: strings.TrimRight(env("LLM_BASE_URL", "http://127.0.0.1:8000/v1"), "/"),
APIKey: os.Getenv("LLM_API_KEY"),
Model: os.Getenv("LLM_MODEL"),
SystemPrompt: env("SYSTEM_PROMPT", DefaultSystemPrompt),
Title: env("LOCALCHAT_TITLE", "localchat"),
MaxHistory: 20,
Timeout: 5 * time.Minute,
}
if v := os.Getenv("MAX_HISTORY"); v != "" {
n, err := strconv.Atoi(v)
if err != nil || n < 1 {
return c, fmt.Errorf("MAX_HISTORY must be a positive integer, got %q", v)
}
c.MaxHistory = n
}
if v := os.Getenv("LLM_TIMEOUT"); v != "" {
d, err := time.ParseDuration(v)
if err != nil {
return c, fmt.Errorf("LLM_TIMEOUT: %w", err)
}
c.Timeout = d
}
return c, nil
}
func env(key, fallback string) string {
if v, ok := os.LookupEnv(key); ok && v != "" {
return v
}
return fallback
}
// LoadEnvFile sets variables from a KEY=VALUE file. Variables that are
// already set in the environment win, so the real environment can override
// the file. Blank lines and lines starting with # are ignored; values may be
// wrapped in single or double quotes.
func LoadEnvFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
sc := bufio.NewScanner(f)
for n := 1; sc.Scan(); n++ {
line := strings.TrimSpace(sc.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, val, ok := strings.Cut(strings.TrimPrefix(line, "export "), "=")
if !ok {
return fmt.Errorf("%s:%d: expected KEY=VALUE", path, n)
}
key, val = strings.TrimSpace(key), strings.TrimSpace(val)
if len(val) >= 2 && (val[0] == '"' || val[0] == '\'') && val[len(val)-1] == val[0] {
val = val[1 : len(val)-1]
}
if _, set := os.LookupEnv(key); !set {
os.Setenv(key, val)
}
}
return sc.Err()
}
+45
View File
@@ -0,0 +1,45 @@
package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoadEnvFile(t *testing.T) {
path := filepath.Join(t.TempDir(), ".env")
os.WriteFile(path, []byte(`
# comment
LLM_MODEL=from-file
export LOCALCHAT_TITLE="My Chat"
SYSTEM_PROMPT='Talk like a pirate = arr'
LLM_BASE_URL=http://file/v1/
`), 0o600)
t.Setenv("LLM_BASE_URL", "http://env/v1") // real env wins over the file
for _, k := range []string{"LLM_MODEL", "LOCALCHAT_TITLE", "SYSTEM_PROMPT"} {
t.Setenv(k, "")
os.Unsetenv(k)
}
if err := LoadEnvFile(path); err != nil {
t.Fatal(err)
}
c, err := Load()
if err != nil {
t.Fatal(err)
}
if c.Model != "from-file" || c.Title != "My Chat" || c.SystemPrompt != "Talk like a pirate = arr" {
t.Fatalf("unexpected config: %+v", c)
}
if c.BaseURL != "http://env/v1" {
t.Fatalf("env should override file, got %q", c.BaseURL)
}
}
func TestLoadValidates(t *testing.T) {
t.Setenv("MAX_HISTORY", "zero")
if _, err := Load(); err == nil {
t.Fatal("want error for bad MAX_HISTORY")
}
}