@@ -17,6 +17,21 @@ import (
17
17
"github.com/acorn-io/gptscript/pkg/version"
18
18
)
19
19
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
+
20
35
type ErrToolNotFound struct {
21
36
ToolName string
22
37
}
@@ -27,7 +42,7 @@ func (e *ErrToolNotFound) Error() string {
27
42
28
43
type Engine struct {
29
44
Client * openai.Client
30
- Progress chan <- types. CompletionMessage
45
+ Progress chan <- openai. Status
31
46
}
32
47
33
48
// +k8s:deepcopy-gen=true
@@ -55,36 +70,48 @@ type CallResult struct {
55
70
}
56
71
57
72
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
+ })
63
93
}
64
94
65
95
var execID int32
66
96
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 {
68
98
callCtx := Context {
69
99
ID : fmt .Sprint (atomic .AddInt32 (& execID , 1 )),
70
100
Ctx : ctx ,
71
101
Parent : parent ,
72
102
Tool : tool ,
73
- Tools : tools ,
74
103
}
75
104
return callCtx
76
105
}
77
106
78
107
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 ,
82
112
}
83
113
}
84
-
85
- return types.Tool {}, & ErrToolNotFound {
86
- ToolName : name ,
87
- }
114
+ return tool , nil
88
115
}
89
116
90
117
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) {
156
183
completion := types.CompletionRequest {
157
184
Model : tool .ModelName ,
158
185
Vision : tool .Vision ,
159
- Tools : nil ,
160
- Messages : nil ,
161
186
MaxToken : tool .MaxTokens ,
162
187
JSONResponse : tool .JSONResponse ,
163
188
Cache : tool .Cache ,
164
189
}
165
190
191
+ if InternalSystemPrompt != "" {
192
+ completion .Messages = append (completion .Messages , types.CompletionMessage {
193
+ Role : types .CompletionMessageRoleTypeSystem ,
194
+ Content : types .Text (InternalSystemPrompt ),
195
+ })
196
+ }
197
+
166
198
for _ , subToolName := range tool .Tools {
167
199
subTool , err := ctx .getTool (subToolName )
168
200
if err != nil {
@@ -203,7 +235,7 @@ func (e *Engine) Start(ctx Context, input string) (*Return, error) {
203
235
204
236
func (e * Engine ) complete (ctx context.Context , state * State ) (* Return , error ) {
205
237
var (
206
- progress = make (chan types. CompletionMessage )
238
+ progress = make (chan openai. Status )
207
239
ret = Return {
208
240
State : state ,
209
241
Calls : map [string ]Call {},
0 commit comments