Create a chatbot with Google Generative AI and Quarkus
I recently created a Quarkus extension that provides access to Google Generative AI. In this article, I’ll use this extension to create a chatbot.
The first step is to create a Quarkus project that includes the REST and Google Cloud Gen AI extensions.
Here are the extensions to add to your dependencies file:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.googlecloudservices</groupId>
<artifactId>quarkus-google-cloud-genai</artifactId>
</dependency>
Or, simpler, here’s a link that will let you create a project from code.quarkus.io.
The extension allows you to inject a Client object into a Quarkus application to access the Google Cloud Generartive AI service. This service lets you use the Gemini generative AI model in a Google Cloud project.
We’ll use this extension in a REST endpoint to build a chatbot. Since I’m not too keen on developing UIs, I’ll use cURL to call the chatbot.
Here’s the starting code for a chatbot:
package fr.loicmathieu.google.genai;
// [...] imports omitted for brevity
// 1. Standard REST endpoint available on /chat
@Path("/chat")
public class GenAIResource {
// 2. System message telling the LLM it is a chatbot
private static final String SYSTEM_INSTRUCTION = """
You are a chatbot named Loïc.
Don't pretend you know everything,
but be helpful with a touch of humour.""";
private GenerateContentConfig config;
// 3. Injection of the provided GenAI Client object
@Inject
Client client;
// 4. Init the configuration with the system instruction
@PostConstruct
void init() {
var systemInstruction = Content.fromParts(Part.fromText(SYSTEM_INSTRUCTION));
config = GenerateContentConfig.builder()
.systemInstruction(systemInstruction).build();
}
// 5. Generate a chat response on each call to /chat
@GET
public String chat(@QueryParam("prompt") String prompt) {
return client.models
.generateContent("gemini-3.8-flash", prompt, config).text() + "\n";
}
}
- A REST endpoint using the JAX-RS standard, available at the
/chatpath in the application. - System message informing the model that it is a chatbot and asking it to respond with a touch of humor.
- Injection of the
ClientGenAI service, which is provided by the Quarkus Google Cloud GenAI extension. - Initialization of the configuration in the REST endpoint’s post-construct method.
- Each time the endpoint is called, we’ll invoke the model and return the first generated response. A real-world application would certainly implement something more advanced. Here, we’re using Gemini Flash 3.8 because Gemini Flash is a model that combines performance with cost efficiency; exactly what we want for a chatbot that needs to respond quickly and doesn’t require overly complex reasoning.
To start the application in dev mode, you can use the Quarkus CLI: quarkus dev; the application will then be available on port 8080.
To call the chatbot, I use the following cURL command: curl -G localhost:8080/chat --data-urlencode "prompt=Tell me a joke". For those who aren’t familiar with this, using -G forces cURL to perform a GET request, which will then pass the data as a query string in the URL. This helps avoid any encoding issues with spaces by using --data-urlencode.
Here’s an example conversation:
As you can see, everything is going well until I ask him what my name is! Even though I just told him, he doesn’t know it because he has no memory.
We’re going to add a memory to the chatbot so we can have a real conversation with it; this is done through a chat session.
package fr.loicmathieu.quarkus.vertexai;
// [...] Imports omitted for brevity
// 1. Standard REST endpoint available on /chat
@Path("/chat")
public class GenAIResource {
// 2. System message telling the LLM it is a chatbot
private static final String SYSTEM_INSTRUCTION = """
You are a chatbot named Loïc.
Don't pretend you know everything;
but be helpful with a touch of humour.""";
private Chat chat;
// 3. Injection of the provided GenAI Client object
@Inject
Client client;
// 4. Init the configuration with the system instruction and a chat session
@PostConstruct
void init() {
var systemInstruction = Content.fromParts(Part.fromText(SYSTEM_INSTRUCTION));
var config = GenerateContentConfig.builder().
systemInstruction(systemInstruction).build();
chat = client.chats.create("gemini-3.8-flash", config);
}
// 5. Generate a chat response on each call to /chat
@GET
public String chat(@QueryParam("prompt") String prompt) {
return chat.sendMessage(prompt).text() + "\n";
}
}
With a memory, the chatbot works much better!
Here, I used a single chat session for the entire application, stored in memory (RAM). In a real-world scenario, you’ll of course need to use one chat session per user, ideally stored persistently.
That’s all for today; we’ve learned how to create a chatbot in Quarkus using Google Generative AI and the Gemini model. Google GenAI supports many other features: RAG, multimodal completion, tools, and more. You can find more information in its documentation.
You can find the project code on GitHub: quarkus-gen-ai.
Thank you to Google for providing Google Cloud credits to write this article.