Skip to content

feat: add support for the subTool option #22

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
Apr 26, 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
1 change: 1 addition & 0 deletions src/gptscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const optToArg = {
cacheDir: "--cache-dir=",
quiet: "--quiet=",
chdir: "--chdir=",
subTool: "--sub-tool=",
}

function toArgs(opts) {
Expand Down
4 changes: 2 additions & 2 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function makeStreamRequest(path, tool, opts = {}) {
const req = http.request(options, (res) => {
res.on('data', (chunk) => {
const c = chunk.toString().replace(/^(data: )/,"").trim();
if (c === 'DONE') {
if (c === '[DONE]') {
} else if (c.startsWith('{"stderr":')) {
stderr.push(JSON.parse(c).stderr);
} else {
Expand Down Expand Up @@ -100,7 +100,7 @@ async function makeStreamRequestWithEvents(path, tool, opts = {}) {
const req = http.request(options, (res) => {
res.on('data', (chunk) => {
const c = chunk.toString().replace(/^(data: )/,"").trim();
if (c === 'DONE') {
if (c === '[DONE]') {
} else if (c.startsWith('{"stderr":')) {
stderr.push(JSON.parse(c).stderr);
} else if (c.startsWith('{"stdout":')) {
Expand Down
59 changes: 42 additions & 17 deletions tests/gptscript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,47 @@ describe('gptscript module', () => {
expect(event).toContain("events: ");
});

test('exec of multiple tools', async () => {
const t0 = new gptscript.Tool({
tools: ["ask"],
instructions: "Only use the ask tool to ask who was the president of the united states in 1928?"
});
const t1 = new gptscript.Tool({
name: "ask",
description: "This tool is used to ask a question",
args: {
question: "The question to ask"
},
instructions: "${question}"
});
describe('exec with multiple tools', () => {
test('multiple tools', async () => {
const t0 = new gptscript.Tool({
tools: ["ask"],
instructions: "Only use the ask tool to ask who was the president of the united states in 1928?"
});
const t1 = new gptscript.Tool({
name: "ask",
description: "This tool is used to ask a question",
args: {
question: "The question to ask"
},
instructions: "${question}"
});

const response = await gptscript.exec([t0, t1]);
expect(response).toBeDefined();
expect(response).toContain("Calvin Coolidge");
}, 30000);
const response = await gptscript.exec([t0, t1]);
expect(response).toBeDefined();
expect(response).toContain("Calvin Coolidge");
}, 30000);

test('with sub tool', async () => {
const t0 = new gptscript.Tool({
tools: ["ask"],
instructions: "Only use the ask tool to ask who was the president of the united states in 1928?"
});
const t1 = new gptscript.Tool({
name: "other",
instructions: "Who was the president of the united states in 1986?"
});
const t2 = new gptscript.Tool({
name: "ask",
description: "This tool is used to ask a question",
args: {
question: "The question to ask"
},
instructions: "${question}"
});

const response = await gptscript.exec([t0, t1, t2], {subTool: 'other'});
expect(response).toBeDefined();
expect(response).toContain("Ronald Reagan");
}, 30000);
});
});