code.oscarkilo.com/klex-git

Hash:
0eed5234e4399428b010162ea276eb7014d37a80
Author:
Igor Naverniouk <[email protected]>
Date:
Thu Apr 10 14:04:18 2025 -0700
Message:
the 'one' binary
diff --git a/one/README.md b/one/README.md
new file mode 100644
index 0000000..24d2ea0
--- /dev/null
+++ b/one/README.md
@@ -0,0 +1,23 @@
+## one: runs an LLM on one input
+
+Before running this script, create a `klex.json` file in the
+root of your git repo, and run this command once:
+```back
+go get oscarkilo.com/klex-git
+```
+
+Hello world example:
+```bash
+go run oscarkilo.com/klex-git/one <<EOF
+{
+ "model": "Any big LLM",
+ "messages": [{
+ "role": "user",
+ "content": {
+ "type": "text",
+ "text": "Who played Hotel California in Lebowski?"
+ }
+ }]
+}
+EOF
+```
diff --git a/one/main.go b/one/main.go
new file mode 100644
index 0000000..b83f789
--- /dev/null
+++ b/one/main.go
@@ -0,0 +1,92 @@
+package main
+
+// This binary runs one LLM inference on one input.
+
+import "flag"
+import "fmt"
+import "encoding/json"
+import "io/ioutil"
+import "log"
+import "os"
+import "strings"
+
+import "oscarkilo.com/klex-git/api"
+import "oscarkilo.com/klex-git/config"
+
+var model = flag.String("model", "", "overrides .Model, if non-empty")
+var system = flag.String("system_file", "", "overrides .System, if non-empty")
+var prompt = flag.String("prompt_file", "", "appends to .Messages")
+var format = flag.String("format", "text", "text|json|jsonindent")
+
+func main() {
+ config, err := config.ReadConfig()
+ if err != nil {
+ log.Fatalf("Failed to read config: %v", err)
+ }
+ client := api.NewClient(config.KlexUrl, config.ApiKey)
+ if client == nil {
+ log.Fatalf("Failed to create Klex client")
+ }
+
+ var req api.MessagesRequest
+ err = json.NewDecoder(os.Stdin).Decode(&req)
+ if err != nil {
+ log.Fatalf("Failed to parse a MessagesRequest from stdin: %v", err)
+ }
+
+ if *model != "" {
+ req.Model = *model
+ }
+ if *system != "" {
+ s, err := ioutil.ReadFile(*system)
+ if err != nil {
+ log.Fatalf("Failed to read --system_file %s: %v", *system, err)
+ }
+ req.System = string(s)
+ }
+ if *prompt != "" {
+ p, err := ioutil.ReadFile(*prompt)
+ if err != nil {
+ log.Fatalf("Failed to read --prompt_file %s: %v", *prompt, err)
+ }
+ req.Messages = append(req.Messages, api.ChatMessage{
+ Role: "user",
+ Content: []api.ContentBlock{{
+ Type: "text",
+ Text: string(p),
+ }},
+ })
+ }
+
+ res, err := client.Messages(req)
+ if err != nil {
+ log.Fatalf("Klex f() failure: %v", err)
+ }
+
+ out, err := formatResponse(res)
+ if err != nil {
+ log.Fatalf("Failed to format response: %v", err)
+ }
+ fmt.Print(out)
+}
+
+func formatResponse(res *api.MessagesResponse) (string, error) {
+ switch *format {
+ case "text":
+ var content []string
+ for _, c := range res.Content {
+ if c.Type == "text" {
+ content = append(content, c.Text + "\n")
+ }
+ }
+ return strings.Join(content, "\n"), nil
+ case "json":
+ buf, err := json.Marshal(res)
+ return string(buf), err
+ case "jsonindent":
+ buf, err := json.MarshalIndent(res, "", " ")
+ return string(buf), err
+ default:
+ return "", fmt.Errorf("Unsupported --format=%s", *format)
+ }
+}
/dev/null
b/one/README.md
1
## one: runs an LLM on one input
2
3
Before running this script, create a `klex.json` file in the
4
root of your git repo, and run this command once:
5
```back
6
go get oscarkilo.com/klex-git
7
```
8
9
Hello world example:
10
```bash
11
go run oscarkilo.com/klex-git/one <<EOF
12
{
13
"model": "Any big LLM",
14
"messages": [{
15
"role": "user",
16
"content": {
17
"type": "text",
18
"text": "Who played Hotel California in Lebowski?"
19
}
20
}]
21
}
22
EOF
23
```
/dev/null
b/one/main.go
1
package main
2
3
// This binary runs one LLM inference on one input.
4
5
import "flag"
6
import "fmt"
7
import "encoding/json"
8
import "io/ioutil"
9
import "log"
10
import "os"
11
import "strings"
12
13
import "oscarkilo.com/klex-git/api"
14
import "oscarkilo.com/klex-git/config"
15
16
var model = flag.String("model", "", "overrides .Model, if non-empty")
17
var system = flag.String("system_file", "", "overrides .System, if non-empty")
18
var prompt = flag.String("prompt_file", "", "appends to .Messages")
19
var format = flag.String("format", "text", "text|json|jsonindent")
20
21
func main() {
22
config, err := config.ReadConfig()
23
if err != nil {
24
log.Fatalf("Failed to read config: %v", err)
25
}
26
client := api.NewClient(config.KlexUrl, config.ApiKey)
27
if client == nil {
28
log.Fatalf("Failed to create Klex client")
29
}
30
31
var req api.MessagesRequest
32
err = json.NewDecoder(os.Stdin).Decode(&req)
33
if err != nil {
34
log.Fatalf("Failed to parse a MessagesRequest from stdin: %v", err)
35
}
36
37
if *model != "" {
38
req.Model = *model
39
}
40
if *system != "" {
41
s, err := ioutil.ReadFile(*system)
42
if err != nil {
43
log.Fatalf("Failed to read --system_file %s: %v", *system, err)
44
}
45
req.System = string(s)
46
}
47
if *prompt != "" {
48
p, err := ioutil.ReadFile(*prompt)
49
if err != nil {
50
log.Fatalf("Failed to read --prompt_file %s: %v", *prompt, err)
51
}
52
req.Messages = append(req.Messages, api.ChatMessage{
53
Role: "user",
54
Content: []api.ContentBlock{{
55
Type: "text",
56
Text: string(p),
57
}},
58
})
59
}
60
61
res, err := client.Messages(req)
62
if err != nil {
63
log.Fatalf("Klex f() failure: %v", err)
64
}
65
66
out, err := formatResponse(res)
67
if err != nil {
68
log.Fatalf("Failed to format response: %v", err)
69
}
70
fmt.Print(out)
71
}
72
73
func formatResponse(res *api.MessagesResponse) (string, error) {
74
switch *format {
75
case "text":
76
var content []string
77
for _, c := range res.Content {
78
if c.Type == "text" {
79
content = append(content, c.Text + "\n")
80
}
81
}
82
return strings.Join(content, "\n"), nil
83
case "json":
84
buf, err := json.Marshal(res)
85
return string(buf), err
86
case "jsonindent":
87
buf, err := json.MarshalIndent(res, "", " ")
88
return string(buf), err
89
default:
90
return "", fmt.Errorf("Unsupported --format=%s", *format)
91
}
92
}