Skip to content

Commit c968226

Browse files
committed
wip: embeddings and vector db rudimentals
1 parent 3f1ab46 commit c968226

File tree

7 files changed

+98
-2
lines changed

7 files changed

+98
-2
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.24.5-bookworm
1+
FROM golang:1.25.1-alpine
22

33
WORKDIR /app/
44

embeddings/embeddings.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package embeddings
2+
3+
import (
4+
"context"
5+
6+
"github.com/openai/openai-go/v2"
7+
"github.com/openai/openai-go/v2/packages/param"
8+
)
9+
10+
func EmbedText(text string) ([]float64, error) {
11+
client := openai.NewClient()
12+
embd, err := client.Embeddings.New(context.TODO(), openai.EmbeddingNewParams{
13+
Input: openai.EmbeddingNewParamsInputUnion{OfString: param.Opt[string]{Value: text}},
14+
Model: openai.EmbeddingModelTextEmbedding3Small,
15+
Dimensions: param.Opt[int64]{Value: 768},
16+
})
17+
if err != nil {
18+
return nil, err
19+
}
20+
embds := [][]float64{}
21+
for _, item := range embd.Data {
22+
embds = append(embds, item.Embedding)
23+
}
24+
return embds[0], nil
25+
}

embeddings/embeddings_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package embeddings
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestEmbedText(t *testing.T) {
9+
_, ok := os.LookupEnv("OPENAI_API_KEY")
10+
if !ok {
11+
t.Skip("Skipping test because OPENAI_API_KEY is not in the environment")
12+
}
13+
embd, err := EmbedText("hello world!")
14+
if err != nil {
15+
t.Errorf("Expecting no error, got %s", err.Error())
16+
}
17+
if len(embd) != 768 {
18+
t.Errorf("Expecting the embedding to be of length 768, got %d", len(embd))
19+
}
20+
}

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module github.com/AstraBert/git-push-blog
22

3-
go 1.24.5
3+
go 1.25.1
44

55
require (
66
github.com/a-h/templ v0.3.943
77
github.com/blevesearch/bleve v1.0.14
88
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a
9+
github.com/openai/openai-go/v2 v2.6.0
10+
github.com/takara-ai/serverlessVector v1.0.0
911
)
1012

1113
require (
@@ -26,6 +28,10 @@ require (
2628
github.com/mschoch/smat v0.2.0 // indirect
2729
github.com/philhofer/fwd v1.0.0 // indirect
2830
github.com/steveyen/gtreap v0.1.0 // indirect
31+
github.com/tidwall/gjson v1.14.4 // indirect
32+
github.com/tidwall/match v1.1.1 // indirect
33+
github.com/tidwall/pretty v1.2.1 // indirect
34+
github.com/tidwall/sjson v1.2.5 // indirect
2935
github.com/tinylib/msgp v1.1.0 // indirect
3036
github.com/willf/bitset v1.1.10 // indirect
3137
go.etcd.io/bbolt v1.3.5 // indirect

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOl
8080
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
8181
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
8282
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
83+
github.com/openai/openai-go/v2 v2.6.0 h1:0t3e5AUr5fsgb9TotDJNTdpGqf/SSSfMX4pr8QrV9OY=
84+
github.com/openai/openai-go/v2 v2.6.0/go.mod h1:sIUkR+Cu/PMUVkSKhkk742PRURkQOCFhiwJ7eRSBqmk=
8385
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
8486
github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ=
8587
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
@@ -103,9 +105,21 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
103105
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
104106
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
105107
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
108+
github.com/takara-ai/serverlessVector v1.0.0 h1:BGXb20ZgNlgJ/4YnqBwzg9414zD4189vy2S1vfiaO/Y=
109+
github.com/takara-ai/serverlessVector v1.0.0/go.mod h1:fCkqNXUgsyg91gqayAoP/2vLXMY1FwU94S5OAVE6pDY=
106110
github.com/tebeka/snowball v0.4.2/go.mod h1:4IfL14h1lvwZcp1sfXuuc7/7yCsvVffTWxWxCLfFpYg=
107111
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok=
108112
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8=
113+
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
114+
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
115+
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
116+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
117+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
118+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
119+
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
120+
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
121+
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
122+
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
109123
github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU=
110124
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
111125
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=

vectordb/vectordb.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package vectordb
2+
3+
import (
4+
"github.com/takara-ai/serverlessVector"
5+
)
6+
7+
func CreateDB() *serverlessVector.VectorDB {
8+
db := serverlessVector.NewVectorDB(384)
9+
return db
10+
}

vectordb/vectordb_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package vectordb
2+
3+
import "testing"
4+
5+
func TestCreateVectorDB(t *testing.T) {
6+
db := CreateDB()
7+
if db.Size() > 0 {
8+
t.Errorf("Expecting new DB to have size zero, got %d", db.Size())
9+
}
10+
if dims, ok := db.GetStats()["dimension"]; ok {
11+
if dimsInt, isInt := dims.(int); isInt {
12+
if dimsInt != 384 {
13+
t.Errorf("Expecting 'dimension' to be 384, but got %d", dimsInt)
14+
}
15+
} else {
16+
t.Error("Expecting 'dimension' to be an integer, but they are not.")
17+
}
18+
} else {
19+
t.Error("Expecting 'dimension' to be found in stats, but it is not.")
20+
}
21+
}

0 commit comments

Comments
 (0)