-
Notifications
You must be signed in to change notification settings - Fork 300
add example nodejs and image generation example #108
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
Conversation
Signed-off-by: Bill Maxwell <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cloudnautique Tested this out and it works great! Awesome example!
I left some suggestions for you to consider, but I don't think any of them are blockers.
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should this be Apache-2.0
to match gptscript
's licence, or maybe just dropped entirely?
@@ -0,0 +1,1224 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
The .gitignore
in the root directory should exclude this, and looks correct to me, but it doesn't seem to be working (could be in conflict with some global git config on your system @cloudnautique, but idk for sure).
Regardless, from what I can tell, committing package-lock.json
is best-practice for nodejs projects, so I think we should remove the entry from the root .gitignore
to save us future headache.
(I'll open a separate PR for that, doesn't need to hold this PR up)
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might be able use git subtrees to make maintaining these copies of our external tools a bit easier.
IIUC, pulling and updating a tool subtree for an example would look something like this:
> # Initialize the subtree, pulling main into a single commit
> git subtree add --prefix=examples/nodejs-imagegen image-generator [email protected]:gptscript-ai/image-generation.git main --squash
> tree -L 2 examples/nodejs-imagegen
examples/nodejs-imagegen
|- tool.gpt
|- server.js
|- ...
|_ image-generation
|- cli.py
|- ...
> # Pull any updates from the image-generation repo since the last commit for the subtree
> git subtree pull --prefix=examples/nodejs-imagegen image-generator [email protected]:gptscript-ai/image-generation.git main --squash
@cloudnautique wdyt? too complex for a short-term quality-of-life hack?
|
||
Python: | ||
|
||
*note: You can use a virtual environment to install the python dependencies, just make sure it is activated before running the server.* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
# (Optional) Create and activate a virtual environment before installing dependencies
python -m venv .venv
source .venv/bin/activate
No description provided.