code.oscarkilo.com/klex-git

Hash:
6f27a7de274e645b3531d3cda88512d9e7139a56
Author:
Igor Naverniouk <[email protected]>
Date:
Sun May 18 20:08:31 2025 -0700
Message:
missed one
diff --git a/embed/main.go b/embed/main.go
index 3a5929b..df04fad 100644
--- a/embed/main.go
+++ b/embed/main.go
@@ -16,7 +16,7 @@ import "oscarkilo.com/klex-git/config"

var model = flag.String("model", "openai:text-embedding-3-small", "")
var dims = flag.Int("dims", 1536, "Number of vector dimensions to return")
-var whole_path = flag.Bool("whole_path", false, "Returns a list of vectors")
+var full_path = flag.Bool("full_path", false, "Returns a list of vectors")

func main() {
flag.Parse()
@@ -42,14 +42,14 @@ func main() {
Text: string(sin),
Model: *model,
Dims: *dims,
- FullPath: *whole_path,
+ FullPath: *full_path,
})
if err != nil {
log.Fatalf("Failed to call Embed: %v", err)
}

/*
- if *whole_path {
+ if *full_path {
text = util.SplitByWord(text[0])
}

a/embed/main.go
b/embed/main.go
1
package main
1
package main
2
2
3
// This binary converts text into embedding vecors.
3
// This binary converts text into embedding vecors.
4
4
5
//import "encoding/json"
5
//import "encoding/json"
6
import "flag"
6
import "flag"
7
import "fmt"
7
import "fmt"
8
import "io/ioutil"
8
import "io/ioutil"
9
import "log"
9
import "log"
10
import "os"
10
import "os"
11
//import "sync"
11
//import "sync"
12
12
13
import "oscarkilo.com/klex-git/api"
13
import "oscarkilo.com/klex-git/api"
14
import "oscarkilo.com/klex-git/config"
14
import "oscarkilo.com/klex-git/config"
15
//import "oscarkilo.com/klex-git/util"
15
//import "oscarkilo.com/klex-git/util"
16
16
17
var model = flag.String("model", "openai:text-embedding-3-small", "")
17
var model = flag.String("model", "openai:text-embedding-3-small", "")
18
var dims = flag.Int("dims", 1536, "Number of vector dimensions to return")
18
var dims = flag.Int("dims", 1536, "Number of vector dimensions to return")
19
var whole_path = flag.Bool("whole_path", false, "Returns a list of vectors")
19
var full_path = flag.Bool("full_path", false, "Returns a list of vectors")
20
20
21
func main() {
21
func main() {
22
flag.Parse()
22
flag.Parse()
23
23
24
// Find the API keys and configure a Klex client.
24
// Find the API keys and configure a Klex client.
25
config, err := config.ReadConfig()
25
config, err := config.ReadConfig()
26
if err != nil {
26
if err != nil {
27
log.Fatalf("Failed to read config: %v", err)
27
log.Fatalf("Failed to read config: %v", err)
28
}
28
}
29
client := api.NewClient(config.KlexUrl, config.ApiKey)
29
client := api.NewClient(config.KlexUrl, config.ApiKey)
30
if client == nil {
30
if client == nil {
31
log.Fatalf("Failed to create Klex client")
31
log.Fatalf("Failed to create Klex client")
32
}
32
}
33
33
34
// Read stdin as text.
34
// Read stdin as text.
35
sin, err := ioutil.ReadAll(os.Stdin)
35
sin, err := ioutil.ReadAll(os.Stdin)
36
if err != nil {
36
if err != nil {
37
log.Fatalf("Failed to read stdin: %v", err)
37
log.Fatalf("Failed to read stdin: %v", err)
38
}
38
}
39
//text := []string{string(sin)}
39
//text := []string{string(sin)}
40
40
41
vectors, err := client.Embed(api.EmbedRequest{
41
vectors, err := client.Embed(api.EmbedRequest{
42
Text: string(sin),
42
Text: string(sin),
43
Model: *model,
43
Model: *model,
44
Dims: *dims,
44
Dims: *dims,
45
FullPath: *whole_path,
45
FullPath: *full_path,
46
})
46
})
47
if err != nil {
47
if err != nil {
48
log.Fatalf("Failed to call Embed: %v", err)
48
log.Fatalf("Failed to call Embed: %v", err)
49
}
49
}
50
50
51
/*
51
/*
52
if *whole_path {
52
if *full_path {
53
text = util.SplitByWord(text[0])
53
text = util.SplitByWord(text[0])
54
}
54
}
55
55
56
f_name := fmt.Sprintf("embed-%s@%d", *model, *dims)
56
f_name := fmt.Sprintf("embed-%s@%d", *model, *dims)
57
vectors := make([][]float32, len(text))
57
vectors := make([][]float32, len(text))
58
wg := sync.WaitGroup{}
58
wg := sync.WaitGroup{}
59
for i := range text {
59
for i := range text {
60
wg.Add(1)
60
wg.Add(1)
61
go func(i int) {
61
go func(i int) {
62
json_vector, err := client.F(f_name, text[i])
62
json_vector, err := client.F(f_name, text[i])
63
if err != nil {
63
if err != nil {
64
log.Fatalf("Failed to call F: %v", err)
64
log.Fatalf("Failed to call F: %v", err)
65
}
65
}
66
err = json.Unmarshal([]byte(json_vector), &vectors[i])
66
err = json.Unmarshal([]byte(json_vector), &vectors[i])
67
if err != nil {
67
if err != nil {
68
log.Fatalf("Failed to parse vector: %v", err)
68
log.Fatalf("Failed to parse vector: %v", err)
69
}
69
}
70
wg.Done()
70
wg.Done()
71
}(i)
71
}(i)
72
}
72
}
73
wg.Wait()
73
wg.Wait()
74
*/
74
*/
75
75
76
for _, vector := range vectors {
76
for _, vector := range vectors {
77
for i, w := range vector {
77
for i, w := range vector {
78
if i > 0 {
78
if i > 0 {
79
fmt.Printf(" ")
79
fmt.Printf(" ")
80
}
80
}
81
fmt.Printf("%g", w)
81
fmt.Printf("%g", w)
82
}
82
}
83
fmt.Printf("\n")
83
fmt.Printf("\n")
84
}
84
}
85
}
85
}