Add quarkus-vs-spring-ai module: the same support assistant (system prompt, tool, RAG) built with Quarkus LangChain4j and Spring AI against a stub OpenAI-compatible server, with wire-format capture, startup/memory benchmarks and a Quarkus native image
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4XrpfdpsacrvqnrBM
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
f4f90b3b30
commit
5e5326c810
@@ -0,0 +1,81 @@
|
||||
<?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>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>4.2.0</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>com.ankurm.qvs</groupId>
|
||||
<artifactId>spring-ai-app</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<java.version>25</java.version>
|
||||
<spring-ai.version>2.1.0</spring-ai.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-bom</artifactId>
|
||||
<version>${spring-ai.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-starter-model-openai</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-vector-store-advisor</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ankurm.qvs</groupId>
|
||||
<artifactId>stub-llm</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource><directory>src/main/resources</directory></resource>
|
||||
<resource><directory>../knowledge</directory><targetPath>knowledge</targetPath></resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration><parameters>true</parameters></configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.ankurm.qvs;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
|
||||
import org.springframework.ai.document.Document;
|
||||
import org.springframework.ai.embedding.EmbeddingModel;
|
||||
import org.springframework.ai.vectorstore.SearchRequest;
|
||||
import org.springframework.ai.vectorstore.SimpleVectorStore;
|
||||
import org.springframework.ai.vectorstore.VectorStore;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Spring AI has no "declare an interface" step. The equivalent of the Quarkus AI service is a
|
||||
* ChatClient built from a builder: system prompt, tools and the RAG advisor are wired in code.
|
||||
*/
|
||||
@Configuration
|
||||
public class AiConfig {
|
||||
|
||||
static final List<String> FILES = List.of("returns.txt", "shipping.txt", "warranty.txt");
|
||||
|
||||
@Bean
|
||||
VectorStore policyStore(EmbeddingModel embeddingModel) throws IOException {
|
||||
SimpleVectorStore store = SimpleVectorStore.builder(embeddingModel).build();
|
||||
List<Document> docs = new ArrayList<>();
|
||||
for (String f : FILES) {
|
||||
try (InputStream in = getClass().getResourceAsStream("/knowledge/" + f)) {
|
||||
docs.add(new Document(new String(in.readAllBytes(), StandardCharsets.UTF_8).trim()));
|
||||
}
|
||||
}
|
||||
store.add(docs);
|
||||
return store;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ChatClient supportAssistant(ChatClient.Builder builder, VectorStore policyStore, OrderTools tools) {
|
||||
return builder
|
||||
.defaultSystem("You are the support assistant of an online electronics shop. "
|
||||
+ "Answer in one sentence. Use the order tool for order questions "
|
||||
+ "and the supplied policy text for policy questions.")
|
||||
.defaultTools(tools)
|
||||
.defaultAdvisors(QuestionAnswerAdvisor.builder(policyStore)
|
||||
.searchRequest(SearchRequest.builder().topK(1).similarityThreshold(0.3).build())
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ankurm.qvs;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class AskController {
|
||||
|
||||
private final ChatClient assistant;
|
||||
|
||||
AskController(ChatClient supportAssistant) {
|
||||
this.assistant = supportAssistant;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/ask", produces = "text/plain")
|
||||
String ask(@RequestParam("q") String q) {
|
||||
return assistant.prompt().user(q).call().content();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ankurm.qvs;
|
||||
|
||||
import org.springframework.ai.tool.annotation.Tool;
|
||||
import org.springframework.ai.tool.annotation.ToolParam;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/** The one tool both apps expose. Quarkus twin: quarkus-app/.../OrderTools.java. */
|
||||
@Component
|
||||
public class OrderTools {
|
||||
|
||||
private static final Map<String, String> ORDERS = Map.of(
|
||||
"1001", "shipped on 2026-11-20, arriving in 2 days",
|
||||
"1002", "being prepared in the warehouse");
|
||||
|
||||
@Tool(description = "Looks up the delivery status of a customer order by its order number")
|
||||
public String orderStatus(@ToolParam(description = "the numeric order number, for example 1001") String orderId) {
|
||||
return "Order " + orderId + " is " + ORDERS.getOrDefault(orderId, "unknown");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.ankurm.qvs;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringAiApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringAiApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
# Point the OpenAI client at the stub. Swap base-url and api-key to use the real thing.
|
||||
spring.ai.openai.base-url=http://localhost:8089/v1
|
||||
spring.ai.openai.api-key=stub
|
||||
spring.ai.openai.chat.options.model=stub-chat
|
||||
spring.ai.openai.embedding.options.model=stub-embed
|
||||
server.port=8080
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.ankurm.qvs;
|
||||
|
||||
import com.ankurm.stub.StubLlmServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
class SupportAssistantTest {
|
||||
|
||||
static {
|
||||
try {
|
||||
StubLlmServer.start(8089); // must be up before the context embeds the policies
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
MockMvc mvc;
|
||||
|
||||
static String stats() throws IOException, InterruptedException {
|
||||
return HttpClient.newHttpClient().send(
|
||||
HttpRequest.newBuilder(URI.create("http://localhost:8089/stats")).build(),
|
||||
HttpResponse.BodyHandlers.ofString()).body();
|
||||
}
|
||||
|
||||
static int chatCount(String stats) {
|
||||
return Integer.parseInt(stats.replaceAll(".*\"chat\":(\\d+).*", "$1"));
|
||||
}
|
||||
|
||||
static int count(String stats, String key) {
|
||||
return Integer.parseInt(stats.replaceAll(".*\"" + key + "\":(\\d+).*", "$1"));
|
||||
}
|
||||
|
||||
static void write(String name, String question, String answer, String before, String after) throws IOException {
|
||||
Files.createDirectories(Path.of("../output"));
|
||||
Files.writeString(Path.of("../output/" + name),
|
||||
"question: " + question + "\nanswer: " + answer
|
||||
+ "\nchat requests sent to the stub for this question: " + (count(after, "chat") - count(before, "chat"))
|
||||
+ "\nembedding requests for this question: " + (count(after, "embeddings") - count(before, "embeddings")) + "\n");
|
||||
}
|
||||
|
||||
String ask(String q) throws Exception {
|
||||
return mvc.perform(get("/ask").param("q", q)).andReturn().getResponse().getContentAsString();
|
||||
}
|
||||
|
||||
@Test
|
||||
void orderQuestionCallsTheTool() throws Exception {
|
||||
String q = "Where is my order 1001?";
|
||||
String before = stats();
|
||||
String answer = ask(q);
|
||||
write("21-spring-tool-call.txt", q, answer, before, stats());
|
||||
assertTrue(answer.contains("Order 1001 is shipped on 2026-11-20"), answer);
|
||||
assertEquals(chatCount(before) + 2, chatCount(stats()), "tool call = two chat round trips");
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyQuestionGetsRetrievedContext() throws Exception {
|
||||
String q = "How many days do I have to return an item?";
|
||||
String before = stats();
|
||||
String answer = ask(q);
|
||||
write("22-spring-rag.txt", q, answer, before, stats());
|
||||
assertTrue(answer.contains("within 30 days of delivery"), answer);
|
||||
assertFalse(answer.contains("Shipping policy"), "only the best segment is injected");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unrelatedQuestionGetsNoContext() throws Exception {
|
||||
assertFalse(ask("What is the weather in Paris").contains("policy:"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user