Created
December 11, 2023 07:10
-
-
Save maxandersen/1e06afbaf295fa910e159d607746780c 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 21+ | |
//PREVIEW | |
//JAVAC_OPTIONS -parameters | |
//DEPS io.quarkus.platform:quarkus-bom:3.5.1@pom | |
//DEPS io.quarkiverse.langchain4j:quarkus-langchain4j-openai:999-SNAPSHOT | |
//DEPS io.quarkus:quarkus-picocli | |
//Q:CONFIG quarkus.banner.enabled=false | |
//Q:CONFIG quarkus.log.level=DEBUG | |
//Q:CONFIG quarkus.log.category."io.quarkiverse.langchain4j".level=DEBUG | |
//Q:CONFIG quarkus.log.category."dev.langchain4j".level=DEBUG | |
//Q:CONFIG quarkus.langchain4j-openai.timeout=60s | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import dev.langchain4j.agent.tool.P; | |
import dev.langchain4j.agent.tool.Tool; | |
import dev.langchain4j.service.SystemMessage; | |
import dev.langchain4j.service.UserMessage; | |
import io.quarkiverse.langchain4j.RegisterAiService; | |
import io.quarkus.logging.Log; | |
import jakarta.enterprise.context.ApplicationScoped; | |
import jakarta.enterprise.context.control.ActivateRequestContext; | |
import jakarta.inject.Inject; | |
import picocli.CommandLine.Command; | |
import picocli.CommandLine.Parameters; | |
@Command(mixinStandardHelpOptions = true, version = "0.1", header = "Have fun with Quarkus and LangChain4j", | |
description = "Uses Quarkus LangChain4j and ChatGP to generate and test some Java code.") | |
public class magic implements Runnable { | |
@Parameters(description = "The question to answer") | |
String question; | |
@ApplicationScoped | |
static class java { | |
@Tool(""" | |
You are an agent designed to write and execute java code to answer questions. | |
You have access to a java REPL, which you can use to execute java code. | |
If you get an error, debug your code and try again. | |
Only use the output of your code to answer the question. | |
You might know the answer without running any code, but you should still run the code to get the answer. | |
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer. | |
You can add dependencies to your code by adding a comment with the format `//DEPS groupId:artifactId:version`. | |
""") | |
public String run(@P("Java code to execute, result MUST be returned by the code. The source MUST have newlines escaped twice.") String source, | |
@P("Path to name the java file to execute") | |
String path) { | |
Log.info("Running JBang on " + path); | |
try { | |
Files.writeString(Paths.get(path), source); | |
ProcessBuilder pb = new ProcessBuilder(System.getProperty("user.home") + "/.jbang/bin/jbang", path); | |
pb.redirectErrorStream(true); | |
Process p = pb.start(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); | |
StringBuilder builder = new StringBuilder(); | |
String line = null; | |
while ((line = reader.readLine()) != null) { | |
builder.append(line); | |
builder.append(System.getProperty("line.separator")); | |
} | |
return builder.toString(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} | |
@RegisterAiService(tools = java.class) | |
public interface MetaGen { | |
@SystemMessage(""" | |
You are to try and make a Java program that does what calculation or operation the | |
user wants to do, execute the program and validate the result. | |
Only using Java. No other languages are allowed. | |
""") | |
@UserMessage("{question}") | |
String generate(String question); | |
} | |
@Inject | |
MetaGen gpt; | |
@Override | |
@ActivateRequestContext | |
public void run() { | |
var result = gpt.generate(question); | |
System.out.println(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment