Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jul 24, 2024
1 parent d52666c commit 25e25b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 15 additions & 6 deletions docs/core_docs/docs/how_to/chat_models_universal_init.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@ import CodeBlock from "@theme/CodeBlock";
Many LLM applications let end users specify what model provider and model they want the application to be powered by.
This requires writing some logic to initialize different ChatModels based on some user configuration.
The `initChatModel()` helper method makes it easy to initialize a number of different model integrations without having to worry about import paths and class names.
Keep in mind this feature is only for chat models.

:::caution
This feature is only intended to be used in Node environments. Use in non Node environments or with bundlers is not guaranteed to work and not officially supported.
:::
:::info Prerequisites

:::tip Supported models
See the [initChatModel()](https://v02.api.js.langchain.com/functions/langchain_chat_models_configurable.initChatModel.html) API reference for a full list of supported integrations.
This guide assumes familiarity with the following concepts:

- [Chat models](/docs/concepts/#chat-models)

- [LangChain Expression Language (LCEL)](/docs/concepts#langchain-expression-language)

- [Tool calling](/docs/concepts#tools)

Make sure you have the integration packages installed for any model providers you want to support. E.g. you should have `@langchain/openai` installed to init an OpenAI model.
:::

:::caution Compatibility
**This feature is only intended to be used in Node environments. Use in non Node environments or with bundlers is not guaranteed to work and not officially supported.**

`initChatModel` requires `langchain>=0.2.11`. See [this guide](/docs/how_to/installation/#installing-integration-packages) for some considerations to take when upgrading.

See the [initChatModel()](https://v02.api.js.langchain.com/functions/langchain_chat_models_configurable.initChatModel.html) API reference for a full list of supported integrations.

Make sure you have the integration packages installed for any model providers you want to support. E.g. you should have `@langchain/openai` installed to init an OpenAI model.
:::

## Basic usage
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/chat_models/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,9 @@ export async function initChatModel<
* Initialize a ChatModel from the model name and provider.
* Must have the integration package corresponding to the model provider installed.
*
* @template {extends BaseLanguageModelInput = BaseLanguageModelInput} RunInput - The input type for the model.
* @template {extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions} CallOptions - Call options for the model.
*
* @param {string | ChatModelProvider} [model] - The name of the model, e.g. "gpt-4", "claude-3-opus-20240229".
* @param {Object} [fields] - Additional configuration options.
* @param {string} [fields.modelProvider] - The model provider. Supported values include:
Expand All @@ -596,7 +599,7 @@ export async function initChatModel<
* - string[]: Specified fields are configurable.
* @param {string} [fields.configPrefix] - Prefix for configurable fields at runtime.
* @param {Record<string, any>} [fields.params] - Additional keyword args to pass to the ChatModel constructor.
* @returns {Promise<_ConfigurableModel>} A class which extends BaseChatModel.
* @returns {Promise<_ConfigurableModel<RunInput, CallOptions>>} A class which extends BaseChatModel.
* @throws {Error} If modelProvider cannot be inferred or isn't supported.
* @throws {Error} If the model provider integration package is not installed.
*
Expand Down

0 comments on commit 25e25b5

Please sign in to comment.