Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts[patch]: Update chat model template cli to support community integrations #6299

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions libs/langchain-scripts/src/cli/docs/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const PACKAGE_NAME_SHORT_SNAKE_CASE_PLACEHOLDER =
"__package_name_short_snake_case__";
const PACKAGE_NAME_SNAKE_CASE_PLACEHOLDER = "__package_name_snake_case__";
const PACKAGE_NAME_PRETTY_PLACEHOLDER = "__package_name_pretty__";
const PACKAGE_IMPORT_PATH_PLACEHOLDER = "__import_path__";
const MODULE_NAME_PLACEHOLDER = "__ModuleName__";
// This should not be prefixed with `Chat` as it's used for API keys.
const MODULE_NAME_ALL_CAPS_PLACEHOLDER = "__MODULE_NAME_ALL_CAPS__";
Expand All @@ -31,6 +32,7 @@ const PY_SUPPORT_PLACEHOLDER = "__py_support__";

const API_REF_BASE_PACKAGE_URL = `https://api.js.langchain.com/modules/langchain_${PACKAGE_NAME_PLACEHOLDER}.html`;
const API_REF_BASE_MODULE_URL = `https://api.js.langchain.com/classes/langchain_${PACKAGE_NAME_PLACEHOLDER}.${MODULE_NAME_PLACEHOLDER}.html`;

const TEMPLATE_PATH = path.resolve("./src/cli/docs/templates/chat.ipynb");
const INTEGRATIONS_DOCS_PATH = path.resolve(
"../../docs/core_docs/docs/integrations/chat"
Expand Down Expand Up @@ -140,6 +142,7 @@ async function promptExtraFields(): Promise<ExtraFields> {
export async function fillChatIntegrationDocTemplate(fields: {
packageName: string;
moduleName: string;
isCommunity: boolean;
}) {
// Ask the user if they'd like to fill in extra fields, if so, prompt them.
let extraFields: ExtraFields | undefined;
Expand All @@ -151,14 +154,27 @@ export async function fillChatIntegrationDocTemplate(fields: {
extraFields = await promptExtraFields();
}

const formattedApiRefPackageUrl = API_REF_BASE_PACKAGE_URL.replace(
PACKAGE_NAME_PLACEHOLDER,
fields.packageName
);
const formattedApiRefModuleUrl = API_REF_BASE_MODULE_URL.replace(
PACKAGE_NAME_PLACEHOLDER,
fields.packageName
).replace(MODULE_NAME_PLACEHOLDER, fields.moduleName);
let formattedApiRefPackageUrl = "";
let formattedApiRefModuleUrl = "";
if (fields.isCommunity) {
formattedApiRefPackageUrl = API_REF_BASE_PACKAGE_URL.replace(
PACKAGE_NAME_PLACEHOLDER,
`community_chat_models_${fields.packageName}`
);
formattedApiRefModuleUrl = API_REF_BASE_MODULE_URL.replace(
PACKAGE_NAME_PLACEHOLDER,
`community_chat_models_${fields.packageName}`
).replace(MODULE_NAME_PLACEHOLDER, fields.moduleName);
} else {
formattedApiRefPackageUrl = API_REF_BASE_PACKAGE_URL.replace(
PACKAGE_NAME_PLACEHOLDER,
fields.packageName
);
formattedApiRefModuleUrl = API_REF_BASE_MODULE_URL.replace(
PACKAGE_NAME_PLACEHOLDER,
fields.packageName
).replace(MODULE_NAME_PLACEHOLDER, fields.moduleName);
}

const success = await Promise.all([
fetchAPIRefUrl(formattedApiRefPackageUrl),
Expand All @@ -170,8 +186,20 @@ export async function fillChatIntegrationDocTemplate(fields: {
}

const packageNameShortSnakeCase = fields.packageName.replaceAll("-", "_");
const fullPackageNameSnakeCase = `langchain_${packageNameShortSnakeCase}`;
const packageNamePretty = `@langchain/${fields.packageName}`;
let fullPackageNameSnakeCase = "";
let packageNamePretty = "";
let fullPackageImportPath = "";

if (fields.isCommunity) {
fullPackageNameSnakeCase = `langchain_community_chat_models_${packageNameShortSnakeCase}`;
fullPackageImportPath = `@langchain/community/chat_models/${fields.packageName}`;
packageNamePretty = "@langchain/community";
} else {
fullPackageNameSnakeCase = `langchain_${packageNameShortSnakeCase}`;
packageNamePretty = `@langchain/${fields.packageName}`;
fullPackageImportPath = packageNamePretty;
}

let moduleNameAllCaps = fields.moduleName.toUpperCase();
if (moduleNameAllCaps.startsWith("CHAT")) {
moduleNameAllCaps = moduleNameAllCaps.replace("CHAT", "");
Expand All @@ -185,6 +213,7 @@ export async function fillChatIntegrationDocTemplate(fields: {
packageNameShortSnakeCase
)
.replaceAll(PACKAGE_NAME_PRETTY_PLACEHOLDER, packageNamePretty)
.replaceAll(PACKAGE_IMPORT_PATH_PLACEHOLDER, fullPackageImportPath)
.replaceAll(MODULE_NAME_PLACEHOLDER, fields.moduleName)
.replaceAll(MODULE_NAME_ALL_CAPS_PLACEHOLDER, moduleNameAllCaps)
.replaceAll(
Expand Down
15 changes: 12 additions & 3 deletions libs/langchain-scripts/src/cli/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type CLIInput = {
package: string;
module: string;
type: string;
community: boolean;
};

async function main() {
Expand All @@ -19,13 +20,17 @@ async function main() {
"Package name, eg openai. Should be value of @langchain/<package>"
)
.option("--module <module>", "Module name, e.g ChatOpenAI")
.option("--type <type>", "Type of integration, e.g. 'chat'");
.option("--type <type>", "Type of integration, e.g. 'chat'")
.option(
"--community",
"If the integration is a community integration. Will effect the fields populated in the template."
);

program.parse();

const options = program.opts<CLIInput>();

const { module: moduleName, type } = options;
const { module: moduleName, type, community: isCommunity } = options;
let { package: packageName } = options;

if (packageName.startsWith("@langchain/")) {
Expand All @@ -34,7 +39,11 @@ async function main() {

switch (type) {
case "chat":
await fillChatIntegrationDocTemplate({ packageName, moduleName });
await fillChatIntegrationDocTemplate({
packageName,
moduleName,
isCommunity,
});
break;
default:
console.error(
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-scripts/src/cli/docs/templates/chat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
},
"outputs": [],
"source": [
"import { __ModuleName__ } from \"__package_name_pretty__\" \n",
"import { __ModuleName__ } from \"__import_path__\"\n",
"\n",
"const llm = new __ModuleName__({\n",
" model: \"model-name\",\n",
Expand Down
Loading