forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
595 lines (504 loc) · 15.1 KB
/
engine.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package engine
import (
"context"
"encoding/json"
"fmt"
"os"
"slices"
"strconv"
"strings"
"sync"
"github.com/gptscript-ai/gptscript/pkg/counter"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/gptscript-ai/gptscript/pkg/version"
)
var maxConsecutiveToolCalls = 50
const AbortedSuffix = "\n\nABORTED BY USER"
func init() {
if val := os.Getenv("GPTSCRIPT_MAX_CONSECUTIVE_TOOL_CALLS"); val != "" {
if i, err := strconv.Atoi(val); err == nil && i > 0 {
maxConsecutiveToolCalls = i
}
}
}
type Model interface {
Call(ctx context.Context, messageRequest types.CompletionRequest, env []string, status chan<- types.CompletionStatus) (*types.CompletionMessage, error)
ProxyInfo([]string) (string, string, error)
}
type RuntimeManager interface {
GetContext(ctx context.Context, tool types.Tool, cmd, env []string) (string, []string, error)
}
type Engine struct {
Model Model
RuntimeManager RuntimeManager
Env []string
Progress chan<- types.CompletionStatus
}
type State struct {
Input string `json:"input,omitempty"`
Completion types.CompletionRequest `json:"completion,omitempty"`
Pending map[string]types.CompletionToolCall `json:"pending,omitempty"`
Results map[string]CallResult `json:"results,omitempty"`
}
type Return struct {
State *State `json:"state,omitempty"`
Calls map[string]Call `json:"calls,omitempty"`
Result *string `json:"result,omitempty"`
}
type Call struct {
Missing bool `json:"missing,omitempty"`
ToolID string `json:"toolID,omitempty"`
Input string `json:"input,omitempty"`
}
type CallResult struct {
ToolID string `json:"toolID,omitempty"`
CallID string `json:"callID,omitempty"`
Result string `json:"result,omitempty"`
User string `json:"user,omitempty"`
}
type commonContext struct {
ID string `json:"id"`
Tool types.Tool `json:"tool"`
CurrentAgent types.ToolReference `json:"currentAgent,omitempty"`
AgentGroup []types.ToolReference `json:"agentGroup,omitempty"`
InputContext []InputContext `json:"inputContext"`
ToolCategory ToolCategory `json:"toolCategory,omitempty"`
}
type CallContext struct {
commonContext `json:",inline"`
ToolName string `json:"toolName,omitempty"`
ParentID string `json:"parentID,omitempty"`
DisplayText string `json:"displayText,omitempty"`
}
type Context struct {
commonContext
Ctx context.Context
Parent *Context
LastReturn *Return
CurrentReturn *Return
Engine *Engine
Program *types.Program
// Input is saved only so that we can render display text, don't use otherwise
Input string
userCancel <-chan struct{}
}
type ChatHistory struct {
History []ChatHistoryCall `json:"history,omitempty"`
}
type ChatHistoryCall struct {
ID string `json:"id,omitempty"`
Tool types.Tool `json:"tool,omitempty"`
Completion types.CompletionRequest `json:"completion,omitempty"`
}
type ToolCategory string
const (
ProviderToolCategory ToolCategory = "provider"
CredentialToolCategory ToolCategory = "credential"
ContextToolCategory ToolCategory = "context"
InputToolCategory ToolCategory = "input"
OutputToolCategory ToolCategory = "output"
NoCategory ToolCategory = ""
)
type InputContext struct {
ToolID string `json:"toolID,omitempty"`
Content string `json:"content,omitempty"`
}
type ErrChatFinish struct {
Message string
}
func (e *ErrChatFinish) Error() string {
return fmt.Sprintf("CHAT FINISH: %s", e.Message)
}
func IsChatFinishMessage(msg string) error {
if msg, ok := strings.CutPrefix(msg, "CHAT FINISH: "); ok {
return &ErrChatFinish{Message: msg}
}
return nil
}
func (c *Context) ParentID() string {
if c.Parent == nil {
return ""
}
return c.Parent.ID
}
func (c *Context) CurrentAgent() types.ToolReference {
for _, ref := range c.AgentGroup {
if ref.ToolID == c.Tool.ID {
return ref
}
}
if c.Parent != nil {
return c.Parent.CurrentAgent()
}
return types.ToolReference{}
}
func (c *Context) GetCallContext() *CallContext {
var toolName string
if c.Parent != nil {
outer:
for name, refs := range c.Parent.Tool.ToolMapping {
for _, ref := range refs {
if ref.ToolID == c.Tool.ID {
toolName = name
break outer
}
}
}
}
result := &CallContext{
commonContext: c.commonContext,
ParentID: c.ParentID(),
ToolName: toolName,
DisplayText: types.ToDisplayText(c.Tool, c.Input),
}
result.CurrentAgent = c.CurrentAgent()
return result
}
func (c *Context) UnmarshalJSON([]byte) error {
panic("this data struct is circular by design and can not be read from json")
}
func (c *Context) MarshalJSON() ([]byte, error) {
return json.Marshal(c.GetCallContext())
}
func (c *Context) OnUserCancel(ctx context.Context, cancel func()) {
go func() {
select {
case <-ctx.Done():
// If the context is canceled, then nothing to do.
case <-c.userCancel:
// If the user is requesting a cancel, then cancel the context.
cancel()
}
}()
}
type toolCategoryKey struct{}
func WithToolCategory(ctx context.Context, toolCategory ToolCategory) context.Context {
return context.WithValue(ctx, toolCategoryKey{}, toolCategory)
}
func ToolCategoryFromContext(ctx context.Context) ToolCategory {
category, _ := ctx.Value(toolCategoryKey{}).(ToolCategory)
return category
}
func NewContext(ctx context.Context, prg *types.Program, input string, userCancel <-chan struct{}) (Context, error) {
category := ToolCategoryFromContext(ctx)
callCtx := Context{
commonContext: commonContext{
ID: counter.Next(),
Tool: prg.ToolSet[prg.EntryToolID],
ToolCategory: category,
},
Ctx: ctx,
Program: prg,
Input: input,
userCancel: userCancel,
}
agentGroup, err := callCtx.Tool.GetToolsByType(prg, types.ToolTypeAgent)
if err != nil {
return callCtx, err
}
callCtx.AgentGroup = agentGroup
if callCtx.Tool.IsAgentsOnly() && len(callCtx.AgentGroup) > 0 {
callCtx.Tool = callCtx.Program.ToolSet[callCtx.AgentGroup[0].ToolID]
}
return callCtx, nil
}
func (c *Context) SubCallContext(ctx context.Context, input, toolID, callID string, toolCategory ToolCategory) (Context, error) {
tool := c.Program.ToolSet[toolID]
if callID == "" {
callID = counter.Next()
}
agentGroup, err := c.Tool.GetNextAgentGroup(c.Program, c.AgentGroup, toolID)
if err != nil {
return Context{}, err
}
return Context{
commonContext: commonContext{
ID: callID,
Tool: tool,
AgentGroup: agentGroup,
ToolCategory: toolCategory,
},
Ctx: ctx,
Parent: c,
Program: c.Program,
CurrentReturn: c.CurrentReturn,
Input: input,
userCancel: c.userCancel,
}, nil
}
type engineContext struct{}
func FromContext(ctx context.Context) (*Context, bool) {
c, ok := ctx.Value(engineContext{}).(*Context)
return c, ok
}
func (c *Context) WrappedContext(e *Engine) context.Context {
cp := *c
cp.Engine = e
return context.WithValue(c.Ctx, engineContext{}, &cp)
}
func populateMessageParams(ctx Context, completion *types.CompletionRequest, tool types.Tool) error {
completion.Model = tool.Parameters.ModelName
completion.MaxTokens = tool.Parameters.MaxTokens
completion.JSONResponse = tool.Parameters.JSONResponse
completion.Cache = tool.Parameters.Cache
completion.Chat = tool.Parameters.Chat
completion.Temperature = tool.Parameters.Temperature
completion.InternalSystemPrompt = tool.Parameters.InternalPrompt
if tool.Chat && completion.InternalSystemPrompt == nil {
completion.InternalSystemPrompt = new(bool)
}
var err error
completion.Tools, err = tool.GetChatCompletionTools(*ctx.Program, ctx.AgentGroup...)
if err != nil {
return err
}
completion.Messages = addUpdateSystem(ctx, tool, completion.Messages)
return nil
}
func (e *Engine) runCommandTools(ctx Context, tool types.Tool, input string) (*Return, error) {
if tool.IsHTTP() {
return e.runHTTP(ctx, tool, input)
} else if tool.IsDaemon() {
return e.runDaemon(ctx, tool, input)
} else if tool.IsOpenAPI() {
return e.runOpenAPI(ctx, tool, input)
} else if tool.IsEcho() {
return e.runEcho(tool)
} else if tool.IsCall() {
return e.runCall(ctx, tool, input)
}
s, err := e.runCommand(ctx, tool, input, ctx.ToolCategory)
return &Return{
Result: &s,
}, err
}
func (e *Engine) Start(ctx Context, input string) (ret *Return, err error) {
tool := ctx.Tool
defer func() {
if ret != nil && ret.State != nil {
ret.State.Input = input
}
select {
case <-ctx.userCancel:
if ret.Result == nil {
ret.Result = new(string)
}
*ret.Result += AbortedSuffix
default:
}
}()
if tool.IsCommand() {
return e.runCommandTools(ctx, tool, input)
}
if ctx.ToolCategory == CredentialToolCategory {
return nil, fmt.Errorf("credential tools cannot make calls to the LLM")
}
var completion types.CompletionRequest
if err := populateMessageParams(ctx, &completion, tool); err != nil {
return nil, err
}
if tool.Chat && input == "{}" {
input = ""
}
if input != "" {
completion.Messages = append(completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeUser,
Content: types.Text(input),
})
}
return e.complete(ctx, &State{
Completion: completion,
})
}
func addUpdateSystem(ctx Context, tool types.Tool, msgs []types.CompletionMessage) []types.CompletionMessage {
var instructions []string
for _, context := range ctx.InputContext {
instructions = append(instructions, context.Content)
}
if tool.Instructions != "" {
instructions = append(instructions, tool.Instructions)
}
if len(instructions) == 0 {
return msgs
}
msg := types.CompletionMessage{
Role: types.CompletionMessageRoleTypeSystem,
Content: types.Text(strings.Join(instructions, "\n")),
}
if len(msgs) > 0 && msgs[0].Role == types.CompletionMessageRoleTypeSystem {
return append([]types.CompletionMessage{msg}, msgs[1:]...)
}
return append([]types.CompletionMessage{msg}, msgs...)
}
func (e *Engine) complete(ctx Context, state *State) (*Return, error) {
var (
progress = make(chan types.CompletionStatus)
ret = Return{
State: state,
Calls: map[string]Call{},
}
wg sync.WaitGroup
)
// ensure we aren't writing to the channel anymore on exit
wg.Add(1)
defer wg.Wait()
defer close(progress)
go func() {
defer wg.Done()
for message := range progress {
if e.Progress != nil {
e.Progress <- message
}
}
}()
// Limit the number of consecutive tool calls.
// We don't want the LLM to call tools unrestricted or get stuck in an error loop.
var messagesSinceLastUserMessage int
for _, msg := range slices.Backward(state.Completion.Messages) {
if msg.Role == types.CompletionMessageRoleTypeUser {
break
} else if msg.Role == types.CompletionMessageRoleTypeAssistant {
for _, content := range msg.Content {
// If this message is requesting that a tool call be made, then count it towards the limit.
if content.ToolCall != nil {
messagesSinceLastUserMessage++
break
}
}
}
}
if messagesSinceLastUserMessage > maxConsecutiveToolCalls {
msg := fmt.Sprintf("We cannot continue because the number of consecutive tool calls is limited to %d.", maxConsecutiveToolCalls)
ret.State.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeAssistant,
Content: []types.ContentPart{{Text: msg}},
})
// Setting this ensures that chat continues as expected when we hit this problem.
state.Pending = map[string]types.CompletionToolCall{}
ret.Result = &msg
return &ret, nil
}
resp, err := e.Model.Call(ctx.WrappedContext(e), state.Completion, e.Env, progress)
if err != nil {
return nil, fmt.Errorf("failed calling model for completion: %w", err)
}
state.Completion.Messages = append(state.Completion.Messages, *resp)
state.Pending = map[string]types.CompletionToolCall{}
for _, content := range resp.Content {
if content.ToolCall != nil {
var (
toolID string
missing bool
)
for _, tool := range state.Completion.Tools {
if strings.EqualFold(tool.Function.Name, content.ToolCall.Function.Name) {
toolID = tool.Function.ToolID
}
}
if toolID == "" {
log.Debugf("failed to find tool id for tool %s in tool_call result", content.ToolCall.Function.Name)
toolID = types.ToolNormalizer(content.ToolCall.Function.Name)
missing = true
}
state.Pending[content.ToolCall.ID] = *content.ToolCall
ret.Calls[content.ToolCall.ID] = Call{
ToolID: toolID,
Missing: missing,
Input: content.ToolCall.Function.Arguments,
}
} else {
cp := content.Text
ret.Result = &cp
}
}
if len(resp.Content) == 0 {
// This can happen if the LLM return no content at all. You can reproduce by just saying, "return an empty response"
empty := ""
ret.Result = &empty
}
return &ret, nil
}
func (e *Engine) Continue(ctx Context, state *State, results ...CallResult) (ret *Return, _ error) {
defer func() {
select {
case <-ctx.userCancel:
if ret.Result == nil {
ret.Result = new(string)
}
*ret.Result += AbortedSuffix
default:
}
}()
if ctx.Tool.IsCommand() {
var input string
if len(results) == 1 {
input = results[0].User
}
return e.runCommandTools(ctx, ctx.Tool, input)
}
if state == nil {
return nil, fmt.Errorf("invalid continue call, missing state")
}
var added bool
state = &State{
Input: state.Input,
Completion: state.Completion,
Pending: state.Pending,
Results: map[string]CallResult{},
}
for _, result := range results {
if result.CallID == "" {
added = true
state.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeUser,
Content: types.Text(result.User),
})
} else {
state.Results[result.CallID] = result
}
}
ret = &Return{
State: state,
Calls: map[string]Call{},
}
for id, pending := range state.Pending {
if _, ok := state.Results[id]; !ok {
ret.Calls[id] = Call{
ToolID: state.Completion.Tools[*pending.Index].Function.ToolID,
Input: pending.Function.Arguments,
}
}
}
if len(ret.Calls) > 0 {
// Outstanding tool calls still pending
return ret, nil
}
for _, content := range state.Completion.Messages[len(state.Completion.Messages)-1].Content {
if content.ToolCall == nil {
continue
}
result, ok := state.Results[content.ToolCall.ID]
if !ok {
return nil, fmt.Errorf("missing tool call result for id %s, most likely a %s BUG",
content.ToolCall.ID, version.ProgramName)
}
pending, ok := state.Pending[content.ToolCall.ID]
if !ok {
return nil, fmt.Errorf("missing tool call pending for id %s, most likely a %s BUG",
content.ToolCall.ID, version.ProgramName)
}
added = true
state.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeTool,
Content: types.Text(result.Result),
ToolCall: &pending,
})
}
if !added {
return nil, fmt.Errorf("invalid continue call, no completion needed")
}
if err := populateMessageParams(ctx, &state.Completion, ctx.Tool); err != nil {
return nil, err
}
return e.complete(ctx, state)
}