Skip to content

Commit a9e8f56

Browse files
Merge pull request #1272 from siri-varma/users/svegiraju/add-conversation-alpha2-example
Modify the example to use Conversation Alpha 2
2 parents bd15f70 + 0a805f5 commit a9e8f56

File tree

3 files changed

+145
-34
lines changed

3 files changed

+145
-34
lines changed

conversation/java/sdk/README.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Dapr Conversation API (Java SDK)
22

3-
In this quickstart, you'll send an input to a mock Large Language Model (LLM) using Dapr's Conversation API. This API is responsible for providing one consistent API entry point to talk to underlying LLM providers.
3+
In this quickstart, you'll send an input to a mock Large Language Model (LLM) using Dapr's Conversation API. This API is responsible for providing one consistent API entry point to talk to underlying LLM providers. This example demonstrates both simple conversation and tool calling capabilities.
44

55
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/) link for more information about Dapr and the Conversation API.
66

77
This quickstart includes one app:
88

9-
- Conversation, responsible for sending an input to the underlying LLM and retrieving an output.
9+
- Conversation, responsible for sending an input to the underlying LLM and retrieving an output, including tool call support.
1010

1111
## Run the app with the template file
1212

@@ -35,8 +35,16 @@ cd ..
3535
<!-- STEP
3636
name: Run multi app run template
3737
expected_stdout_lines:
38-
- '== APP - conversation == Input: What is Dapr?'
39-
- '== APP - conversation == Output response: What is Dapr?'
38+
- '== APP - conversation == === Simple Conversation ==='
39+
- '== APP - conversation == Conversation input sent: What is dapr?'
40+
- '== APP - conversation == Output response: What is dapr?'
41+
- '== APP - conversation == === Tool Calling ==='
42+
- '== APP - conversation == Tool calling input sent: What is the weather like in San Francisco in celsius?'
43+
- '== APP - conversation == Output message: What is the weather like in San Francisco in celsius?'
44+
- '== APP - conversation == Tool calls detected:'
45+
- '== APP - conversation == Tool call: {"id": "0", "function": {"name": "get_weather", "arguments": location,unit}}'
46+
- '== APP - conversation == Function name: get_weather'
47+
- '== APP - conversation == Function arguments: location,unit'
4048
expected_stderr_lines:
4149
output_match_mode: substring
4250
match_order: none
@@ -51,12 +59,23 @@ dapr run -f .
5159

5260
The terminal console output should look similar to this, where:
5361

54-
- The app sends an input `What is Dapr?` to the `echo` Component mock LLM.
55-
- The mock LLM echoes `What is Dapr?`.
62+
- The app sends a simple conversation input `What is dapr?` to the `echo` Component mock LLM.
63+
- The mock LLM echoes back the response.
64+
- The app then demonstrates tool calling by sending `What is the weather like in San Francisco in celsius?`.
65+
- The response includes detected tool calls with function name and arguments.
5666

5767
```text
58-
== APP - conversation == Input: What is Dapr?
59-
== APP - conversation == Output response: What is Dapr?
68+
== APP - conversation == === Simple Conversation ===
69+
== APP - conversation == Conversation input sent: What is dapr?
70+
== APP - conversation == Output response: What is dapr?
71+
72+
== APP - conversation == === Tool Calling ===
73+
== APP - conversation == Tool calling input sent: What is the weather like in San Francisco in celsius?
74+
== APP - conversation == Output message: What is the weather like in San Francisco in celsius?
75+
== APP - conversation == Tool calls detected:
76+
== APP - conversation == Tool call: {"id": "0", "function": {"name": "get_weather", "arguments": location,unit}}
77+
== APP - conversation == Function name: get_weather
78+
== APP - conversation == Function arguments: location,unit
6079
```
6180

6281
<!-- END_STEP -->
@@ -80,21 +99,27 @@ dapr stop -f .
8099
```bash
81100
cd ./conversation
82101
mvn clean install
83-
java -jar ConversationAIService-0.0.1-SNAPSHOT.jar com.service.Conversation
102+
java -jar target/ConversationAIService-0.0.1-SNAPSHOT.jar com.service.Conversation
84103
```
85104

86-
2. Run the Dapr process alongside the application.
105+
2. Run the Dapr process alongside the application
87106

88107
```bash
89108
dapr run --app-id conversation --resources-path ../../../components/
90109
```
91110

92-
The terminal console output should look similar to below, where:
93-
94-
- The app sends an input `What is Dapr?` to the `echo` Component mock LLM.
95-
- The mock LLM echoes `What is Dapr?`.
111+
The terminal console output should look similar to this:
96112

97113
```text
98-
== APP - conversation == Input: What is Dapr?
99-
== APP - conversation == Output response: What is Dapr?
114+
=== Simple Conversation ===
115+
Conversation input sent: What is dapr?
116+
Output response: What is dapr?
117+
118+
=== Tool Calling ===
119+
Tool calling input sent: What is the weather like in San Francisco in celsius?
120+
Output message: What is the weather like in San Francisco in celsius?
121+
Tool calls detected:
122+
Tool call: {"id": "0", "function": {"name": "get_weather", "arguments": location,unit}}
123+
Function name: get_weather
124+
Function arguments: location,unit
100125
```

conversation/java/sdk/conversation/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<dependency>
2424
<groupId>io.dapr</groupId>
2525
<artifactId>dapr-sdk</artifactId>
26-
<version>1.16.0</version>
26+
<version>1.16.1-rc-2</version>
2727
</dependency>
2828
</dependencies>
2929
</dependencyManagement>

conversation/java/sdk/conversation/src/main/java/com/service/Conversation.java

Lines changed: 103 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,117 @@
11
package com.service;
22

3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
37
import io.dapr.client.DaprClientBuilder;
48
import io.dapr.client.DaprPreviewClient;
5-
import io.dapr.client.domain.ConversationInput;
6-
import io.dapr.client.domain.ConversationRequest;
7-
import io.dapr.client.domain.ConversationResponse;
8-
import reactor.core.publisher.Mono;
9-
10-
import java.util.List;
9+
import io.dapr.client.domain.ConversationInputAlpha2;
10+
import io.dapr.client.domain.ConversationMessageContent;
11+
import io.dapr.client.domain.ConversationRequestAlpha2;
12+
import io.dapr.client.domain.ConversationResponseAlpha2;
13+
import io.dapr.client.domain.ConversationResultAlpha2;
14+
import io.dapr.client.domain.ConversationResultChoices;
15+
import io.dapr.client.domain.ConversationResultMessage;
16+
import io.dapr.client.domain.ConversationToolCalls;
17+
import io.dapr.client.domain.ConversationTools;
18+
import io.dapr.client.domain.ConversationToolsFunction;
19+
import io.dapr.client.domain.UserMessage;
1120

1221
public class Conversation {
1322

14-
public static void main(String[] args) {
15-
String prompt = "What is Dapr?";
23+
private static final String CONVERSATION_COMPONENT_NAME = "echo";
24+
private static final String CONVERSATION_TEXT = "What is dapr?";
25+
private static final String TOOL_CALL_INPUT = "What is the weather like in San Francisco in celsius?";
1626

27+
public static void main(String[] args) {
1728
try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) {
18-
System.out.println("Input: " + prompt);
1929

20-
ConversationInput daprConversationInput = new ConversationInput(prompt);
30+
// Define tool function parameters schema
31+
Map<String, Object> locationProperty = new HashMap<>();
32+
locationProperty.put("type", "string");
33+
locationProperty.put("description", "The city and state, e.g. San Francisco, CA");
34+
35+
Map<String, Object> unitProperty = new HashMap<>();
36+
unitProperty.put("type", "string");
37+
unitProperty.put("enum", List.of("celsius", "fahrenheit"));
38+
unitProperty.put("description", "The temperature unit to use");
39+
40+
Map<String, Object> properties = new HashMap<>();
41+
properties.put("location", locationProperty);
42+
properties.put("unit", unitProperty);
43+
44+
Map<String, Object> parameters = new HashMap<>();
45+
parameters.put("type", "object");
46+
parameters.put("properties", properties);
47+
parameters.put("required", List.of("location"));
48+
49+
// Create the tool function
50+
ConversationToolsFunction getWeatherFunction = new ConversationToolsFunction("get_weather", parameters)
51+
.setDescription("Get the current weather for a location");
52+
ConversationTools weatherTool = new ConversationTools(getWeatherFunction);
53+
54+
// ==========================================
55+
// Simple Conversation
56+
// ==========================================
57+
System.out.println("=== Simple Conversation ===");
58+
59+
UserMessage conversationMessage = new UserMessage(
60+
List.of(new ConversationMessageContent(CONVERSATION_TEXT)))
61+
.setName("TestUser");
62+
ConversationInputAlpha2 conversationInput = new ConversationInputAlpha2(List.of(conversationMessage));
63+
64+
ConversationResponseAlpha2 conversationResponse = client.converseAlpha2(
65+
new ConversationRequestAlpha2(CONVERSATION_COMPONENT_NAME, List.of(conversationInput))
66+
.setScrubPii(false)
67+
.setToolChoice("auto")
68+
.setTemperature(0.7)
69+
.setTools(List.of(weatherTool))).block();
70+
71+
System.out.println("Conversation input sent: " + CONVERSATION_TEXT);
72+
String outputContent = conversationResponse.getOutputs().get(0)
73+
.getChoices().get(0).getMessage().getContent();
74+
System.out.println("Output response: " + outputContent);
75+
76+
// ==========================================
77+
// Tool Calling
78+
// ==========================================
79+
System.out.println("\n=== Tool Calling ===");
80+
81+
UserMessage toolCallMessage = new UserMessage(
82+
List.of(new ConversationMessageContent(TOOL_CALL_INPUT)))
83+
.setName("TestUser");
84+
ConversationInputAlpha2 toolCallInput = new ConversationInputAlpha2(List.of(toolCallMessage));
85+
86+
ConversationResponseAlpha2 toolCallResponse = client.converseAlpha2(
87+
new ConversationRequestAlpha2(CONVERSATION_COMPONENT_NAME, List.of(toolCallInput))
88+
.setScrubPii(false)
89+
.setToolChoice("auto")
90+
.setTemperature(0.7)
91+
.setTools(List.of(weatherTool))).block();
92+
93+
System.out.println("Tool calling input sent: " + TOOL_CALL_INPUT);
94+
95+
ConversationResultAlpha2 result = toolCallResponse.getOutputs().get(0);
96+
ConversationResultChoices choice = result.getChoices().get(0);
97+
ConversationResultMessage message = choice.getMessage();
98+
99+
System.out.println("Output message: " + message.getContent());
100+
101+
if (message.hasToolCalls()) {
102+
System.out.println("Tool calls detected:");
103+
for (ConversationToolCalls toolCall : message.getToolCalls()) {
104+
String functionName = toolCall.getFunction().getName();
105+
String functionArguments = toolCall.getFunction().getArguments();
106+
107+
System.out.println("Tool call: {\"id\": \"" + toolCall.getId()
108+
+ "\", \"function\": {\"name\": \"" + functionName
109+
+ "\", \"arguments\": " + functionArguments + "}}");
110+
System.out.println("Function name: " + functionName);
111+
System.out.println("Function arguments: " + functionArguments);
112+
}
113+
}
21114

22-
// Component name is the name provided in the metadata block of the conversation.yaml file.
23-
Mono<ConversationResponse> responseMono = client.converse(new ConversationRequest("echo",
24-
List.of(daprConversationInput))
25-
.setContextId("contextId")
26-
.setScrubPii(true).setTemperature(1.1d));
27-
ConversationResponse response = responseMono.block();
28-
System.out.printf("Output response: %s", response.getConversationOutputs().get(0).getResult());
29115
} catch (Exception e) {
30116
throw new RuntimeException(e);
31117
}

0 commit comments

Comments
 (0)