Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jul 30, 2024
1 parent 08ba362 commit 994c13c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
67 changes: 52 additions & 15 deletions libs/langchain-scripts/src/cli/docs/chat.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -62,37 +67,59 @@ type ExtraFields = {

async function promptExtraFields(): Promise<ExtraFields> {
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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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`
);
}
30 changes: 28 additions & 2 deletions libs/langchain-scripts/src/cli/utils/get-input.ts
Original file line number Diff line number Diff line change
@@ -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<string>} The user input.
*/
export async function getUserInput(question: string): Promise<string> {
export async function getUserInput(
question: string,
color?: Color,
bold?: boolean
): Promise<string> {
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);
});
Expand Down

0 comments on commit 994c13c

Please sign in to comment.