# HG changeset patch # User bgruening # Date 1726072581 0 # Node ID 7770a4bd42e2ea1ac142c28e058a3c018f665943 # Parent dab494dce30390af2a1985adb832229c7a482816 planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/chatgpt commit c21d9a2cb410ee30dc47f4a13247862481816266 diff -r dab494dce303 -r 7770a4bd42e2 README.md --- a/README.md Fri Aug 23 10:21:21 2024 +0000 +++ b/README.md Wed Sep 11 16:36:21 2024 +0000 @@ -2,14 +2,16 @@ ## What it does -This tool leverages OpenAI's ChatGPT API to generate responses based on user-provided context and questions. -Users can upload context data in various formats and ask questions related to that data. +This tool leverages OpenAI's ChatGPT API to generate responses based on user-provided context and prompt. +Users can upload context data in various formats and ask questions or execute prompts related to that data. The tool then uploads the data to a OpenAI server and processes them using the selected ChatGPT model, returning an AI-generated response tailored to the context provided. To utilize this tool, users need to input their OpenAI API key in the user preferences. To obtain an API key, visit https://platform.openai.com/account/api-keys. +Make sure to setup the payment method in your OpenAI account to use the API key in here: https://platform.openai.com/settings/organization/billing/ + When you run this tool, your input data is sent to OpenAI's servers using your API-key. -OpenAI's models process the data and generate a response based on the context and question provided. +OpenAI's models process the data and generate a response based on the context and prompt provided. After receiving the response from the OpenAI server, the tool returns it to Galaxy and puts it in your history. The files that have been uploaded are then deleted from the OpenAI's server, so they are not stored beyond their necessary use. If the tool fails to delete your uploaded files automatically, you can manually delete them by visiting https://platform.openai.com/storage/. You might want to check your OpenAI storage from time to time as they also have a quota. @@ -19,10 +21,14 @@ **Input** 1. **Upload Context Data**: Users can upload up to 500 files in formats such as DOC, DOCX, HTML, JSON, PDF, TXT, JPG, JPEG, PNG, WEBP, or GIF. -This context data serves as the background information for the question you wish to ask. +This context data serves as the input for the prompt you wish to execute. -2. **Ask a Question**: Once the context data is added, users can pose a question related to the content. -The more specific the question, the more tailored the response will be. +2. **Provide a Prompt**: Once the context data is added, users can provide a prompt for a task ChatGPT should execute. +The more specific the prompt, the more tailored the response will be. + + [General thoughts on prompting with GPT-4](https://help.openai.com/en/articles/4936848-how-do-i-create-a-good-prompt-for-an-ai-model-like-gpt-4) + + [Open Ai's prompt example page for more information](https://platform.openai.com/docs/examples) 3. **Select a Model**: Choose the ChatGPT model that best fits your needs. Information about different models and their pricing can be found at https://platform.openai.com/docs/models and https://openai.com/api/pricing. @@ -30,6 +36,6 @@ **Output** -The output is a response generated by ChatGPT, crafted based on the provided context data and the question posed. +The output is a response generated by ChatGPT, crafted based on the provided context data and the prompt posed. This response is saved in the `output.txt` file. diff -r dab494dce303 -r 7770a4bd42e2 chatgpt.py --- a/chatgpt.py Fri Aug 23 10:21:21 2024 +0000 +++ b/chatgpt.py Wed Sep 11 16:36:21 2024 +0000 @@ -1,9 +1,10 @@ +import json import os import sys -from openai import OpenAI +from openai import AuthenticationError, OpenAI -context_files = sys.argv[1].split(",") +context_files = json.loads(sys.argv[1]) question = sys.argv[2] model = sys.argv[3] with open(sys.argv[4], "r") as f: @@ -14,51 +15,39 @@ client = OpenAI(api_key=openai_api_key) -file_search_sup_ext = [ - "c", - "cs", - "cpp", - "doc", - "docx", - "html", - "java", - "json", - "md", - "pdf", - "php", - "pptx", - "py", - "rb", - "tex", - "txt", - "css", - "js", - "sh", - "ts", -] - -vision_sup_ext = ["jpg", "jpeg", "png", "webp", "gif"] - file_search_file_streams = [] image_files = [] -for path in context_files: - ext = path.split(".")[-1].lower() - if ext in vision_sup_ext: +for path, type in context_files: + if type == "image": if os.path.getsize(path) > 20 * 1024 * 1024: print(f"File {path} exceeds the 20MB limit and will not be processed.") sys.exit(1) file = client.files.create(file=open(path, "rb"), purpose="vision") promt = {"type": "image_file", "image_file": {"file_id": file.id}} image_files.append(promt) - elif ext in file_search_sup_ext: + else: file_search_file_streams.append(open(path, "rb")) -assistant = client.beta.assistants.create( - instructions="You will receive questions about files from file searches and image files. For file search queries, identify and retrieve the relevant files based on the question. For image file queries, analyze the image content and provide relevant information or insights based on the image data.", - model=model, - tools=[{"type": "file_search"}] if file_search_file_streams else [], -) +try: + assistant = client.beta.assistants.create( + instructions=( + "You will receive questions about files from file searches " + "and image files. For file search queries, identify and " + "retrieve the relevant files based on the question. " + "For image file queries, analyze the image content and " + "provide relevant information or insights based on the image data." + ), + model=model, + tools=[{"type": "file_search"}] if file_search_file_streams else [], + ) +except AuthenticationError as e: + print(f"Authentication error: {e.message}") + sys.exit(1) +except Exception as e: + print(f"An error occurred: {str(e)}") + sys.exit(1) + if file_search_file_streams: vector_store = client.beta.vector_stores.create() file_batch = client.beta.vector_stores.file_batches.upload_and_poll( @@ -88,7 +77,12 @@ assistant_messages = list( client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id) ) - +if not assistant_messages: + print( + "No output was generated!\nPlease ensure that your OpenAI account has sufficient credits.\n" + "You can check your balance here: https://platform.openai.com/settings/organization/billing" + ) + sys.exit(1) message_content = assistant_messages[0].content[0].text.value print("Output has been saved!") with open("output.txt", "w") as f: diff -r dab494dce303 -r 7770a4bd42e2 chatgpt.xml --- a/chatgpt.xml Fri Aug 23 10:21:21 2024 +0000 +++ b/chatgpt.xml Wed Sep 11 16:36:21 2024 +0000 @@ -2,29 +2,32 @@ Integrating OpenAI's ChatGPT into Galaxy 2024 - 1 + 2 python openai @@ -40,7 +43,7 @@ - + @@ -48,7 +51,7 @@ - + @@ -58,7 +61,7 @@ - + @@ -68,7 +71,7 @@ - + @@ -81,14 +84,15 @@ **What it does** -This tool leverages OpenAI's ChatGPT API to generate responses based on user-provided context and questions. -Users can upload context data in various formats and ask questions related to that data. +This tool leverages OpenAI's ChatGPT API to generate responses based on user-provided context and prompt. +Users can upload context data in various formats and ask questions or execute prompts related to that data. The tool then uploads the data to a OpenAI server and processes them using the selected ChatGPT model, returning an AI-generated response tailored to the context provided. To utilize this tool, users need to input their OpenAI API key in the user preferences. To obtain an API key, visit API keys page in your OpenAI Dashboard. +Make sure to setup the payment method in your OpenAI account to use the API key. When you run this tool, your input data is sent to OpenAI's servers using your API-key. -OpenAI's models process the data and generate a response based on the context and question provided. +OpenAI's models process the data and generate a response based on the context and prompt provided. After receiving the response from the OpenAI server, the tool returns it to Galaxy and puts it in your history. The files that have been uploaded are then deleted from the OpenAI's server, so they are not stored beyond their necessary use. If the tool fails to delete your uploaded files automatically, you can manually delete them in your openai account page. You might want to check your OpenAI storage from time to time as they also have a quota. @@ -103,10 +107,10 @@ **Input** 1. **Upload Context Data**: Users can upload up to 500 files in formats such as DOC, DOCX, HTML, JSON, PDF, TXT, JPG, JPEG, PNG, WEBP, or GIF. -This context data serves as the background information for the question you wish to ask. +This context data serves as the input for the prompt you wish to execute. -2. **Ask a Question**: Once the context data is added, users can pose a question related to the content. -The more specific the question, the more tailored the response will be. +2. **Provide a Prompt**: Once the context data is added, users can provide a prompt for a task ChatGPT should execute. +The more specific the prompt, the more tailored the response will be. 3. **Select a Model**: Choose the ChatGPT model that best fits your needs. Information about different models and their pricing can be found on the OpenAI website. @@ -114,7 +118,7 @@ **Output** -The output is a response generated by ChatGPT, crafted based on the provided context data and the question posed. +The output is a response generated by ChatGPT, crafted based on the provided context data and the prompt posed. This response is saved in the `output.txt` file.