|
| 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 | +} |
0 commit comments