Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/platform/embeddings/embeddings.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 |
| Branch | master |
| Path or archive member | internal/platform/embeddings/embeddings.go |
| Original SHA-256 | 19504dc4c63a9e85761f99a163462d32ee526203cc352a8650d7c8336de9138f |
| Git blob SHA | 8ee69b33088c4d14303064a6588f259341fa1a5a |
| Reading-copy SHA-256 | 09087a4343fa77d5d7915131b8c5fe82176097ad83ffafbf5f37d42943a9e9ab |
Every recorded occurrence
These 43 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | m5-price-to-win | 41000ad30fff4129fdaf5ec84e15ba14c27d4890 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws3-clearance | 4b8e7ce594c068621867f9c9deed0ec64ae6c553 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | collision-deconfliction | 4ee5a09972608ed89f8377c6665542d5f450f0a6 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws7-federal-signal | 6a9f6ce9cfb13345ffb47770d4960e7c2d9c17c2 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws6-retrieval | 6c56e29231ec05735927fa265c97d517499dfeaf | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | per-tenant-routing | 9bde0f9a74b89d01980fa658e5253acda7528d0c | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ai-brain-live | aac32fc49c878cc5bca099459307611562d0bac1 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | graphrag-foundation | b0ddeb9f633fd138d09309d1a17b7d13bc3a67f7 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws1-ontology-sync | b596f449a32a1016fbd403c36505cc00a1ac9ee6 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | time-to-fill-survival | b66e1e505863d4f17741a4b9ede73509e45bd6c6 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | mag-substrate | b82c7100a492dec67dfd1eda6b3f53def859be35 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | capture-readiness | bae6af7cb2997e819174a9e9cfff5e16067a1975 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | deploy-and-ops | bdacd9d0b4d5998d25fc74bf4007f4c3ec538c7a | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws8-teaching-loop | c823f0fb6bb2aba0f39001eb6de96f4000d9321f | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | phase2-eval-fairness | d361af254701975fe239927656ababb56374655e | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | phase2-connectors | e038d4c687aec16f8bd9bc4d907eea14d91f8704 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws2-alias-relationship | e4dada4601c6eb101830f9d223a9655dfa120616 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws9-bench-ingestion | f585a9ef32fa554663ce1fba9a414a54b6e49cf0 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | vendor-intelligence | fe9796a19c261951d3f2565206c0c1b5ac294465 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | per-tenant-routing | 9bde0f9a74b89d01980fa658e5253acda7528d0c | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | collision-deconfliction | 4ee5a09972608ed89f8377c6665542d5f450f0a6 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws6-retrieval | 6c56e29231ec05735927fa265c97d517499dfeaf | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | m5-price-to-win | 41000ad30fff4129fdaf5ec84e15ba14c27d4890 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws1-ontology-sync | b596f449a32a1016fbd403c36505cc00a1ac9ee6 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws8-teaching-loop | c823f0fb6bb2aba0f39001eb6de96f4000d9321f | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | mag-substrate | b82c7100a492dec67dfd1eda6b3f53def859be35 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws3-clearance | 4b8e7ce594c068621867f9c9deed0ec64ae6c553 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | phase2-eval-fairness | d361af254701975fe239927656ababb56374655e | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | time-to-fill-survival | b66e1e505863d4f17741a4b9ede73509e45bd6c6 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | graphrag-foundation | b0ddeb9f633fd138d09309d1a17b7d13bc3a67f7 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | deploy-and-ops | bdacd9d0b4d5998d25fc74bf4007f4c3ec538c7a | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws9-bench-ingestion | f585a9ef32fa554663ce1fba9a414a54b6e49cf0 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws2-alias-relationship | e4dada4601c6eb101830f9d223a9655dfa120616 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ws7-federal-signal | 6a9f6ce9cfb13345ffb47770d4960e7c2d9c17c2 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | ai-brain-live | aac32fc49c878cc5bca099459307611562d0bac1 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | phase2-connectors | e038d4c687aec16f8bd9bc4d907eea14d91f8704 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
| Janady13/missionmatch / internal/platform/embeddings/embeddings.go | capture-readiness | bae6af7cb2997e819174a9e9cfff5e16067a1975 | Open original |
Recovered source transcript
Download complete source text ↓219 lines · 7.1 KB
Credential-bearing and unrelated personal details were removed from this reading copy. The source identity and original checksum remain recorded.
// Package embeddings is the cloud-agnostic embedding provider port.
//
// The core app NEVER depends on a native ML stack, it calls a provider over HTTP, or uses the
// deterministic offline provider for dev/tests (CLAUDE.md). Selecting a provider is configuration:
// - fake : deterministic, offline (dev/tests)
// - openai : any OpenAI-compatible /v1/embeddings endpoint, self-hosted vLLM (the
// production CUI path) or OpenAI
// - azure_openai : Azure Government OpenAI (OpenAI-compatible; full adapter wired at deploy)
// - bedrock : AWS GovCloud Bedrock (InvokeModel; adapter wired at deploy, needs the AWS SDK)
//
// All vectors are L2-normalized so cosine distance (the schema's vector_cosine_ops / <=>) is meaningful.
package embeddings
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"sort"
"strings"
"time"
"github.com/missionworkforce/missionmatch/internal/platform/config"
)
// SkillEmbedText is the canonical text to embed for one skill: the name, with the category in
// parentheses when present. The backfill (the writer of skills.embedding) and the boot vector model
// check (the verifier that re embeds a stored skill to confirm the model) MUST embed the SAME text, or
// the check would see a false mismatch on every categorized skill. This function is that single source.
func SkillEmbedText(name, category string) string {
if category != "" {
return name + " (" + category + ")"
}
return name
}
// Embedder turns text into a fixed-dimension, L2-normalized embedding.
// Implementations MUST be deterministic for a given input where feasible (the fake one always is).
type Embedder interface {
Embed(ctx context.Context, text string) ([]float32, error)
EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)
Dimension() int
}
// New selects an Embedder from config. The bedrock/azure_openai managed adapters that require cloud
// SDKs are intentionally not compiled into the core module yet; they are wired at deploy time behind
// this same interface (STACK_DECISIONS.md §9).
func New(cfg config.Embeddings) (Embedder, error) {
switch cfg.Provider {
case config.ProviderFake:
return NewDeterministic(cfg.Dimension), nil
case config.ProviderOpenAI, config.ProviderAzureOpenAI:
return NewHTTP(cfg.Endpoint, cfg.Model, cfg.APIKey, cfg.Dimension, nil), nil
case config.ProviderMegamind:
return newMegamind(cfg)
case config.ProviderBedrock:
return nil, fmt.Errorf("embeddings: provider %q adapter not wired into core build; add the AWS SDK adapter at deploy", cfg.Provider)
default:
return nil, fmt.Errorf("embeddings: unknown provider %q", cfg.Provider)
}
}
// ---------- deterministic offline provider ----------
type deterministicEmbedder struct{ dim int }
// NewDeterministic returns an offline Embedder that maps text to a stable, L2-normalized vector.
// Same text => same vector; different text => (almost surely) different vector. Good for dev, tests,
// and as the engine's offline semantic backend.
func NewDeterministic(dim int) Embedder { return deterministicEmbedder{dim: dim} }
func (d deterministicEmbedder) Dimension() int { return d.dim }
func (d deterministicEmbedder) Embed(_ context.Context, text string) ([]float32, error) {
return deterministicVector(text, d.dim), nil
}
func (d deterministicEmbedder) EmbedBatch(_ context.Context, texts []string) ([][]float32, error) {
out := make([][]float32, len(texts))
for i, t := range texts {
out[i] = deterministicVector(t, d.dim)
}
return out, nil
}
func deterministicVector(text string, dim int) []float32 {
v := make([]float32, dim)
sum := sha256.Sum256([]byte(strings.ToLower(strings.TrimSpace(text))))
var x uint64
for i := 0; i < 8; i++ {
x = x<<8 | uint64(sum[i])
}
if x == 0 {
x = 0x9e3779b97f4a7c15
}
for i := range v {
// xorshift64, deterministic, well-spread.
x ^= x << 13
x ^= x >> 7
x ^= x << 17
v[i] = float32(int64(x%2000)-1000) / 1000.0
}
normalize(v)
return v
}
// ---------- OpenAI-compatible HTTP provider ----------
type httpEmbedder struct {
endpoint string // full embeddings URL, e.g. https://host/v1/embeddings
model string
apiKey string
dim int
client *http.Client
}
// NewHTTP returns an Embedder for any OpenAI-compatible embeddings endpoint. endpoint is the full
// URL. A nil client gets a sensible default with a timeout.
func NewHTTP(endpoint, model, apiKey string, dim int, client *http.Client) Embedder {
if client == nil {
client = &http.Client{Timeout: 30 * time.Second}
}
return &httpEmbedder{endpoint: endpoint, model: model, apiKey: [credential removed], dim: dim, client: client}
}
func (h *httpEmbedder) Dimension() int { return h.dim }
func (h *httpEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {
out, err := h.EmbedBatch(ctx, []string{text})
if err != nil {
return nil, err
}
return out[0], nil
}
type embedRequest struct {
Model string `json:"model"`
Input []string `json:"input"`
}
type embedResponse struct {
Data []struct {
Index int `json:"index"`
Embedding []float32 `json:"embedding"`
} `json:"data"`
}
func (h *httpEmbedder) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error) {
if len(texts) == 0 {
return nil, nil
}
body, err := json.Marshal(embedRequest{Model: h.model, Input: texts})
if err != nil {
return nil, fmt.Errorf("embeddings: marshal request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, h.endpoint, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("embeddings: new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if h.apiKey != "" {
req.Header.Set("Authorization", "Bearer "+h.apiKey)
req.Header.Set("api-key", h.apiKey) // Azure OpenAI uses this header
}
resp, err := h.client.Do(req)
if err != nil {
return nil, fmt.Errorf("embeddings: request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("embeddings: provider returned %s", resp.Status)
}
var parsed embedResponse
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
return nil, fmt.Errorf("embeddings: decode response: %w", err)
}
if len(parsed.Data) != len(texts) {
return nil, fmt.Errorf("embeddings: expected %d vectors, got %d", len(texts), len(parsed.Data))
}
// Order by index, providers may return out of order.
sort.Slice(parsed.Data, func(i, j int) bool { return parsed.Data[i].Index < parsed.Data[j].Index })
out := make([][]float32, len(parsed.Data))
for i, d := range parsed.Data {
if h.dim > 0 && len(d.Embedding) != h.dim {
return nil, fmt.Errorf("embeddings: vector %d has dim %d, want %d (check MM_EMBED_DIMENSION vs the model)", i, len(d.Embedding), h.dim)
}
vec := d.Embedding
normalize(vec)
out[i] = vec
}
return out, nil
}
// ---------- shared ----------
func normalize(v []float32) {
var ss float64
for _, f := range v {
ss += float64(f) * float64(f)
}
if ss == 0 {
return
}
inv := float32(1.0 / math.Sqrt(ss))
for i := range v {
v[i] *= inv
}
}
// ErrEmpty is returned by helpers that refuse empty input.
var ErrEmpty = errors.New("embeddings: empty input")