From 7b5fb3ab15cd8e8d13b756d25eb0b3ca1fbfd098 Mon Sep 17 00:00:00 2001 From: Bill Maxwell Date: Mon, 25 Mar 2024 17:00:09 -0700 Subject: [PATCH] refactor: update node example to use new module The new module had breaking changes to accept Tool types. These updates modify the codebase to make use of the updates. This now makes use of the packaging in gptscript. Signed-off-by: Bill Maxwell --- examples/nodejs-imagegen/README.md | 10 ---- examples/nodejs-imagegen/cli.py | 67 ---------------------- examples/nodejs-imagegen/package-lock.json | 8 +-- examples/nodejs-imagegen/package.json | 2 +- examples/nodejs-imagegen/requirements.txt | 3 - examples/nodejs-imagegen/server.js | 23 +++++--- examples/nodejs-imagegen/tool.gpt | 8 --- 7 files changed, 21 insertions(+), 100 deletions(-) delete mode 100644 examples/nodejs-imagegen/cli.py delete mode 100644 examples/nodejs-imagegen/requirements.txt delete mode 100644 examples/nodejs-imagegen/tool.gpt diff --git a/examples/nodejs-imagegen/README.md b/examples/nodejs-imagegen/README.md index 3f2084a5..f3b01680 100644 --- a/examples/nodejs-imagegen/README.md +++ b/examples/nodejs-imagegen/README.md @@ -21,16 +21,6 @@ git clone https://github.com/gptscript-ai/gptscript.git 1. Navigate to the 'examples/nodejs-imagegen' directory and install dependencies. -Python: - -*note: You can use a virtual environment to install the python dependencies, just make sure it is activated before running the server.* - -```bash -pip install -r requirements.txt -``` - -this will install the python dependencies for the image generation tool. - Node: ```bash diff --git a/examples/nodejs-imagegen/cli.py b/examples/nodejs-imagegen/cli.py deleted file mode 100644 index 9f0fd7b3..00000000 --- a/examples/nodejs-imagegen/cli.py +++ /dev/null @@ -1,67 +0,0 @@ -import os -import sys -import argparse -import openai - -# Set up defaults and get API key from environment variable -defaults = { - "api_key": os.getenv('OPENAI_API_KEY'), - "model": "dall-e-3", - "size": "1024x1024", - "quality": "standard", - "number": "1", -} - -# Function to validate and parse arguments -def validate_and_parse_args(parser): - args = parser.parse_args() - - for key, value in vars(args).items(): - if not value: - args.__dict__[key] = parser.get_default(key) - - if not args.api_key: - parser.error('The --api-key argument is required if OPENAI_API_KEY environment variable is not set.') - if not args.prompt: - parser.error('The --prompt argument is required.') - if not args.number.isdigit(): - parser.error('The --number argument must be a number.') - args.number = int(args.number) - - return args - -def main(): - # Parse the command line arguments - parser = argparse.ArgumentParser(description="CLI for image generation prompt using OpenAI's DALL-E model.") - parser.add_argument('-k', '--api-key', type=str, default=defaults["api_key"], - help='OpenAI API key. Can also be set with OPENAI_API_KEY environment variable.') - parser.add_argument('-p', '--prompt', type=str, required=True, help='Prompt for image generation.') - parser.add_argument('-m', '--model', type=str, default=defaults["model"], - help=f'Model to use for image generation. Default is "{defaults["model"]}".') - parser.add_argument('-s', '--size', type=str, default=defaults["size"], - help=f'Size of the image to generate, format WxH (e.g. {defaults["size"]}). Default is {defaults["size"]}.') - parser.add_argument('-q', '--quality', type=str, default=defaults["quality"], - help=f'Quality of the generated image. Allowed values are "standard" or "hd". Default is "{defaults["quality"]}"') - parser.add_argument('-n', '--number', type=str, default=defaults["number"], - help='Number of images to generate. Default is 1.') - args = validate_and_parse_args(parser) - - # Initialize OpenAI client - client = openai.OpenAI(api_key=args.api_key) - - # Make request to the OpenAI API - try: - response = client.images.generate( - model=args.model, - prompt=args.prompt, - size=args.size, - quality=args.quality, - n=args.number - ) - print([image.url for image in response.data]) - except openai.OpenAIError as e: - print(f"Received an error code while generating images: {e}", file=sys.stderr) - sys.exit(1) - -if __name__ == "__main__": - main() diff --git a/examples/nodejs-imagegen/package-lock.json b/examples/nodejs-imagegen/package-lock.json index 11e91975..1d913c25 100644 --- a/examples/nodejs-imagegen/package-lock.json +++ b/examples/nodejs-imagegen/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@gptscript-ai/gptscript": "^0.1.2", + "@gptscript-ai/gptscript": "^0.2.0", "express": "^4.18.3", "express-ws": "^5.0.2", "nodemon": "^3.1.0", @@ -17,9 +17,9 @@ } }, "node_modules/@gptscript-ai/gptscript": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/@gptscript-ai/gptscript/-/gptscript-0.1.2.tgz", - "integrity": "sha512-SI1iifrcy+pl9vjMtq0Ez9sA639ftbmz+ehRjLS9tDTFlm/uhCXlDP1mzcq15O6Kwm9knHeJd9skcz7dxdNw+Q==", + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@gptscript-ai/gptscript/-/gptscript-0.2.0.tgz", + "integrity": "sha512-6SojHRS7PhFSnNpgNvHJA7uq8u1d5A26VuuAw3mrkZ/GssL/6yF/HGaCnkWNs/Z2NmQGu0JmWdzAGisFcy/eLA==", "hasInstallScript": true, "dependencies": { "adm-zip": "^0.5.10", diff --git a/examples/nodejs-imagegen/package.json b/examples/nodejs-imagegen/package.json index 9479a557..f635f36d 100644 --- a/examples/nodejs-imagegen/package.json +++ b/examples/nodejs-imagegen/package.json @@ -11,7 +11,7 @@ "author": "", "license": "ISC", "dependencies": { - "@gptscript-ai/gptscript": "^0.1.2", + "@gptscript-ai/gptscript": "^0.2.0", "express": "^4.18.3", "express-ws": "^5.0.2", "nodemon": "^3.1.0", diff --git a/examples/nodejs-imagegen/requirements.txt b/examples/nodejs-imagegen/requirements.txt deleted file mode 100644 index 15738306..00000000 --- a/examples/nodejs-imagegen/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -# requirements.txt -openai -argparse diff --git a/examples/nodejs-imagegen/server.js b/examples/nodejs-imagegen/server.js index 19c1e157..e9ea0e33 100644 --- a/examples/nodejs-imagegen/server.js +++ b/examples/nodejs-imagegen/server.js @@ -32,8 +32,7 @@ app.get('/', (req, res) => { app.post('/generate-logo', async (req, res) => { const description = req.body.description; const artists = getArtistsData(); - const prompt = ` - tools: ./tool.gpt + const instructions = ` For each of the three amazing and capable artists described here ${JSON.stringify(artists)} make sure to include the artistName field in the response. @@ -51,8 +50,14 @@ app.post('/generate-logo', async (req, res) => { }] } `; + const tool = new gptscript.Tool({ + tools: ['github.com/gptscript-ai/image-generation'], + jsonResponse: true, + instructions: instructions, + }) + try { - const output = await gptscript.exec(prompt); + const output = await gptscript.exec(tool); const cleanedResp = output.trim().replace(/^```json.*/, '').replace(/```/g, ''); res.json(JSON.parse(cleanedResp)); } catch (error) { @@ -63,9 +68,7 @@ app.post('/generate-logo', async (req, res) => { // Route to request new artists app.post('/new-artists', async (req, res) => { - const prompt = ` - tools: sys.write - + const instructions = ` Create three short graphic artist descriptions and their muses. These should be descriptive and explain their point of view. Also come up with a made up name, they each should be from different @@ -80,8 +83,14 @@ the response format should be json and MUST NOT have other content or formatting }] } `; + const tool = new gptscript.Tool({ + tools: ['sys.write'], + jsonResponse: true, + instructions: instructions, + }); + try { - const output = await gptscript.exec(prompt); + const output = await gptscript.exec(tool); const cleanedResp = output.trim().replace(/^```json.*/, '').replace(/```/g, ''); newArtistsData = JSON.parse(cleanedResp); res.json(newArtistsData); diff --git a/examples/nodejs-imagegen/tool.gpt b/examples/nodejs-imagegen/tool.gpt deleted file mode 100644 index fd5bddc8..00000000 --- a/examples/nodejs-imagegen/tool.gpt +++ /dev/null @@ -1,8 +0,0 @@ -description: I am a tool that can generate images based on arguments that are sent to me. I return a list of URLs to the generated images. -args: prompt: (required) The text prompt based on which the GPT model will generate a response -args: model: (optional) The model to use for image generation. Defaults to "dall-e-3". -args: size: (optional) The size of the image to generate, format WxH (e.g. 1024x1024). Defaults to 1024x1024. -args: quality: (optional) The quality of the generated image. Allowed values are "standard" or "hd". Default is "standard". -args: number: (optional) The number of images to generate. Defaults to 1. - -#!/usr/bin/env python3 ./cli.py --prompt="${prompt}" --model="${model}" --size="${size}" --quality="${quality}" --number="${number}" \ No newline at end of file