Initialize a ChatModel from the model name and provider. Must have the integration package corresponding to the model provider installed. Args: model: The name of the model, e.g. "gpt-4o", "claude-3-opus-20240229". model_provider: The model provider. Supported model_provide
(
model: str, *, model_provider: Optional[str] = None, **kwargs: Any
)
| 23 | # existing providers. |
| 24 | @beta() |
| 25 | def init_chat_model( |
| 26 | model: str, *, model_provider: Optional[str] = None, **kwargs: Any |
| 27 | ) -> BaseChatModel: |
| 28 | """Initialize a ChatModel from the model name and provider. |
| 29 | |
| 30 | Must have the integration package corresponding to the model provider installed. |
| 31 | |
| 32 | Args: |
| 33 | model: The name of the model, e.g. "gpt-4o", "claude-3-opus-20240229". |
| 34 | model_provider: The model provider. Supported model_provider values and the |
| 35 | corresponding integration package: |
| 36 | - openai (langchain-openai) |
| 37 | - anthropic (langchain-anthropic) |
| 38 | - azure_openai (langchain-openai) |
| 39 | - google_vertexai (langchain-google-vertexai) |
| 40 | - google_genai (langchain-google-genai) |
| 41 | - bedrock (langchain-aws) |
| 42 | - cohere (langchain-cohere) |
| 43 | - fireworks (langchain-fireworks) |
| 44 | - together (langchain-together) |
| 45 | - mistralai (langchain-mistralai) |
| 46 | - huggingface (langchain-huggingface) |
| 47 | - groq (langchain-groq) |
| 48 | - ollama (langchain-community) |
| 49 | |
| 50 | Will attempt to infer model_provider from model if not specified. The |
| 51 | following providers will be inferred based on these model prefixes: |
| 52 | - gpt-3... or gpt-4... -> openai |
| 53 | - claude... -> anthropic |
| 54 | - amazon.... -> bedrock |
| 55 | - gemini... -> google_vertexai |
| 56 | - command... -> cohere |
| 57 | - accounts/fireworks... -> fireworks |
| 58 | kwargs: Additional keyword args to pass to |
| 59 | ``<<selected ChatModel>>.__init__(model=model_name, **kwargs)``. |
| 60 | |
| 61 | Returns: |
| 62 | The BaseChatModel corresponding to the model_name and model_provider specified. |
| 63 | |
| 64 | Raises: |
| 65 | ValueError: If model_provider cannot be inferred or isn't supported. |
| 66 | ImportError: If the model provider integration package is not installed. |
| 67 | |
| 68 | Example: |
| 69 | .. code-block:: python |
| 70 | |
| 71 | from langchain.chat_models import init_chat_model |
| 72 | |
| 73 | gpt_4o = init_chat_model("gpt-4o", model_provider="openai", temperature=0) |
| 74 | claude_opus = init_chat_model("claude-3-opus-20240229", model_provider="anthropic", temperature=0) |
| 75 | gemini_15 = init_chat_model("gemini-1.5-pro", model_provider="google_vertexai", temperature=0) |
| 76 | |
| 77 | gpt_4o.invoke("what's your name") |
| 78 | claude_opus.invoke("what's your name") |
| 79 | gemini_15.invoke("what's your name") |
| 80 | """ # noqa: E501 |
| 81 | model_provider = model_provider or _attempt_infer_model_provider(model) |
| 82 | if not model_provider: |