Skip to content

chore: add with * syntax to context tools #825

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 3 commits into from
Aug 27, 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
10 changes: 9 additions & 1 deletion pkg/repos/runtimes/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var releasesData []byte
const (
downloadURL = "https://nodejs.org/dist/%s/"
packageJSON = "package.json"
nodeModules = "node_modules"
)

type Runtime struct {
Expand Down Expand Up @@ -64,8 +65,15 @@ func (r *Runtime) supports(testCmd string, cmd []string) bool {

func (r *Runtime) GetHash(tool types.Tool) (string, error) {
if !tool.Source.IsGit() && tool.WorkingDir != "" {
var prefix string
// This hashes if the node_modules directory was deleted
if s, err := os.Stat(filepath.Join(tool.WorkingDir, nodeModules)); err == nil {
prefix = hash.Digest(tool.WorkingDir + s.ModTime().String())[:7]
} else if s, err := os.Stat(tool.WorkingDir); err == nil {
prefix = hash.Digest(tool.WorkingDir + s.ModTime().String())[:7]
}
if s, err := os.Stat(filepath.Join(tool.WorkingDir, packageJSON)); err == nil {
return hash.Digest(tool.WorkingDir + s.ModTime().String())[:7], nil
return prefix + hash.Digest(tool.WorkingDir + s.ModTime().String())[:7], nil
}
}
return "", nil
Expand Down
99 changes: 23 additions & 76 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ func (r *Runner) Chat(ctx context.Context, prevState ChatState, prg types.Progra
return resp, err
}

if state == nil || state.StartContinuation {
if state != nil {
state = state.WithResumeInput(&input)
input = state.InputContextContinuationInput
}
if state == nil {
state, err = r.start(callCtx, state, monitor, env, input)
if err != nil {
return resp, err
Expand All @@ -186,11 +182,9 @@ func (r *Runner) Chat(ctx context.Context, prevState ChatState, prg types.Progra
state.ResumeInput = &input
}

if !state.StartContinuation {
state, err = r.resume(callCtx, monitor, env, state)
if err != nil {
return resp, err
}
state, err = r.resume(callCtx, monitor, env, state)
if err != nil {
return resp, err
}

if state.Result != nil {
Expand Down Expand Up @@ -260,6 +254,10 @@ func getToolRefInput(prg *types.Program, ref types.ToolReference, input string)
targetArgs := prg.ToolSet[ref.ToolID].Arguments
targetKeys := map[string]string{}

if ref.Arg == "*" {
return input, nil
}

if targetArgs == nil {
return "", nil
}
Expand Down Expand Up @@ -331,24 +329,10 @@ func getToolRefInput(prg *types.Program, ref types.ToolReference, input string)
return string(output), err
}

func (r *Runner) getContext(callCtx engine.Context, state *State, monitor Monitor, env []string, input string) (result []engine.InputContext, _ *State, _ error) {
func (r *Runner) getContext(callCtx engine.Context, state *State, monitor Monitor, env []string, input string) (result []engine.InputContext, _ error) {
toolRefs, err := callCtx.Tool.GetContextTools(*callCtx.Program)
if err != nil {
return nil, nil, err
}

var newState *State
if state != nil {
cp := *state
newState = &cp
if newState.InputContextContinuation != nil {
newState.InputContexts = nil
newState.InputContextContinuation = nil
newState.InputContextContinuationInput = ""
newState.ResumeInput = state.InputContextContinuationResumeInput

input = state.InputContextContinuationInput
}
return nil, err
}

for i, toolRef := range toolRefs {
Expand All @@ -359,47 +343,31 @@ func (r *Runner) getContext(callCtx engine.Context, state *State, monitor Monito

contextInput, err := getToolRefInput(callCtx.Program, toolRef, input)
if err != nil {
return nil, nil, err
return nil, err
}

var content *State
if state != nil && state.InputContextContinuation != nil {
content, err = r.subCallResume(callCtx.Ctx, callCtx, monitor, env, toolRef.ToolID, "", state.InputContextContinuation.WithResumeInput(state.ResumeInput), engine.ContextToolCategory)
} else {
content, err = r.subCall(callCtx.Ctx, callCtx, monitor, env, toolRef.ToolID, contextInput, "", engine.ContextToolCategory)
}
content, err = r.subCall(callCtx.Ctx, callCtx, monitor, env, toolRef.ToolID, contextInput, "", engine.ContextToolCategory)
if err != nil {
return nil, nil, err
return nil, err
}
if content.Continuation != nil {
if newState == nil {
newState = &State{}
}
newState.InputContexts = result
newState.InputContextContinuation = content
newState.InputContextContinuationInput = input
if state != nil {
newState.InputContextContinuationResumeInput = state.ResumeInput
}
return nil, newState, nil
return nil, fmt.Errorf("invalid state: context tool [%s] can not result in a continuation", toolRef.ToolID)
}
result = append(result, engine.InputContext{
ToolID: toolRef.ToolID,
Content: *content.Result,
})
}

return result, newState, nil
return result, nil
}

func (r *Runner) call(callCtx engine.Context, monitor Monitor, env []string, input string) (*State, error) {
result, err := r.start(callCtx, nil, monitor, env, input)
if err != nil {
return nil, err
}
if result.StartContinuation {
return result, nil
}
return r.resume(callCtx, monitor, env, result)
}

Expand Down Expand Up @@ -431,15 +399,10 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
}
}

var newState *State
callCtx.InputContext, newState, err = r.getContext(callCtx, state, monitor, env, input)
callCtx.InputContext, err = r.getContext(callCtx, state, monitor, env, input)
if err != nil {
return nil, err
}
if newState != nil && newState.InputContextContinuation != nil {
newState.StartContinuation = true
return newState, nil
}

e := engine.Engine{
Model: r.c,
Expand Down Expand Up @@ -489,11 +452,7 @@ type State struct {
SubCalls []SubCallResult `json:"subCalls,omitempty"`
SubCallID string `json:"subCallID,omitempty"`

InputContexts []engine.InputContext `json:"inputContexts,omitempty"`
InputContextContinuation *State `json:"inputContextContinuation,omitempty"`
InputContextContinuationInput string `json:"inputContextContinuationInput,omitempty"`
InputContextContinuationResumeInput *string `json:"inputContextContinuationResumeInput,omitempty"`
StartContinuation bool `json:"startContinuation,omitempty"`
InputContexts []engine.InputContext `json:"inputContexts,omitempty"`
}

func (s State) WithResumeInput(input *string) *State {
Expand All @@ -506,10 +465,6 @@ func (s State) ContinuationContentToolID() (string, error) {
return s.ContinuationToolID, nil
}

if s.InputContextContinuation != nil {
return s.InputContextContinuation.ContinuationContentToolID()
}

for _, subCall := range s.SubCalls {
if s.SubCallID == subCall.CallID {
return subCall.State.ContinuationContentToolID()
Expand All @@ -523,10 +478,6 @@ func (s State) ContinuationContent() (string, error) {
return *s.Continuation.Result, nil
}

if s.InputContextContinuation != nil {
return s.InputContextContinuation.ContinuationContent()
}

for _, subCall := range s.SubCalls {
if s.SubCallID == subCall.CallID {
return subCall.State.ContinuationContent()
Expand All @@ -545,10 +496,6 @@ func (r *Runner) resume(callCtx engine.Context, monitor Monitor, env []string, s
retState, retErr = r.handleOutput(callCtx, monitor, env, retState, retErr)
}()

if state.StartContinuation {
return nil, fmt.Errorf("invalid state, resume should not have StartContinuation set to true")
}

if state.Continuation == nil {
return nil, errors.New("invalid state, resume should have Continuation data")
}
Expand Down Expand Up @@ -653,8 +600,12 @@ func (r *Runner) resume(callCtx engine.Context, monitor Monitor, env []string, s
contentInput = state.Continuation.State.Input
}

callCtx.InputContext, state, err = r.getContext(callCtx, state, monitor, env, contentInput)
if err != nil || state.InputContextContinuation != nil {
if state.ResumeInput != nil {
contentInput = *state.ResumeInput
}

callCtx.InputContext, err = r.getContext(callCtx, state, monitor, env, contentInput)
if err != nil {
return state, err
}

Expand Down Expand Up @@ -764,10 +715,6 @@ func (r *Runner) subCalls(callCtx engine.Context, monitor Monitor, env []string,
callCtx.LastReturn = state.Continuation
}

if state.InputContextContinuation != nil {
return state, nil, nil
}

if state.SubCallID != "" {
if state.ResumeInput == nil {
return nil, nil, fmt.Errorf("invalid state, input must be set for sub call continuation on callID [%s]", state.SubCallID)
Expand Down
34 changes: 34 additions & 0 deletions pkg/tests/runner2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tests

import (
"context"
"testing"

"github.com/gptscript-ai/gptscript/pkg/loader"
"github.com/gptscript-ai/gptscript/pkg/tests/tester"
"github.com/stretchr/testify/require"
)

func TestContextWithAsterick(t *testing.T) {
r := tester.NewRunner(t)
prg, err := loader.ProgramFromSource(context.Background(), `
chat: true
context: foo with *

Say hi

---
name: foo

#!/bin/bash

echo This is the input: ${GPTSCRIPT_INPUT}
`, "")
require.NoError(t, err)

resp, err := r.Chat(context.Background(), nil, prg, nil, "input 1")
r.AssertStep(t, resp, err)

resp, err = r.Chat(context.Background(), resp.State, prg, nil, "input 2")
r.AssertStep(t, resp, err)
}
78 changes: 2 additions & 76 deletions pkg/tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,82 +212,8 @@ func TestContextSubChat(t *testing.T) {
prg, err := r.Load("")
require.NoError(t, err)

resp, err := r.Chat(context.Background(), nil, prg, os.Environ(), "User 1")
require.NoError(t, err)
r.AssertResponded(t)
assert.False(t, resp.Done)
autogold.Expect("Assistant Response 1 - from chatbot1").Equal(t, resp.Content)
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+"/step1"))

r.RespondWith(tester.Result{
Content: []types.ContentPart{
{
ToolCall: &types.CompletionToolCall{
ID: "call_2",
Function: types.CompletionFunctionCall{
Name: types.ToolNormalizer("sys.chat.finish"),
Arguments: "Response from context chatbot",
},
},
},
},
}, tester.Result{
Text: "Assistant Response 2 - from context tool",
}, tester.Result{
Text: "Assistant Response 3 - from main chat tool",
})
resp, err = r.Chat(context.Background(), resp.State, prg, os.Environ(), "User 2")
require.NoError(t, err)
r.AssertResponded(t)
assert.False(t, resp.Done)
autogold.Expect("Assistant Response 3 - from main chat tool").Equal(t, resp.Content)
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+"/step2"))

r.RespondWith(tester.Result{
Content: []types.ContentPart{
{
ToolCall: &types.CompletionToolCall{
ID: "call_3",
Function: types.CompletionFunctionCall{
Name: "chatbot",
Arguments: "Input to chatbot1 on resume",
},
},
},
},
}, tester.Result{
Text: "Assistant Response 4 - from chatbot1",
})
resp, err = r.Chat(context.Background(), resp.State, prg, os.Environ(), "User 3")
require.NoError(t, err)
r.AssertResponded(t)
assert.False(t, resp.Done)
autogold.Expect("Assistant Response 3 - from main chat tool").Equal(t, resp.Content)
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+"/step3"))

r.RespondWith(tester.Result{
Content: []types.ContentPart{
{
ToolCall: &types.CompletionToolCall{
ID: "call_4",
Function: types.CompletionFunctionCall{
Name: types.ToolNormalizer("sys.chat.finish"),
Arguments: "Response from context chatbot after resume",
},
},
},
},
}, tester.Result{
Text: "Assistant Response 5 - from context tool resume",
}, tester.Result{
Text: "Assistant Response 6 - from main chat tool resume",
})
resp, err = r.Chat(context.Background(), resp.State, prg, os.Environ(), "User 4")
require.NoError(t, err)
r.AssertResponded(t)
assert.False(t, resp.Done)
autogold.Expect("Assistant Response 6 - from main chat tool resume").Equal(t, resp.Content)
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+"/step4"))
_, err = r.Chat(context.Background(), nil, prg, os.Environ(), "User 1")
autogold.Expect("invalid state: context tool [testdata/TestContextSubChat/test.gpt:subtool] can not result in a continuation").Equal(t, err.Error())
}

func TestSubChat(t *testing.T) {
Expand Down
9 changes: 0 additions & 9 deletions pkg/tests/testdata/TestContextSubChat/call10-resp.golden

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/tests/testdata/TestContextSubChat/call3-resp.golden

This file was deleted.

Loading