From 9302eca2950c2c04cf65f2dfd310b70c7325cc82 Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Thu, 14 Nov 2024 14:49:55 +1100 Subject: [PATCH] Replace json-rpc with REST Signed-off-by: Phillip Kruger --- pom.xml | 16 +++++------ publish.sh | 2 -- .../java/org/chappiebot/ChappieService.java | 8 ------ src/main/java/org/chappiebot/CommonInput.java | 8 ++++++ .../java/org/chappiebot/doc/DocAssistant.java | 7 +++-- .../java/org/chappiebot/doc/DocEndpoint.java | 27 ++++++++++-------- .../java/org/chappiebot/doc/DocInput.java | 10 +++++++ .../java/org/chappiebot/doc/DocOutput.java | 4 +++ .../exception/ExceptionAssistant.java | 21 ++++++++++---- .../exception/ExceptionEndpoint.java | 28 ++++++++++++------- .../chappiebot/exception/ExceptionInput.java | 10 +++++++ ...SuggestedFix.java => ExceptionOutput.java} | 2 +- .../chappiebot/explain/ExplainAssistant.java | 8 +++--- .../chappiebot/explain/ExplainEndpoint.java | 27 +++++++++++------- .../org/chappiebot/explain/ExplainInput.java | 9 ++++++ .../org/chappiebot/explain/ExplainOutput.java | 4 +++ .../org/chappiebot/test/TestAssistant.java | 7 +++-- .../org/chappiebot/test/TestEndpoint.java | 26 +++++++++++------ .../java/org/chappiebot/test/TestInput.java | 9 ++++++ .../{SuggestedTest.java => TestOutput.java} | 2 +- .../resources/application-chappie.properties | 2 ++ 21 files changed, 161 insertions(+), 76 deletions(-) delete mode 100755 publish.sh create mode 100644 src/main/java/org/chappiebot/CommonInput.java create mode 100644 src/main/java/org/chappiebot/doc/DocInput.java create mode 100644 src/main/java/org/chappiebot/doc/DocOutput.java create mode 100644 src/main/java/org/chappiebot/exception/ExceptionInput.java rename src/main/java/org/chappiebot/exception/{SuggestedFix.java => ExceptionOutput.java} (56%) create mode 100644 src/main/java/org/chappiebot/explain/ExplainInput.java create mode 100644 src/main/java/org/chappiebot/explain/ExplainOutput.java create mode 100644 src/main/java/org/chappiebot/test/TestInput.java rename src/main/java/org/chappiebot/test/{SuggestedTest.java => TestOutput.java} (63%) diff --git a/pom.xml b/pom.xml index 903c9b2..2407fdd 100644 --- a/pom.xml +++ b/pom.xml @@ -5,6 +5,7 @@ chappie-server 999-SNAPSHOT + Chappie Server The Chappie Server. AI for your project @@ -45,17 +46,16 @@ 3.13.0 - 17 + 21 UTF-8 UTF-8 quarkus-bom io.quarkus.platform - 3.14.3 + 3.16.2 true - 3.2.5 + 3.3.1 - 0.34.0 - 0.0.5 + 0.35.0 @@ -76,9 +76,8 @@ quarkus-arc - io.quarkiverse.json-rpc - quarkus-json-rpc - ${json-rpc.version} + io.quarkus + quarkus-rest-jackson dev.langchain4j @@ -96,7 +95,6 @@ ${langchain4j.version} - io.quarkus quarkus-junit5 diff --git a/publish.sh b/publish.sh deleted file mode 100755 index 94c34a8..0000000 --- a/publish.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -mvn clean deploy -Dquarkus.profile=chappie -Prelease diff --git a/src/main/java/org/chappiebot/ChappieService.java b/src/main/java/org/chappiebot/ChappieService.java index 857e8a6..621fe60 100644 --- a/src/main/java/org/chappiebot/ChappieService.java +++ b/src/main/java/org/chappiebot/ChappieService.java @@ -1,9 +1,6 @@ package org.chappiebot; import dev.langchain4j.model.chat.ChatLanguageModel; -import dev.langchain4j.model.chat.request.ResponseFormat; -import dev.langchain4j.model.chat.request.json.JsonObjectSchema; -import dev.langchain4j.model.chat.request.json.JsonSchema; import dev.langchain4j.model.ollama.OllamaChatModel; import dev.langchain4j.model.openai.OpenAiChatModel; import dev.langchain4j.service.AiServices; @@ -19,12 +16,8 @@ import org.eclipse.microprofile.config.inject.ConfigProperty; import java.time.Duration; -import java.util.Map; import java.util.Optional; -import static dev.langchain4j.model.chat.request.ResponseFormatType.JSON; -import static dev.langchain4j.model.chat.request.json.JsonStringSchema.JSON_STRING_SCHEMA; - /** * The Chappie Server * @@ -46,7 +39,6 @@ public class ChappieService { Optional timeout; // OpenAI - @ConfigProperty(name = "chappie.openai.api-key") Optional openaiKey; diff --git a/src/main/java/org/chappiebot/CommonInput.java b/src/main/java/org/chappiebot/CommonInput.java new file mode 100644 index 0000000..f9f64a2 --- /dev/null +++ b/src/main/java/org/chappiebot/CommonInput.java @@ -0,0 +1,8 @@ +package org.chappiebot; + +public record CommonInput(String programmingLanguage, + String programmingLanguageVersion, + String product, + String productVersion) { + +} diff --git a/src/main/java/org/chappiebot/doc/DocAssistant.java b/src/main/java/org/chappiebot/doc/DocAssistant.java index 5ce5883..3d6fb42 100644 --- a/src/main/java/org/chappiebot/doc/DocAssistant.java +++ b/src/main/java/org/chappiebot/doc/DocAssistant.java @@ -7,7 +7,7 @@ public interface DocAssistant { static final String SYSTEM_MESSAGE = """ - You are an AI assistant helping to add or modify {{doc}} in {{programmingLanguage}} code from a {{product}} {{version}} application. + You are an AI assistant helping to add or modify {{doc}} in {{programmingLanguage}} {{programmingLanguageVersion}} code from a {{product}} {{productVersion}} application. You will receive the code that needs the {{doc}}. Please use that as input when considering the response. Approach this task step-by-step, take your time and do not skip steps. @@ -30,9 +30,10 @@ public interface DocAssistant { Please add or modify the {{doc}} to reflect the code. """) - public String addDoc(@V("programmingLanguage")String programmingLanguage, + public DocOutput addDoc(@V("programmingLanguage")String programmingLanguage, + @V("programmingLanguageVersion")String programmingLanguageVersion, @V("product")String product, - @V("version")String version, + @V("productVersion")String version, @V("extraContext")String extraContext, @V("doc")String doc, @V("source")String source); diff --git a/src/main/java/org/chappiebot/doc/DocEndpoint.java b/src/main/java/org/chappiebot/doc/DocEndpoint.java index 561b3b4..02dfb37 100644 --- a/src/main/java/org/chappiebot/doc/DocEndpoint.java +++ b/src/main/java/org/chappiebot/doc/DocEndpoint.java @@ -1,25 +1,30 @@ package org.chappiebot.doc; -import io.quarkiverse.jsonrpc.runtime.api.JsonRPCApi; +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.infrastructure.Infrastructure; import jakarta.inject.Inject; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; /** - * The JsonRPC Endpoint for doc creation + * The Endpoint for doc creation * @author Phillip Kruger (phillip.kruger@gmail.com) */ -@JsonRPCApi("doc") +@Path("/api/doc") public class DocEndpoint { @Inject DocAssistant docAssistant; - public String addDoc(String programmingLanguage, - String product, - String version, - String extraContext, - String doc, - String source) { - - return docAssistant.addDoc(programmingLanguage, product, version, extraContext, doc, source); + @POST + public Uni addDoc(DocInput docInput) { + return Uni.createFrom().item(() -> docAssistant.addDoc(docInput.commonInput().programmingLanguage(), + docInput.commonInput().programmingLanguageVersion(), + docInput.commonInput().product(), + docInput.commonInput().productVersion(), + docInput.extraContext().orElse(""), + docInput.doc(), docInput.source())) + .runSubscriptionOn(Infrastructure.getDefaultWorkerPool()); } + } diff --git a/src/main/java/org/chappiebot/doc/DocInput.java b/src/main/java/org/chappiebot/doc/DocInput.java new file mode 100644 index 0000000..b449aff --- /dev/null +++ b/src/main/java/org/chappiebot/doc/DocInput.java @@ -0,0 +1,10 @@ +package org.chappiebot.doc; + +import java.util.Optional; +import org.chappiebot.CommonInput; + +public record DocInput(CommonInput commonInput, + Optional extraContext, + String doc, + String source){ +} diff --git a/src/main/java/org/chappiebot/doc/DocOutput.java b/src/main/java/org/chappiebot/doc/DocOutput.java new file mode 100644 index 0000000..6c7d220 --- /dev/null +++ b/src/main/java/org/chappiebot/doc/DocOutput.java @@ -0,0 +1,4 @@ +package org.chappiebot.doc; + +public record DocOutput(String sourceWithDoc){ +} diff --git a/src/main/java/org/chappiebot/exception/ExceptionAssistant.java b/src/main/java/org/chappiebot/exception/ExceptionAssistant.java index 66d9320..ba0acc9 100644 --- a/src/main/java/org/chappiebot/exception/ExceptionAssistant.java +++ b/src/main/java/org/chappiebot/exception/ExceptionAssistant.java @@ -8,7 +8,7 @@ public interface ExceptionAssistant { static final String SYSTEM_MESSAGE = """ # IDENTITY - You are the worlds best AI coding assistant helping to debug {{programmingLanguage}} exceptions in a {{product}} {{version}} application. + You are the worlds best AI coding assistant helping to debug {{programmingLanguage}} {{programmingLanguageVersion}} exceptions in a {{product}} {{productVersion}} application. # STEPS Consume the exception stacktrace. @@ -73,9 +73,10 @@ public interface ExceptionAssistant { 'suggestedSource: 'String' } """) - public SuggestedFix suggestFix(@V("programmingLanguage")String programmingLanguage, + public ExceptionOutput suggestFix(@V("programmingLanguage")String programmingLanguage, + @V("programmingLanguageVersion")String programmingLanguageVersion, @V("product")String product, - @V("version")String version, + @V("productVersion")String productVersion, @V("extraContext")String extraContext, @V("stacktrace")String stacktrace, @V("source")String source); @@ -90,10 +91,20 @@ public SuggestedFix suggestFix(@V("programmingLanguage")String programmingLangua {{extraContext}} Please help me fix it. + + You must answer strictly in the following JSON format: + + { + 'response': 'String', + 'explanation': 'String', + 'diff': 'String', + 'suggestedSource: 'String' + } """) - public SuggestedFix suggestFix(@V("programmingLanguage")String programmingLanguage, + public ExceptionOutput suggestFix(@V("programmingLanguage")String programmingLanguage, + @V("programmingLanguageVersion")String programmingLanguageVersion, @V("product")String product, - @V("version")String version, + @V("productVersion")String productVersion, @V("extraContext")String extraContext, @V("stacktrace")String stacktrace); diff --git a/src/main/java/org/chappiebot/exception/ExceptionEndpoint.java b/src/main/java/org/chappiebot/exception/ExceptionEndpoint.java index c9585da..65a3a64 100644 --- a/src/main/java/org/chappiebot/exception/ExceptionEndpoint.java +++ b/src/main/java/org/chappiebot/exception/ExceptionEndpoint.java @@ -1,23 +1,31 @@ package org.chappiebot.exception; -import io.quarkiverse.jsonrpc.runtime.api.JsonRPCApi; +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.infrastructure.Infrastructure; import jakarta.inject.Inject; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + /** - * The JsonRPC Endpoint for exceptions + * The Endpoint for exceptions * @author Phillip Kruger (phillip.kruger@gmail.com) */ -@JsonRPCApi("exception") +@Path("/api/exception") public class ExceptionEndpoint { @Inject ExceptionAssistant exceptionAssistant; - public SuggestedFix suggestfix(String programmingLanguage, - String product, - String version, - String extraContext, - String stacktrace, - String source) { + @POST + public Uni suggestfix(ExceptionInput exceptionInput) { - return exceptionAssistant.suggestFix(programmingLanguage, product, version, extraContext, stacktrace, source); + return Uni.createFrom().item(() -> exceptionAssistant.suggestFix( + exceptionInput.commonInput().programmingLanguage(), + exceptionInput.commonInput().programmingLanguageVersion(), + exceptionInput.commonInput().product(), + exceptionInput.commonInput().productVersion(), + exceptionInput.extraContext().orElse(""), + exceptionInput.stacktrace(), + exceptionInput.source())) + .runSubscriptionOn(Infrastructure.getDefaultWorkerPool()); } } diff --git a/src/main/java/org/chappiebot/exception/ExceptionInput.java b/src/main/java/org/chappiebot/exception/ExceptionInput.java new file mode 100644 index 0000000..644fe8b --- /dev/null +++ b/src/main/java/org/chappiebot/exception/ExceptionInput.java @@ -0,0 +1,10 @@ +package org.chappiebot.exception; + +import org.chappiebot.CommonInput; +import java.util.Optional; + +public record ExceptionInput(CommonInput commonInput, + Optional extraContext, + String stacktrace, + String source){ +} diff --git a/src/main/java/org/chappiebot/exception/SuggestedFix.java b/src/main/java/org/chappiebot/exception/ExceptionOutput.java similarity index 56% rename from src/main/java/org/chappiebot/exception/SuggestedFix.java rename to src/main/java/org/chappiebot/exception/ExceptionOutput.java index d3c260a..7a775af 100644 --- a/src/main/java/org/chappiebot/exception/SuggestedFix.java +++ b/src/main/java/org/chappiebot/exception/ExceptionOutput.java @@ -5,5 +5,5 @@ * * @author Phillip Kruger (phillip.kruger@gmail.com) */ -public record SuggestedFix(String response, String explanation, String diff, String suggestedSource) { +public record ExceptionOutput(String response, String explanation, String diff, String suggestedSource) { } diff --git a/src/main/java/org/chappiebot/explain/ExplainAssistant.java b/src/main/java/org/chappiebot/explain/ExplainAssistant.java index ca08d67..73616b9 100644 --- a/src/main/java/org/chappiebot/explain/ExplainAssistant.java +++ b/src/main/java/org/chappiebot/explain/ExplainAssistant.java @@ -4,11 +4,10 @@ import dev.langchain4j.service.UserMessage; import dev.langchain4j.service.V; - public interface ExplainAssistant { static final String SYSTEM_MESSAGE = """ - You are an AI assistant helping to explain source code in {{programmingLanguage}} code from a {{product}} {{version}} application. + You are an AI assistant helping to explain source code in {{programmingLanguage}} {{programmingLanguageVersion}} code from a {{product}} {{productVersion}} application. You will receive the code that needs to be explained. Approach this task step-by-step, take your time and do not skip steps. @@ -28,9 +27,10 @@ public interface ExplainAssistant { Please explain it to me. """) - public String explain(@V("programmingLanguage")String programmingLanguage, + public ExplainOutput explain(@V("programmingLanguage")String programmingLanguage, + @V("programmingLanguageVersion")String programmingLanguageVersion, @V("product")String product, - @V("version")String version, + @V("productVersion")String productVersion, @V("extraContext")String extraContext, @V("source")String source); diff --git a/src/main/java/org/chappiebot/explain/ExplainEndpoint.java b/src/main/java/org/chappiebot/explain/ExplainEndpoint.java index 0e1d724..55a0fad 100644 --- a/src/main/java/org/chappiebot/explain/ExplainEndpoint.java +++ b/src/main/java/org/chappiebot/explain/ExplainEndpoint.java @@ -1,23 +1,30 @@ package org.chappiebot.explain; -import dev.langchain4j.service.V; -import io.quarkiverse.jsonrpc.runtime.api.JsonRPCApi; +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.infrastructure.Infrastructure; import jakarta.inject.Inject; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + /** - * The JsonRPC Endpoint for explanation + * The Endpoint for explanation * @author Phillip Kruger (phillip.kruger@gmail.com) */ -@JsonRPCApi("explanation") +@Path("/api/explanation") public class ExplainEndpoint { @Inject ExplainAssistant explainAssistant; - public String explain(@V("programmingLanguage")String programmingLanguage, - @V("product")String product, - @V("version")String version, - @V("extraContext")String extraContext, - @V("source")String source) { + @POST + public Uni explain(ExplainInput explainInput) { - return explainAssistant.explain(programmingLanguage, product, version, extraContext, source); + return Uni.createFrom().item(() -> explainAssistant.explain( + explainInput.commonInput().programmingLanguage(), + explainInput.commonInput().programmingLanguageVersion(), + explainInput.commonInput().product(), + explainInput.commonInput().productVersion(), + explainInput.extraContext().orElse(""), + explainInput.source())) + .runSubscriptionOn(Infrastructure.getDefaultWorkerPool()); } } diff --git a/src/main/java/org/chappiebot/explain/ExplainInput.java b/src/main/java/org/chappiebot/explain/ExplainInput.java new file mode 100644 index 0000000..ba89b40 --- /dev/null +++ b/src/main/java/org/chappiebot/explain/ExplainInput.java @@ -0,0 +1,9 @@ +package org.chappiebot.explain; + +import java.util.Optional; +import org.chappiebot.CommonInput; + +public record ExplainInput(CommonInput commonInput, + Optional extraContext, + String source){ +} diff --git a/src/main/java/org/chappiebot/explain/ExplainOutput.java b/src/main/java/org/chappiebot/explain/ExplainOutput.java new file mode 100644 index 0000000..2515bf4 --- /dev/null +++ b/src/main/java/org/chappiebot/explain/ExplainOutput.java @@ -0,0 +1,4 @@ +package org.chappiebot.explain; + +public record ExplainOutput(String explanation){ +} diff --git a/src/main/java/org/chappiebot/test/TestAssistant.java b/src/main/java/org/chappiebot/test/TestAssistant.java index 9447ee3..6757b36 100644 --- a/src/main/java/org/chappiebot/test/TestAssistant.java +++ b/src/main/java/org/chappiebot/test/TestAssistant.java @@ -7,7 +7,7 @@ public interface TestAssistant { static final String SYSTEM_MESSAGE = """ - You are an AI assistant helping to create Unit tests in {{programmingLanguage}} code from a {{product}} {{version}} application. + You are an AI assistant helping to create Unit tests in {{programmingLanguage}} {{programmingLanguageVersion}} code from a {{product}} {{productVersion}} application. You will receive the code that needs a test. Please use that as input when considering the response. Approach this task step-by-step, take your time and do not skip steps. @@ -39,9 +39,10 @@ public interface TestAssistant { Please provide a test. """) - public SuggestedTest suggestTest(@V("programmingLanguage")String programmingLanguage, + public TestOutput suggestTest(@V("programmingLanguage")String programmingLanguage, + @V("programmingLanguageVersion")String programmingLanguageVersion, @V("product")String product, - @V("version")String version, + @V("productVersion")String productVersion, @V("extraContext")String extraContext, @V("source")String source); diff --git a/src/main/java/org/chappiebot/test/TestEndpoint.java b/src/main/java/org/chappiebot/test/TestEndpoint.java index 5c65a4c..b3c3c68 100644 --- a/src/main/java/org/chappiebot/test/TestEndpoint.java +++ b/src/main/java/org/chappiebot/test/TestEndpoint.java @@ -1,23 +1,31 @@ package org.chappiebot.test; -import io.quarkiverse.jsonrpc.runtime.api.JsonRPCApi; +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.infrastructure.Infrastructure; import jakarta.inject.Inject; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; /** - * The JsonRPC Endpoint for test creation + * The Endpoint for test creation * @author Phillip Kruger (phillip.kruger@gmail.com) */ -@JsonRPCApi("testing") +@Path("/api/testing") public class TestEndpoint { @Inject TestAssistant testAssistant; - public SuggestedTest suggesttest(String programmingLanguage, - String product, - String version, - String extraContext, - String source) { + @POST + public Uni suggesttest(TestInput testInput) { - return testAssistant.suggestTest(programmingLanguage, product, version, extraContext, source); + return Uni.createFrom().item(() -> testAssistant.suggestTest( + testInput.commonInput().programmingLanguage(), + testInput.commonInput().programmingLanguageVersion(), + testInput.commonInput().product(), + testInput.commonInput().productVersion(), + testInput.extraContext().orElse(""), + testInput.source())) + .runSubscriptionOn(Infrastructure.getDefaultWorkerPool()); + } } diff --git a/src/main/java/org/chappiebot/test/TestInput.java b/src/main/java/org/chappiebot/test/TestInput.java new file mode 100644 index 0000000..ec03814 --- /dev/null +++ b/src/main/java/org/chappiebot/test/TestInput.java @@ -0,0 +1,9 @@ +package org.chappiebot.test; + +import java.util.Optional; +import org.chappiebot.CommonInput; + +public record TestInput(CommonInput commonInput, + Optional extraContext, + String source){ +} diff --git a/src/main/java/org/chappiebot/test/SuggestedTest.java b/src/main/java/org/chappiebot/test/TestOutput.java similarity index 63% rename from src/main/java/org/chappiebot/test/SuggestedTest.java rename to src/main/java/org/chappiebot/test/TestOutput.java index e68c2b6..946edb7 100644 --- a/src/main/java/org/chappiebot/test/SuggestedTest.java +++ b/src/main/java/org/chappiebot/test/TestOutput.java @@ -5,5 +5,5 @@ * * @author Phillip Kruger (phillip.kruger@gmail.com) */ -public record SuggestedTest(String explanation, String suggestedTestSource) { +public record TestOutput(String explanation, String suggestedTestSource) { } diff --git a/src/main/resources/application-chappie.properties b/src/main/resources/application-chappie.properties index 980ab7a..ec86b7f 100644 --- a/src/main/resources/application-chappie.properties +++ b/src/main/resources/application-chappie.properties @@ -4,3 +4,5 @@ quarkus.package.jar.add-runner-suffix=false quarkus.banner.path=asciiart.txt +quarkus.log.console.enable = true +quarkus.log.file.enable = false