-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy patheval.go
86 lines (73 loc) · 1.96 KB
/
eval.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
package cli
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/gptscript-ai/gptscript/pkg/chat"
"github.com/gptscript-ai/gptscript/pkg/gptscript"
"github.com/gptscript-ai/gptscript/pkg/input"
"github.com/gptscript-ai/gptscript/pkg/loader"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/spf13/cobra"
)
type Eval struct {
Tools []string `usage:"Tools available to call"`
Chat bool `usage:"Enable chat"`
MaxTokens int `usage:"Maximum number of tokens to output"`
Model string `usage:"The model to use"`
JSON bool `usage:"Output JSON"`
Temperature string `usage:"Set the temperature, \"creativity\""`
gptscript *GPTScript
}
func (e *Eval) Run(cmd *cobra.Command, args []string) error {
tool := types.Tool{
ToolDef: types.ToolDef{
Parameters: types.Parameters{
Description: "inline script",
Tools: e.Tools,
MaxTokens: e.MaxTokens,
ModelName: e.Model,
JSONResponse: e.JSON,
Chat: e.Chat,
},
Instructions: strings.Join(args, " "),
},
}
if e.Temperature != "" {
temp, err := strconv.ParseFloat(e.Temperature, 32)
if err != nil {
return fmt.Errorf("failed to parse %v: %v", e.Temperature, err)
}
temp32 := float32(temp)
tool.Temperature = &temp32
}
opts, err := e.gptscript.NewGPTScriptOpts()
if err != nil {
return err
}
runner, err := gptscript.New(cmd.Context(), opts)
if err != nil {
return err
}
prg, err := loader.ProgramFromSource(cmd.Context(), tool.String(), "", loader.Options{
Cache: runner.Cache,
})
if err != nil {
return err
}
toolInput, err := input.FromFile(e.gptscript.Input)
if err != nil {
return err
}
if e.Chat {
return chat.Start(cmd.Context(), nil, runner, func() (types.Program, error) {
return prg, nil
}, os.Environ(), toolInput, "")
}
toolOutput, err := runner.Run(cmd.Context(), prg, opts.Env, toolInput)
if err != nil {
return err
}
return e.gptscript.PrintOutput("", toolOutput)
}