gptscript-ai / gptscript

Build AI assistants that interact with your systems
https://gptscript.ai
Apache License 2.0
2.95k stars 261 forks source link

UI - Not able to run python inline tools with production builds. #741

Closed sangee2004 closed 1 month ago

sangee2004 commented 1 month ago

Production electron build (npm run build:electron) with commit id 6e2f8ed99637f Steps to reproduce the problem:

  1. Create an assistant with python inline tool.
  2. Try to run this assiatnt.

Following error is seen:

"failed to load program: line https://gateway-api.gptscript.ai/sangeetha@acorn.io/sangeetestdep.gpt:83: only the first tool in a file can have no name"

Assistant that was used :

Name: Sangeetestdep
Type: Tool
Tools: Mytestpython
Chat: true

You are a good assistant

---
Name: Mytestpython
Description: Generates images based on the specified parameters and returns a list of URLs to the generated images.
Type: Tool

#!python
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()

---
!metadata:Mytestpython:requirements.txt
openai
argparse

Note - I am able to run the same assistant using dev builds - npm run dev:electron

tylerslaton commented 1 month ago

This should be fixed now. Can you give it another try?

sangee2004 commented 1 month ago

Tested with commit id - c5e7e2937

Able to run python inline tools successfully using production builds.