|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Data models for Dotprompt prompts, messages, and content parts. |
| 21 | + * |
| 22 | + * <p>This package contains the core data structures used throughout Dotprompt for representing |
| 23 | + * prompts, rendered messages, and their content. These models are designed to be immutable records |
| 24 | + * for thread-safety and ease of use. |
| 25 | + * |
| 26 | + * <h2>Type Hierarchy</h2> |
| 27 | + * |
| 28 | + * <pre> |
| 29 | + * ┌─────────────────────────┐ |
| 30 | + * │ Part │ (sealed interface) |
| 31 | + * │ Base for all content │ |
| 32 | + * └───────────┬─────────────┘ |
| 33 | + * │ |
| 34 | + * ┌───────────────┬───────────┼───────────┬─────────────────┐ |
| 35 | + * │ │ │ │ │ |
| 36 | + * ▼ ▼ ▼ ▼ ▼ |
| 37 | + * ┌──────────┐ ┌───────────┐ ┌──────────┐ ┌────────────┐ ┌────────────────┐ |
| 38 | + * │ TextPart │ │ MediaPart │ │ DataPart │ │ ToolRequest│ │ ToolResponse │ |
| 39 | + * │ │ │ │ │ │ │ Part │ │ Part │ |
| 40 | + * └──────────┘ └───────────┘ └──────────┘ └────────────┘ └────────────────┘ |
| 41 | + * │ │ │ │ │ |
| 42 | + * │ ▼ │ │ │ |
| 43 | + * │ ┌───────────┐ │ │ │ |
| 44 | + * │ │MediaContent│ │ │ │ |
| 45 | + * │ │ url, │ │ │ │ |
| 46 | + * │ │ contentType│ │ │ │ |
| 47 | + * │ └───────────┘ │ │ │ |
| 48 | + * │ │ │ │ |
| 49 | + * └─────────────┬──────────────┴─────────────┴───────────────┘ |
| 50 | + * │ |
| 51 | + * ▼ |
| 52 | + * ┌──────────┐ |
| 53 | + * │ Message │ (role + List<Part> + metadata) |
| 54 | + * └────┬─────┘ |
| 55 | + * │ |
| 56 | + * ▼ |
| 57 | + * ┌───────────────┐ |
| 58 | + * │RenderedPrompt│ (config + List<Message>) |
| 59 | + * └───────────────┘ |
| 60 | + * </pre> |
| 61 | + * |
| 62 | + * <h2>Core Types</h2> |
| 63 | + * |
| 64 | + * <h3>Content Parts</h3> |
| 65 | + * |
| 66 | + * <ul> |
| 67 | + * <li>{@link com.google.dotprompt.models.TextPart} - Plain text content |
| 68 | + * <li>{@link com.google.dotprompt.models.MediaPart} - Images, audio, video via URL |
| 69 | + * <li>{@link com.google.dotprompt.models.DataPart} - Structured data (JSON) |
| 70 | + * <li>{@link com.google.dotprompt.models.ToolRequestPart} - Tool/function call requests |
| 71 | + * <li>{@link com.google.dotprompt.models.ToolResponsePart} - Tool/function call responses |
| 72 | + * </ul> |
| 73 | + * |
| 74 | + * <h3>Messages and Prompts</h3> |
| 75 | + * |
| 76 | + * <ul> |
| 77 | + * <li>{@link com.google.dotprompt.models.Message} - A message with role, parts, and metadata |
| 78 | + * <li>{@link com.google.dotprompt.models.Prompt} - Parsed template with config |
| 79 | + * <li>{@link com.google.dotprompt.models.RenderedPrompt} - Fully rendered prompt ready for LLM |
| 80 | + * <li>{@link com.google.dotprompt.models.PromptFunction} - Compiled prompt for repeated use |
| 81 | + * </ul> |
| 82 | + * |
| 83 | + * <h3>Storage Types</h3> |
| 84 | + * |
| 85 | + * <ul> |
| 86 | + * <li>{@link com.google.dotprompt.models.PromptData} - Raw prompt data from storage |
| 87 | + * <li>{@link com.google.dotprompt.models.PromptRef} - Reference to a stored prompt |
| 88 | + * <li>{@link com.google.dotprompt.models.PartialData} - Raw partial data |
| 89 | + * <li>{@link com.google.dotprompt.models.PartialRef} - Reference to a stored partial |
| 90 | + * </ul> |
| 91 | + * |
| 92 | + * <h3>Roles</h3> |
| 93 | + * |
| 94 | + * <p>The {@link com.google.dotprompt.models.Role} enum defines message roles: |
| 95 | + * |
| 96 | + * <pre> |
| 97 | + * ┌────────┬────────────────────────────────────────────┐ |
| 98 | + * │ Role │ Description │ |
| 99 | + * ├────────┼────────────────────────────────────────────┤ |
| 100 | + * │ USER │ Input from the user │ |
| 101 | + * │ MODEL │ Output from the AI model │ |
| 102 | + * │ SYSTEM │ System instructions │ |
| 103 | + * │ TOOL │ Tool/function call responses │ |
| 104 | + * └────────┴────────────────────────────────────────────┘ |
| 105 | + * </pre> |
| 106 | + * |
| 107 | + * <h2>Usage Example</h2> |
| 108 | + * |
| 109 | + * <pre>{@code |
| 110 | + * // Create a message with mixed content |
| 111 | + * Message message = new Message( |
| 112 | + * Role.USER, |
| 113 | + * List.of( |
| 114 | + * new TextPart("Describe this image:"), |
| 115 | + * new MediaPart(new MediaContent("https://example.com/photo.jpg", "image/jpeg")) |
| 116 | + * ), |
| 117 | + * Map.of() |
| 118 | + * ); |
| 119 | + * |
| 120 | + * // Work with a rendered prompt |
| 121 | + * RenderedPrompt rendered = promptFn.render(data).get(); |
| 122 | + * for (Message msg : rendered.messages()) { |
| 123 | + * System.out.println(msg.role() + ": " + msg.content()); |
| 124 | + * } |
| 125 | + * }</pre> |
| 126 | + * |
| 127 | + * @see com.google.dotprompt.models.Part |
| 128 | + * @see com.google.dotprompt.models.Message |
| 129 | + * @see com.google.dotprompt.models.RenderedPrompt |
| 130 | + */ |
| 131 | +package com.google.dotprompt.models; |
0 commit comments