Skip to content

Commit

Permalink
Merge pull request #39 from phillip-kruger/local
Browse files Browse the repository at this point in the history
Local
  • Loading branch information
phillip-kruger authored Sep 6, 2024
2 parents 81fa51c + a9ce1db commit 56834f7
Show file tree
Hide file tree
Showing 32 changed files with 1,112 additions and 547 deletions.
3 changes: 1 addition & 2 deletions deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<name>Quarkus Chappie - Deployment</name>

<properties>
<langchain4j.version>0.33.0</langchain4j.version>
<chappie-server.version>0.0.11</chappie-server.version>
<chappie-server.version>0.0.12</chappie-server.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkiverse.chappie.deployment;

import io.quarkus.builder.item.SimpleBuildItem;

final public class ChappieAvailableBuildItem extends SimpleBuildItem {

public ChappieAvailableBuildItem() {
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package io.quarkiverse.chappie.deployment;

import io.quarkiverse.chappie.deployment.devservice.DevServicesConfig;
import io.quarkiverse.chappie.deployment.devservice.LLM;
import io.quarkiverse.chappie.deployment.devservice.OpenAIConfig;
import io.quarkiverse.chappie.deployment.devservice.ollama.OllamaConfig;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.assistant")
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
public interface ChappieConfig {

/**
* The LLM to use, for now we only support openai
* OpenAI config
*/
@WithDefault("openai")
LLM llm();
@ConfigDocSection
OpenAIConfig openai();

/**
* OpenAI config
* Ollama config
*/
@ConfigDocSection
OpenAIConfig openai();
OllamaConfig ollama();

/**
* Dev Services
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.quarkiverse.chappie.deployment;

import java.util.List;
import java.util.Optional;

import io.quarkiverse.chappie.deployment.devservice.ollama.OllamaBuildItem;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.devui.spi.page.CardPageBuildItem;
import io.quarkus.devui.spi.page.Page;

@BuildSteps(onlyIf = IsDevelopment.class)
class ChappieDevUIProcessor {

@BuildStep
void pages(List<ChappiePageBuildItem> chappiePageBuildItems,
Optional<OllamaBuildItem> ollamaBuildItem,
BuildProducer<CardPageBuildItem> cardPageProducer,
ChappieConfig config) {

if (config.openai().apiKey().isPresent()) {
configuredOpenAiPage(chappiePageBuildItems, cardPageProducer, config);
} else if (ollamaBuildItem.isPresent()) {
configuredOllamaPage(chappiePageBuildItems, cardPageProducer, config);
} else {
unconfiguredPage(cardPageProducer);
}
}

private void configuredOpenAiPage(List<ChappiePageBuildItem> chappiePageBuildItems,
BuildProducer<CardPageBuildItem> cardPageProducer,
ChappieConfig config) {
configuredPage(chappiePageBuildItems, cardPageProducer, "OpenAI", config.openai().modelName());
}

private void configuredOllamaPage(List<ChappiePageBuildItem> chappiePageBuildItems,
BuildProducer<CardPageBuildItem> cardPageProducer,
ChappieConfig config) {
configuredPage(chappiePageBuildItems, cardPageProducer, "Ollama", config.ollama().modelName());
}

private void configuredPage(List<ChappiePageBuildItem> chappiePageBuildItems,
BuildProducer<CardPageBuildItem> cardPageProducer,
String llm, String modelName) {
CardPageBuildItem chappieCard = new CardPageBuildItem();
chappieCard.setCustomCard("qwc-chappie-custom-card.js");

chappieCard.addBuildTimeData("llm", llm);
chappieCard.addBuildTimeData("modelName", modelName);

for (ChappiePageBuildItem cpbi : chappiePageBuildItems) {
chappieCard.addPage(cpbi.getPageBuilder());
}

cardPageProducer.produce(chappieCard);
}

private void unconfiguredPage(BuildProducer<CardPageBuildItem> cardPageProducer) {
CardPageBuildItem chappieCard = new CardPageBuildItem();

chappieCard.addPage(Page.webComponentPageBuilder()
.icon("font-awesome-solid:circle-question")
.title("Configure assistant")
.componentLink("qwc-chappie-unconfigured.js"));

cardPageProducer.produce(chappieCard);
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package io.quarkiverse.chappie.deployment;

import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

import io.quarkiverse.chappie.deployment.devservice.ChappieClient;
import io.quarkiverse.chappie.deployment.devservice.ChappieClientBuildItem;
import io.quarkiverse.chappie.deployment.devservice.ollama.OllamaBuildItem;
import io.quarkiverse.chappie.deployment.exception.LastException;
import io.quarkiverse.chappie.deployment.exception.LastExceptionBuildItem;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.console.ConsoleCommand;
import io.quarkus.deployment.console.ConsoleInstalledBuildItem;
import io.quarkus.deployment.console.ConsoleStateManager;
import io.quarkus.deployment.dev.testing.MessageFormat;
import io.quarkus.deployment.logging.LoggingDecorateBuildItem;
import io.vertx.core.Vertx;

/**
* Main console Chappie Processor. This create a few Build Items also used by the DevUI processor
*
* @author Phillip Kruger ([email protected])
*/
@BuildSteps(onlyIf = IsDevelopment.class)
class ConsoleProcessor {
static volatile ConsoleStateManager.ConsoleContext chappieConsoleContext;

@BuildStep
void chappieAvailable(BuildProducer<ChappieAvailableBuildItem> chappieAvailableProducer,
ChappieConfig config,
Optional<OllamaBuildItem> ollamaBuildItem) {

if (config.openai().apiKey().isPresent() || ollamaBuildItem.isPresent()) {
chappieAvailableProducer.produce(new ChappieAvailableBuildItem());
}
}

@Consume(ConsoleInstalledBuildItem.class)
@BuildStep
void setupConsole(BuildProducer<FeatureBuildItem> featureProducer,
LastExceptionBuildItem lastExceptionBuildItem,
ChappieClientBuildItem chappieClientBuildItem,
LoggingDecorateBuildItem loggingDecorateBuildItem,
Optional<ChappieAvailableBuildItem> chappieAvailable) {

if (chappieAvailable.isPresent()) {

Path srcMainJava = loggingDecorateBuildItem.getSrcMainJava();

if (chappieConsoleContext == null) {
chappieConsoleContext = ConsoleStateManager.INSTANCE.createContext("Assistant");
}

Vertx vertx = Vertx.vertx();
chappieConsoleContext.reset(
new ConsoleCommand('a', "Help with the latest exception",
new ConsoleCommand.HelpState(new Supplier<String>() {
@Override
public String get() {
return MessageFormat.RED;
}
}, new Supplier<String>() {
@Override
public String get() {
LastException lastException = lastExceptionBuildItem.getLastException().get();
if (lastException == null) {
return "none";
}
return lastException.throwable().getMessage();
}
}), new Runnable() {
@Override
public void run() {
LastException lastException = lastExceptionBuildItem.getLastException().get();
if (lastException == null) {
return;
}

String sourceString = SourceCodeFinder.getSourceCode(srcMainJava,
lastException.stackTraceElement());

String stacktraceString = lastException.getStackTraceString();

System.out.println(
"===============\nAssisting with the exception, please wait");

long timer = vertx.setPeriodic(800, id -> System.out.print("."));

ChappieClient chappieClient = chappieClientBuildItem.getChappieClient();
Object[] params = ParameterCreator.getParameters("", stacktraceString, sourceString);

CompletableFuture<Object> futureResult = chappieClient.executeRPC("exception#suggestfix",
params);

futureResult.thenAccept(r -> {
vertx.cancelTimer(timer);
Map result = (Map) r;

System.out.println("\n\n" + result.get("response"));
System.out.println("\n\n" + result.get("explanation"));
System.out.println("\n------ Diff ------ ");
System.out.println("\n\n" + result.get("diff"));
System.out.println("\n------ Suggested source ------ ");
System.out.println("\n\n" + result.get("suggestedSource"));
}).exceptionally(throwable -> {
// Handle any errors
System.out.println("\n\nCould not get a response from ai due the this exception:");
throwable.printStackTrace();
return null;
});
}
}));
}

featureProducer.produce(new FeatureBuildItem(Feature.FEATURE));
}
}

This file was deleted.

Loading

0 comments on commit 56834f7

Please sign in to comment.