UNPKG

langchain

Version:
1 lines • 9.39 kB
{"version":3,"file":"base.cjs","names":["Client"],"sources":["../../src/hub/base.ts"],"sourcesContent":["import type { BaseLanguageModel } from \"@langchain/core/language_models/base\";\nimport type { Runnable } from \"@langchain/core/runnables\";\n\nimport { Client } from \"langsmith\";\nimport type { PromptCommit } from \"langsmith/schemas\";\n\n/**\n * Push a prompt to the hub.\n * If the specified repo doesn't already exist, it will be created.\n * @param repoFullName The full name of the repo.\n * @param runnable The prompt to push.\n * @param options\n * @returns The URL of the newly pushed prompt in the hub.\n */\nexport async function basePush(\n repoFullName: string,\n runnable: Runnable,\n options?: {\n apiKey?: string;\n apiUrl?: string;\n parentCommitHash?: string;\n isPublic?: boolean;\n description?: string;\n readme?: string;\n tags?: string[];\n client?: Client;\n }\n): Promise<string> {\n const client = options?.client ?? new Client(options);\n const payloadOptions = {\n object: runnable,\n parentCommitHash: options?.parentCommitHash,\n isPublic: options?.isPublic,\n description: options?.description,\n readme: options?.readme,\n tags: options?.tags,\n };\n return client.pushPrompt(repoFullName, payloadOptions);\n}\n\nexport async function basePull(\n ownerRepoCommit: string,\n options?: {\n apiKey?: string;\n apiUrl?: string;\n includeModel?: boolean;\n skipCache?: boolean;\n client?: Client;\n }\n): Promise<PromptCommit> {\n const client = options?.client ?? new Client(options);\n\n const promptObject = await client.pullPromptCommit(ownerRepoCommit, {\n includeModel: options?.includeModel,\n skipCache: options?.skipCache,\n });\n\n if (promptObject.manifest.kwargs?.metadata === undefined) {\n promptObject.manifest.kwargs = {\n ...promptObject.manifest.kwargs,\n metadata: {},\n };\n }\n\n promptObject.manifest.kwargs.metadata = {\n ...promptObject.manifest.kwargs.metadata,\n lc_hub_owner: promptObject.owner,\n lc_hub_repo: promptObject.repo,\n lc_hub_commit_hash: promptObject.commit_hash,\n };\n\n // Some nested mustache prompts have improperly parsed variables that include a dot.\n if (promptObject.manifest.kwargs.template_format === \"mustache\") {\n const stripDotNotation = (varName: string) => varName.split(\".\")[0];\n\n const { input_variables } = promptObject.manifest.kwargs;\n if (Array.isArray(input_variables)) {\n promptObject.manifest.kwargs.input_variables =\n input_variables.map(stripDotNotation);\n }\n\n const { messages } = promptObject.manifest.kwargs;\n if (Array.isArray(messages)) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n promptObject.manifest.kwargs.messages = messages.map((message: any) => {\n const nestedVars = message?.kwargs?.prompt?.kwargs?.input_variables;\n if (Array.isArray(nestedVars)) {\n message.kwargs.prompt.kwargs.input_variables =\n nestedVars.map(stripDotNotation);\n }\n return message;\n });\n }\n }\n return promptObject;\n}\n\nexport function generateModelImportMap(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n modelClass?: new (...args: any[]) => BaseLanguageModel\n) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const modelImportMap: Record<string, any> = {};\n if (modelClass !== undefined) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const modelLcName = (modelClass as any)?.lc_name();\n let importMapKey;\n if (modelLcName === \"ChatOpenAI\") {\n importMapKey = \"chat_models__openai\";\n } else if (modelLcName === \"ChatAnthropic\") {\n importMapKey = \"chat_models__anthropic\";\n } else if (modelLcName === \"ChatAzureOpenAI\") {\n importMapKey = \"chat_models__openai\";\n } else if (modelLcName === \"ChatVertexAI\") {\n importMapKey = \"chat_models__vertexai\";\n } else if (modelLcName === \"ChatGoogleGenerativeAI\") {\n importMapKey = \"chat_models__google_genai\";\n } else if (modelLcName === \"ChatBedrockConverse\") {\n importMapKey = \"chat_models__chat_bedrock_converse\";\n } else if (modelLcName === \"ChatMistralAI\") {\n importMapKey = \"chat_models__mistralai\";\n } else if (modelLcName === \"ChatMistral\") {\n importMapKey = \"chat_models__mistralai\";\n } else if (modelLcName === \"ChatFireworks\") {\n importMapKey = \"chat_models__fireworks\";\n } else if (modelLcName === \"ChatGroq\") {\n importMapKey = \"chat_models__groq\";\n } else {\n throw new Error(\"Received unsupported model class when pulling prompt.\");\n }\n modelImportMap[importMapKey] = {\n ...modelImportMap[importMapKey],\n [modelLcName]: modelClass,\n };\n }\n return modelImportMap;\n}\n\nexport function generateOptionalImportMap(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n modelClass?: new (...args: any[]) => BaseLanguageModel\n) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const optionalImportMap: Record<string, any> = {};\n if (modelClass !== undefined) {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const modelLcName = (modelClass as any)?.lc_name();\n let optionalImportMapKey;\n if (modelLcName === \"ChatGoogleGenerativeAI\") {\n optionalImportMapKey = \"langchain_google_genai/chat_models\";\n } else if (modelLcName === \"ChatBedrockConverse\") {\n optionalImportMapKey = \"langchain_aws/chat_models\";\n } else if (modelLcName === \"ChatGroq\") {\n optionalImportMapKey = \"langchain_groq/chat_models\";\n }\n if (optionalImportMapKey !== undefined) {\n optionalImportMap[optionalImportMapKey] = {\n [modelLcName]: modelClass,\n };\n }\n }\n return optionalImportMap;\n}\n\nexport function bindOutputSchema<T extends Runnable>(loadedSequence: T) {\n if (\n \"first\" in loadedSequence &&\n loadedSequence.first !== null &&\n typeof loadedSequence.first === \"object\" &&\n \"schema\" in loadedSequence.first &&\n \"last\" in loadedSequence &&\n loadedSequence.last !== null &&\n typeof loadedSequence.last === \"object\"\n ) {\n if (\n \"bound\" in loadedSequence.last &&\n loadedSequence.last.bound !== null &&\n typeof loadedSequence.last.bound === \"object\" &&\n \"withStructuredOutput\" in loadedSequence.last.bound &&\n typeof loadedSequence.last.bound.withStructuredOutput === \"function\"\n ) {\n loadedSequence.last.bound =\n loadedSequence.last.bound.withStructuredOutput(\n loadedSequence.first.schema\n );\n } else if (\n \"withStructuredOutput\" in loadedSequence.last &&\n typeof loadedSequence.last.withStructuredOutput === \"function\"\n ) {\n loadedSequence.last = loadedSequence.last.withStructuredOutput(\n loadedSequence.first.schema\n );\n }\n }\n return loadedSequence;\n}\n"],"mappings":";;;;;;;;;;AAcA,eAAsB,SACpB,cACA,UACA,S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