|
16 | 16 |
|
17 | 17 | package org.springframework.ai.chat.cache.semantic; |
18 | 18 |
|
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.security.MessageDigest; |
| 21 | +import java.security.NoSuchAlgorithmException; |
| 22 | +import java.util.HexFormat; |
19 | 23 | import java.util.List; |
20 | 24 | import java.util.Optional; |
21 | 25 |
|
@@ -89,11 +93,12 @@ void adviseCallWithSystemPromptUsesContextHash() { |
89 | 93 | verify(this.mockCache).get(queryCaptor.capture(), hashCaptor.capture()); |
90 | 94 |
|
91 | 95 | assertThat(queryCaptor.getValue()).isEqualTo(userText); |
92 | | - assertThat(hashCaptor.getValue()).isNotNull().hasSize(8); // 8 hex chars |
| 96 | + assertThat(hashCaptor.getValue()).isNotNull().hasSize(64); // 64 hex chars (32 |
| 97 | + // bytes) |
93 | 98 |
|
94 | 99 | // Assert - verify cache.set was called with same parameters |
95 | 100 | verify(this.mockCache).set(eq(userText), eq(chatResponse), hashCaptor.capture()); |
96 | | - assertThat(hashCaptor.getValue()).isNotNull().hasSize(8); |
| 101 | + assertThat(hashCaptor.getValue()).isNotNull().hasSize(64); |
97 | 102 | } |
98 | 103 |
|
99 | 104 | @Test |
@@ -224,10 +229,38 @@ void emptySystemPromptTreatedAsNoSystemPrompt() { |
224 | 229 | this.advisor.adviseCall(request, this.mockChain); |
225 | 230 |
|
226 | 231 | // Assert - empty system prompt should result in null context hash |
227 | | - verify(this.mockCache).get(eq(userText), (String) isNull()); |
| 232 | + verify(this.mockCache).get(eq(userText), isNull()); |
228 | 233 | verify(this.mockCache).set(eq(userText), eq(chatResponse), (String) isNull()); |
229 | 234 | } |
230 | 235 |
|
| 236 | + @Test |
| 237 | + void contextHashMatchesFullSha256Digest() throws NoSuchAlgorithmException { |
| 238 | + String systemPromptText = "You are a helpful assistant."; |
| 239 | + String userText = "What is AI?"; |
| 240 | + |
| 241 | + Prompt prompt = new Prompt(List.of(new SystemMessage(systemPromptText), new UserMessage(userText))); |
| 242 | + |
| 243 | + ChatClientRequest request = ChatClientRequest.builder().prompt(prompt).build(); |
| 244 | + |
| 245 | + ChatResponse chatResponse = createMockChatResponse("AI is artificial intelligence."); |
| 246 | + ChatClientResponse clientResponse = ChatClientResponse.builder().chatResponse(chatResponse).build(); |
| 247 | + |
| 248 | + when(this.mockCache.get(eq(userText), any(String.class))).thenReturn(Optional.empty()); |
| 249 | + when(this.mockChain.nextCall(request)).thenReturn(clientResponse); |
| 250 | + |
| 251 | + this.advisor.adviseCall(request, this.mockChain); |
| 252 | + |
| 253 | + ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); |
| 254 | + verify(this.mockCache).get(eq(userText), hashCaptor.capture()); |
| 255 | + |
| 256 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 257 | + byte[] expectedHash = digest.digest(systemPromptText.getBytes(StandardCharsets.UTF_8)); |
| 258 | + String expectedHex = HexFormat.of().formatHex(expectedHash); |
| 259 | + |
| 260 | + assertThat(hashCaptor.getValue()).isEqualTo(expectedHex); |
| 261 | + assertThat(hashCaptor.getValue()).matches("^[0-9a-f]{64}$"); |
| 262 | + } |
| 263 | + |
231 | 264 | private ChatResponse createMockChatResponse(String text) { |
232 | 265 | return ChatResponse.builder().generations(List.of(new Generation(new AssistantMessage(text)))).build(); |
233 | 266 | } |
|
0 commit comments