|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "How to: Author and manage Dapr Conversation AI in the Java SDK" |
| 4 | +linkTitle: "How to: Author and manage Conversation AI" |
| 5 | +weight: 20000 |
| 6 | +description: How to get up and running with Conversation AI using the Dapr Java SDK |
| 7 | +--- |
| 8 | + |
| 9 | +As part of this demonstration, we will look at how to use the Conversation API to converse with a Large Language Model (LLM). The API |
| 10 | +will return the response from the LLM for the given prompt. With the [provided conversation ai example](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/conversation), you will: |
| 11 | + |
| 12 | +- You will provide a prompt using the [Conversation AI example](https://github.com/dapr/java-sdk/blob/master/examples/src/main/java/io/dapr/examples/conversation/DemoConversationAI.java) |
| 13 | +- Filter out Personally identifiable information (PII). |
| 14 | + |
| 15 | +This example uses the default configuration from `dapr init` in [self-hosted mode](https://github.com/dapr/cli#install-dapr-on-your-local-machine-self-hosted). |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started). |
| 20 | +- Java JDK 11 (or greater): |
| 21 | + - [Oracle JDK](https://www.oracle.com/java/technologies/downloads), or |
| 22 | + - OpenJDK |
| 23 | +- [Apache Maven](https://maven.apache.org/install.html), version 3.x. |
| 24 | +- [Docker Desktop](https://www.docker.com/products/docker-desktop) |
| 25 | + |
| 26 | +## Set up the environment |
| 27 | + |
| 28 | +Clone the [Java SDK repo](https://github.com/dapr/java-sdk) and navigate into it. |
| 29 | + |
| 30 | +```bash |
| 31 | +git clone https://github.com/dapr/java-sdk.git |
| 32 | +cd java-sdk |
| 33 | +``` |
| 34 | + |
| 35 | +Run the following command to install the requirements for running the Conversation AI example with the Dapr Java SDK. |
| 36 | + |
| 37 | +```bash |
| 38 | +mvn clean install -DskipTests |
| 39 | +``` |
| 40 | + |
| 41 | +From the Java SDK root directory, navigate to the examples' directory. |
| 42 | + |
| 43 | +```bash |
| 44 | +cd examples |
| 45 | +``` |
| 46 | + |
| 47 | +Run the Dapr sidecar. |
| 48 | + |
| 49 | +```sh |
| 50 | +dapr run --app-id conversationapp --dapr-grpc-port 51439 --dapr-http-port 3500 --app-port 8080 |
| 51 | +``` |
| 52 | + |
| 53 | +> Now, Dapr is listening for HTTP requests at `http://localhost:3500` and gRPC requests at `http://localhost:51439`. |
| 54 | +
|
| 55 | +## Send a prompt with Personally identifiable information (PII) to the Conversation AI API |
| 56 | + |
| 57 | +In the `DemoConversationAI` there are steps to send a prompt using the `converse` method under the `DaprPreviewClient`. |
| 58 | + |
| 59 | +```java |
| 60 | +public class DemoConversationAI { |
| 61 | + /** |
| 62 | + * The main method to start the client. |
| 63 | + * |
| 64 | + * @param args Input arguments (unused). |
| 65 | + */ |
| 66 | + public static void main(String[] args) { |
| 67 | + try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) { |
| 68 | + System.out.println("Sending the following input to LLM: Hello How are you? This is the my number 672-123-4567"); |
| 69 | + |
| 70 | + ConversationInput daprConversationInput = new ConversationInput("Hello How are you? " |
| 71 | + + "This is the my number 672-123-4567"); |
| 72 | + |
| 73 | + // Component name is the name provided in the metadata block of the conversation.yaml file. |
| 74 | + Mono<ConversationResponse> responseMono = client.converse(new ConversationRequest("echo", |
| 75 | + List.of(daprConversationInput)) |
| 76 | + .setContextId("contextId") |
| 77 | + .setScrubPii(true).setTemperature(1.1d)); |
| 78 | + ConversationResponse response = responseMono.block(); |
| 79 | + System.out.printf("Conversation output: %s", response.getConversationOutputs().get(0).getResult()); |
| 80 | + } catch (Exception e) { |
| 81 | + throw new RuntimeException(e); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Run the `DemoConversationAI` with the following command. |
| 88 | + |
| 89 | +```sh |
| 90 | +java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.conversation.DemoConversationAI |
| 91 | +``` |
| 92 | + |
| 93 | +### Sample output |
| 94 | +``` |
| 95 | +== APP == Conversation output: Hello How are you? This is the my number <ISBN> |
| 96 | +``` |
| 97 | + |
| 98 | +As shown in the output, the number sent to the API is obfuscated and returned in the form of <ISBN>. |
| 99 | +The example above uses an ["echo"](https://docs.dapr.io/developing-applications/building-blocks/conversation/howto-conversation-layer/#set-up-the-conversation-component) |
| 100 | +component for testing, which simply returns the input message. |
| 101 | +When integrated with LLMs like OpenAI or Claude, you’ll receive meaningful responses instead of echoed input. |
| 102 | + |
| 103 | +## Next steps |
| 104 | +- [Learn more about Conversation AI]({{< ref conversation-overview.md >}}) |
| 105 | +- [Conversation AI API reference]({{< ref conversation_api.md >}}) |
0 commit comments