-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (86 loc) · 2.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// LLM inference in Helix.ml from Dagger
//
// This module lets you call LLM inference on open source models in Helix.ml
//
// For more info, see https://helixml.substack.com/p/introducing-the-helix-cli-and-dagger
package main
import (
"context"
"strings"
)
type Helix struct{}
// example usage: "dagger call get-secret --helix-credentials ~/.helix/credentials"
func (m *Helix) GetSecret(ctx context.Context, helixCredentials *File) (string, error) {
ctr, err := m.WithHelixSecret(ctx, dag.Container().From("ubuntu:latest"), helixCredentials)
if err != nil {
return "", err
}
return ctr.
WithExec([]string{"bash", "-c", "cat /root/.helix/credentials |base64"}).
Stdout(ctx)
}
func (m *Helix) WithHelixSecret(ctx context.Context, ctr *Container, helixCredentials *File) (*Container, error) {
credsFile, err := helixCredentials.Contents(ctx)
if err != nil {
return nil, err
}
/* we expect ~/.helix/credentials to be of the form:
HELIX_API_KEY=a1b2
HELIX_API_URL=http://localhost
*/
var apiKey string
var apiUrl string = "https://app.tryhelix.ai"
var activeTools string
lines := strings.Split(credsFile, "\n")
for _, line := range lines {
if len(line) > 0 && string(line[0]) == "#" {
continue
}
if strings.Contains(line, "HELIX_API_KEY=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) > 1 {
apiKey = parts[1]
}
}
if strings.Contains(line, "HELIX_API_URL=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) > 1 {
apiUrl = parts[1]
}
}
if strings.Contains(line, "HELIX_ACTIVE_TOOLS=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) > 1 {
activeTools = parts[1]
}
}
}
secret := dag.SetSecret("helix-api-key", apiKey)
ctr = ctr.
WithSecretVariable("HELIX_API_KEY", secret).
WithEnvVariable("HELIX_API_URL", apiUrl).
WithEnvVariable("HELIX_ACTIVE_TOOLS", activeTools)
return ctr, nil
}
func (m *Helix) HelixCli(ctx context.Context, helixCredentials *File) (*Container, error) {
ctr := dag.Container().
// From("helix-controlplane:dev")
From("europe-docker.pkg.dev/helixml/helix/controlplane-dev:dev002")
ctr, err := m.WithHelixSecret(ctx, ctr, helixCredentials)
if err != nil {
return nil, err
}
return ctr, nil
}
// example usage: "dagger call run --helix-credentials ~/.helix/credentials --prompt hi"
func (m *Helix) Run(ctx context.Context, prompt string, helixCredentials *File) (string, error) {
ctr, err := m.HelixCli(ctx, helixCredentials)
if err != nil {
return "", err
}
// TODO: json output from run cmd?
return ctr.
WithEntrypoint([]string{"/helix"}).
WithExec([]string{"run", "--prompt", prompt}).
Stdout(ctx)
}