From 994c13ce1069c8228d1854a1e86859248014b9cd Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 30 Jul 2024 13:08:01 -0700 Subject: [PATCH] cr --- libs/langchain-scripts/src/cli/docs/chat.ts | 67 ++++++++++++++----- .../src/cli/utils/get-input.ts | 30 ++++++++- 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/libs/langchain-scripts/src/cli/docs/chat.ts b/libs/langchain-scripts/src/cli/docs/chat.ts index bae144aaa6d4..c0760bb32c54 100644 --- a/libs/langchain-scripts/src/cli/docs/chat.ts +++ b/libs/langchain-scripts/src/cli/docs/chat.ts @@ -1,6 +1,11 @@ import * as path from "node:path"; import * as fs from "node:fs"; -import { getUserInput } from "../utils/get-input.js"; +import { + boldText, + getUserInput, + greenText, + redBackground, +} from "../utils/get-input.js"; const PACKAGE_NAME_PLACEHOLDER = "__package_name__"; const PACKAGE_NAME_SHORT_SNAKE_CASE_PLACEHOLDER = @@ -62,37 +67,59 @@ type ExtraFields = { async function promptExtraFields(): Promise { const hasToolCalling = await getUserInput( - "Does the tool support tool calling? (y/n) " + "Does the tool support tool calling? (y/n) ", + undefined, + true ); const hasJsonMode = await getUserInput( - "Does the tool support JSON mode? (y/n) " + "Does the tool support JSON mode? (y/n) ", + undefined, + true ); const hasImageInput = await getUserInput( - "Does the tool support image input? (y/n) " + "Does the tool support image input? (y/n) ", + undefined, + true ); const hasAudioInput = await getUserInput( - "Does the tool support audio input? (y/n) " + "Does the tool support audio input? (y/n) ", + undefined, + true ); const hasVideoInput = await getUserInput( - "Does the tool support video input? (y/n) " + "Does the tool support video input? (y/n) ", + undefined, + true ); const hasTokenLevelStreaming = await getUserInput( - "Does the tool support token level streaming? (y/n) " + "Does the tool support token level streaming? (y/n) ", + undefined, + true ); const hasTokenUsage = await getUserInput( - "Does the tool support token usage? (y/n) " + "Does the tool support token usage? (y/n) ", + undefined, + true ); const hasLogprobs = await getUserInput( - "Does the tool support logprobs? (y/n) " + "Does the tool support logprobs? (y/n) ", + undefined, + true ); const hasLocal = await getUserInput( - "Does the tool support local usage? (y/n) " + "Does the tool support local usage? (y/n) ", + undefined, + true ); const hasSerializable = await getUserInput( - "Does the tool support serializable output? (y/n) " + "Does the tool support serializable output? (y/n) ", + undefined, + true ); const hasPySupport = await getUserInput( - "Does the tool support Python support? (y/n) " + "Does the tool support Python support? (y/n) ", + undefined, + true ); return { @@ -117,7 +144,8 @@ export async function fillChatIntegrationDocTemplate(fields: { // Ask the user if they'd like to fill in extra fields, if so, prompt them. let extraFields: ExtraFields | undefined; const shouldPromptExtraFields = await getUserInput( - "Would you like to fill out optional fields? (y/n) " + "Would you like to fill out optional fields? (y/n) ", + "white_background" ); if (shouldPromptExtraFields.toLowerCase() === "y") { extraFields = await promptExtraFields(); @@ -182,9 +210,18 @@ export async function fillChatIntegrationDocTemplate(fields: { `${packageNameShortSnakeCase}.ipynb` ); await fs.promises.writeFile(docPath, docTemplate); + const prettyDocPath = docPath.split("docs/core_docs/")[1]; + + const updatePythonDocUrlText = ` ${redBackground( + "- Update the Python documentation URL with the proper URL." + )}`; + const successText = `\nSuccessfully created new chat model integration doc at ${prettyDocPath}.`; console.log( - `Successfully created new chat model integration doc at ${docPath}.\n -Please run the cells in the doc to record the outputs, and replace the Python documentation support URL with the proper URL.` + `${greenText(successText)}\n +${boldText("Next steps:")} +${extraFields?.pySupport ? updatePythonDocUrlText : ""} + - Run all code cells in the generated doc to record the outputs. + - Add extra sections on integration specific features.\n` ); } diff --git a/libs/langchain-scripts/src/cli/utils/get-input.ts b/libs/langchain-scripts/src/cli/utils/get-input.ts index f8bca08bf593..0f753dd807ea 100644 --- a/libs/langchain-scripts/src/cli/utils/get-input.ts +++ b/libs/langchain-scripts/src/cli/utils/get-input.ts @@ -1,19 +1,45 @@ import * as readline from "readline"; +type Color = "green" | "red_background" | "white_background"; + +export const greenText = (text: string) => `\x1b[1m\x1b[92m${text}\x1b[0m`; +export const boldText = (text: string) => `\x1b[1m${text}\x1b[0m`; +export const redBackground = (text: string) => `\x1b[41m\x1b[37m${text}\x1b[0m`; +export const whiteBackground = (text: string) => + `\x1b[30m\x1b[47m${text}\x1b[0m`; + /** * Prompts the user with a question and returns the user input. * * @param {string} question The question to log to the users terminal. + * @param {Color | undefined} color The color to use for the question. + * @param {boolean | undefined} bold Whether to make the question bold. * @returns {Promise} The user input. */ -export async function getUserInput(question: string): Promise { +export async function getUserInput( + question: string, + color?: Color, + bold?: boolean +): Promise { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); + let questionWithStyling = question; + if (bold) { + questionWithStyling = boldText(questionWithStyling); + } + if (color === "green") { + questionWithStyling = greenText(questionWithStyling); + } else if (color === "red_background") { + questionWithStyling = redBackground(questionWithStyling); + } else if (color === "white_background") { + questionWithStyling = whiteBackground(questionWithStyling); + } + return new Promise((resolve) => { - rl.question(`\x1b[30m\x1b[47m${question}\x1b[0m`, (input) => { + rl.question(questionWithStyling, (input) => { rl.close(); resolve(input); });