Skip to content

feat: add remote SDK environment variable #21

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
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ you will see "Hello, World!" in the output of the command.
These are optional options that can be passed to the various `exec` functions.
None of the options is required, and the defaults will reduce the number of calls made to the Model API.

- `cache`: Enable or disable caching. Default (true).
- `disableCache`: Enable or disable caching. Default (true).
- `cacheDir`: Specify the cache directory.
- `quiet`: No output logging
- `chdir`: Change current working directory
Expand Down Expand Up @@ -99,7 +99,7 @@ Executes a GPT script file with optional input and arguments. The script is rela
const gptscript = require('@gptscript-ai/gptscript');

const opts = {
cache: false,
disableCache: false,
};

async function execFile() {
Expand All @@ -120,7 +120,7 @@ Executes a gptscript with optional input and arguments, and returns the output s
const gptscript = require('@gptscript-ai/gptscript');

const opts = {
cache: false,
disableCache: false,
};

const t = new gptscript.Tool({
Expand Down Expand Up @@ -154,7 +154,7 @@ Executes a gptscript with optional input and arguments, and returns the output a
const gptscript = require('@gptscript-ai/gptscript');

const opts = {
cache: false,
disableCache: false,
};

const t = new gptscript.Tool({
Expand Down Expand Up @@ -192,7 +192,7 @@ The script is relative to the callers source directory.
const gptscript = require('@gptscript-ai/gptscript');

const opts = {
cache: false,
disableCache: false,
};

async function streamExecFile() {
Expand Down Expand Up @@ -222,7 +222,7 @@ The script is relative to the callers source directory.
const gptscript = require('@gptscript-ai/gptscript');

const opts = {
cache: false,
disableCache: false,
};

async function streamExecFileWithEvents() {
Expand Down Expand Up @@ -252,19 +252,19 @@ async function streamExecFileWithEvents() {

### Tool Parameters

| Argument | Type | Default | Description |
|-------------------|----------------|-------------|-----------------------------------------------------------------------------------------------|
| name | string | `""` | The name of the tool. Optional only on the first tool if there are multiple tools defined. |
| description | string | `""` | A brief description of what the tool does, this is important for explaining to the LLM when it should be used. |
| tools | array | `[]` | An array of tools that the current tool might depend on or use. |
| maxTokens | number/undefined | `undefined` | The maximum number of tokens to be used. Prefer `undefined` for uninitialized or optional values. |
| model | string | `""` | The model that the tool uses, if applicable. |
| cache | boolean | `true` | Whether caching is enabled for the tool. |
| temperature | number/undefined | `undefined` | The temperature setting for the model, affecting randomness. `undefined` for default behavior. |
| args | object | `{}` | Additional arguments specific to the tool, described by key-value pairs. |
| internalPrompt | boolean | `false` | An internal prompt used by the tool, if any. |
| instructions | string | `""` | Instructions on how to use the tool. |
| jsonResponse | boolean | `false` | Whether the tool returns a JSON response instead of plain text. You must include the word 'json' in the body of the prompt |
| Argument | Type | Default | Description |
|----------------|----------------|-------------|-----------------------------------------------------------------------------------------------|
| name | string | `""` | The name of the tool. Optional only on the first tool if there are multiple tools defined. |
| description | string | `""` | A brief description of what the tool does, this is important for explaining to the LLM when it should be used. |
| tools | array | `[]` | An array of tools that the current tool might depend on or use. |
| maxTokens | number/undefined | `undefined` | The maximum number of tokens to be used. Prefer `undefined` for uninitialized or optional values. |
| model | string | `""` | The model that the tool uses, if applicable. |
| disableCache | boolean | `true` | Whether caching is enabled for the tool. |
| temperature | number/undefined | `undefined` | The temperature setting for the model, affecting randomness. `undefined` for default behavior. |
| args | object | `{}` | Additional arguments specific to the tool, described by key-value pairs. |
| internalPrompt | boolean | `false` | An internal prompt used by the tool, if any. |
| instructions | string | `""` | Instructions on how to use the tool. |
| jsonResponse | boolean | `false` | Whether the tool returns a JSON response instead of plain text. You must include the word 'json' in the body of the prompt |

### FreeForm Parameters

Expand Down
3 changes: 1 addition & 2 deletions src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ async function streamExecWithEvents(command, args, stdin, cwd = './', env) {
const namedPipe = '\\\\.\\pipe\\gptscript-' + Math.floor(Math.random() * 1000000);
events = new stream.Readable({
encoding: 'utf-8',
read() {
}
read() {}
});

server = net.createServer((connection) => {
Expand Down
80 changes: 61 additions & 19 deletions src/gptscript.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const execlib = require('./exec');
const path = require('path');
const reqlib = require('./request');
const tools = require('./tool');
const path = require('path');

function getCmdPath() {
if (process.env.GPTSCRIPT_BIN) {
Expand All @@ -10,7 +11,7 @@ function getCmdPath() {
}

const optToArg = {
cache: "--disable-cache=",
disableCache: "--disable-cache=",
cacheDir: "--cache-dir=",
quiet: "--quiet=",
chdir: "--chdir=",
Expand All @@ -20,11 +21,7 @@ function toArgs(opts) {
let args = ["--quiet=false"];
for (const [key, value] of Object.entries(opts)) {
if (optToArg[key]) {
if (key === "cache") {
args.push(optToArg[key] + !value);
} else {
args.push(optToArg[key] + value);
}
args.push(optToArg[key] + value);
}
}
return args;
Expand All @@ -34,12 +31,7 @@ function getToolString(tool) {
let toolString;

if (Array.isArray(tool)) {
toolString = tool.map(singleTool => {
if (!(singleTool instanceof tools.Tool || singleTool instanceof tools.FreeForm)) {
throw new TypeError("Each tool must be an instance of Tool or FreeForm.");
}
return singleTool.toString();
}).join('\n---\n');
toolString = toolArrayToContents(tool);
} else {
if (!(tool instanceof tools.Tool || tool instanceof tools.FreeForm)) {
throw new TypeError("The tool must be an instance of Tool or FreeForm.");
Expand All @@ -49,6 +41,15 @@ function getToolString(tool) {
return toolString;
}

function toolArrayToContents(toolArray) {
return toolArray.map(singleTool => {
if (!(singleTool instanceof tools.Tool || singleTool instanceof tools.FreeForm)) {
throw new TypeError("Each tool must be an instance of Tool or FreeForm.");
}
return singleTool.toString();
}).join('\n---\n');
}

function cliArgBuilder(args, stdin, gptPath, input) {
let returnArgs = []
returnArgs.push(...args);
Expand Down Expand Up @@ -85,50 +86,91 @@ function streamRunWithEvents(args = [], stdin, gptPath = './', input = "", env =
return execlib.streamExecWithEvents(cmdPath, cmdArgs, stdin, './', env);
}

function listTools() {
return run(['--list-tools']);
async function listTools() {
if (process.env['GPTSCRIPT_URL']) {
return await reqlib.makeRequest('list-tools');
}
return await run(['--list-tools']);
}

function version() {
return run(['--version']);
async function version() {
if (process.env['GPTSCRIPT_URL']) {
return await reqlib.makeRequest('version');
}
return await run(['--version']);
}

async function listModels() {
if (process.env['GPTSCRIPT_URL']) {
return await reqlib.makeRequest('list-models');
}
const models = await run(['--list-models']);
return models.trim().split('\n');
}

async function exec(tool, opts = {}) {
if (process.env['GPTSCRIPT_URL']) {
if (Array.isArray(tool)) {
return await reqlib.makeRequest('run-tool', {content: toolArrayToContents(tool)}, opts);
}
return await reqlib.makeRequest('run-tool', tool, opts);
}

const args = toArgs(opts);
const toolString = getToolString(tool);
return await run(args, toolString);
}

function execFile(scriptPath, input = "", opts = {}) {
async function execFile(scriptPath, input = "", opts = {}) {
if (process.env['GPTSCRIPT_URL']) {
return await reqlib.makeRequest('run-file', {file: scriptPath, input: input}, opts);
}
const args = toArgs(opts);
return run(args, undefined, scriptPath, input);
return await run(args, undefined, scriptPath, input);
}

function streamExec(tool, opts = {}) {
if (process.env['GPTSCRIPT_URL']) {
if (Array.isArray(tool)) {
return reqlib.makeRequest('run-tool', {content: toolArrayToContents(tool)}, opts);
}
return reqlib.makeStreamRequest('run-tool-stream', tool, opts);
}

const args = toArgs(opts);
const toolString = getToolString(tool);

return streamRun(args, toolString);
}

function streamExecWithEvents(tool, opts = {}) {
if (process.env['GPTSCRIPT_URL']) {
if (Array.isArray(tool)) {
return reqlib.makeRequest('run-tool', {content: toolArrayToContents(tool)}, opts);
}
return reqlib.makeStreamRequestWithEvents('run-tool-stream-with-events', tool, opts);
}

const args = toArgs(opts);
const toolString = getToolString(tool);

return streamRunWithEvents(args, toolString);
}

function streamExecFile(scriptPath, input = "", opts = {}) {
if (process.env['GPTSCRIPT_URL']) {
return reqlib.makeStreamRequest('run-file-stream', {file: scriptPath, input: input}, opts);
}

const args = toArgs(opts);
return streamRun(args, undefined, scriptPath, input);
}

function streamExecFileWithEvents(scriptPath, input = "", opts = {}) {
if (process.env['GPTSCRIPT_URL']) {
return reqlib.makeStreamRequestWithEvents('run-file-stream-with-events', {file: scriptPath, input: input}, opts);
}

const args = toArgs(opts);

return streamRunWithEvents(args, undefined, scriptPath, input);
Expand Down
Loading