diff --git a/README.md b/README.md index 565bf86..74b8227 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,8 @@ option. Any `env` provided in the run options are appended. - `APIKey`: Specify an OpenAI API key for authenticating requests - `BaseURL`: A base URL for an OpenAI compatible API (the default is `https://api.openai.com/v1`) -- `DefaultModel`: The default model to use for OpenAI requests +- `DefaultModel`: The default model to use for chat completion requests +- `DefaultModelProvider`: The default model provider to use for chat completion requests - `Env`: Replace the system's environment variables with these in the for `KEY=VAL` ## Run Options diff --git a/package-lock.json b/package-lock.json index e1c33d4..5347066 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gptscript-ai/gptscript", - "version": "v0.9.3", + "version": "v0.9.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@gptscript-ai/gptscript", - "version": "v0.9.3", + "version": "v0.9.4", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index c19a6e3..a9280c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gptscript-ai/gptscript", - "version": "v0.9.3", + "version": "v0.9.4", "description": "Run gptscript in node.js", "source": "src/gptscript.ts", "main": "dist/gptscript.js", diff --git a/scripts/install-binary.js b/scripts/install-binary.js index 28705ef..622796b 100644 --- a/scripts/install-binary.js +++ b/scripts/install-binary.js @@ -72,7 +72,7 @@ if (process.platform === 'win32') { const gptscript_info = { name: "gptscript", url: "https://github.com/gptscript-ai/gptscript/releases/download/", - version: "v0.9.3" + version: "v0.9.4" } const pltfm = { diff --git a/src/gptscript.ts b/src/gptscript.ts index 3143350..71e45f6 100644 --- a/src/gptscript.ts +++ b/src/gptscript.ts @@ -2,11 +2,13 @@ import http from "http" import path from "path" import child_process from "child_process" import {fileURLToPath} from "url" +import {gunzipSync} from "zlib"; export interface GlobalOpts { APIKey?: string BaseURL?: string DefaultModel?: string + DefaultModelProvider?: string Env?: string[] } @@ -24,6 +26,9 @@ function globalOptsToEnv(env: NodeJS.ProcessEnv, opts?: GlobalOpts) { if (opts.DefaultModel) { env["GPTSCRIPT_SDKSERVER_DEFAULT_MODEL"] = opts.DefaultModel } + if (opts.DefaultModelProvider) { + env["GPTSCRIPT_SDKSERVER_DEFAULT_MODEL_PROVIDER"] = opts.DefaultModelProvider + } } export interface RunOpts { @@ -805,6 +810,22 @@ export interface PromptResponse { responses: Record } +export function getEnv(key: string, def: string = ''): string { + let v = process.env[key] || '' + if (v == '') { + return def + } + + if (v.startsWith('{"_gz":"') && v.endsWith('"}')) { + try { + return gunzipSync(Buffer.from(v.slice(8, -2), 'base64')).toString('utf8') + } catch (e) { + } + } + + return v +} + function getCmdPath(): string { if (process.env.GPTSCRIPT_BIN) { return process.env.GPTSCRIPT_BIN diff --git a/tests/gptscript.test.ts b/tests/gptscript.test.ts index 2920c6c..0389408 100644 --- a/tests/gptscript.test.ts +++ b/tests/gptscript.test.ts @@ -1,5 +1,5 @@ import * as gptscript from "../src/gptscript" -import {ArgumentSchemaType, PropertyType, RunEventType, ToolType} from "../src/gptscript" +import {ArgumentSchemaType, getEnv, PropertyType, RunEventType, ToolType} from "../src/gptscript" import path from "path" import {fileURLToPath} from "url" @@ -535,4 +535,15 @@ describe("gptscript module", () => { expect(run.err).toEqual("") }) + + test("test get_env default", async () => { + const env = getEnv('TEST_ENV_MISSING', 'foo') + expect(env).toEqual('foo') + }) + + test("test get_env", async () => { + process.env.TEST_ENV = '{"_gz":"H4sIAEosrGYC/ytJLS5RKEvMKU0FACtB3ewKAAAA"}' + const env = getEnv('TEST_ENV', 'missing') + expect(env).toEqual('test value') + }) })