From 3c6ecae46466f3fb9eb1dd4f35881941c25d5b9a Mon Sep 17 00:00:00 2001 From: Dennis Traub Date: Mon, 18 Mar 2024 12:49:15 +0100 Subject: [PATCH] Replace custom input handling with scenario tools --- .../tools/foundation_models.js | 86 --------------- .../bedrock-runtime/tools/user_input.js | 101 ------------------ 2 files changed, 187 deletions(-) delete mode 100644 javascriptv3/example_code/bedrock-runtime/tools/foundation_models.js delete mode 100644 javascriptv3/example_code/bedrock-runtime/tools/user_input.js diff --git a/javascriptv3/example_code/bedrock-runtime/tools/foundation_models.js b/javascriptv3/example_code/bedrock-runtime/tools/foundation_models.js deleted file mode 100644 index a547c4df992..00000000000 --- a/javascriptv3/example_code/bedrock-runtime/tools/foundation_models.js +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -export const FoundationModels = Object.freeze({ - CLAUDE_3_HAIKU: { - modelId: "anthropic.claude-3-haiku-20240307-v1:0", - modelName: "Anthropic Claude 3 Haiku", - module: () => import("../models/anthropic_claude/claude_3.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - CLAUDE_3_SONNET: { - modelId: "anthropic.claude-3-sonnet-20240229-v1:0", - modelName: "Anthropic Claude 3 Sonnet", - module: () => import("../models/anthropic_claude/claude_3.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - CLAUDE_2_1: { - modelId: "anthropic.claude-v2:1", - modelName: "Anthropic Claude 2.1", - module: () => import("../models/anthropic_claude/claude_2.js"), - invoker: (/** @type {Function} */ module) => - module.invokeTextCompletionsApi, - }, - CLAUDE_2: { - modelId: "anthropic.claude-v2", - modelName: "Anthropic Claude 2.0", - module: () => import("../models/anthropic_claude/claude_2.js"), - invoker: (/** @type {Function} */ module) => - module.invokeTextCompletionsApi, - }, - CLAUDE_INSTANT: { - modelId: "anthropic.claude-instant-v1", - modelName: "Anthropic Claude Instant", - module: () => import("../models/anthropic_claude/claude_instant_1.js"), - invoker: (/** @type {Function} */ module) => - module.invokeTextCompletionsApi, - }, - JURASSIC2_MID: { - modelId: "ai21.j2-mid-v1", - modelName: "Jurassic-2 Mid", - module: () => import("../models/ai21_labs_jurassic2/jurassic2.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - JURASSIC2_ULTRA: { - modelId: "ai21.j2-ultra-v1", - modelName: "Jurassic-2 Ultra", - module: () => import("../models/ai21_labs_jurassic2/jurassic2.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - LLAMA2_CHAT_13B: { - modelId: "meta.llama2-13b-chat-v1", - modelName: "Llama 2 Chat 13B", - module: () => import("../models/meta_llama2/llama2_chat.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - LLAMA2_CHAT_70B: { - modelId: "meta.llama2-70b-chat-v1", - modelName: "Llama 2 Chat 70B", - module: () => import("../models/meta_llama2/llama2_chat.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - MISTRAL_7B: { - modelId: "mistral.mistral-7b-instruct-v0:2", - modelName: "Mistral 7B Instruct", - module: () => import("../models/mistral_ai/mistral_7b.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - MIXTRAL_8X7B: { - modelId: "mistral.mixtral-8x7b-instruct-v0:1", - modelName: "Mixtral 8X7B Instruct", - module: () => import("../models/mistral_ai/mixtral_8x7b.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - TITAN_TEXT_G1_EXPRESS: { - modelId: "amazon.titan-text-express-v1", - modelName: "Titan Text G1 - Express", - module: () => import("../models/amazon_titan/titan_text.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, - TITAN_TEXT_G1_LITE: { - modelId: "amazon.titan-text-lite-v1", - modelName: "Titan Text G1 - Lite", - module: () => import("../models/amazon_titan/titan_text.js"), - invoker: (/** @type {Function} */ module) => module.invokeModel, - }, -}); diff --git a/javascriptv3/example_code/bedrock-runtime/tools/user_input.js b/javascriptv3/example_code/bedrock-runtime/tools/user_input.js deleted file mode 100644 index 691b483680b..00000000000 --- a/javascriptv3/example_code/bedrock-runtime/tools/user_input.js +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -import * as readline from "readline"; - -/** - * @typedef {Object} FoundationModel - * @property {string} modelName - The name of the model - */ - -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, -}); - -const ask = (question, validate, onValid, repeat) => { - if (repeat) { - // Overwrite previous line - readline.moveCursor(process.stdout, 0, -1); - readline.clearLine(process.stdout, 0); - readline.cursorTo(process.stdout, 0); - } - - rl.question(question, (answer) => { - if (validate(answer)) { - onValid(answer); - } else { - ask(question, validate, onValid, true); - } - }); -}; - -const select = ( - /** @type {string[]} options */ options, - text, - validate, - onValid, -) => { - // Print the options - options.forEach((option, index) => { - console.log(`${index + 1}. ${option}`); - }); - - ask(text, validate, onValid, false); -}; - -export const selectModel = (/** @type {FoundationModel[]} */ models) => { - return new Promise((resolve) => { - const validate = (answer) => { - if (answer === "q") return true; - else { - const selectedIndex = parseInt(answer, 10) - 1; - return selectedIndex >= 0 && selectedIndex < models.length; - } - }; - - const onValid = (answer) => { - if (answer === "q") { - rl.close(); - resolve(null); - } else { - resolve(models[parseInt(answer, 10) - 1]); - } - }; - - const text = "Select a model number (q to quit): "; - select( - models.map((m) => m.modelName), - text, - validate, - onValid, - ); - }); -}; - -export const selectNextStep = (/** @type {string} */ modelName) => { - return new Promise((resolve) => { - const options = [`Prompt ${modelName} again`, "Select another model"]; - const text = "Choose your next step (q to quit): "; - - const validate = (answer) => ["1", "2", "q"].includes(answer); - const onValid = (answer) => { - if (answer === "q") { - rl.close(); - resolve(null); - } else { - resolve(answer); - } - }; - - select(options, text, validate, onValid); - }); -}; - -export const askForPrompt = () => { - return new Promise((resolve) => { - const validate = (/** @type {string} */ answer) => answer.trim() !== ""; - const onValid = (answer) => resolve(answer); - ask("Now, enter your prompt: ", validate, onValid); - }); -};