Skip to content

Commit 4a9a829

Browse files
authored
Merge branch 'master' into 1548-remote-activities-sb
2 parents d796609 + ac073e2 commit 4a9a829

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3298
-145
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.conversation;
15+
16+
import io.dapr.client.DaprClientBuilder;
17+
import io.dapr.client.DaprPreviewClient;
18+
import io.dapr.client.domain.AssistantMessage;
19+
import io.dapr.client.domain.ConversationInputAlpha2;
20+
import io.dapr.client.domain.ConversationMessage;
21+
import io.dapr.client.domain.ConversationMessageContent;
22+
import io.dapr.client.domain.ConversationRequestAlpha2;
23+
import io.dapr.client.domain.ConversationResponseAlpha2;
24+
import io.dapr.client.domain.ConversationResultAlpha2;
25+
import io.dapr.client.domain.ConversationResultChoices;
26+
import io.dapr.client.domain.ConversationToolCalls;
27+
import io.dapr.client.domain.ConversationToolCallsOfFunction;
28+
import io.dapr.client.domain.SystemMessage;
29+
import io.dapr.client.domain.ToolMessage;
30+
import io.dapr.client.domain.UserMessage;
31+
import reactor.core.publisher.Mono;
32+
33+
import java.util.ArrayList;
34+
import java.util.List;
35+
36+
public class AssistantMessageDemo {
37+
/**
38+
* The main method to demonstrate conversation AI with assistant messages and conversation history.
39+
*
40+
* @param args Input arguments (unused).
41+
*/
42+
public static void main(String[] args) {
43+
try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) {
44+
System.out.println("Demonstrating Conversation AI with Assistant Messages and Conversation History");
45+
46+
// Create a conversation history with multiple message types
47+
List<ConversationMessage> conversationHistory = new ArrayList<>();
48+
49+
// 1. System message to set context
50+
SystemMessage systemMessage = new SystemMessage(List.of(
51+
new ConversationMessageContent("You are a helpful assistant that can help with weather queries.")
52+
));
53+
systemMessage.setName("WeatherBot");
54+
conversationHistory.add(systemMessage);
55+
56+
// 2. Initial user greeting
57+
UserMessage greeting = new UserMessage(List.of(
58+
new ConversationMessageContent("Hello! I need help with weather information.")
59+
));
60+
greeting.setName("User123");
61+
conversationHistory.add(greeting);
62+
63+
// 3. Assistant response with tool call
64+
AssistantMessage assistantResponse = new AssistantMessage(
65+
List.of(new ConversationMessageContent("I'll help you with weather information. Let me check the weather for you.")),
66+
List.of(new ConversationToolCalls(
67+
new ConversationToolCallsOfFunction("get_weather", "{\"location\": \"San Francisco\", \"unit\": \"fahrenheit\"}")
68+
))
69+
);
70+
assistantResponse.setName("WeatherBot");
71+
conversationHistory.add(assistantResponse);
72+
73+
// 4. Tool response (simulating weather API response)
74+
ToolMessage toolResponse = new ToolMessage(List.of(
75+
new ConversationMessageContent("{\"temperature\": \"72F\", \"condition\": \"sunny\", \"humidity\": \"65%\"}")
76+
));
77+
toolResponse.setName("weather_api");
78+
conversationHistory.add(toolResponse);
79+
80+
// 5. Current user question
81+
UserMessage currentQuestion = new UserMessage(List.of(
82+
new ConversationMessageContent("Based on that weather data, should I wear a jacket today?")
83+
));
84+
currentQuestion.setName("User123");
85+
conversationHistory.add(currentQuestion);
86+
87+
// Create conversation input with the full history
88+
ConversationInputAlpha2 conversationInput = new ConversationInputAlpha2(conversationHistory);
89+
conversationInput.setScrubPii(false);
90+
91+
// Create the conversation request
92+
ConversationRequestAlpha2 request = new ConversationRequestAlpha2("echo", List.of(conversationInput))
93+
.setContextId("assistant-demo-context")
94+
.setTemperature(0.8d);
95+
96+
// Send the request
97+
System.out.println("Sending conversation with assistant messages and history...");
98+
System.out.println("Conversation includes:");
99+
System.out.println("- System message (context setting)");
100+
System.out.println("- User greeting");
101+
System.out.println("- Assistant response with tool call");
102+
System.out.println("- Tool response with weather data");
103+
System.out.println("- User follow-up question");
104+
105+
Mono<ConversationResponseAlpha2> responseMono = client.converseAlpha2(request);
106+
ConversationResponseAlpha2 response = responseMono.block();
107+
108+
// Process and display the response
109+
if (response != null && response.getOutputs() != null && !response.getOutputs().isEmpty()) {
110+
ConversationResultAlpha2 result = response.getOutputs().get(0);
111+
if (result.getChoices() != null && !result.getChoices().isEmpty()) {
112+
ConversationResultChoices choice = result.getChoices().get(0);
113+
114+
if (choice.getMessage() != null && choice.getMessage().getContent() != null) {
115+
System.out.printf("Assistant Response: %s%n", choice.getMessage().getContent());
116+
}
117+
118+
// Check for additional tool calls in the response
119+
if (choice.getMessage() != null && choice.getMessage().getToolCalls() != null) {
120+
System.out.println("Assistant requested additional tool calls:");
121+
choice.getMessage().getToolCalls().forEach(toolCall -> {
122+
System.out.printf("Tool: %s, Arguments: %s%n",
123+
toolCall.getFunction().getName(),
124+
toolCall.getFunction().getArguments());
125+
});
126+
}
127+
}
128+
}
129+
130+
System.out.println("Assistant message demonstration completed.");
131+
132+
} catch (Exception e) {
133+
throw new RuntimeException(e);
134+
}
135+
}
136+
}

examples/src/main/java/io/dapr/examples/conversation/DemoConversationAI.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

examples/src/main/java/io/dapr/examples/conversation/README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,51 @@ Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initi
4545
### Running the example
4646

4747
This example uses the Java SDK Dapr client in order to **Converse** with an LLM.
48-
`DemoConversationAI.java` is the example class demonstrating these features.
48+
`UserMessageDemo.java` is the example class demonstrating these features.
4949
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.
5050

5151
```java
52-
public class DemoConversationAI {
52+
public class UserMessageDemo {
5353
/**
5454
* The main method to start the client.
5555
*
5656
* @param args Input arguments (unused).
5757
*/
5858
public static void main(String[] args) {
59-
try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) {
59+
Map<Property<?>, String> overrides = Map.of(
60+
Properties.HTTP_PORT, "3500",
61+
Properties.GRPC_PORT, "50001"
62+
);
63+
64+
try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) {
6065
System.out.println("Sending the following input to LLM: Hello How are you? This is the my number 672-123-4567");
6166

62-
ConversationInput daprConversationInput = new ConversationInput("Hello How are you? "
63-
+ "This is the my number 672-123-4567");
67+
// Create user message with content
68+
UserMessage userMessage = new UserMessage(List.of(new ConversationMessageContent("Hello How are you? "
69+
+ "This is the my number 672-123-4567")));
70+
71+
// Create conversation input with the user message
72+
ConversationInputAlpha2 daprConversationInput = new ConversationInputAlpha2(List.of(userMessage));
6473

6574
// Component name is the name provided in the metadata block of the conversation.yaml file.
66-
Mono<ConversationResponse> responseMono = client.converse(new ConversationRequest("echo",
75+
Mono<ConversationResponseAlpha2> responseMono = client.converseAlpha2(new ConversationRequestAlpha2("echo",
6776
List.of(daprConversationInput))
6877
.setContextId("contextId")
69-
.setScrubPii(true).setTemperature(1.1d));
70-
ConversationResponse response = responseMono.block();
71-
System.out.printf("Conversation output: %s", response.getConversationOutpus().get(0).getResult());
78+
.setScrubPii(true)
79+
.setTemperature(1.1d));
80+
81+
ConversationResponseAlpha2 response = responseMono.block();
82+
83+
// Extract and print the conversation result
84+
if (response != null && response.getOutputs() != null && !response.getOutputs().isEmpty()) {
85+
ConversationResultAlpha2 result = response.getOutputs().get(0);
86+
if (result.getChoices() != null && !result.getChoices().isEmpty()) {
87+
ConversationResultChoices choice = result.getChoices().get(0);
88+
if (choice.getMessage() != null && choice.getMessage().getContent() != null) {
89+
System.out.printf("Conversation output: %s", choice.getMessage().getContent());
90+
}
91+
}
92+
}
7293
} catch (Exception e) {
7394
throw new RuntimeException(e);
7495
}
@@ -88,7 +109,7 @@ sleep: 10
88109
-->
89110

90111
```bash
91-
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
112+
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.UserMessageDemo
92113
```
93114

94115
<!-- END_STEP -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.conversation;
15+
16+
import io.dapr.client.DaprClientBuilder;
17+
import io.dapr.client.DaprPreviewClient;
18+
import io.dapr.client.domain.ConversationInputAlpha2;
19+
import io.dapr.client.domain.ConversationMessageContent;
20+
import io.dapr.client.domain.ConversationRequestAlpha2;
21+
import io.dapr.client.domain.ConversationResponseAlpha2;
22+
import io.dapr.client.domain.ConversationResultAlpha2;
23+
import io.dapr.client.domain.ConversationResultChoices;
24+
import io.dapr.client.domain.ConversationTools;
25+
import io.dapr.client.domain.ConversationToolsFunction;
26+
import io.dapr.client.domain.SystemMessage;
27+
import io.dapr.client.domain.UserMessage;
28+
import reactor.core.publisher.Mono;
29+
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
public class ToolsCallDemo {
35+
/**
36+
* The main method to demonstrate conversation AI with tools/function calling.
37+
*
38+
* @param args Input arguments (unused).
39+
*/
40+
public static void main(String[] args) {
41+
try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) {
42+
System.out.println("Demonstrating Conversation AI with Tools/Function Calling");
43+
44+
// Create system message to set context
45+
SystemMessage systemMessage = new SystemMessage(List.of(
46+
new ConversationMessageContent("You are a helpful weather assistant. Use the provided tools to get weather information.")
47+
));
48+
49+
// Create user message asking for weather
50+
UserMessage userMessage = new UserMessage(List.of(
51+
new ConversationMessageContent("What's the weather like in San Francisco?")
52+
));
53+
54+
// Create conversation input with messages
55+
ConversationInputAlpha2 conversationInput = new ConversationInputAlpha2(List.of(systemMessage, userMessage));
56+
57+
// Define function parameters for the weather tool
58+
Map<String, Object> functionParams = new HashMap<>();
59+
functionParams.put("location", "string");
60+
functionParams.put("unit", "string");
61+
62+
// Create the weather function definition
63+
ConversationToolsFunction weatherFunction = new ConversationToolsFunction("get_current_weather", functionParams);
64+
weatherFunction.setDescription("Get the current weather for a specified location");
65+
66+
// Create the tool wrapper
67+
ConversationTools weatherTool = new ConversationTools(weatherFunction);
68+
69+
// Create the conversation request with tools
70+
ConversationRequestAlpha2 request = new ConversationRequestAlpha2("echo", List.of(conversationInput))
71+
.setContextId("weather-demo-context")
72+
.setTemperature(0.7d)
73+
.setTools(List.of(weatherTool));
74+
75+
// Send the request
76+
System.out.println("Sending request to AI with weather tool available...");
77+
Mono<ConversationResponseAlpha2> responseMono = client.converseAlpha2(request);
78+
ConversationResponseAlpha2 response = responseMono.block();
79+
80+
// Process and display the response
81+
if (response != null && response.getOutputs() != null && !response.getOutputs().isEmpty()) {
82+
ConversationResultAlpha2 result = response.getOutputs().get(0);
83+
if (result.getChoices() != null && !result.getChoices().isEmpty()) {
84+
ConversationResultChoices choice = result.getChoices().get(0);
85+
86+
// Check if the AI wants to call a tool
87+
if (choice.getMessage() != null && choice.getMessage().getToolCalls() != null) {
88+
System.out.println("AI requested to call tools:");
89+
choice.getMessage().getToolCalls().forEach(toolCall -> {
90+
System.out.printf("Tool: %s, Arguments: %s%n",
91+
toolCall.getFunction().getName(),
92+
toolCall.getFunction().getArguments());
93+
});
94+
}
95+
96+
// Display the message content if available
97+
if (choice.getMessage() != null && choice.getMessage().getContent() != null) {
98+
System.out.printf("AI Response: %s%n", choice.getMessage().getContent());
99+
}
100+
}
101+
}
102+
103+
System.out.println("Tools call demonstration completed.");
104+
105+
} catch (Exception e) {
106+
throw new RuntimeException(e);
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)