Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created November 16, 2023 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxandersen/19454823a4a421d09c95dbaef5c076d1 to your computer and use it in GitHub Desktop.
Save maxandersen/19454823a4a421d09c95dbaef5c076d1 to your computer and use it in GitHub Desktop.
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
//JAVAC_OPTIONS -parameters
//DEPS io.quarkus.platform:quarkus-bom:$3.5.1@pom
//DEPS io.quarkiverse.langchain4j:quarkus-langchain4j-openai:0.1.0
//DEPS io.quarkus:quarkus-picocli
//Q:CONFIG quarkus.banner.enabled=false
//Q:CONFIG quarkus.log.level=WARN
//Q:CONFIG quarkus.langchain4-openai.timeout=60s
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import jakarta.inject.Inject;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
@Command(
mixinStandardHelpOptions = true,
version = "0.1",
header = "Explain usage of a source file using ChatGPT.",
description = """
Uses Quarkus LangChain4j and ChatGP to explain what
a source file does in a Quarkus project.
Note: Be aware the source code is sent to remote server.
"""
)
public class explain4j implements Runnable {
@Parameters(index = "0", arity="1", description = "The source file to explain")
Path sourceFile;
@RegisterAiService
public interface Explain {
@SystemMessage("""
You are to advise a software developer on what the
following code found in a file at {sourceFile} does.
""")
@UserMessage("{content}")
String explain(Path sourceFile, String content);
}
@Inject Explain gpt;
@Override
public void run() {
System.out.println("Requesting explanation of " + sourceFile + ". Have patience...");
try {
var result = gpt.explain(sourceFile,
Files.readString(sourceFile));
System.out.println(result);
} catch (IOException e) {
throw new IllegalStateException("Could not read " + sourceFile, e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment