Skip to content

Commit 18da184

Browse files
Add file loading from github
1 parent 4a81d2b commit 18da184

File tree

11 files changed

+472
-96
lines changed

11 files changed

+472
-96
lines changed

examples/helloworld.gpt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Say hello world

pkg/cli/root.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
"github.com/acorn-io/cmd"
9-
"github.com/acorn-io/gptscript/pkg/parser"
9+
"github.com/acorn-io/gptscript/pkg/loader"
1010
"github.com/acorn-io/gptscript/pkg/runner"
1111
"github.com/acorn-io/gptscript/pkg/version"
1212
"github.com/spf13/cobra"
@@ -15,7 +15,8 @@ import (
1515

1616
type GPTScript struct {
1717
runner.Options
18-
Output string `usage:"Save output to a file" short:"o"`
18+
Output string `usage:"Save output to a file" short:"o"`
19+
SubTool string `usage:"Target tool name in file to run"`
1920
}
2021

2122
func New() *cobra.Command {
@@ -25,17 +26,10 @@ func New() *cobra.Command {
2526
func (r *GPTScript) Customize(cmd *cobra.Command) {
2627
cmd.Use = version.ProgramName
2728
cmd.Args = cobra.MinimumNArgs(1)
28-
cmd.Flags().SetInterspersed(false)
2929
}
3030

3131
func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
32-
in, err := os.Open(args[0])
33-
if err != nil {
34-
return err
35-
}
36-
defer in.Close()
37-
38-
mainTool, toolSet, err := parser.Parse(in)
32+
tool, err := loader.Tool(cmd.Context(), args[0], r.SubTool)
3933
if err != nil {
4034
return err
4135
}
@@ -51,7 +45,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
5145
return err
5246
}
5347

54-
s, err := runner.Run(cmd.Context(), mainTool, toolSet, "")
48+
s, err := runner.Run(cmd.Context(), *tool, "")
5549
if err != nil {
5650
return err
5751
}

pkg/engine/engine.go

+50-18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ import (
1717
"github.com/acorn-io/gptscript/pkg/version"
1818
)
1919

20+
// InternalSystemPrompt is added to all threads. Changing this is very dangerous as it has a
21+
// terrible global effect and changes the behavior of all scripts.
22+
var InternalSystemPrompt = `
23+
You are task oriented system.
24+
You receive input from a user, process the input from the give instructions, and then output the result.
25+
Your objective is to provide consist and correct results.
26+
You are referred to as a tool.
27+
`
28+
29+
func init() {
30+
if p := os.Getenv("GPTSCRIPT_INTERNAL_SYSTEM_PROMPT"); p != "" {
31+
InternalSystemPrompt = p
32+
}
33+
}
34+
2035
type ErrToolNotFound struct {
2136
ToolName string
2237
}
@@ -27,7 +42,7 @@ func (e *ErrToolNotFound) Error() string {
2742

2843
type Engine struct {
2944
Client *openai.Client
30-
Progress chan<- types.CompletionMessage
45+
Progress chan<- openai.Status
3146
}
3247

3348
// +k8s:deepcopy-gen=true
@@ -55,36 +70,48 @@ type CallResult struct {
5570
}
5671

5772
type Context struct {
58-
ID string `json:"id,omitempty"`
59-
Ctx context.Context `json:"-"`
60-
Parent *Context `json:"parent,omitempty"`
61-
Tool types.Tool `json:"tool,omitempty"`
62-
Tools map[string]types.Tool `json:"tools,omitempty"`
73+
ID string
74+
Ctx context.Context
75+
Parent *Context
76+
Tool types.Tool
77+
}
78+
79+
func (c *Context) UnmarshalJSON(data []byte) error {
80+
panic("this data struct is circular by design and can not be read from json")
81+
}
82+
83+
func (c *Context) MarshalJSON() ([]byte, error) {
84+
var parentID string
85+
if c.Parent != nil {
86+
parentID = c.Parent.ID
87+
}
88+
return json.Marshal(map[string]any{
89+
"id": c.ID,
90+
"parentID": parentID,
91+
"tool": c.Tool,
92+
})
6393
}
6494

6595
var execID int32
6696

67-
func NewContext(ctx context.Context, parent *Context, tool types.Tool, tools map[string]types.Tool) Context {
97+
func NewContext(ctx context.Context, parent *Context, tool types.Tool) Context {
6898
callCtx := Context{
6999
ID: fmt.Sprint(atomic.AddInt32(&execID, 1)),
70100
Ctx: ctx,
71101
Parent: parent,
72102
Tool: tool,
73-
Tools: tools,
74103
}
75104
return callCtx
76105
}
77106

78107
func (c *Context) getTool(name string) (types.Tool, error) {
79-
for _, tool := range c.Tools {
80-
if tool.Name == name {
81-
return tool, nil
108+
tool, ok := c.Tool.ToolSet[name]
109+
if !ok {
110+
return types.Tool{}, &ErrToolNotFound{
111+
ToolName: name,
82112
}
83113
}
84-
85-
return types.Tool{}, &ErrToolNotFound{
86-
ToolName: name,
87-
}
114+
return tool, nil
88115
}
89116

90117
func (e *Engine) runCommand(tool types.Tool, input string) (string, error) {
@@ -156,13 +183,18 @@ func (e *Engine) Start(ctx Context, input string) (*Return, error) {
156183
completion := types.CompletionRequest{
157184
Model: tool.ModelName,
158185
Vision: tool.Vision,
159-
Tools: nil,
160-
Messages: nil,
161186
MaxToken: tool.MaxTokens,
162187
JSONResponse: tool.JSONResponse,
163188
Cache: tool.Cache,
164189
}
165190

191+
if InternalSystemPrompt != "" {
192+
completion.Messages = append(completion.Messages, types.CompletionMessage{
193+
Role: types.CompletionMessageRoleTypeSystem,
194+
Content: types.Text(InternalSystemPrompt),
195+
})
196+
}
197+
166198
for _, subToolName := range tool.Tools {
167199
subTool, err := ctx.getTool(subToolName)
168200
if err != nil {
@@ -203,7 +235,7 @@ func (e *Engine) Start(ctx Context, input string) (*Return, error) {
203235

204236
func (e *Engine) complete(ctx context.Context, state *State) (*Return, error) {
205237
var (
206-
progress = make(chan types.CompletionMessage)
238+
progress = make(chan openai.Status)
207239
ret = Return{
208240
State: state,
209241
Calls: map[string]Call{},

0 commit comments

Comments
 (0)