# HG changeset patch # User bgruening # Date 1723559119 0 # Node ID f256dc85e1a03f75ad08feaa43876a63bc3a253f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/chatgpt commit 840e66e7f23cf775ebd3083c6e07d1819b17f74c diff -r 000000000000 -r f256dc85e1a0 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Tue Aug 13 14:25:19 2024 +0000 @@ -0,0 +1,35 @@ +# ChatGPT Galaxy tool + +## 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. +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. + +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. +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. + +## Usage + +**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. + +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. + +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. + + +**Output** + +The output is a response generated by ChatGPT, crafted based on the provided context data and the question posed. +This response is saved in the `output.txt` file. + diff -r 000000000000 -r f256dc85e1a0 README.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.rst Tue Aug 13 14:25:19 2024 +0000 @@ -0,0 +1,19 @@ +ChatGPT +======= + +Set up user credentials on Galaxy +--------------------------------- + +To enable users to set their credentials for this tool, make sure the +file ``config/user_preferences_extra.yml`` has the following section: + +:: + + preferences: + chatgpt: + description: Your ChatGPT API settings + inputs: + - name: api_key + label: API Key + type: password + required: False diff -r 000000000000 -r f256dc85e1a0 chatgpt.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chatgpt.py Tue Aug 13 14:25:19 2024 +0000 @@ -0,0 +1,98 @@ +import os +import sys + +from openai import OpenAI + +context_files = sys.argv[1].split(",") +question = sys.argv[2] +model = sys.argv[3] +with open(sys.argv[4], "r") as f: + openai_api_key = f.read().strip() +if not openai_api_key: + raise Exception("OpenAI API key is not provided in user preferences!") + +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_urls = [] + +for path in context_files: + ext = path.split(".")[-1].lower() + if ext in vision_sup_ext and model in ["gpt-4o", "gpt-4o-mini", "gpt-4-turbo"]: + if os.path.getsize(path) > 20 * 1024 * 1024: + raise Exception(f"File {path} exceeds the 20MB limit and will not be processed.") + file = client.files.create(file=open(path, "rb"), purpose="vision") + promt = {"type": "image_file", "image_file": {"file_id": file.id}} + image_urls.append(promt) + + elif ext in file_search_sup_ext: + file_search_file_streams.append(open(path, "rb")) + else: + raise Exception("Not supported file!") + +assistant = client.beta.assistants.create( + instructions="You are going to get question about the file(s).", + model=model, + tools=[{"type": "file_search"}] if file_search_file_streams else None, +) +if file_search_file_streams: + vector_store = client.beta.vector_stores.create() + file_batch = client.beta.vector_stores.file_batches.upload_and_poll( + vector_store_id=vector_store.id, files=file_search_file_streams + ) + assistant = client.beta.assistants.update( + assistant_id=assistant.id, tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}} + ) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": question, + }, + *image_urls, + ], + } +] +thread = client.beta.threads.create(messages=messages) +run = client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=assistant.id) +messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id)) + +message_content = messages[0].content[0].text.value +print("Output has been saved!") +with open("output.txt", "w") as f: + f.write(message_content) + +for image in image_urls: + client.files.delete(image["image_file"]["file_id"]) +if file_search_file_streams: + client.beta.vector_stores.delete(vector_store.id) +client.beta.threads.delete(thread.id) +client.beta.assistants.delete(assistant.id) diff -r 000000000000 -r f256dc85e1a0 chatgpt.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chatgpt.xml Tue Aug 13 14:25:19 2024 +0000 @@ -0,0 +1,113 @@ + + Integrating OpenAI's ChatGPT into Galaxy + + 2024 + 0 + + + python + openai + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @misc{openai, + author = {OpenAI}, + title = {OpenAI's ChatGPT}, + howpublished = {\url{https://openai.com/chatgpt}}, + year = {2024}, + note = {Accessed: 2024-07-26} + } + + + diff -r 000000000000 -r f256dc85e1a0 test-data/chatgpt_test.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/chatgpt_test.txt Tue Aug 13 14:25:19 2024 +0000 @@ -0,0 +1,1 @@ +This is just a test file! \ No newline at end of file