Skip to content
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

Merged
merged 4 commits into from
Nov 7, 2023
Merged

introducing chatGPT support #67

merged 4 commits into from
Nov 7, 2023

Conversation

haesleinhuepf
Copy link
Member

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:

image

E.g. producing ImageJ Macro:

image

It also works with Groovy if this language is selected:

image

If this gets merged, we need to:

  • Update documentation to tell people how to use it and how to configure an OpenAI API Key in the system environment
  • Include these libraries and potential dependencies to upload to the ImageJ update site:

    script-editor/pom.xml

    Lines 212 to 221 in 51b7d2a

    <dependency>
    <groupId>com.theokanning.openai-gpt3-java</groupId>
    <artifactId>client</artifactId>
    <version>0.14.0</version>
    </dependency>
    <dependency>
    <groupId>com.theokanning.openai-gpt3-java</groupId>
    <artifactId>service</artifactId>
    <version>0.14.0</version>
    </dependency>

Let me know what you think!

Best,
Robert

Copy link
Member

@ctrueden ctrueden left a 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>
Copy link
Member

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>
Copy link
Member

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.*;
Copy link
Member

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...

Copy link
Member Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a setting in IntelliJ to change this behaviour:
grafik

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");
Copy link
Member

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);
Copy link
Member

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
Copy link
Member

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();
Copy link
Member

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));
Copy link
Member

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.

pom.xml Outdated Show resolved Hide resolved
@ctrueden
Copy link
Member

ctrueden commented Nov 7, 2023

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.

@ctrueden ctrueden merged commit 02cb540 into master Nov 7, 2023
1 check passed
@ctrueden ctrueden deleted the chatGPT branch November 7, 2023 17:56
@ctrueden
Copy link
Member

ctrueden commented Nov 7, 2023

Thanks again, @haesleinhuepf 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants