langchain

01: First model call

01: First model call

What this does

Sends one request to a local LLM (llama3.2:3b, served by Ollama) through LangChain's chat model interface and prints the text of the response.

from langchain_ollama import ChatOllama

llm = ChatOllama(model="llama3.2:3b")

response = llm.invoke("What is LangChain in one sentence?")

print(response.content)

What ChatOllama(...) actually is

ChatOllama is a chat model wrapper — a class that implements LangChain's BaseChatModel interface on top of Ollama's local HTTP API (Ollama runs a server on localhost:11434; this class is an HTTP client for it). Constructing it does nothing over the network yet — it just configures the object: which model, which host, and which generation parameters to use on every call.

Parameters worth knowing now, even unset (they default, but control real behavior):

Every LangChain chat model class — ChatOllama, ChatOpenAI, ChatAnthropic — implements the same invoke() / .content interface. That uniform interface is the actual value LangChain adds here: swapping providers is a one-line change, not a rewrite.

What .invoke() returns

response is not a string — it's an AIMessage object. .content is one field on it. The object also carries:

This matters because in a real application you rarely print .content and stop — you log token usage, you check response_metadata for truncation/finish reasons, and later (tool calling) you inspect other fields on this same object (.tool_calls).

Error modes worth knowing now

Why a local model for learning, not a hosted one

No API key, no cost, no internet dependency once pulled. The LangChain code is nearly identical either way — this is a deliberate choice to remove payment/account friction while learning the framework, not a claim that local models are how this would run in production for most companies.

One-line interview answer

"ChatOllama.invoke() sends a request to a local model server and returns an AIMessage object — the text is one field on it, alongside token usage and metadata. Every LangChain chat model implements the same interface, so switching from a local model to a hosted one is a one-line change."