Skip to content

Commit 2285367

Browse files
committed
Improved examples [skip ci]
1 parent 48fdcf2 commit 2285367

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

examples/hybrid/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func main() {
5151
"The cat is purring",
5252
"The bear is growling",
5353
}
54-
embeddings, err := FetchEmbeddings(input)
54+
embeddings, err := Embed(input)
5555
if err != nil {
5656
panic(err)
5757
}
@@ -87,7 +87,7 @@ ORDER BY score DESC
8787
LIMIT 5
8888
`
8989
query := "growling bear"
90-
queryEmbedding, err := FetchEmbeddings([]string{query})
90+
queryEmbedding, err := Embed([]string{query})
9191
if err != nil {
9292
panic(err)
9393
}
@@ -118,7 +118,7 @@ type apiRequest struct {
118118
Model string `json:"model"`
119119
}
120120

121-
func FetchEmbeddings(input []string) ([][]float32, error) {
121+
func Embed(input []string) ([][]float32, error) {
122122
url := "http://localhost:11434/api/embed"
123123
data := &apiRequest{
124124
Input: input,

examples/openai/main.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
"The cat is purring",
5454
"The bear is growling",
5555
}
56-
embeddings, err := FetchEmbeddings(input, apiKey)
56+
embeddings, err := Embed(input, apiKey)
5757
if err != nil {
5858
panic(err)
5959
}
@@ -65,8 +65,13 @@ func main() {
6565
}
6666
}
6767

68-
documentId := 1
69-
rows, err := conn.Query(ctx, "SELECT id, content FROM documents WHERE id != $1 ORDER BY embedding <=> (SELECT embedding FROM documents WHERE id = $1) LIMIT 5", documentId)
68+
query := "forest"
69+
queryEmbedding, err := Embed([]string{query}, apiKey)
70+
if err != nil {
71+
panic(err)
72+
}
73+
74+
rows, err := conn.Query(ctx, "SELECT id, content FROM documents ORDER BY embedding <=> $1 LIMIT 5", pgvector.NewVector(queryEmbedding[0]))
7075
if err != nil {
7176
panic(err)
7277
}
@@ -92,7 +97,7 @@ type apiRequest struct {
9297
Model string `json:"model"`
9398
}
9499

95-
func FetchEmbeddings(input []string, apiKey string) ([][]float32, error) {
100+
func Embed(input []string, apiKey string) ([][]float32, error) {
96101
url := "https://api.openai.com/v1/embeddings"
97102
data := &apiRequest{
98103
Input: input,

examples/sparse/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
"The cat is purring",
5454
"The bear is growling",
5555
}
56-
embeddings, err := FetchEmbeddings(input)
56+
embeddings, err := Embed(input)
5757
if err != nil {
5858
panic(err)
5959
}
@@ -66,7 +66,7 @@ func main() {
6666
}
6767

6868
query := "forest"
69-
queryEmbeddings, err := FetchEmbeddings([]string{query})
69+
queryEmbeddings, err := Embed([]string{query})
7070
if err != nil {
7171
panic(err)
7272
}
@@ -94,7 +94,7 @@ type apiRequest struct {
9494
Inputs []string `json:"inputs"`
9595
}
9696

97-
func FetchEmbeddings(inputs []string) ([]map[int32]float32, error) {
97+
func Embed(inputs []string) ([]map[int32]float32, error) {
9898
url := "http://localhost:3000/embed_sparse"
9999
data := &apiRequest{
100100
Inputs: inputs,

0 commit comments

Comments
 (0)