Created
November 16, 2023 15:57
-
-
Save maxandersen/f34160cecd5ed88a8d8d13bbd2562c33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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.File; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
import org.eclipse.microprofile.rest.client.RestClientBuilder; | |
import dev.langchain4j.service.SystemMessage; | |
import dev.langchain4j.service.UserMessage; | |
import picocli.CommandLine.*; | |
import io.quarkiverse.langchain4j.RegisterAiService; | |
import jakarta.inject.Inject; | |
@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