第 21 期 | 外部 API 集成:使用 OpenAPI 查询天气与汇率 (EN)

⏱ Est. reading time: 20 min Updated on 5/7/2026

🎯 Learning Objectives for This Session

Hey there, future AI masters! Welcome to the fifth stop of the LangChain Full-Stack Masterclass. In previous sessions, we gained a foundational understanding of LangChain. But to truly bring your Intelligent Support Copilot to life, we need to dive deep into its "brain"—the incredibly powerful Large Language Models (LLMs). In this session, we'll say goodbye to the black box and fully master how LangChain communicates with these models efficiently and elegantly.

By the end of this session, you will:

  1. Thoroughly understand the core differences and best use cases between LLM and ChatModel in LangChain, eliminating confusion and enabling precise selection.
  2. Master how to configure and invoke mainstream large language models (like OpenAI's GPT series) in LangChain, injecting powerful language understanding and generation capabilities into your support copilot.
  3. Learn to flexibly switch between different LLM providers using LangChain's unified interface, giving your application higher portability and resilience.
  4. Gain insight into the critical impact of model parameters (such as temperature and max_tokens) on generation results, becoming a true model tuning expert.

📖 Principle Analysis

Alright folks, buckle up! We're entering the "engine room" of LangChain.

Imagine you're a top-tier chef, and Large Language Models (LLMs) are the advanced appliances in your kitchen: an oven for baking bread, a food processor for chopping vegetables, and a high-precision digital scale for seasoning. These appliances are powerful, but their manuals, control panels, and power plugs might all be different. Operating them directly means you have to learn a bunch of different interfaces and protocols. What a hassle!

LangChain's LLM and ChatModel act as your "universal control panel" or "smart central controller." They provide a standardized set of interfaces, allowing you to use a unified set of commands regardless of whether you're facing an oven (OpenAI), a food processor (Anthropic), or a digital scale (Hugging Face).

LLM vs. ChatModel: More Than Just a Name Difference

  • BaseLLM (Text Completion Model):

    • Core Function: Takes a piece of text (string) as input and returns a completed piece of text (string).
    • Working Mode: It's like writing half a sentence in a text box, and the model helps you "predict" and complete the rest.
    • Typical Scenarios: Early large models (like GPT-3's text-davinci-003) primarily used this mode. Suitable for simple text generation, summarization, translation, etc., without involving multi-turn conversations or role-playing.
    • Application in the Copilot Project: If you just want the support assistant to quickly summarize a user's sentence or extract keywords from a text block, an LLM is up to the task.
  • BaseChatModel (Chat Model):

    • Core Function: Takes a list of messages (List[BaseMessage]) as input and returns a message object (BaseMessage).
    • Working Mode: This is the mainstream interface for modern large models (like GPT-3.5-turbo, GPT-4, Claude). It better understands the concept of a "conversation" and can distinguish between system instructions (SystemMessage), user questions (HumanMessage), and AI responses (AIMessage). This enables the model to better grasp context, engage in multi-turn dialogues, and play specific roles.
    • Typical Scenarios: Almost all scenarios requiring "intelligent conversation." Multi-turn Q&A, role-playing, content creation, code generation, etc.
    • Application in the Copilot Project: This is the core brain of our Intelligent Support Copilot! It can remember previous conversations, understand user intent, play the role of a "professional support agent," and reply based on the knowledge base information provided by the system.

Why is ChatModel becoming increasingly important?

Because the design philosophy of modern large models has shifted. They are no longer just "text completers" but "conversation engines." Through explicit SystemMessages, we can assign a "persona" and "behavioral guidelines" to the model, such as "You are a friendly and professional customer support agent, please answer concisely in English." For our intelligent support copilot, this is the cornerstone of delivering personalized and professional service.

LangChain's Abstraction Hierarchy

LangChain's design philosophy is "abstraction." It encapsulates the complexity of different providers (OpenAI, Anthropic, Hugging Face, etc.) and different model types (LLM, ChatModel), exposing a unified interface.

graph TD
    subgraph LangChain Unified Interface
        A[Invoke LangChain LLM/ChatModel] --> B{Select Model Interface}
    end

    B -- LLM Type --> C[BaseLLM Interface]
    B -- ChatModel Type --> D[BaseChatModel Interface]

    C --> C1(OpenAI LLM)
    C --> C2(HuggingFace LLM)
    C --> C3(Google Palm LLM)

    D --> D1(ChatOpenAI)
    D --> D2(ChatAnthropic)
    D --> D3(ChatGoogleGenerativeAI)
    D --> D4(ChatHuggingFace)

    subgraph External LLM Services
        C1 --> E1(OpenAI API)
        C2 --> E2(HuggingFace Transformers)
        C3 --> E3(Google Palm API)

        D1 --> E1
        D2 --> E4(Anthropic API)
        D3 --> E5(Google Generative AI API)
        D4 --> E2
    end

    E1 -- Text/Message --> F[LLM Response]
    E2 -- Text/Message --> F
    E3 -- Text/Message --> F
    E4 -- Text/Message --> F
    E5 -- Text/Message --> F

    F --> A

Diagram: LangChain LLM/ChatModel Abstraction Layer

This diagram clearly illustrates how LangChain builds a bridge between your application and various large models. You only need to interact with LangChain's BaseLLM or BaseChatModel interfaces. As for whether the underlying layer is OpenAI or Anthropic, LangChain handles all of that for you. This significantly reduces development difficulty and enhances application scalability.

💻 Practical Code Drill (Specific Application in the Copilot Project)

Alright, enough theory. It's time to roll up our sleeves and let our Intelligent Support Copilot actually start talking!

First, ensure your environment has the necessary libraries installed. We'll use OpenAI as an example, as it remains the industry benchmark.

pip install langchain langchain-openai python-dotenv
# Or for TypeScript/JavaScript
# npm install langchain @langchain/openai dotenv

Don't forget to set your API key. The safest way is using environment variables. Create a .env file with the following content:

OPENAI_API_KEY="sk-YOUR_OPENAI_API_KEY_HERE"

Then, load it in your code.

1. Using ChatModel (Python)

We'll start directly with ChatModel because it's more powerful and better aligns with modern large model application scenarios. Our Intelligent Support Copilot relies on ChatModel for multi-turn conversations and role-playing.

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage

# 1. Load environment variables
load_dotenv()

# Ensure OPENAI_API_KEY is set
if not os.getenv("OPENAI_API_KEY"):
    raise ValueError("OPENAI_API_KEY environment variable is not set. Please configure it in the .env file.")

print("--- LangChain ChatModel Practice: First Experience with the Support Copilot ---")

# 2. Initialize ChatModel
# model_name can be "gpt-3.5-turbo", "gpt-4", "gpt-4o", etc.
# temperature controls the randomness of the generated text. 0.0 means highest determinism, 1.0 means highest creativity.
# For support scenarios, we usually want accurate and consistent replies, so the temperature is set lower.
chat_model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.2)

# 3. Simulate support conversation: Single-turn question
print("\n--- Scenario 1: Support Copilot handles a single-turn user question ---")
user_query_1 = "My mouse suddenly stopped working, how should I troubleshoot it?"
messages_1 = [
    SystemMessage(content="You are a professional computer hardware support assistant. Please provide concise and effective troubleshooting steps."), # Set system role and instructions
    HumanMessage(content=user_query_1) # User question
]

print(f"User question: {user_query_1}")
response_1 = chat_model.invoke(messages_1)
print(f"Support Copilot reply: {response_1.content}")
# Expected output might include: check connection, replace battery, restart computer, update drivers, etc.

# 4. Simulate support conversation: Multi-turn follow-up (The power of ChatModel)
print("\n--- Scenario 2: Support Copilot engages in a multi-turn conversation ---")
user_query_2_part1 = "I tried your methods but it still doesn't work. Is the mouse broken?"
messages_2 = [
    SystemMessage(content="You are a professional computer hardware support assistant. Please provide concise and effective troubleshooting steps."),
    HumanMessage(content=user_query_1), # First user question
    AIMessage(content=response_1.content), # Copilot's first reply
    HumanMessage(content=user_query_2_part1) # User follow-up question
]

print(f"User follow-up: {user_query_2_part1}")
response_2 = chat_model.invoke(messages_2)
print(f"Support Copilot reply: {response_2.content}")
# Expected output will be based on the previous conversation context, offering deeper troubleshooting or suggesting repair.

# 5. Explore model parameters: The impact of temperature
print("\n--- Scenario 3: Adjust temperature to observe model reply styles ---")
# Lower temperature for more rigorous, factual answers
chat_model_strict = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.0)
messages_strict = [
    SystemMessage(content="You are a rigorous scientific researcher. Please objectively describe whether the Earth is round or flat."),
    HumanMessage(content="What shape is the Earth?")
]
response_strict = chat_model_strict.invoke(messages_strict)
print(f"Rigorous mode (temperature=0.0): {response_strict.content}")

# Higher temperature for more creative, divergent answers
chat_model_creative = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.8)
messages_creative = [
    SystemMessage(content="You are an imaginative poet. Please describe the shape of the Earth using poetry."),
    HumanMessage(content="What shape is the Earth?")
]
response_creative = chat_model_creative.invoke(messages_creative)
print(f"Creative mode (temperature=0.8): {response_creative.content}")

print("\n--- Congratulations, you have grasped the core usage of LangChain ChatModel! ---")

1. Using ChatModel (TypeScript)

import { config } from 'dotenv';
import { ChatOpenAI } from '@langchain/openai';
import { HumanMessage, SystemMessage, AIMessage } from '@langchain/core/messages';

// 1. Load environment variables
config();

// Ensure OPENAI_API_KEY is set
if (!process.env.OPENAI_API_KEY) {
  throw new Error("OPENAI_API_KEY environment variable is not set. Please configure it in the .env file.");
}

console.log("--- LangChain ChatModel Practice: First Experience with the Support Copilot ---");

async function runChatModelDemo() {
  // 2. Initialize ChatModel
  // modelName can be "gpt-3.5-turbo", "gpt-4", "gpt-4o", etc.
  // temperature controls the randomness of the generated text. 0.0 means highest determinism, 1.0 means highest creativity.
  // For support scenarios, we usually want accurate and consistent replies, so the temperature is set lower.
  const chatModel = new ChatOpenAI({
    modelName: "gpt-3.5-turbo",
    temperature: 0.2,
  });

  // 3. Simulate support conversation: Single-turn question
  console.log("\n--- Scenario 1: Support Copilot handles a single-turn user question ---");
  const userQuery1 = "My mouse suddenly stopped working, how should I troubleshoot it?";
  const messages1 = [
    new SystemMessage("You are a professional computer hardware support assistant. Please provide concise and effective troubleshooting steps."), // Set system role and instructions
    new HumanMessage(userQuery1) // User question
  ];

  console.log(`User question: ${userQuery1}`);
  const response1 = await chatModel.invoke(messages1);
  console.log(`Support Copilot reply: ${response1.content}`);
  // Expected output might include: check connection, replace battery, restart computer, update drivers, etc.

  // 4. Simulate support conversation: Multi-turn follow-up (The power of ChatModel)
  console.log("\n--- Scenario 2: Support Copilot engages in a multi-turn conversation ---");
  const userQuery2Part1 = "I tried your methods but it still doesn't work. Is the mouse broken?";
  const messages2 = [
    new SystemMessage("You are a professional computer hardware support assistant. Please provide concise and effective troubleshooting steps."),
    new HumanMessage(userQuery1), // First user question
    new AIMessage(response1.content as string), // Copilot's first reply
    new HumanMessage(userQuery2Part1) // User follow-up question
  ];

  console.log(`User follow-up: ${userQuery2Part1}`);
  const response2 = await chatModel.invoke(messages2);
  console.log(`Support Copilot reply: ${response2.content}`);
  // Expected output will be based on the previous conversation context, offering deeper troubleshooting or suggesting repair.

  // 5. Explore model parameters: The impact of temperature
  console.log("\n--- Scenario 3: Adjust temperature to observe model reply styles ---");
  // Lower temperature for more rigorous, factual answers
  const chatModelStrict = new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0.0 });
  const messagesStrict = [
    new SystemMessage("You are a rigorous scientific researcher. Please objectively describe whether the Earth is round or flat."),
    new HumanMessage("What shape is the Earth?")
  ];
  const responseStrict = await chatModelStrict.invoke(messagesStrict);
  console.log(`Rigorous mode (temperature=0.0): ${responseStrict.content}`);

  // Higher temperature for more creative, divergent answers
  const chatModelCreative = new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0.8 });
  const messagesCreative = [
    new SystemMessage("You are an imaginative poet. Please describe the shape of the Earth using poetry."),
    new HumanMessage("What shape is the Earth?")
  ];
  const responseCreative = await chatModelCreative.invoke(messagesCreative);
  console.log(`Creative mode (temperature=0.8): ${responseCreative.content}`);

  console.log("\n--- Congratulations, you have grasped the core usage of LangChain ChatModel! ---");
}

runChatModelDemo();

2. Using LLM (Python - For understanding only, ChatModel is more common)

Although ChatModel is highly recommended, for the sake of completeness, let's take a quick look at how to use LLM.

import os
from dotenv import load_dotenv
from langchain_openai import OpenAI # Note: This is OpenAI, not ChatOpenAI

load_dotenv()

if not os.getenv("OPENAI_API_KEY"):
    raise ValueError("OPENAI_API_KEY environment variable is not set. Please configure it in the .env file.")

print("\n--- LangChain LLM Practice: Simple Text Completion ---")

# Initialize LLM model (usually for older models or specific scenarios)
# Note: OpenAI officially recommends using ChatModels like gpt-3.5-turbo for text completion as well.
# Here, to demonstrate the LLM class, we would use text-davinci-003 (if available, but it's usually marked as deprecated).
# Alternatively, we can use ChatOpenAI directly, but pass a string instead of a message list.
# To avoid confusion, we'll use ChatOpenAI here but only pass a single string.
# If you really need the old LLM interface, you might need to downgrade langchain-openai or use another provider.
# Here we use ChatOpenAI to simulate LLM's text completion behavior, as it's more modern.
llm_model_as_chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.7)

prompt = "Please summarize the core value of an intelligent support knowledge base in one sentence:"
print(f"Input text: {prompt}")
response_llm = llm_model_as_chat.invoke(prompt) # Pass the string directly
print(f"LLM style reply: {response_llm.content}")
# Expected output: An intelligent support knowledge base centralizes information to quickly respond to user queries, improving service efficiency and user satisfaction.

print("\n--- End of LLM style demonstration ---")

2. Using LLM (TypeScript - For understanding only, ChatModel is more common)

import { config } from 'dotenv';
import { OpenAI } from '@langchain/openai'; // Note: This is OpenAI, not ChatOpenAI
import { ChatOpenAI } from '@langchain/openai'; // Import ChatOpenAI for simulation

config();

if (!process.env.OPENAI_API_KEY) {
  throw new Error("OPENAI_API_KEY environment variable is not set. Please configure it in the .env file.");
}

console.log("\n--- LangChain LLM Practice: Simple Text Completion ---");

async function runLLMDemo() {
  // Initialize LLM model (usually for older models or specific scenarios)
  // Note: OpenAI officially recommends using ChatModels like gpt-3.5-turbo for text completion as well.
  // Here, to demonstrate the LLM class, we would use text-davinci-003 (if available, but it's usually marked as deprecated).
  // Alternatively, we can use ChatOpenAI directly, but pass a string instead of a message list.
  // To avoid confusion, we'll use ChatOpenAI here but only pass a single string.
  // If you really need the old LLM interface, you might need to downgrade langchain-openai or use another provider.
  // Here we use ChatOpenAI to simulate LLM's text completion behavior, as it's more modern.
  const llmModelAsChat = new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0.7 });

  const prompt = "Please summarize the core value of an intelligent support knowledge base in one sentence:";
  console.log(`Input text: ${prompt}`);
  const responseLlm = await llmModelAsChat.invoke(prompt); // Pass the string directly
  console.log(`LLM style reply: ${responseLlm.content}`);
  // Expected output: An intelligent support knowledge base centralizes information to quickly respond to user queries, improving service efficiency and user satisfaction.

  console.log("\n--- End of LLM style demonstration ---");
}

runLLMDemo();

Through this code, you should now have a clear understanding of how LangChain interacts with large models. For our Intelligent Support Copilot project, ChatModel will be our main workhorse, as it handles conversational context and role-playing much better.

🚧 Pitfalls & Best Practices

As a senior AI architect, I've seen countless beginners fall into traps here. Don't worry, I've marked all the "landmines" on the road for you.

  1. API Key Management: The Number One Taboo!

    • Pitfall: Hardcoding the OPENAI_API_KEY directly in your code or uploading it to a Git repository. This is basically running naked; it will be stolen in minutes, and your credit card bill will explode!
    • Best Practice: Always use environment variables! A .env file paired with the dotenv library is the best practice. In production environments, use secret management services provided by cloud vendors (like AWS Secrets Manager, Azure Key Vault, or Google Secret Manager).
  2. The LLM vs. ChatModel Selection Dilemma

    • Pitfall: Struggling over which one to use, or stubbornly using LLM to handle multi-turn conversations out of old habits.
    • Best Practice: Prioritize ChatModel! Unless you have a very specific reason (like integrating with an older model that only accepts plain text), modern large models (especially those based on the Transformer architecture) are built for conversation. ChatModel gives the model a "persona" via SystemMessage and maintains context via HumanMessage/AIMessage. This is the correct way to unleash the potential of large models. Your Intelligent Support Copilot will use ChatModel in 99% of its scenarios.
  3. The Mysticism of the temperature Parameter

    • Pitfall: Not understanding the role of temperature and setting a random value, causing the model's replies to be either too rigid or complete nonsense.
    • Best Practice:
      • temperature close to 0.0: Suitable for scenarios requiring high accuracy, factuality, and determinism. Examples: extracting information from a knowledge base, summarizing reports, generating code. When providing solutions or information, the Support Copilot will typically lean towards a low temperature to reduce hallucinations and uncertainty.
      • temperature close to 1.0: Suitable for scenarios requiring high creativity, divergence, and novelty. Examples: writing poetry, brainstorming, story creation.
      • Rule of Thumb: For customer support applications, temperature is usually set between 0.0 and 0.5. You can fine-tune it based on actual testing results.
  4. Token Limits and Cost Awareness

    • Pitfall: Ignoring the Token limits of large models and stuffing all historical conversations and knowledge base content into the prompt, resulting in either errors or astronomical bills.
    • Best Practice:
      • Every model has its context window limit (e.g., GPT-3.5-turbo might be 4K or 16K, GPT-4 might be larger). Exceeding the limit will cause the model to truncate the input or throw an error.
      • Token consumption directly impacts costs. Every API call is billed based on the number of input and output Tokens.
      • Strategy: For the Support Copilot, we need to intelligently manage conversation history (e.g., only keeping the most recent turns) and summarize or retrieve knowledge base content when necessary (this will be the focus of the upcoming RAG chapters!).
  5. Error Handling and Retries

    • Pitfall: Failing to handle network errors, API rate limits, etc., leading to application crashes or poor user experiences.
    • Best Practice: LangChain has built-in retry mechanisms for some common API errors, but you still need to consider more comprehensive error catching and user-friendly prompts. For example, when the model returns empty content, your Support Copilot should be able to say, "Sorry, I cannot answer this question at the moment. Please try asking in a different way," instead of just throwing an error.

📝 Session Summary

Congratulations! In this session, we deeply explored the core foundation of LangChain's interaction with large language models: LLM and ChatModel. You now not only understand the differences between them but have also mastered how to flexibly use ChatModel to drive conversations and role-playing in your Intelligent Support Copilot project.

We also uncovered the mysteries of the temperature model parameter together and learned how to avoid common "landmines" like API Key management, Token limits, and model selection. Remember, for your Intelligent Support Copilot, ChatModel paired with a low temperature will be your golden combination.

This knowledge is the cornerstone of building any LangChain application. In the upcoming lessons, we will build upon this foundation to add more advanced capabilities to our Intelligent Support Copilot, such as memory and tool calling, making it a truly versatile AI assistant!

Ready? Next stop, we'll explore how to make the model "remember" previous conversations—namely, Memory! That will be the crucial step in taking your Support Copilot from "basic" to "brilliant"!