-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introducing chatGPT support #67
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow @haesleinhuepf, this is very cool. Thank you for working on it!
I would love to get this integrated into Fiji soon. However, I'm insanely busy, even more so than usual unfortunately, so super slow on my response times. 😢 I am also very tempted to factor out this work into its own small plugin, since it adds new dependencies. I am adding this PR to my shortlist ("short" in the sense of 79 items, rather than 3336), and will get to it as soon as I can.
<dependency> | ||
<groupId>com.theokanning.openai-gpt3-java</groupId> | ||
<artifactId>service</artifactId> | ||
<version>0.14.0</version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make a property ${com.theokanning.openai-gpt3-java.service.version}
set to 0.14.0, and same for client
, both extending a common property, so that these dependency versions can be easily overridden in various ways e.g. from the command line.
pom.xml
Outdated
@@ -144,6 +144,7 @@ | |||
<package-name>org.scijava.ui.swing.script</package-name> | |||
<license.licenseName>bsd_2</license.licenseName> | |||
<license.copyrightOwners>SciJava developers.</license.copyrightOwners> | |||
<scijava.app.directory>C:/programs/fiji-win64/Fiji.app/</scijava.app.directory> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be removed.
import javax.swing.KeyStroke; | ||
import javax.swing.SwingUtilities; | ||
import javax.swing.UIManager; | ||
import javax.swing.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SciJava style is to eschew wildcard imports. Whether that's a good policy is arguable, but it's the current style convention...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I think IntelliJ did this automatically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the relevant setting(s) to change in this screenshot are "[Class/Names] count to use [static] import with '*'", or at least, you should also change those as well as the highlighted one.
|
||
private String askChatGPT(String text) { | ||
// Modified from: https://github.com/TheoKanning/openai-java/blob/main/example/src/main/java/example/OpenAiApiFunctionsExample.java | ||
String token = System.getenv("OPENAI_API_KEY"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can introduce a new OpenAIOptions
class or some such, where the user can paste this key. Edit → Options → Open AI.... Then the user can change it in the GUI without needing to set a system property.
messages.add(responseMessage); | ||
|
||
String result = responseMessage.getContent(); | ||
System.out.println(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should remove the print statement.
String answer = askChatGPT(prompt); | ||
|
||
if (answer.contains("```")) { | ||
// clean answer by removing blabla outside the code block |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it makes sense to make any blabla into a block comment before the code block?
"Write concise and high quality code for ImageJ/Fiji.\n" + | ||
"Put minimal comments explaining what the code does.\n" + | ||
"The code should do the following:\n" + | ||
getTextArea().getSelectedText(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice prompt engineering! 💯 😎
|
||
//getTextArea().insert(answer, getTextArea().getCaretPosition()); | ||
getTextArea().replaceSelection(answer + "\n"); | ||
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the askChatGPT
routine throw any unchecked exceptions? If so, we should try/finally
this part, so that the cursor doesn't get stuck in hourglass mode.
I added the Edit>Options>OpenAI... settings dialog. Gonna merge this now, so that we can get it into the next Fiji release. Better exception handling can be added later if/when users have issues with flaky responses from ChatGPT. |
Thanks again, @haesleinhuepf 😸 |
Hi @ctrueden and, @acardona ,
this PR adds OpenAI's chatGPT integration into the Script Editor. Just write a prompt, select it and click on this menu:
E.g. producing ImageJ Macro:
It also works with Groovy if this language is selected:
If this gets merged, we need to:
script-editor/pom.xml
Lines 212 to 221 in 51b7d2a
Let me know what you think!
Best,
Robert