Skip to content

feat: add sys.chat.history #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/BurntSushi/locker"
"github.com/google/shlex"
"github.com/gptscript-ai/gptscript/pkg/engine"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/jaytaylor/html2text"
)

var SafeTools = map[string]struct{}{
"sys.echo": {},
"sys.time.now": {},
"sys.prompt": {},
"sys.chat.finish": {},
"sys.echo": {},
"sys.time.now": {},
"sys.prompt": {},
"sys.chat.finish": {},
"sys.chat.history": {},
}

var tools = map[string]types.Tool{
Expand Down Expand Up @@ -182,6 +184,13 @@ var tools = map[string]types.Tool{
},
BuiltinFunc: SysPrompt,
},
"sys.chat.history": {
Parameters: types.Parameters{
Description: "Retrieves the previous chat dialog",
Arguments: types.ObjectSchema(),
},
BuiltinFunc: SysChatHistory,
},
}

func SysProgram() *types.Program {
Expand Down Expand Up @@ -567,6 +576,33 @@ func (e *ErrChatFinish) Error() string {
return fmt.Sprintf("CHAT FINISH: %s", e.Message)
}

func SysChatHistory(ctx context.Context, _ []string, _ string) (string, error) {
engineContext, _ := engine.FromContext(ctx)

data, err := json.Marshal(engine.ChatHistory{
History: writeHistory(engineContext),
})

return string(data), err
}

func writeHistory(ctx *engine.Context) (result []engine.ChatHistoryCall) {
if ctx == nil {
return nil
}
if ctx.Parent != nil {
result = append(result, writeHistory(ctx.Parent)...)
}
if ctx.LastReturn != nil && ctx.LastReturn.State != nil {
result = append(result, engine.ChatHistoryCall{
ID: ctx.ID,
Tool: ctx.Tool,
Completion: ctx.LastReturn.State.Completion,
})
}
return
}

func SysChatFinish(ctx context.Context, env []string, input string) (string, error) {
var params struct {
Message string `json:"return,omitempty"`
Expand Down
16 changes: 12 additions & 4 deletions pkg/engine/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/gptscript-ai/gptscript/pkg/version"
)

func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string, toolCategory ToolCategory) (cmdOut string, cmdErr error) {
func (e *Engine) runCommand(ctx Context, tool types.Tool, input string, toolCategory ToolCategory) (cmdOut string, cmdErr error) {
id := counter.Next()

defer func() {
Expand All @@ -42,10 +42,18 @@ func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string,
"input": input,
},
}
return tool.BuiltinFunc(ctx, e.Env, input)
return tool.BuiltinFunc(ctx.WrappedContext(), e.Env, input)
}

cmd, stop, err := e.newCommand(ctx, nil, tool, input)
var instructions []string
for _, inputContext := range ctx.InputContext {
instructions = append(instructions, inputContext.Content)
}
var extraEnv = []string{
strings.TrimSpace(fmt.Sprintf("GPTSCRIPT_CONTEXT=%s", strings.Join(instructions, "\n"))),
}

cmd, stop, err := e.newCommand(ctx.Ctx, extraEnv, tool, input)
if err != nil {
return "", err
}
Expand All @@ -66,7 +74,7 @@ func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string,
cmd.Stdout = io.MultiWriter(all, output)

if toolCategory == CredentialToolCategory {
pause := context2.GetPauseFuncFromCtx(ctx)
pause := context2.GetPauseFuncFromCtx(ctx.Ctx)
unpause := pause()
defer unpause()
}
Expand Down
23 changes: 19 additions & 4 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,20 @@ type CallContext struct {

type Context struct {
commonContext
Ctx context.Context
Parent *Context
Program *types.Program
Ctx context.Context
Parent *Context
LastReturn *Return
Program *types.Program
}

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
Expand Down Expand Up @@ -194,7 +205,7 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
} else if tool.IsEcho() {
return e.runEcho(tool)
}
s, err := e.runCommand(ctx.WrappedContext(), tool, input, ctx.ToolCategory)
s, err := e.runCommand(ctx, tool, input, ctx.ToolCategory)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -233,6 +244,10 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
input = ""
}

if tool.Chat && input == "{}" {
input = ""
}

if input != "" {
completion.Messages = append(completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeUser,
Expand Down
4 changes: 4 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,10 @@ func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string,
resultLock sync.Mutex
)

if state.Continuation != nil {
callCtx.LastReturn = state.Continuation
}

if state.InputContextContinuation != nil {
return state, nil, nil
}
Expand Down
84 changes: 24 additions & 60 deletions pkg/tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,8 @@ func TestSubChat(t *testing.T) {
"state": {
"input": "Hello",
"completion": {
"Model": "gpt-4o",
"InternalSystemPrompt": null,
"Tools": [
"model": "gpt-4o",
"tools": [
{
"function": {
"toolID": "testdata/TestSubChat/test.gpt:chatbot",
Expand All @@ -238,7 +237,7 @@ func TestSubChat(t *testing.T) {
}
}
],
"Messages": [
"messages": [
{
"role": "system",
"content": [
Expand Down Expand Up @@ -272,12 +271,7 @@ func TestSubChat(t *testing.T) {
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
]
},
"pending": {
"call_1": {
Expand All @@ -303,10 +297,9 @@ func TestSubChat(t *testing.T) {
"continuation": {
"state": {
"completion": {
"Model": "gpt-4o",
"InternalSystemPrompt": false,
"Tools": null,
"Messages": [
"model": "gpt-4o",
"internalSystemPrompt": false,
"messages": [
{
"role": "system",
"content": [
Expand All @@ -325,12 +318,7 @@ func TestSubChat(t *testing.T) {
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
]
}
},
"result": "Assistant 1"
Expand All @@ -355,9 +343,8 @@ func TestSubChat(t *testing.T) {
"state": {
"input": "Hello",
"completion": {
"Model": "gpt-4o",
"InternalSystemPrompt": null,
"Tools": [
"model": "gpt-4o",
"tools": [
{
"function": {
"toolID": "testdata/TestSubChat/test.gpt:chatbot",
Expand All @@ -366,7 +353,7 @@ func TestSubChat(t *testing.T) {
}
}
],
"Messages": [
"messages": [
{
"role": "system",
"content": [
Expand Down Expand Up @@ -400,12 +387,7 @@ func TestSubChat(t *testing.T) {
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
]
},
"pending": {
"call_1": {
Expand All @@ -431,10 +413,9 @@ func TestSubChat(t *testing.T) {
"continuation": {
"state": {
"completion": {
"Model": "gpt-4o",
"InternalSystemPrompt": false,
"Tools": null,
"Messages": [
"model": "gpt-4o",
"internalSystemPrompt": false,
"messages": [
{
"role": "system",
"content": [
Expand Down Expand Up @@ -471,12 +452,7 @@ func TestSubChat(t *testing.T) {
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
]
}
},
"result": "Assistant 2"
Expand Down Expand Up @@ -513,10 +489,9 @@ func TestChat(t *testing.T) {
"state": {
"input": "Hello",
"completion": {
"Model": "gpt-4o",
"InternalSystemPrompt": false,
"Tools": null,
"Messages": [
"model": "gpt-4o",
"internalSystemPrompt": false,
"messages": [
{
"role": "system",
"content": [
Expand Down Expand Up @@ -544,12 +519,7 @@ func TestChat(t *testing.T) {
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
]
}
},
"result": "Assistant 1"
Expand All @@ -570,10 +540,9 @@ func TestChat(t *testing.T) {
"state": {
"input": "Hello",
"completion": {
"Model": "gpt-4o",
"InternalSystemPrompt": false,
"Tools": null,
"Messages": [
"model": "gpt-4o",
"internalSystemPrompt": false,
"messages": [
{
"role": "system",
"content": [
Expand Down Expand Up @@ -619,12 +588,7 @@ func TestChat(t *testing.T) {
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
]
}
},
"result": "Assistant 2"
Expand Down
14 changes: 4 additions & 10 deletions pkg/tests/testdata/TestCase/call1.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
`{
"Model": "gpt-4o",
"InternalSystemPrompt": null,
"Tools": [
"model": "gpt-4o",
"tools": [
{
"function": {
"toolID": "testdata/TestCase/test.gpt:Bob",
Expand All @@ -19,7 +18,7 @@
}
}
],
"Messages": [
"messages": [
{
"role": "system",
"content": [
Expand All @@ -29,10 +28,5 @@
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
]
}`
Loading