|
1 | 1 | package com.javaaidev.chatagent.springai; |
2 | 2 |
|
3 | | -import com.javaaidev.chatagent.model.ChatRequest; |
| 3 | +import com.javaaidev.chatagent.model.ChatAgentRequest; |
| 4 | +import com.javaaidev.chatagent.model.ChatAgentResponse; |
4 | 5 | import com.javaaidev.chatagent.model.TextContentPart; |
5 | 6 | import com.javaaidev.chatagent.model.ThreadAssistantContentPart; |
6 | 7 | import com.javaaidev.chatagent.model.ThreadAssistantMessage; |
7 | 8 | import com.javaaidev.chatagent.model.ThreadUserMessage; |
8 | 9 | import java.util.ArrayList; |
9 | 10 | import java.util.List; |
| 11 | +import java.util.Objects; |
10 | 12 | import java.util.stream.Stream; |
11 | 13 | import org.springframework.ai.chat.messages.AssistantMessage; |
12 | 14 | import org.springframework.ai.chat.messages.Message; |
13 | 15 | import org.springframework.ai.chat.messages.UserMessage; |
14 | 16 | import org.springframework.ai.chat.model.ChatResponse; |
15 | 17 | import org.springframework.ai.chat.model.Generation; |
16 | 18 |
|
| 19 | +/** |
| 20 | + * Convert models between chat agent and Spring AI |
| 21 | + */ |
17 | 22 | public class ModelAdapter { |
18 | 23 |
|
19 | 24 | private ModelAdapter() { |
20 | 25 | } |
21 | 26 |
|
22 | | - public static List<Message> fromRequest(ChatRequest request) { |
| 27 | + /** |
| 28 | + * Convert a {@linkplain ChatAgentRequest} to a list of Spring AI {@linkplain Message} |
| 29 | + * |
| 30 | + * @param request {@linkplain ChatAgentRequest} of chat agent |
| 31 | + * @return A list of Spring AI {@linkplain Message} |
| 32 | + */ |
| 33 | + public static List<Message> fromRequest(ChatAgentRequest request) { |
23 | 34 | return request.messages().stream().flatMap(message -> { |
24 | 35 | if (message instanceof ThreadUserMessage userMessage) { |
25 | 36 | return userMessage.content().stream().map(part -> { |
26 | 37 | if (part instanceof TextContentPart textContentPart) { |
27 | 38 | return new UserMessage(textContentPart.text()); |
28 | 39 | } |
29 | 40 | return null; |
30 | | - }); |
| 41 | + }).filter(Objects::nonNull); |
31 | 42 | } else if (message instanceof ThreadAssistantMessage assistantMessage) { |
32 | 43 | return assistantMessage.content().stream().map(part -> { |
33 | 44 | if (part instanceof TextContentPart textContentPart) { |
34 | 45 | return new AssistantMessage(textContentPart.text()); |
35 | 46 | } |
36 | 47 | return null; |
37 | | - }); |
| 48 | + }).filter(Objects::nonNull); |
38 | 49 | } |
39 | 50 | return Stream.<Message>of(); |
40 | 51 | }).toList(); |
41 | 52 | } |
42 | 53 |
|
43 | | - public com.javaaidev.chatagent.model.ChatResponse toResponse(ChatResponse chatResponse) { |
| 54 | + /** |
| 55 | + * Convert a Spring AI {@linkplain ChatResponse} to {@linkplain ChatAgentResponse} |
| 56 | + * |
| 57 | + * @param chatResponse Spring AI {@linkplain ChatResponse} |
| 58 | + * @return {@linkplain ChatAgentResponse} of chat agent |
| 59 | + */ |
| 60 | + public static ChatAgentResponse toResponse(ChatResponse chatResponse) { |
44 | 61 | var content = new ArrayList<ThreadAssistantContentPart>(); |
45 | 62 | for (Generation generation : chatResponse.getResults()) { |
46 | 63 | content.add(new TextContentPart(generation.getOutput().getText())); |
47 | 64 | } |
48 | | - return new com.javaaidev.chatagent.model.ChatResponse(content); |
| 65 | + return new ChatAgentResponse(content); |
49 | 66 | } |
50 | 67 | } |
0 commit comments