From c28abdae4e1fe5d3d440854f6a5f50ba1e2bb23f Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Fri, 17 May 2024 14:30:09 -0400 Subject: [PATCH] feat: add support for tools context --- examples/example-file-stream.js | 21 ------------ examples/example-models.js | 11 ------ examples/example-parse-file.js | 11 ------ examples/example-parse.js | 11 ------ examples/example-tool-chat-stream.js | 49 --------------------------- examples/example-tool-stream.js | 23 ------------- examples/example-tools.js | 11 ------ examples/example-version.js | 11 ------ examples/sangeetha.js | 40 ---------------------- src/gptscript.ts | 7 ++++ tests/fixtures/acorn-labs-context.gpt | 1 + tests/gptscript.test.ts | 20 +++++++++++ 12 files changed, 28 insertions(+), 188 deletions(-) delete mode 100644 examples/example-file-stream.js delete mode 100644 examples/example-models.js delete mode 100644 examples/example-parse-file.js delete mode 100644 examples/example-parse.js delete mode 100644 examples/example-tool-chat-stream.js delete mode 100644 examples/example-tool-stream.js delete mode 100644 examples/example-tools.js delete mode 100644 examples/example-version.js delete mode 100644 examples/sangeetha.js create mode 100644 tests/fixtures/acorn-labs-context.gpt diff --git a/examples/example-file-stream.js b/examples/example-file-stream.js deleted file mode 100644 index dbed360..0000000 --- a/examples/example-file-stream.js +++ /dev/null @@ -1,21 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; - -(async () => { - const testGptPath = `/Users/thedadams/code/gptscript-examples/envvar.gpt`; - const opts = { - disableCache: true - } - - try { - const client = new gptscript.Client(process.env.GPTSCRIPT_URL, process.env.GPTSCRIPT_BIN) - const run = await client.run(testGptPath, opts); - run.on(gptscript.RunEventType.Event, data => { - console.log(`event: ${JSON.stringify(data)}`) - }); - - console.log(await run.text()) - } catch (e) { - console.log(e) - console.error(JSON.stringify(e)); - } -})(); diff --git a/examples/example-models.js b/examples/example-models.js deleted file mode 100644 index 3194e8f..0000000 --- a/examples/example-models.js +++ /dev/null @@ -1,11 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; - -(async () => { - try { - const client = new gptscript.Client(process.env.GPTSCRIPT_URL) - const response = await client.listModels(); - console.log(response); - } catch (error) { - console.error(error); - } -})(); diff --git a/examples/example-parse-file.js b/examples/example-parse-file.js deleted file mode 100644 index 20fed82..0000000 --- a/examples/example-parse-file.js +++ /dev/null @@ -1,11 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; - -(async () => { - try { - const client = new gptscript.Client(process.env.GPTSCRIPT_URL) - const response = await client.parse("/Users/thedadams/code/gptscript-examples/envvar.gpt"); - console.log(JSON.stringify(response)); - } catch (error) { - console.error(error); - } -})(); diff --git a/examples/example-parse.js b/examples/example-parse.js deleted file mode 100644 index e85ce8f..0000000 --- a/examples/example-parse.js +++ /dev/null @@ -1,11 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; - -(async () => { - try { - const client = new gptscript.Client(process.env.GPTSCRIPT_URL) - const response = await client.parseTool("hello world"); - console.log(JSON.stringify(response)); - } catch (error) { - console.error(error); - } -})(); diff --git a/examples/example-tool-chat-stream.js b/examples/example-tool-chat-stream.js deleted file mode 100644 index aeda325..0000000 --- a/examples/example-tool-chat-stream.js +++ /dev/null @@ -1,49 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; -import * as readline from "readline"; -import {stdin as input, stdout as output} from 'node:process'; - -const client = new gptscript.Client(process.env.GPTSCRIPT_URL) - -const rl = readline.createInterface({input, output}); - -const t = { - chat: true, - tools: ["sys.chat.finish"], - instructions: "Say hello and start a chat session.", -}; - -let r = client.evaluate(t, { - disableCache: true, -}); - -(async () => { - r.on(gptscript.RunEventType.Event, (data) => { - console.log(JSON.stringify(data)) - }) - console.log(await r.text()); - const recursiveAsyncReadLine = function () { - rl.question(`>> `, async function (answer) { - if (answer === "") //we need some base case, for recursion - return rl.close(); //closing RL and returning from function. - try { - r = r.nextChat(answer); - } catch (e) { - console.error(e); - } - console.log(await r.text()); - console.log(r.state); - if (r.state === gptscript.RunState.Finished) { - console.log("The conversation is finished. Goodbye!"); - return rl.close(); - } - recursiveAsyncReadLine(); //Calling this function again to ask new question - }); - }; - - recursiveAsyncReadLine(); -})() - -rl.on("close", function () { - console.log("\nBYE BYE !!!"); - process.exit(0); -}); diff --git a/examples/example-tool-stream.js b/examples/example-tool-stream.js deleted file mode 100644 index ef4131b..0000000 --- a/examples/example-tool-stream.js +++ /dev/null @@ -1,23 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; - -(async () => { - try { - const client = new gptscript.Client(process.env.GPTSCRIPT_URL) - - const t = { - instructions: "who was the president of the united states in 1928?" - } - - const r = client.evaluate(t, { - disableCache: true - }) - - r.on(gptscript.RunEventType.Event, data => { - console.log(`event: ${JSON.stringify(data)}`); - }); - - console.log(await r.text()) - } catch (e) { - console.error(e) - } -})() diff --git a/examples/example-tools.js b/examples/example-tools.js deleted file mode 100644 index 531a0ec..0000000 --- a/examples/example-tools.js +++ /dev/null @@ -1,11 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; - -(async () => { - try { - const client = new gptscript.Client(process.env.GPTSCRIPT_URL) - const response = await client.listTools(); - console.log(response) - } catch (error) { - console.error(error) - } -})() diff --git a/examples/example-version.js b/examples/example-version.js deleted file mode 100644 index 6423e3e..0000000 --- a/examples/example-version.js +++ /dev/null @@ -1,11 +0,0 @@ -import * as gptscript from "../dist/gptscript.js"; - -(async () => { - try { - const client = new gptscript.Client(process.env.GPTSCRIPT_URL) - const response = await client.version(); - console.log(response); - } catch (error) { - console.error(error); - } -})(); diff --git a/examples/sangeetha.js b/examples/sangeetha.js deleted file mode 100644 index 3b393e3..0000000 --- a/examples/sangeetha.js +++ /dev/null @@ -1,40 +0,0 @@ -import * as gptscript from '../dist/gptscript.js'; - -const opts = { - disableCache: true -}; - -const client = new gptscript.Client() -const t = { - chat: true, - tools: ["sys.chat.finish", "github.com/gptscript-ai/search/duckduckgo", "sys.http.html2text?"], - instructions: "You are a chat bot. Don't finish the conversation until I say 'bye'.Search using duckduckgo. If the search tool fails to return any information stop execution of the script with message 'Sorry! Search dis not retrun any results' .Feel free to get the contents of the returned URLs in order to get more information. Provide as much detail as you can.Search for who won superbowl 2024" -}; - -streamExecFileWithEvents() - -async function streamExecFileWithEvents() { - let i = 0; - const chats = ["where was this game played?", "when and where was this game played last year?", "who was the winner of this game", "bye"]; - let run = client.evaluate(t, opts); - try { - // Wait for the initial run to complete. - console.log(await run.text()) - - while (run.state === gptscript.RunState.Continue) { - // ...Get the next input from the user somehow... - - run = run.nextChat(chats[i++]) - - // Get the output from gptscript - const output = await run.text() - console.log(output) - } - } catch (e) { - console.log("ERROR") - console.error(e); - } - - // The state here should either be RunState.Finished (on success) or RunState.Error (on error). - console.log(run.state) -} \ No newline at end of file diff --git a/src/gptscript.ts b/src/gptscript.ts index 69c68c5..0da1cbf 100644 --- a/src/gptscript.ts +++ b/src/gptscript.ts @@ -729,6 +729,7 @@ export interface ToolDef { arguments: ArgumentSchema tools: string[] globalTools: string[] + context: string[] export: string[] blocking: boolean instructions: string @@ -960,6 +961,12 @@ function toolDefToString(tool: ToolDef) { if (tool.tools?.length > 0) { toolInfo.push(`Tools: ${tool.tools.join(", ")}`) } + if (tool.context?.length > 0) { + toolInfo.push(`Context: ${tool.context.join(", ")}`) + } + if (tool.export?.length > 0) { + toolInfo.push(`Export: ${tool.export.join(", ")}`) + } if (tool.maxTokens !== undefined) { toolInfo.push(`Max Tokens: ${tool.maxTokens}`) } diff --git a/tests/fixtures/acorn-labs-context.gpt b/tests/fixtures/acorn-labs-context.gpt new file mode 100644 index 0000000..e4dbe2c --- /dev/null +++ b/tests/fixtures/acorn-labs-context.gpt @@ -0,0 +1 @@ +Say "Answer every query with 'Acorn Labs'" \ No newline at end of file diff --git a/tests/gptscript.test.ts b/tests/gptscript.test.ts index df298be..57e9776 100644 --- a/tests/gptscript.test.ts +++ b/tests/gptscript.test.ts @@ -63,6 +63,26 @@ describe("gptscript module", () => { expect(err).toEqual("") }) + test("evaluate executes a prompt correctly with context", async () => { + let out = "" + let err = undefined + const t = { + instructions: "who was the president of the united states in 1928?", + context: [path.join(__dirname, "fixtures", "acorn-labs-context.gpt")] + } + + try { + const run = client.evaluate(t as any, {disableCache: true}) + out = await run.text() + err = run.err + } catch (e) { + console.error(e) + } + + expect(out).toContain("Acorn Labs") + expect(err).toEqual("") + }) + describe("run with test.gpt fixture", () => { test("should execute test.gpt correctly", async () => { const testGptPath = path.join(__dirname, "fixtures", "test.gpt")