Torna al blog
Intelligenza Artificiale

LangChain JavaScript with Ollama: how I build local AI apps with memory and guard rails

LangChain.js and Ollama together let you build complete AI applications without cloud and without API keys. In this article I share how I build LLM chains, manage persistent memory with SQLite and implement guard rails to control model behavior.

Why LangChain.js instead of the native Ollama library

The native Ollama library is simpler and more direct — perfect for simple integrations, quick tests, or maximum control over request details. LangChain.js adds an abstraction layer that becomes valuable as complexity grows: operation chains, conversational memory, multiple pipelines, guard rails, vector database integration. My rule: use the native library for initial experimentation and simple use cases. As soon as I need memory, multiple pipelines, or moderation, I switch to LangChain.js.

The first chain: PromptTemplate and Chain

The fundamental building block of LangChain is the Chain: a sequence of operations that transforms an input into an output through one or more models. The .pipe() syntax makes LangChain elegant: each component receives the output of the previous one. PromptTemplate produces the prompt, the model generates the response, the parser transforms it into a simple string.

Multiple pipelines: chains that concatenate

LangChain's real power emerges when chaining multiple operations. A case I use often: translate text and simultaneously extract keywords, all in a single flow. This approach is much cleaner than manually managing sequential calls. LangChain handles output passing between steps, rate limiting, and errors uniformly.

The memory problem: LLMs are stateless

Something that surprises people starting with LLMs: every request is completely isolated. The model has no memory of previous conversations. LangChain solves this with its Memory system. BufferMemory keeps the conversation history in memory during the session. For production, persistent memory is needed.

REST API with memory: the architecture I use

I almost always expose my LLMs through a Node.js REST API. The key is associating memory with the session, not the server instance — so different sessions can have independent conversations on the same model instance.

Persistent memory with SQLite

The production solution for conversational memory is SQLite — an embedded database with no server to configure, but full SQL robustness. With this configuration, a user can resume their conversation from where they left off even after a server restart. The history is structured by session and queryable with standard SQL.

Guard rails: controlling model behavior

Guard rails are one of the most important aspects when building real AI applications. LangChain implements them at three levels: system prompt (basic guardrail defining role and domain), pre-guard rail (a moderator LLM that analyzes the request before it reaches the main model), and post-guard rail (a moderator that filters the response before sending it to the client). For business applications, the pre-guard rail is what I use almost always — it's the most efficient filter because it blocks problematic requests before consuming inference on the main model.

From notebook to production

The most important thing I learned: start simple. A PromptTemplate and a basic chain. Add memory when you really need it. Add guard rails when the model produces out-of-scope responses. Don't build the complete architecture upfront. LangChain.js has a rich but complex ecosystem. The course guides you step by step, with code you can run immediately, without cloud accounts and without per-query costs.

Condividi