50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package developmentembedding
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/sha256"
|
||
|
|
"fmt"
|
||
|
|
"math"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
const Dimensions = 1536
|
||
|
|
|
||
|
|
// Vector is a deterministic local-development projection. It is not a
|
||
|
|
// production semantic embedding and exists only to exercise the storage,
|
||
|
|
// filtering and lifecycle contracts without an external model account.
|
||
|
|
func Vector(content string) []float64 {
|
||
|
|
values := make([]float64, Dimensions)
|
||
|
|
for block := 0; block < Dimensions/32; block++ {
|
||
|
|
digest := sha256.Sum256([]byte(fmt.Sprintf("%d:%s", block, content)))
|
||
|
|
for index, value := range digest {
|
||
|
|
values[block*32+index] = (float64(value) - 127.5) / 127.5
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var squared float64
|
||
|
|
for _, value := range values {
|
||
|
|
squared += value * value
|
||
|
|
}
|
||
|
|
norm := math.Sqrt(squared)
|
||
|
|
if norm != 0 {
|
||
|
|
for index := range values {
|
||
|
|
values[index] /= norm
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return values
|
||
|
|
}
|
||
|
|
|
||
|
|
func Literal(values []float64) string {
|
||
|
|
var builder strings.Builder
|
||
|
|
builder.Grow(len(values) * 12)
|
||
|
|
builder.WriteByte('[')
|
||
|
|
for index, value := range values {
|
||
|
|
if index > 0 {
|
||
|
|
builder.WriteByte(',')
|
||
|
|
}
|
||
|
|
builder.WriteString(strconv.FormatFloat(value, 'g', 8, 64))
|
||
|
|
}
|
||
|
|
builder.WriteByte(']')
|
||
|
|
return builder.String()
|
||
|
|
}
|