addrkit: strict ordinal matching, LLM-variant guard, externalized prompt

- match: require candidate ordinal to equal the query ordinal in fuzzy search
  (deterministic typo recall 73% -> 80%, no wrong houses)
- resolver: compatibleVariant guard — LLM may only fix street spelling, keeping
  the same words, ordinal and house unit; damerau distance for transpositions
- llm: prompt moved out of code (go:embed default_prompt.txt) with
  llm.system_prompt / llm.system_prompt_file overrides; guided JSON parsing
  tolerant of thinking blocks
- address: recognize ordinals appearing after the street name
- tests: first coverage for the LLM adapter and config prompt loading; README

Measured (sample 200): deterministic 94.7-95.8% exact / 0 wrong across 4 seeds;
LLM+guard typo 90.7-89.8% / 0 wrong. LLM fallback latency ~1.2-1.9s.
This commit is contained in:
addrkit
2026-10-09 20:40:20 +07:00
parent 5cc3556ac5
commit 78691f3a22
17 changed files with 590 additions and 48 deletions
+82 -26
View File
@@ -11,21 +11,33 @@ package llm
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"git.totmin.ru/en2zmax/addrkit/internal/config"
)
// defaultSystemPrompt is embedded from a readable text file so the prompt can
// be reviewed and versioned like documentation. It can be overridden per
// deployment via llm.system_prompt or llm.system_prompt_file in the config.
//
//go:embed default_prompt.txt
var defaultSystemPrompt string
// Rewriter asks a model for normalised variants of an address.
type Rewriter struct {
endpoint string
token string
model string
http *http.Client
endpoint string
token string
model string
systemPrompt string
maxTokens int
disableThinking bool
http *http.Client
}
// New builds a rewriter from configuration. The URL is treated as a base; the
@@ -39,24 +51,31 @@ func New(cfg config.LLMConfig) *Rewriter {
if timeout <= 0 {
timeout = 3 * time.Second
}
maxTokens := cfg.MaxTokens
if maxTokens <= 0 {
maxTokens = 512
}
prompt := strings.TrimSpace(cfg.SystemPrompt)
if prompt == "" {
prompt = strings.TrimSpace(defaultSystemPrompt)
}
return &Rewriter{
endpoint: base,
token: cfg.Token,
model: cfg.Model,
http: &http.Client{Timeout: timeout},
endpoint: base,
token: cfg.Token,
model: cfg.Model,
systemPrompt: prompt,
maxTokens: maxTokens,
disableThinking: cfg.DisableThinking,
http: &http.Client{Timeout: timeout},
}
}
const systemPrompt = `Ты — нормализатор адресов. Тебе дают адрес, написанный как угодно.
Верни ТОЛЬКО JSON вида {"variants":["...","..."]} — до 3 вариантов записи того же адреса
в формате "<название улицы> <дом>[/<дробь>][ <литера>][ к<корпус>][ стр.<строение>]".
Не выдумывай улицы и дома, которых нет во входе. Если не уверен — верни пустой список.`
type chatRequest struct {
Model string `json:"model"`
Messages []chatMessage `json:"messages"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
Model string `json:"model"`
Messages []chatMessage `json:"messages"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
ChatTemplateKwargs map[string]any `json:"chat_template_kwargs,omitempty"`
}
type chatMessage struct {
@@ -72,15 +91,20 @@ type chatResponse struct {
// Rewrite returns up to three normalised variants of raw.
func (r *Rewriter) Rewrite(ctx context.Context, raw string) ([]string, error) {
payload, err := json.Marshal(chatRequest{
reqBody := chatRequest{
Model: r.model,
Messages: []chatMessage{
{Role: "system", Content: systemPrompt},
{Role: "system", Content: r.systemPrompt},
{Role: "user", Content: raw},
},
Temperature: 0,
MaxTokens: 256,
})
MaxTokens: r.maxTokens,
}
if r.disableThinking {
// Qwen3 and friends: skip chain-of-thought, we only need the JSON.
reqBody.ChatTemplateKwargs = map[string]any{"enable_thinking": false}
}
payload, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
@@ -114,17 +138,18 @@ func (r *Rewriter) Rewrite(ctx context.Context, raw string) ([]string, error) {
}
// parseVariants extracts the {"variants":[...]} object from a model reply that
// may be wrapped in prose or code fences.
// may be wrapped in reasoning or code fences. Thinking models are asked to
// skip reasoning, but we tolerate it anyway.
func parseVariants(content string) []string {
start := strings.Index(content, "{")
end := strings.LastIndex(content, "}")
if start < 0 || end <= start {
content = stripThinking(content)
raw := extractVariantsObject(content)
if raw == "" {
return nil
}
var parsed struct {
Variants []string `json:"variants"`
}
if err := json.Unmarshal([]byte(content[start:end+1]), &parsed); err != nil {
if err := json.Unmarshal([]byte(raw), &parsed); err != nil {
return nil
}
out := make([]string, 0, len(parsed.Variants))
@@ -135,3 +160,34 @@ func parseVariants(content string) []string {
}
return out
}
var reThinkBlock = regexp.MustCompile(`(?s)<think>.*?</think>`)
// stripThinking removes blocks, including an unterminated one left
// behind when the token budget is exhausted mid-reasoning.
func stripThinking(content string) string {
content = reThinkBlock.ReplaceAllString(content, "")
if i := strings.Index(content, "<think>"); i >= 0 {
if j := strings.Index(content[i:], "</think>"); j < 0 {
content = content[:i]
}
}
return content
}
// extractVariantsObject returns the smallest {...} that contains "variants".
func extractVariantsObject(content string) string {
idx := strings.Index(content, `"variants"`)
if idx < 0 {
return ""
}
start := strings.LastIndex(content[:idx], "{")
if start < 0 {
return ""
}
end := strings.Index(content[idx:], "}")
if end < 0 {
return ""
}
return content[start : idx+end+1]
}