LangChain ChatModel Creation and Invocation Guide
A Juejin learning note explains that LangChain chat models implement the Runnable interface and can be created by instantiating provider classes or through init_chat_model with a provider:model-name identifier. It covers invoke, stream and batch calls, along with the asynchronous ainvoke, astream and abatch variants, and notes that true incremental streaming depends on provider support.
The guide gives developers a concise reference for using one model interface across providers, including synchronous, streaming, batch and asynchronous invocation patterns.
1. 核心认知:模型是 Runnable
在 LangChain 中,聊天模型(ChatModel)不是孤立的存在,它实现了 Runnable 接口。这意味着你学过的 Tool 调用方式——invoke、stream、batch——同样适用于模型本身。理解这一点,后续所有调用方式都是同一个协议的变体。
2. 创建模型的两种方式
方式一:直接实例化提供商类
最直观的方式,需要安装对应的集成包:
#LangChain#ChatOpenAI#ChatAnthropic#ChatGoogleGenerativeAI#ChatOllama#LangSmith