Skip to content

Commit dc51ffb

Browse files
committed
Move customer data to application.properties instead of using constants
1 parent a504814 commit dc51ffb

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

documentation/modules/ROOT/pages/20_embed_documents.adoc

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,21 @@ quarkus.langchain4j.openai.timeout=60s
5252
5353
%dev.quarkus.mailer.mock=false
5454
55-
quarkus.langchain4j.openai.chat-model.temperature=0.0 #<1>
56-
quarkus.langchain4j.easy-rag.path=src/main/resources/catalog #<2>
55+
quarkus.langchain4j.openai.chat-model.temperature=0.0
56+
quarkus.langchain4j.easy-rag.path=src/main/resources/catalog
5757
58-
booking.daystostart=1
58+
booking.daystostart=1 #<3>
5959
booking.daystoend=3
60+
booking.firstname=john
61+
booking.lastname=doe
62+
booking.number=123-456
6063
61-
quarkus.langchain4j.openai.chat-model.model-name=gpt-4o #<3>
64+
quarkus.langchain4j.openai.chat-model.model-name=gpt-4o #<4>
6265
----
6366
<1> The "temperature" parameter in AI language models controls the randomness of text generation. Lower values result in more predictable outputs, while higher values encourage creativity and diversity in responses.
6467
<2> Path to where documents are stored for the Retrieval Augmentation Generation (ie. the documents the AI model will use to build local knowledge)
65-
<3> The specific model to use. IMPORTANT: gpt3.5-turbo is much cheaper but the results will be slower and less reliable.
68+
<3> Sample booking data
69+
<4> The specific model to use. IMPORTANT: gpt3.5-turbo is much cheaper but the results will be slower and less reliable.
6670

6771

6872
== Embedding the business document
@@ -162,27 +166,24 @@ Create a new `BookingService` Java class in `src/main/java` in the `com.redhat.d
162166
package com.redhat.developers;
163167
164168
import java.time.LocalDate;
169+
import java.util.Map;
170+
165171
import org.eclipse.microprofile.config.inject.ConfigProperty;
166172
167173
import jakarta.enterprise.context.ApplicationScoped;
174+
import jakarta.inject.Inject;
168175
169176
@ApplicationScoped
170177
public class BookingService {
171178
172-
@ConfigProperty(name="booking.daystostart")
173-
int daystostart;
174-
175-
@ConfigProperty(name="booking.daystoend")
176-
int daystoend;
177-
178-
private static String FIRSTNAME="john";
179-
private static String LASTNAME="doe";
180-
private static String BOOKINGNUMBER ="123-456"; // <1>
179+
@Inject
180+
@ConfigProperty(name = "booking") #<1>
181+
Map<String, String> booking;
181182
182183
public Booking getBookingDetails(String bookingNumber, String customerName, String customerSurname) {
183184
ensureExists(bookingNumber, customerName, customerSurname);
184-
LocalDate bookingFrom = LocalDate.now().plusDays(daystostart);
185-
LocalDate bookingTo = LocalDate.now().plusDays(daystoend);
185+
LocalDate bookingFrom = LocalDate.now().plusDays(Long.parseLong(booking.get("daystostart")));
186+
LocalDate bookingTo = LocalDate.now().plusDays(Long.parseLong(booking.get("daystoend")));
186187
// Retrieval from DB mocking
187188
Customer customer = new Customer(customerName, customerSurname);
188189
return new Booking(bookingNumber, bookingFrom, bookingTo, customer);
@@ -191,15 +192,16 @@ public class BookingService {
191192
public void cancelBooking(String bookingNumber, String customerName, String customerSurname) {
192193
ensureExists(bookingNumber, customerName, customerSurname);
193194
194-
// TODO add logic to double check booking conditions in case the LLM got it wrong.
195+
// TODO add logic to double check booking conditions in case the LLM got it
196+
// wrong.
195197
// throw new BookingCannotBeCancelledException(bookingNumber);
196198
}
197199
198200
private void ensureExists(String bookingNumber, String customerName, String customerSurname) {
199201
// Check mocking
200-
if (!(bookingNumber.equals(BOOKINGNUMBER)
201-
&& customerName.toLowerCase().equals(FIRSTNAME)
202-
&& customerSurname.toLowerCase().equals(LASTNAME))) {
202+
if (!(bookingNumber.equals(booking.get("number"))
203+
&& customerName.toLowerCase().equals(booking.get("firstname"))
204+
&& customerSurname.toLowerCase().equals(booking.get("lastname")))) {
203205
throw new BookingNotFoundException(bookingNumber);
204206
}
205207
}
@@ -219,7 +221,7 @@ class BookingCannotBeCancelledException extends RuntimeException {
219221
}
220222
}
221223
----
222-
<1> We hardcoded a booking entry for simplicity's sake. Of course, in a real world scenario this would likely come from a database.
224+
<1> Retrieve a single booking from the application.properties file. (in the real world this data would likely come from a DB instead :) )
223225

224226
Now we define a `BookingTools` singleton that will serve our AI with proper tools.
225227

@@ -297,7 +299,7 @@ Create a new `ChatSocket` Java record in `src/main/java` in the `com.redhat.deve
297299
----
298300
package com.redhat.developers;
299301
300-
import jakarta.websocket.OnOpen;
302+
import io.quarkus.websockets.next.OnOpen;
301303
import io.quarkus.websockets.next.OnTextMessage;
302304
import io.quarkus.websockets.next.WebSocket;
303305
@@ -316,7 +318,7 @@ public class ChatSocket {
316318
}
317319
318320
@OnTextMessage
319-
public String onMessage(String userMessage){
321+
public String onMessage(String userMessage) {
320322
return assistant.chat(userMessage);
321323
}
322324
}

0 commit comments

Comments
 (0)