|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.utils.message; |
| 7 | + |
| 8 | +import static org.opensearch.common.xcontent.json.JsonXContent.jsonXContent; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 17 | +import org.opensearch.ml.common.transport.memorycontainer.memory.MessageInput; |
| 18 | +import org.opensearch.ml.common.utils.StringUtils; |
| 19 | + |
| 20 | +import lombok.extern.log4j.Log4j2; |
| 21 | + |
| 22 | +/** |
| 23 | + * Message formatter for Claude models (and similar models using system_prompt parameter). |
| 24 | + * |
| 25 | + * <p>Format characteristics: |
| 26 | + * <ul> |
| 27 | + * <li>System prompt: Placed in "system_prompt" parameter</li> |
| 28 | + * <li>Messages: Array of user/assistant messages (NO system role)</li> |
| 29 | + * <li>Content: Normalized to have "type" field</li> |
| 30 | + * </ul> |
| 31 | + * |
| 32 | + * <p>Compatible with: |
| 33 | + * <ul> |
| 34 | + * <li>Claude 3.x (Bedrock, Anthropic API)</li> |
| 35 | + * <li>Claude 4.x (Sonnet, Opus)</li> |
| 36 | + * <li>Any model with system_prompt in input schema</li> |
| 37 | + * </ul> |
| 38 | + * |
| 39 | + * <p>Example output: |
| 40 | + * <pre> |
| 41 | + * { |
| 42 | + * "system_prompt": "You are a helpful assistant", |
| 43 | + * "messages": "[{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"Hello\"}]}]" |
| 44 | + * } |
| 45 | + * </pre> |
| 46 | + */ |
| 47 | +@Log4j2 |
| 48 | +public class ClaudeMessageFormatter implements MessageFormatter { |
| 49 | + |
| 50 | + @Override |
| 51 | + public Map<String, String> formatRequest(String systemPrompt, List<MessageInput> messages, Map<String, Object> additionalConfig) { |
| 52 | + Map<String, String> parameters = new HashMap<>(); |
| 53 | + |
| 54 | + // Claude-style: system_prompt as parameter |
| 55 | + if (systemPrompt != null && !systemPrompt.isBlank()) { |
| 56 | + parameters.put("system_prompt", systemPrompt); |
| 57 | + log.debug("System prompt added as parameter"); |
| 58 | + } |
| 59 | + |
| 60 | + // Build messages array with content processing |
| 61 | + try { |
| 62 | + String messagesJson = buildMessagesArray(messages, additionalConfig); |
| 63 | + parameters.put("messages", messagesJson); |
| 64 | + log.debug("Built messages array with {} messages", messages != null ? messages.size() : 0); |
| 65 | + } catch (IOException e) { |
| 66 | + log.error("Failed to build messages array", e); |
| 67 | + throw new RuntimeException("Failed to format Claude request", e); |
| 68 | + } |
| 69 | + |
| 70 | + return parameters; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public List<Map<String, Object>> processContent(List<Map<String, Object>> content) { |
| 75 | + if (content == null || content.isEmpty()) { |
| 76 | + return content; |
| 77 | + } |
| 78 | + |
| 79 | + return content.stream().map(this::normalizeContentObject).collect(Collectors.toList()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Normalize a single content object to ensure it has "type" field. |
| 84 | + * |
| 85 | + * <p>Rules: |
| 86 | + * <ul> |
| 87 | + * <li>If object has "type" field → return as-is (standard LLM format)</li> |
| 88 | + * <li>If object lacks "type" field → wrap as {"type": "text", "text": JSON_STRING}</li> |
| 89 | + * </ul> |
| 90 | + * |
| 91 | + * @param obj Content object to normalize |
| 92 | + * @return Normalized content object with "type" field |
| 93 | + */ |
| 94 | + private Map<String, Object> normalizeContentObject(Map<String, Object> obj) { |
| 95 | + if (obj == null || obj.isEmpty()) { |
| 96 | + return obj; |
| 97 | + } |
| 98 | + |
| 99 | + // Already has type field → standard format |
| 100 | + if (obj.containsKey("type")) { |
| 101 | + return obj; |
| 102 | + } |
| 103 | + |
| 104 | + // No type field → user-defined object, wrap as text |
| 105 | + Map<String, Object> wrapped = new HashMap<>(); |
| 106 | + wrapped.put("type", "text"); |
| 107 | + String jsonText = StringUtils.toJson(obj); |
| 108 | + wrapped.put("text", jsonText); |
| 109 | + return wrapped; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Build messages JSON array from MessageInput list. |
| 114 | + * |
| 115 | + * <p>Includes: |
| 116 | + * <ul> |
| 117 | + * <li>Optional system_prompt_message from config (added first)</li> |
| 118 | + * <li>User/assistant messages with processed content</li> |
| 119 | + * <li>Optional user_prompt_message from config (added last)</li> |
| 120 | + * </ul> |
| 121 | + * |
| 122 | + * @param messages List of messages to include |
| 123 | + * @param additionalConfig Optional config with extra messages |
| 124 | + * @return JSON string representing messages array |
| 125 | + * @throws IOException if JSON building fails |
| 126 | + */ |
| 127 | + private String buildMessagesArray(List<MessageInput> messages, Map<String, Object> additionalConfig) throws IOException { |
| 128 | + XContentBuilder builder = jsonXContent.contentBuilder(); |
| 129 | + builder.startArray(); |
| 130 | + |
| 131 | + // Optional system_prompt_message from config |
| 132 | + if (additionalConfig != null && additionalConfig.containsKey("system_prompt_message")) { |
| 133 | + Object systemPromptMsg = additionalConfig.get("system_prompt_message"); |
| 134 | + if (systemPromptMsg instanceof Map) { |
| 135 | + @SuppressWarnings("unchecked") |
| 136 | + Map<String, Object> msgMap = (Map<String, Object>) systemPromptMsg; |
| 137 | + builder.map(msgMap); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // User messages (with content processing) |
| 142 | + if (messages != null) { |
| 143 | + for (MessageInput message : messages) { |
| 144 | + builder.startObject(); |
| 145 | + |
| 146 | + if (message.getRole() != null) { |
| 147 | + builder.field("role", message.getRole()); |
| 148 | + } |
| 149 | + |
| 150 | + // Process content to ensure type fields |
| 151 | + if (message.getContent() != null) { |
| 152 | + List<Map<String, Object>> processedContent = processContent(message.getContent()); |
| 153 | + builder.field("content", processedContent); |
| 154 | + } |
| 155 | + |
| 156 | + builder.endObject(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Optional user_prompt_message from config |
| 161 | + if (additionalConfig != null && additionalConfig.containsKey("user_prompt_message")) { |
| 162 | + Object userPromptMsg = additionalConfig.get("user_prompt_message"); |
| 163 | + if (userPromptMsg instanceof Map) { |
| 164 | + @SuppressWarnings("unchecked") |
| 165 | + Map<String, Object> msgMap = (Map<String, Object>) userPromptMsg; |
| 166 | + builder.map(msgMap); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + builder.endArray(); |
| 171 | + return builder.toString(); |
| 172 | + } |
| 173 | +} |
0 commit comments