Skip to content

Commit

Permalink
Modify the fraud detection sample to demonstrate how streamed respons…
Browse files Browse the repository at this point in the history
…es can use blocking tools
  • Loading branch information
cescoffier committed Nov 5, 2024
1 parent b33c558 commit 00cd5ef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion samples/fraud-detection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<quarkus.platform.version>3.15.1</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.2.5</surefire-plugin.version>
<quarkus-langchain4j.version>0.21.0.CR4</quarkus-langchain4j.version>
<quarkus-langchain4j.version>999-SNAPSHOT</quarkus-langchain4j.version>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.temporal.ChronoUnit;

import io.smallrye.mutiny.Multi;
import org.eclipse.microprofile.faulttolerance.Timeout;

import dev.langchain4j.service.SystemMessage;
Expand Down Expand Up @@ -36,6 +37,31 @@ public interface FraudDetectionAi {
@Timeout(value = 2, unit = ChronoUnit.MINUTES)
String detectAmountFraudForCustomer(long customerId);

@SystemMessage("""
You are a bank account fraud detection AI. You have to detect frauds in transactions.
""")
@UserMessage("""
Your task is to detect whether a fraud was committed for the customer {{customerId}}.
To detect a fraud, perform the following actions:
1 - Retrieve the name of the customer {{customerId}}
2 - Retrieve the transactions for the customer {{customerId}} for the last 15 minutes.
3 - Sum the amount of the all these transactions. Make sure the sum is correct.
4 - If the amount is greater than 10000, a fraud is detected.
Answer with a **single** JSON document containing:
- the customer name in the 'customer-name' key
- the computed sum in the 'total' key
- the 'fraud' key set to a boolean value indicating if a fraud was detected
- the 'transactions' key containing the list of transaction amounts
- the 'explanation' key containing a explanation of your answer, especially how the sum is computed.
- if there is a fraud, the 'email' key containing an email to the customer {{customerId}} to warn him about the fraud. The text must be formal and polite. It must ask the customer to contact the bank ASAP.
Your response must be just the raw JSON document, without ```json, ``` or anything else.
""")
@Timeout(value = 2, unit = ChronoUnit.MINUTES)
Multi<String> detectAmountFraudForCustomerStreamed(long customerId);

@SystemMessage("""
You are a bank account fraud detection AI. You have to detect frauds in transactions.
""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.util.List;

import io.smallrye.mutiny.Multi;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.jboss.resteasy.reactive.RestQuery;

@Path("/fraud")
Expand All @@ -30,6 +33,13 @@ public String detectBaseOnAmount(@RestQuery long customerId) {
return service.detectAmountFraudForCustomer(customerId);
}

@GET
@Path("/amount/streamed")
@Produces(MediaType.SERVER_SENT_EVENTS)
public Multi<String> detectBaseOnAmountReactive(@RestQuery long customerId) {
return service.detectAmountFraudForCustomerStreamed(customerId);
}

@GET
@Path("/transactions")
public List<Transaction> list(@RestQuery long customerId) {
Expand Down

0 comments on commit 00cd5ef

Please sign in to comment.