Skip to content

Add Conversation AI to Java SDK #1235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
75f487f
Conversation first commit
siri-varma Mar 6, 2025
88fe015
Add unit tests
siri-varma Mar 7, 2025
c27d3ab
Merge branch 'master' into users/svegiraju/conversation-api-2
siri-varma Mar 7, 2025
ca2dd02
Merge branch 'master' into users/svegiraju/conversation-api-2
salaboy Mar 13, 2025
fada33f
change ai to conv
siri-varma Mar 14, 2025
d14ce3e
Merge branch 'users/svegiraju/conversation-api-2' of https://github.c…
siri-varma Mar 14, 2025
842b0e7
Move to single module
siri-varma Mar 18, 2025
c18ccf5
Remove module
siri-varma Mar 18, 2025
a169d00
Add Integration tests
siri-varma Mar 18, 2025
dd44b79
Merge branch 'master' into users/svegiraju/conversation-api-2
siri-varma Mar 18, 2025
fae44b7
Merge branch 'master' into users/svegiraju/conversation-api-2
siri-varma Mar 24, 2025
7958555
Merge branch 'master' into users/svegiraju/conversation-api-2
siri-varma Apr 7, 2025
e29e376
Merge branch 'master' into users/svegiraju/conversation-api-2
siri-varma Apr 9, 2025
4dde2f6
Update sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprConversa…
siri-varma Apr 12, 2025
33807c0
Add example and mechanical markdown
siri-varma Apr 12, 2025
738eee8
Add example and mechanical markdown
siri-varma Apr 12, 2025
e7fb13b
Add example and mechanical markdown
siri-varma Apr 12, 2025
4cfe6e7
Add example and mechanical markdown
siri-varma Apr 12, 2025
0b07cc7
Merge branch 'master' into users/svegiraju/conversation-api-2
cicoyle Apr 14, 2025
6798bc2
Update conversation.yaml
artur-ciocanu Apr 14, 2025
720a427
Merge branch 'master' into users/svegiraju/conversation-api-2
artur-ciocanu Apr 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ jobs:
run: ./mvnw install -q
env:
DOCKER_HOST: ${{steps.setup_docker.outputs.sock}}
- name: Validate conversation ai example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/conversation/README.md
env:
DOCKER_HOST: ${{steps.setup_docker.outputs.sock}}
- name: Validate invoke http example
working-directory: ./examples
run: |
Expand Down
7 changes: 7 additions & 0 deletions examples/components/conversation/conversation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: echo
spec:
type: conversation.echo
version: v1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.examples.conversation;

import io.dapr.client.DaprClientBuilder;
import io.dapr.client.DaprPreviewClient;
import io.dapr.client.domain.ConversationInput;
import io.dapr.client.domain.ConversationRequest;
import io.dapr.client.domain.ConversationResponse;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Collections;

public class DemoConversationAI {
/**
* The main method to start the client.
*
* @param args Input arguments (unused).
*/
public static void main(String[] args) {
try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) {
ConversationInput daprConversationInput = new ConversationInput("Hello How are you? "
+ "This is the my number 672-123-4567");

// Component name is the name provided in the metadata block of the conversation.yaml file.
Mono<ConversationResponse> responseMono = client.converse(new ConversationRequest("echo",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a more modern approach from Java 11 List.of(...) instead of Collections.singleton(...)

new ArrayList<>(Collections.singleton(daprConversationInput)))
.setContextId("contextId")
.setScrubPii(true).setTemperature(1.1d));
ConversationResponse response = responseMono.block();
System.out.printf("Conversation output: %s", response.getConversationOutpus().get(0).getResult());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line

117 changes: 117 additions & 0 deletions examples/src/main/java/io/dapr/examples/conversation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
## Manage Dapr Jobs via the Conversation API
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manage Dapr Jobs looks confusing ... is this related to Job API or Conversation API?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, lets remove jobs from this unless you are using the jobs api.


This example provides the different capabilities provided by Dapr Java SDK for Conversation. For further information about Job APIs please refer to [this link](https://docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/)

### Using the Conversation API

The Java SDK exposes several methods for this -
* `client.converse(...)` for conversing with an LLM through Dapr.

## Pre-requisites

* [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/).
* Java JDK 11 (or greater):
* [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11)
* [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11)
* [OpenJDK 11](https://jdk.java.net/11/)
* [Apache Maven](https://maven.apache.org/install.html) version 3.x.

### Checking out the code

Clone this repository:

```sh
git clone https://github.com/dapr/java-sdk.git
cd java-sdk
```

Then build the Maven project:

```sh
# make sure you are in the `java-sdk` directory.
mvn install
```

Then get into the examples directory:

```sh
cd examples
```

### Initialize Dapr

Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initialized.

### Running the example

This example uses the Java SDK Dapr client in order to **Converse** with an LLM.
`DemoConversationAI.java` is the example class demonstrating these features.
Kindly check [DaprPreviewClient.java](https://github.com/dapr/java-sdk/blob/master/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java) for a detailed description of the supported APIs.

```java
public class DemoConversationAI {
/**
* The main method to start the client.
*
* @param args Input arguments (unused).
*/
public static void main(String[] args) {
try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) {
ConversationInput daprConversationInput = new ConversationInput("Hello How are you? "
+ "This is the my number 672-123-4567");

// Component name is the name provided in the metadata block of the conversation.yaml file.
Mono<ConversationResponse> responseMono = client.converse(new ConversationRequest("echo",
new ArrayList<>(Collections.singleton(daprConversationInput)))
.setContextId("contextId")
.setScrubPii(true).setTemperature(1.1d));
ConversationResponse response = responseMono.block();
System.out.printf("Conversation output: %s", response.getConversationOutpus().get(0).getResult());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
```

Get into the examples' directory:
```sh
cd examples
```

Use the following command to run this example-

<!-- STEP
name: Run Demo Conversation Client example
expected_stdout_lines:
- "== APP == Conversation output: Hello How are you? This is the my number <ISBN>"
background: true
output_match_mode: substring
sleep: 10
-->

```bash
dapr run --resources-path ./components/conversation --app-id myapp --app-port 8080 --dapr-http-port 3500 --dapr-grpc-port 51439 --log-level debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.conversation.DemoConversationAI
```

<!-- END_STEP -->

### Sample output
```
== APP == Conversation output: Hello How are you? This is the my number <ISBN>
```
### Cleanup

To stop the app, run (or press CTRL+C):

<!-- STEP

name: Cleanup
-->

```bash
dapr stop --app-id myapp
```

<!-- END_STEP -->

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<grpc.version>1.69.0</grpc.version>
<protobuf.version>3.25.5</protobuf.version>
<protocCommand>protoc</protocCommand>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated to latest

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1.15.4 is latest as of now - think this was just bc the PR has been open a while and we more recently released 1.15.3 && 1.15.4

<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.14.4/dapr/proto</dapr.proto.baseurl>
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.15.2/dapr/proto</dapr.proto.baseurl>
<dapr.sdk.version>1.15.0-SNAPSHOT</dapr.sdk.version>
<dapr.sdk.alpha.version>0.15.0-SNAPSHOT</dapr.sdk.alpha.version>
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 2021 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.it.testcontainers;

import io.dapr.client.DaprPreviewClient;
import io.dapr.client.domain.ConversationInput;
import io.dapr.client.domain.ConversationRequest;
import io.dapr.client.domain.ConversationResponse;
import io.dapr.testcontainers.Component;
import io.dapr.testcontainers.DaprContainer;
import io.dapr.testcontainers.DaprLogLevel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = {
TestDaprConversationConfiguration.class,
TestConversationApplication.class
}
)
@Testcontainers
@Tag("testcontainers")
public class DaprConversationIT {

private static final Network DAPR_NETWORK = Network.newNetwork();
private static final Random RANDOM = new Random();
private static final int PORT = RANDOM.nextInt(1000) + 8000;

@Container
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd:1.15.2")
.withAppName("conversation-dapr-app")
.withComponent(new Component("echo", "conversation.echo", "v1", new HashMap<>()))
.withNetwork(DAPR_NETWORK)
.withDaprLogLevel(DaprLogLevel.DEBUG)
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withAppChannelAddress("host.testcontainers.internal")
.withAppPort(PORT);

/**
* Expose the Dapr ports to the host.
*
* @param registry the dynamic property registry
*/
@DynamicPropertySource
static void daprProperties(DynamicPropertyRegistry registry) {
registry.add("dapr.http.endpoint", DAPR_CONTAINER::getHttpEndpoint);
registry.add("dapr.grpc.endpoint", DAPR_CONTAINER::getGrpcEndpoint);
registry.add("server.port", () -> PORT);
}

@Autowired
private DaprPreviewClient daprPreviewClient;

@BeforeEach
public void setUp(){
org.testcontainers.Testcontainers.exposeHostPorts(PORT);
}

@Test
public void testConversationSDKShouldHaveSameOutputAndInput() {
ConversationInput conversationInput = new ConversationInput("input this");
List<ConversationInput> conversationInputList = new ArrayList<>();
conversationInputList.add(conversationInput);

ConversationResponse response =
this.daprPreviewClient.converse(new ConversationRequest("echo", conversationInputList)).block();

Assertions.assertEquals("", response.getContextId());
Assertions.assertEquals("input this", response.getConversationOutpus().get(0).getResult());
}

@Test
public void testConversationSDKShouldScrubPIIEntirelyWhenScrubPIIIsSetInRequestBody() {
List<ConversationInput> conversationInputList = new ArrayList<>();
conversationInputList.add(new ConversationInput("input this [email protected]"));
conversationInputList.add(new ConversationInput("input this +12341567890"));

ConversationResponse response =
this.daprPreviewClient.converse(new ConversationRequest("echo", conversationInputList)
.setScrubPii(true)).block();

Assertions.assertEquals("", response.getContextId());
Assertions.assertEquals("input this <EMAIL_ADDRESS>",
response.getConversationOutpus().get(0).getResult());
Assertions.assertEquals("input this <PHONE_NUMBER>",
response.getConversationOutpus().get(1).getResult());
}

@Test
public void testConversationSDKShouldScrubPIIOnlyForTheInputWhereScrubPIIIsSet() {
List<ConversationInput> conversationInputList = new ArrayList<>();
conversationInputList.add(new ConversationInput("input this [email protected]"));
conversationInputList.add(new ConversationInput("input this +12341567890").setScrubPii(true));

ConversationResponse response =
this.daprPreviewClient.converse(new ConversationRequest("echo", conversationInputList)).block();

Assertions.assertEquals("", response.getContextId());
Assertions.assertEquals("input this [email protected]",
response.getConversationOutpus().get(0).getResult());
Assertions.assertEquals("input this <PHONE_NUMBER>",
response.getConversationOutpus().get(1).getResult());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.it.testcontainers;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestConversationApplication {

public static void main(String[] args) {
SpringApplication.run(TestConversationApplication.class, args);
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line

Loading
Loading