|
| 1 | +import * as fs from "fs" |
| 2 | +import {ChatMessage} from "@cdklabs/generative-ai-cdk-constructs/lib/cdk-lib/bedrock" |
| 3 | +import {Construct} from "constructs" |
| 4 | + |
| 5 | +export type BedrockPromptSettingsType = "system" | "user" | "reformulation" |
| 6 | + |
| 7 | +export interface BedrockPromptInferenceConfig { |
| 8 | + temperature: number, |
| 9 | + topP: number, |
| 10 | + maxTokens: number, |
| 11 | + stopSequences: Array<string> |
| 12 | +} |
| 13 | + |
| 14 | +/** BedrockPromptSettings is responsible for loading and providing |
| 15 | + * the system, user, and reformulation prompts along with their |
| 16 | + * inference configurations. |
| 17 | + */ |
| 18 | +export class BedrockPromptSettings extends Construct { |
| 19 | + public readonly systemPrompt: ChatMessage |
| 20 | + public readonly userPrompt: ChatMessage |
| 21 | + public readonly reformulationPrompt: ChatMessage |
| 22 | + public readonly inferenceConfig: BedrockPromptInferenceConfig |
| 23 | + |
| 24 | + /** |
| 25 | + * @param scope The Construct scope |
| 26 | + * @param id The Construct ID |
| 27 | + * @param props BedrockPromptSettingsProps containing optional version info for each prompt |
| 28 | + */ |
| 29 | + constructor(scope: Construct, id: string) { |
| 30 | + super(scope, id) |
| 31 | + |
| 32 | + const systemPromptData = this.getTypedPrompt("system") |
| 33 | + this.systemPrompt = ChatMessage.assistant(systemPromptData.text) |
| 34 | + |
| 35 | + const userPromptData = this.getTypedPrompt("user") |
| 36 | + this.userPrompt = ChatMessage.user(userPromptData.text) |
| 37 | + |
| 38 | + const reformulationPrompt = this.getTypedPrompt("reformulation") |
| 39 | + this.reformulationPrompt = ChatMessage.user(reformulationPrompt.text) |
| 40 | + |
| 41 | + const temperature = this.node.tryGetContext("ragTemperature") |
| 42 | + const maxTokens = this.node.tryGetContext("ragMaxTokens") |
| 43 | + const topP = this.node.tryGetContext("ragTopP") |
| 44 | + |
| 45 | + this.inferenceConfig = { |
| 46 | + temperature: parseInt(temperature, 10), |
| 47 | + topP: parseInt(topP, 10), |
| 48 | + maxTokens: parseInt(maxTokens, 10), |
| 49 | + stopSequences: [ |
| 50 | + "Human:" |
| 51 | + ] |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** Get the latest prompt text from files in the specified directory. |
| 56 | + * If a version is provided, it retrieves that specific version. |
| 57 | + * Otherwise, it retrieves the latest version based on file naming. |
| 58 | + * |
| 59 | + * @param type The type of prompt (system, user, reformulation) |
| 60 | + * @returns An object containing the prompt text and filename |
| 61 | + */ |
| 62 | + private getTypedPrompt(type: BedrockPromptSettingsType) |
| 63 | + : { text: string; filename: string } { |
| 64 | + // Read all files in the folder |
| 65 | + const files = fs |
| 66 | + .readdirSync("../../../prompts") |
| 67 | + .filter(f => f.startsWith(`${type}_v`) && f.endsWith(".txt")) |
| 68 | + |
| 69 | + if (files.length === 0) { |
| 70 | + throw new Error("No variant files found in the folder.") |
| 71 | + } |
| 72 | + |
| 73 | + const file = files.find(file => file.startsWith(`${type}Prompt`))! |
| 74 | + |
| 75 | + const text = fs.readFileSync(file, "utf-8") |
| 76 | + |
| 77 | + return {text, filename: file} |
| 78 | + } |
| 79 | +} |
0 commit comments