Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created November 16, 2023 15:58
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/e56cd8e3d64fe0d47f7c988f18659e31 to your computer and use it in GitHub Desktop.
Save maxandersen/e56cd8e3d64fe0d47f7c988f18659e31 to your computer and use it in GitHub Desktop.
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11+
// Update the Quarkus version to what you want here or run jbang with
// `-Dquarkus.version=<version>` to override it.
//DEPS io.quarkus.platform:quarkus-bom:${quarkus.version: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
//FILES application.properties
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
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, headerHeading = "@|bold,underline Usage|@:%n%n",
version = "0.1",
synopsisHeading = "%n",
descriptionHeading = "%n@|bold,underline Description|@:%n%n",
parameterListHeading = "%n@|bold,underline Parameters|@:%n",
optionListHeading = "%n@|bold,underline Options|@:%n",
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.\n\n"+
"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.readAllLines(sourceFile).stream().collect(Collectors.joining("\n")));
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