// Create a partially configurable model with no default model: constconfigurableModel = awaitinitChatModel(undefined, { kwargs: { temperature:0 }, });
// Create a fully configurable model with a default model and a config prefix: constconfigurableModelWithDefault = awaitinitChatModel("gpt-4", { modelProvider:"openai", configurableFields:"any", configPrefix:"foo", kwargs: { temperature:0 }, });
Example: Create a partially configurable model with no default model
constGetWeather = z .object({ location:z .string() .describe("The city and state, e.g. San Francisco, CA"), }) .describe("Get the current weather in a given location");
constgetWeatherTool = tool( (input) => { // Do something with the input }, { schema:GetWeather, name:"GetWeather", description:"Get the current weather in a given location", } );
constGetPopulation = z .object({ location:z .string() .describe("The city and state, e.g. San Francisco, CA"), }) .describe("Get the current population in a given location");
constgetPopulationTool = tool( (input) => { // Do something with the input }, { schema:GetPopulation, name:"GetPopulation", description:"Get the current population in a given location", } );
configurableModelWithTools.invoke( "Which city is hotter today and which is bigger: LA or NY?" ); // GPT-4 response with tool calls
configurableModelWithTools.invoke( "Which city is hotter today and which is bigger: LA or NY?", { config: { configurable: { model:"claude-3-5-sonnet-20240620" } } } ); // Claude-3.5 sonnet response with tools
Description
This function initializes a ChatModel based on the provided model name and provider.
It supports various model providers and allows for runtime configuration of model parameters.
Security Note: Setting configurableFields to "any" means fields like api_key, base_url, etc.
can be altered at runtime, potentially redirecting model requests to a different service/user.
Make sure that if you're accepting untrusted configurations, you enumerate the
configurableFields explicitly.
The function will attempt to infer the model provider from the model name if not specified.
Certain model name prefixes are associated with specific providers:
Initialize a ChatModel from the model name and provider. Must have the integration package corresponding to the model provider installed.
Param: model
The name of the model, e.g. "gpt-4", "claude-3-opus-20240229".
Param: fields
Additional configuration options.
Param: fields.modelProvider
The model provider. Supported values include:
Param: fields.configurableFields
Which model parameters are configurable:
Param: fields.configPrefix
Prefix for configurable fields at runtime.
Param: fields.kwargs
Additional keyword args to pass to the ChatModel constructor.
Returns
A class which extends BaseChatModelWithBindTools that implements
bindTools
for proper typing.Throws
If modelProvider cannot be inferred or isn't supported.
Throws
If the model provider integration package is not installed.
Example: Initialize non-configurable models
Example: Create a partially configurable model with no default model
Example: Create a fully configurable model with a default model and a config prefix
Example: Bind tools to a configurable model:
Description
This function initializes a ChatModel based on the provided model name and provider. It supports various model providers and allows for runtime configuration of model parameters.
Security Note: Setting
configurableFields
to "any" means fields like api_key, base_url, etc. can be altered at runtime, potentially redirecting model requests to a different service/user. Make sure that if you're accepting untrusted configurations, you enumerate theconfigurableFields
explicitly.The function will attempt to infer the model provider from the model name if not specified. Certain model name prefixes are associated with specific providers:
Since
0.2.7
Version
0.2.8