Skip to content

Commit

Permalink
Improve instructions for better results and better code readability i…
Browse files Browse the repository at this point in the history
…n ChatGPT tool (#1486)

* Improve instructions for better results and better code readablity

* Less error raising on python side instead having conditional parameters

* use actual filename instead of input{i}.{ext}

* remove test codes

* Update the inputs with vision and all models
  • Loading branch information
arash77 authored Aug 23, 2024
1 parent e80ca9b commit d2d08c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
33 changes: 18 additions & 15 deletions tools/chatgpt/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,33 @@
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()
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]}}
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)

messages = [
Expand All @@ -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)
Expand Down
60 changes: 39 additions & 21 deletions tools/chatgpt/chatgpt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -32,17 +34,33 @@ $__user__.extra_preferences.get('chatgpt|api_key', "")
]]></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"/>
Expand Down

0 comments on commit d2d08c3

Please sign in to comment.