Skip to content

Commit

Permalink
Merge pull request #437 from quarkiverse/#436
Browse files Browse the repository at this point in the history
Ensure that templates don't cause CL issues during reload
  • Loading branch information
geoand authored Apr 4, 2024
2 parents 1d583b5 + c1f5da7 commit a3a9906
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkiverse.langchain4j;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -14,21 +15,28 @@

public class QuarkusPromptTemplateFactory implements PromptTemplateFactory {

private final LazyValue<Engine> engineLazyValue;
private static final AtomicReference<LazyValue<Engine>> engineLazyValue = new AtomicReference<>();

public QuarkusPromptTemplateFactory() {
engineLazyValue = new LazyValue<>(new Supplier<Engine>() {
engineLazyValue.set(new LazyValue<>(new Supplier<Engine>() {
@Override
public Engine get() {
return Arc.container().instance(Engine.class).get().newBuilder()
.addParserHook(new MustacheTemplateVariableStyleParserHook()).build();
}
});
}));
}

public static void clear() {
LazyValue<Engine> lazyValue = engineLazyValue.get();
if (lazyValue != null) {
lazyValue.clear();
}
}

@Override
public Template create(Input input) {
return new QuteTemplate(engineLazyValue.get().parse(input.getTemplate()));
return new QuteTemplate(engineLazyValue.get().get().parse(input.getTemplate()));
}

public static class MustacheTemplateVariableStyleParserHook implements ParserHook {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkiverse.langchain4j.runtime;

import io.quarkiverse.langchain4j.QuarkusPromptTemplateFactory;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;

Expand All @@ -11,6 +12,7 @@ public void cleanUp(ShutdownContext shutdown) {
@Override
public void run() {
StructuredPromptsRecorder.clearTemplates();
QuarkusPromptTemplateFactory.clear();
AiServicesRecorder.clearMetadata();
ToolsRecorder.clearMetadata();
}
Expand Down
63 changes: 63 additions & 0 deletions openai/openai-vanilla/devmode-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkiverse.langchain4j</groupId>
<artifactId>quarkus-langchain4j-openai-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>quarkus-langchain4j-openai-devmode-tests</artifactId>
<name>Quarkus LangChain4j - OpenAI - DevMode tests</name>

<dependencies>
<dependency>
<groupId>io.quarkiverse.langchain4j</groupId>
<artifactId>quarkus-langchain4j-openai-deployment</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkiverse.langchain4j.openai.test;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
public class Configuration extends PanacheEntity {

@Enumerated(EnumType.STRING)
public ConfigurationKey key;

public String value;

public static boolean displayNewSpeakers() {
Configuration config = Configuration.find("key", ConfigurationKey.DISPLAY_NEW_SPEAKERS).firstResult();
return config != null && config.value.equals("true");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkiverse.langchain4j.openai.test;

public enum ConfigurationKey {
DISPLAY_NEW_SPEAKERS,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.quarkiverse.langchain4j.openai.test;

import static io.restassured.RestAssured.get;

import java.util.function.Function;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

public class PromptDevModeTest {

@RegisterExtension
static final QuarkusDevModeTest devModeTest = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(Service.class, Resource.class, Configuration.class, ConfigurationKey.class,
Resource.ApplicationGlobals.class)
.addAsResource(
new StringAsset(
"quarkus.langchain4j.openai.api-key=whatever\nquarkus.langchain4j.openai.base-url= https://mockgpt.wiremockapi.cloud/v1"),
"application.properties"));

@Test
public void test() {
get("test")
.then()
.statusCode(200);

devModeTest.modifySourceFile(Resource.class, new Function<String, String>() {
@Override
public String apply(String s) {
return s.replace("java", "kotlin");
}
});

get("test")
.then()
.statusCode(200);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkiverse.langchain4j.openai.test;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import io.quarkus.qute.TemplateGlobal;

@Path("/test")
public class Resource {

private final Service service;

public Resource(Service service) {
this.service = service;
}

@GET
public String hello() {
return service.findTalks("java");
}

@TemplateGlobal
public static class ApplicationGlobals {

public static boolean displayNewSpeakers() {
return Configuration.displayNewSpeakers();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkiverse.langchain4j.openai.test;

import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;

@RegisterAiService
public interface Service {

@SystemMessage("You are a computer science conference organiser")
@UserMessage("""
Help me select talks that match my favorite topics: {topics}. Give me the list of talks.
""")
String findTalks(String topics);
}
1 change: 1 addition & 0 deletions openai/openai-vanilla/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<modules>
<module>deployment</module>
<module>devmode-tests</module>
<module>runtime</module>
</modules>

Expand Down

0 comments on commit a3a9906

Please sign in to comment.