Objectives
- Understand the difference between using proprietary wrappers and core abstractions.
- Develop using both approaches to see how they differ in code.
- Adopt core abstractions for our implementations.
- Practice invoking various provider APIs in a uniform manner.
Understanding the LLM Interaction API (Step-by-Step)
Before writing the code, letβs understand the main API components used during the LLM interaction process in LangChain:Step 1: Model Initialization (init_chat_model)
- API Call:
init_chat_model("model_name", model_provider="provider") - What it returns: It returns an initialized instance of a LangChain chat model wrapper (which inherits from the base
BaseChatModelclass). - Purpose: Dynamically switches and loads the appropriate client library (such as OpenAI, Anthropic, Google GenAI, or Groq) under a single uniform interface.
Step 2: Model Invocation (invoke)
- API Call:
model.invoke(input_data) - What it takes: It accepts a prompt string, a list of chat message objects (like
HumanMessage,SystemMessage), or a formattedPromptValue. - What it returns: It returns an
AIMessageresponse object from the language model.
Step 3: Handling the Response (AIMessage)
- What it is: An object structure that encapsulates the output generated by the assistant model.
- Key Attributes:
content: The raw string text returned by the model.response_metadata: A dictionary holding extra metadata (such as token usage billing count, model name, and stop reason).id: An optional unique identifier for the output message.
Step 4: Extracting the Output Text
- To read the direct text answer generated by the model (filtering out structural metadata wrappers), access the
.contentattribute:
Chat Model Basic
Goal
Create a model instance using core abstractions and query it for the result of a simple math calculation.Sample Input
Sample Output
Plan & Code Implementation
Method 1: Using Core Abstraction Helper (Recommended)
Plan:- Import the core abstraction helper
init_chat_modelfromlangchain.chat_models. - Initialize the model using
init_chat_model("gpt-4o", model_provider="openai"). - Call
model.invoke()with the input string and print the response.
Method 2: Using Proprietary Wrapper Class
Plan:- Import the proprietary model wrapper
ChatOpenAIfromlangchain_openai. - Initialize the model using
ChatOpenAI(model="gpt-4o"). - Call
model.invoke()with the input string and print the response.
Proprietary Wrappers vs. Core Abstractions
As shown above, we used the unified core abstraction helperinit_chat_model. Letβs compare this with proprietary wrappers:
- Proprietary Wrappers: Using specific provider classes (e.g.,
ChatOpenAIfromlangchain_openai).- Advantages: Direct access to unique provider features and parameter optimizations.
- Disadvantages: High code coupling; swapping providers requires refactoring imports and classes across your codebase.
- Example:
- Core Abstractions: Using the unified initializer helper (
init_chat_modelfromlangchain.chat_models).- Advantages: Standardized interface; swap providers simply by changing parameter strings.
- Disadvantages: Advanced provider-specific parameters must be passed indirectly via
model_kwargs. - Example:
[!IMPORTANT]
To ensure maximum flexibility and avoid having to remember different SDK endpoints, imports, and API conventions, we will adopt Core Abstractions (init_chat_model) throughout this module to practice invoking various provider APIs in a uniform manner.
Shifting Providers Seamlessly (Gemini and Groq)
Because we use core abstractions, shifting our implementation to another provider requires zero changes to imports. We can swap the model provider simply by updating parameter strings:1. Shifting to Google Gemini
2. Shifting to Groq
Exercise: Capital City Agent π
Goal
Build a basic prompt program that queries the chat model to find the capital of any given country.Sample Input
Sample Output
Plan
- Use
init_chat_modelto load the OpenAI model. - Invoke the model with a string querying the capital of India.
- Print the result content.
Solution
Solution
Practice & Exercises
To practice, open the interactive notebook:Practice & Exercises
Practice initializing and running basic chat model queries.π» VS Code | π Colab | π₯ Download