changeset 2:dab494dce303 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/chatgpt commit d2d08c3866c0f4a2f10372ae15c5dac5ea2d0bf0
author bgruening
date Fri, 23 Aug 2024 10:21:21 +0000
parents 08c658e9aa9e
children 7770a4bd42e2
files chatgpt.py chatgpt.xml
diffstat 2 files changed, 57 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/chatgpt.py	Wed Aug 14 11:33:44 2024 +0000
+++ b/chatgpt.py	Fri Aug 23 10:21:21 2024 +0000
@@ -40,26 +40,24 @@
 vision_sup_ext = ["jpg", "jpeg", "png", "webp", "gif"]
 
 file_search_file_streams = []
-image_urls = []
+image_files = []
 
 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 ext in vision_sup_ext:
         if os.path.getsize(path) > 20 * 1024 * 1024:
-            raise Exception(f"File {path} exceeds the 20MB limit and will not be processed.")
+            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_urls.append(promt)
-
+        image_files.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).",
+    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 None,
+    tools=[{"type": "file_search"}] if file_search_file_streams else [],
 )
 if file_search_file_streams:
     vector_store = client.beta.vector_stores.create()
@@ -67,7 +65,8 @@
         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]}}
+        assistant_id=assistant.id,
+        tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
     )
 
 messages = [
@@ -78,20 +77,24 @@
                 "type": "text",
                 "text": question,
             },
-            *image_urls,
+            *image_files,
         ],
     }
 ]
 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))
+run = client.beta.threads.runs.create_and_poll(
+    thread_id=thread.id, assistant_id=assistant.id
+)
+assistant_messages = list(
+    client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id)
+)
 
-message_content = messages[0].content[0].text.value
+message_content = assistant_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:
+for image in image_files:
     client.files.delete(image["image_file"]["file_id"])
 if file_search_file_streams:
     client.beta.vector_stores.delete(vector_store.id)
--- a/chatgpt.xml	Wed Aug 14 11:33:44 2024 +0000
+++ b/chatgpt.xml	Fri Aug 23 10:21:21 2024 +0000
@@ -2,26 +2,28 @@
     <description>Integrating OpenAI's ChatGPT into Galaxy</description>
     <macros>
         <token name="@TOOL_VERSION@">2024</token>
-        <token name="@VERSION_SUFFIX@">0</token>
+        <token name="@VERSION_SUFFIX@">1</token>
     </macros>
     <requirements>
         <requirement type="package" version="3.12">python</requirement>
         <requirement type="package" version="1.35.13">openai</requirement>
     </requirements>
     <command detect_errors="exit_code"><![CDATA[
-#set LINK_LIST = ''
-#for $count, $input in enumerate($context):
-    #set LINK = 'input' + str($count) + '.' + $input.ext
-    ln -s '$input' '$LINK' &&
-    #if count == 0
-        #set LINK_LIST = $LINK
-    #else
-        #set LINK_LIST = $LINK_LIST + ',' + $LINK
-    #end if
-#end for
+    #import os
+    #import re
+    #set $LINK_LIST = []
+    #for $input in $context:
+        #set file_name, ext = os.path.splitext($input.element_identifier)
+        #if ext == ''
+            #set ext = '.'+$input.ext
+        #end if
+        #set LINK = re.sub('[^\w\-]', '_', str($file_name))+$ext
+        ln -s '$input' '$LINK' &&
+        $LINK_LIST.append($LINK)
+    #end for
 
 python '$__tool_directory__/chatgpt.py'
-'$LINK_LIST'
+'$str(",".join($LINK_LIST))'
 '$question'
 '$model'
 '$openai_api_key_file'
@@ -32,17 +34,33 @@
         ]]></configfile>
     </configfiles>
     <inputs>
-        <param name="context" type="data" multiple="true" optional="false" format="doc,docx,html,json,pdf,txt,jpg,jpeg,png,webp,gif" label="Context" max="500" help="This data will be uploaded to OpenAI's servers for processing."/>
-        <param name="question" type="text" optional="false" label="Question" help="Question about the text provided" area="true">
+        <conditional name="input_type">
+            <param name="input_type_selector" type="select" label="Choose the model" help="Vision models are capable to have image as input.">
+                <option value="vision" selected="true">Vision models</option>
+                <option value="all">All models</option>
+            </param>
+            <when value="vision">
+                <param name="model" type="select" optional="false" label="Model" help="Select the model you want to use">
+                    <option value="gpt-4o-mini" selected="true">Affordable and intelligent small model for fast, lightweight tasks (gpt-4o-mini)</option>
+                    <option value="gpt-4o">High-intelligence flagship model for complex, multi-step tasks (gpt-4o)</option>
+                    <option value="gpt-4-turbo">The previous set of high-intelligence model with vision capabilities (gpt-4-turbo)</option>
+                </param>
+                <param name="context" type="data" multiple="true" optional="false" format="doc,docx,html,json,pdf,txt,jpg,jpeg,png,webp,gif" label="Context" max="500" help="This data will be uploaded to OpenAI's servers for processing."/>
+            </when>
+            <when value="all">
+                <param name="model" type="select" optional="false" label="Model" help="Select the model you want to use">
+                    <option value="gpt-4o-mini" selected="true">Affordable and intelligent small model for fast, lightweight tasks (gpt-4o-mini)</option>
+                    <option value="gpt-4o">High-intelligence flagship model for complex, multi-step tasks (gpt-4o)</option>
+                    <option value="gpt-4-turbo">The previous set of high-intelligence model with vision capabilities (gpt-4-turbo)</option>
+                    <option value="gpt-4" selected="true">The previous set of high-intelligence model (gpt-4)</option>
+                    <option value="gpt-3.5-turbo">A fast, inexpensive model for simple tasks (GPT-3.5-turbo)</option>
+                </param>
+                <param name="context" type="data" multiple="true" optional="false" format="doc,docx,html,json,pdf,txt" label="Context" max="500" help="This data will be uploaded to OpenAI's servers for processing."/>
+            </when>
+        </conditional>
+        <param name="question" type="text" optional="false" label="Question" help="Question about the text provided." area="true">
             <validator type="empty_field"/>
         </param>
-        <param name="model" type="select" optional="false" label="Model" help="Select the model you want to use">
-            <option value="gpt-4o-mini" selected="true">Affordable and intelligent small model for fast, lightweight tasks (gpt-4o-mini)</option>
-            <option value="gpt-4o">High-intelligence flagship model for complex, multi-step tasks (gpt-4o)</option>
-            <option value="gpt-4-turbo">The previous set of high-intelligence model with vision capabilities (gpt-4-turbo)</option>
-            <option value="gpt-4">The previous set of high-intelligence model (gpt-4) (not supporting images)</option>
-            <option value="gpt-3.5-turbo">A fast, inexpensive model for simple tasks (GPT-3.5-turbo) (not supporting images)</option>
-        </param>
     </inputs>
     <outputs>
         <data name="output" format="txt" label="${tool.name} on ${on_string}" from_work_dir="./output.txt"/>