LangChain#

LangCain is software that enables the development of applications based on LLMs. Since all models/providers/inference servers have a slightly it different input/ouput format. Langchain builds a set of unified APIs. The same or nearly the same code can be used to build systems with different models and their hosting principles.

There is a set of packages that implement a typical integrations of the langchain. The following table lists the most typical of them.

Package Name

Description

langchain-community

A general package for a wide variety of community-contributed tools and integrations, including web search (DuckDuckGo, Tavily), Python REPL, and various database connectors.

langchain-core

The foundational package with the core tool abstractions and base classes.

langchain-experimental

A package for new and experimental tools, which may not yet be stable.

langchain-tavily

A dedicated package for the Tavily search tool.

langchain-brave-search

A dedicated package for the Brave Search tool.

langchain-google-genai

Includes tools for interacting with Google’s Generative AI services.

langchain-anthropic

Includes tools for interacting with the Anthropic API.

langchain-openai

Contains integrations for OpenAI’s models and services.

langchain-mongodb

A package for interacting with MongoDB.

langchain-postgres

A package for interacting with PostgreSQL.

langchain-ollama

A package implements tools to request models lanched with ollama.

langchain-huggingface

A package implements tools to interact with models from hugging face.

Check the LangChain Python reference.

Setup#

Typically, you must export the API key corresponding to the model type you want to use from your environment.


The following cell shows the code that will only work if the “GOOGLE_API_KEY” variable exists in your environment with the corresponding value.

from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")
ans = llm.invoke("Test request")
print(ans.content)
Please provide me with the details of your test request.  I need information such as:

* **What kind of test are you requesting?** (e.g., a unit test, an integration test, a performance test, a stress test, a usability test, a grammar test, a logic test, a factual accuracy test, etc.)
* **What is the subject of the test?** (e.g., a piece of code, a website, a document, a sentence, an argument, etc.)
* **What are the inputs or data for the test?** (If applicable)
* **What are the expected outputs or results?** (If applicable)
* **What are the acceptance criteria?** (How will you know if the test passed or failed?)

The more information you give me, the better I can assist you.

Some examles use ollama-specfic tools. Therefore, you must have have Ollama launched in your system. The most usitable option for me is to launch the Ollama in Docker. The following command lauches Ollama with the most basic settings in Docker:

docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama

After launching ollama, you should also pull the models you’re interested in:

docker exec ollama ollama pull <model name>

Texts transforming#

There is a set of tools in the langchain for texts transforming. They are includes:

Check Texts transforming page for details.


For example, consider the process of loading, chunking, and building embeddings for the GNU Opearting System.

import numpy as np
from langchain_ollama import OllamaEmbeddings
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter

The following cell uses the WebBaseLoader to load the HTML page.

loader = WebBaseLoader(
    web_path="https://www.gnu.org/gnu/gnu-history.html"
)
page = loader.load()[0]
print(page.page_content[1000:2000])
onym for “GNU's Not
Unix,” second, because it was a real word, and third, it was fun
to say (or

sing).

The word “free” in “free software” pertains to
freedom, not price.  You may or
may not pay a price to get GNU software.  Either way, once you have
the software you have four specific freedoms in using it.  The freedom
to run the program as you wish; the freedom to copy the program and
give it away to your friends and co-workers; the freedom to change the
program as you wish, by having full access to source code; the freedom
to distribute an improved version and thus help build the community.
(If you redistribute GNU software, you may charge a fee for the
physical act of transferring a copy, or you may give away copies.)

The project to develop the GNU system is called the “GNU
Project.”  The GNU Project was conceived in 1983 as a way of
bringing back the cooperative spirit that prevailed in the computing
community in earlier days—to make cooperation possible once again by
removing t

Following cell splits the input text to chunks.

recursive_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splist = recursive_splitter.split_documents([page])
splist
[Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content="Overview of the GNU System\n- GNU Project - Free Software Foundation\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSkip to main text\n\n\n\n\n\n\n\n\n\nFree Software Supporter:\n  \n\n\n\n\nJOIN\xa0THE\xa0FSF\n\n\n\n\n\nGNU Operating System\nSupported by the\n Free Software Foundation\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSite navigation\nSkip\n\n\n=\nABOUT\xa0GNU\n\n=\n\nPHILOSOPHY\nLICENSES\nEDUCATION\nSOFTWARE\nDISTROS\nDOCS\nMALWARE\nHELP\xa0GNU\nAUDIO\xa0&\xa0VIDEO\nGNU\xa0ART\nFUN\nGNU'S\xa0WHO?\nSOFTWARE\xa0DIRECTORY\nHARDWARE\nSITEMAP\n\n\n\n\n\n\n\n\xa0/\nAbout\xa0GNU\xa0/\nGNU\xa0history\xa0/\n\n\n\nOverview of the GNU System\n\n\nThe GNU operating system is a complete free software system,\nupward-compatible with Unix.  GNU stands for “GNU's Not Unix.”\nIt is pronounced as one syllable with a\nhard g.\nRichard Stallman made the\nInitial Announcement of\nthe GNU Project in September 1983.  A longer version called\nthe GNU Manifesto was published in\nMarch 1985.  It has been translated into several\nother languages."),
 Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content="The name “GNU” was chosen because it met a few\nrequirements; first, it was a recursive acronym for “GNU's Not\nUnix,” second, because it was a real word, and third, it was fun\nto say (or\n\nsing).\n\nThe word “free” in “free software” pertains to\nfreedom, not price.  You may or\nmay not pay a price to get GNU software.  Either way, once you have\nthe software you have four specific freedoms in using it.  The freedom\nto run the program as you wish; the freedom to copy the program and\ngive it away to your friends and co-workers; the freedom to change the\nprogram as you wish, by having full access to source code; the freedom\nto distribute an improved version and thus help build the community.\n(If you redistribute GNU software, you may charge a fee for the\nphysical act of transferring a copy, or you may give away copies.)"),
 Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='The project to develop the GNU system is called the “GNU\nProject.”  The GNU Project was conceived in 1983 as a way of\nbringing back the cooperative spirit that prevailed in the computing\ncommunity in earlier days—to make cooperation possible once again by\nremoving the obstacles to cooperation imposed by the owners of\nproprietary software.\n\nIn 1971, when Richard Stallman started his career at MIT, he worked in\na group which used free\nsoftware exclusively.  Even computer companies often distributed\nfree software.  Programmers were free to cooperate with each other,\nand often did.\n\nBy the 1980s, almost all software was\nproprietary,\nwhich means that it had owners who forbid and\nprevent cooperation by users.  This made the GNU Project necessary.'),
 Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content="By the 1980s, almost all software was\nproprietary,\nwhich means that it had owners who forbid and\nprevent cooperation by users.  This made the GNU Project necessary.\n\nEvery computer user needs an operating system; if there is no free\noperating system, then you can't even get started using a computer\nwithout resorting to proprietary software.  So the first item on the\nfree software agenda obviously had to be a free operating system.\n\nWe decided to make the operating system compatible with Unix because\nthe overall design was already proven and portable, and because\ncompatibility makes it easy for Unix users to switch from Unix to GNU."),
 Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='A Unix-like operating system includes a kernel, compilers, editors,\ntext formatters, mail software, graphical interfaces, libraries, games\nand many other things.  Thus, writing a whole operating system is a\nvery large job.  We started in January 1984.\nThe  Free Software Foundation was\nfounded in October 1985, initially to raise funds to help develop\nGNU.\nBy 1990 we had either found or written all the major components\nexcept one—the kernel.  Then Linux, a Unix-like kernel, was\ndeveloped by Linus Torvalds in 1991 and made free software in 1992.\nCombining Linux with the almost-complete GNU system resulted in a\ncomplete operating system: the GNU/Linux system.  Estimates are that\ntens of millions of people now use GNU/Linux systems, typically\nvia GNU/Linux distributions.  The principal\nversion of Linux now contains nonfree firmware “blobs”;\nfree software activists now maintain a modified free version of Linux,\ncalled \nLinux-libre.'),
 Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='However, the GNU Project is not limited to the core operating system.\nWe aim to provide a whole spectrum of software, whatever many users\nwant to have.  This includes application software.  See\nthe Free Software Directory for a catalogue\nof free software application programs.\n\nWe also want to provide software for users who are not computer\nexperts.  Therefore we developed a\ngraphical desktop (called GNOME) to help\nbeginners use the GNU system.\nWe also want to provide games and other recreations.  Plenty of free games are\nalready available.\n\nHow far can free software go?  There are no limits, except\nwhen laws such as\nthe patent system prohibit free software.  The ultimate goal is to\nprovide free software to do all of the jobs computer users want to\ndo—and thus make proprietary software a thing of the past.\n\n\n\n\n\n\n\n▲\n\n\n\nBACK TO TOP\n\n\n\n\n   Set language\n   \n\n\nAvailable for this page:'),
 Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='▲\n\n\n\nBACK TO TOP\n\n\n\n\n   Set language\n   \n\n\nAvailable for this page:\n\n\n[en]\xa0English \xa0\n[ar]\xa0العربية \xa0\n[bg]\xa0български \xa0\n[ca]\xa0català \xa0\n[cs]\xa0čeština \xa0\n[de]\xa0Deutsch \xa0\n[el]\xa0ελληνικά \xa0\n[es]\xa0español \xa0\n[fa]\xa0فارسی \xa0\n[fr]\xa0français \xa0\n[hr]\xa0hrvatski \xa0\n[it]\xa0italiano \xa0\n[ja]\xa0日本語 \xa0\n[ml]\xa0മലയാളം \xa0\n[nl]\xa0Nederlands \xa0\n[pl]\xa0polski \xa0\n[pt-br]\xa0português \xa0\n[ro]\xa0română \xa0\n[ru]\xa0русский \xa0\n[sq]\xa0Shqip \xa0\n[sr]\xa0српски \xa0\n[tr]\xa0Türkçe \xa0\n[uk]\xa0українська \xa0\n[zh-cn]\xa0简体中文 \xa0\n[zh-tw]\xa0繁體中文 \xa0\n\n\n\n\n\n\n\nBACK TO TOP ▲\n\n\n\n\n“The Free Software Foundation (FSF) is a nonprofit with a worldwide\nmission to promote computer user freedom. We defend the rights of all\nsoftware users.”\n\n\nJOIN\nDONATE\nSHOP\n\n\n\n\n\nPlease send general FSF & GNU inquiries to\n<gnu@gnu.org>.\nThere are also other ways to contact\nthe FSF.  Broken links and other corrections or suggestions can be sent\nto <webmasters@gnu.org>.\n\nPlease see the Translations\nREADME for information on coordinating and contributing translations\nof this article.'),
 Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='Please see the Translations\nREADME for information on coordinating and contributing translations\nof this article.\n\n\nCopyright © 1996, 1997, 2003, 2005, 2008, 2012, 2017, 2025\nFree Software Foundation, Inc.\nThis page is licensed under a Creative\nCommons Attribution-NoDerivatives 4.0 International License.\n\n\nCopyright Infringement Notification\n\n\n\n\nUpdated:\n\n$Date: 2025/01/20 10:22:06 $')]

Finally, OllamaEmbeddings transforms each chunk into a vector.

embeddings = OllamaEmbeddings(model="all-minilm")
np.array(embeddings.embed_documents([doc.page_content for doc in splist]))
array([[-0.0935436 ,  0.02679712, -0.01115783, ..., -0.08409031,
         0.05330889,  0.04408916],
       [-0.10084906,  0.03020417, -0.00327516, ..., -0.06176688,
         0.06849608, -0.04358619],
       [-0.11154472,  0.04795909, -0.02063497, ..., -0.01737392,
         0.01384512,  0.02849417],
       ...,
       [-0.05303629, -0.01137888,  0.03749163, ..., -0.07315554,
         0.04760291,  0.02396866],
       [-0.03733822, -0.04157385, -0.06982869, ..., -0.06890925,
         0.04996402, -0.01484258],
       [-0.05137261,  0.02855329, -0.05077768, ..., -0.0165134 ,
        -0.03141483, -0.08961581]], shape=(8, 384))

Core#

The langchain_core package contains the core interfaces and abstractions used across the entire LangChain ecosystem. For example:

  • Messages abstractions that describe the different types of messages that can appear in the agentic workflow.

  • Prompts prompt templates for chat models.

  • Vector stores intefaces to interact with vector stores.

  • Output parsers the parsers of the output.

For more details check:

LLMs#

LangChain has special modules that implement interfaces for different ways of interacting with the language model.

Provider

Type

LangChain Class Name (Python)

Python Package

OpenAI

Commercial API

langchain_openai.chat_models.ChatOpenAI, langchain_openai.llms.OpenAI

langchain-openai

Google

Commercial API

langchain_google_genai.chat_models.ChatGoogleGenerativeAI

langchain-google-genai

Anthropic

Commercial API

langchain_anthropic.chat_models.ChatAnthropic, langchain_anthropic.llms.AnthropicLLM

langchain-anthropic

Mistral AI

Commercial API

langchain_mistralai.chat_models.ChatMistralAI

langchain-mistralai

Cohere

Commercial API

langchain_cohere.chat_models.ChatCohere, langchain_cohere.llms.CohereLLM

langchain-cohere

AWS

Cloud Platform

langchain_aws.chat_models.ChatBedrock, langchain_aws.llms.BedrockLLM

langchain-aws

Hugging Face

Community/Open-Source

langchain_huggingface.llms.HuggingFaceHub, langchain_huggingface.llms.HuggingFacePipeline

langchain-huggingface

Ollama

On-Premise/Local

langchain_ollama.ChatOllama, langchain_ollama.OllamaLLM

langchain-ollama

Llama.cpp

On-Premise/Local

langchain_community.llms.LlamaCpp

llama-cpp-python

Replicate

Commercial API

langchain_replicate.llms.Replicate

langchain-replicate

Fireworks AI

Commercial API

langchain_fireworks.chat_models.ChatFireworks, langchain_fireworks.llms.FireworksLLM

langchain-fireworks

Databricks

Cloud Platform

databricks_langchain.llms.Databricks

databricks-langchain

Azure OpenAI

Commercial API

langchain_openai.chat_models.AzureChatOpenAI, langchain_openai.llms.AzureOpenAI

langchain-openai

AI21 Labs

Commercial API

langchain_ai21.llms.AI21LLM, langchain_ai21.chat_models.ChatAI21

langchain-ai21

Aleph Alpha

Commercial API

langchain_community.llms.AlephAlpha

langchain-aleph-alpha

Groq

Commercial API

langchain_groq.chat_models.ChatGroq

langchain-groq

Together AI

Commercial API

langchain_together.llms.TogetherLLM, langchain_together.chat_models.ChatTogether

langchain-together

IBM

Cloud Platform

langchain_community.chat_models.ChatWatsonx

langchain-ibm

DeepInfra

Commercial API

langchain_deepinfra.llms.DeepInfra

langchain-deepinfra

Yandex

Commercial API

langchain_community.llms.YandexGPT, langchain_community.llms.YandexGPTPredictor

langchain-yandex

For a more detailed description, check out the:

  • Chat models introduction page.

  • Models for more detailed explanation of the popular use cases.

  • LLMs page.


Consider, for example, using Ollama in the LangChain framework. For the following examples to run, ollama must be awailable on your local host.

from langchain_ollama import ChatOllama

llm = ChatOllama(model="llama3.1:latest")
ans = llm.invoke("What is the capital of France?")
print(ans.content)
The capital of France is Paris.

Agents#

Agents combine language models and tools to create the systems that can reason about tasks and decide which tools to use, and iteratively work around solution.

The langchain.agents.create_agent is a function allows you to create an agent.

Check more in the Agents page.


The following code creates an agent with llama in Ollama inference. There is one tool that provides information about the weather.

from langchain.agents import create_agent
from langchain_ollama import ChatOllama
llm = ChatOllama(model="llama3.2:1b", temperature=0)

def get_weather(city: str) -> str:
    """Get weather for a given city."""
    return f"It's always sunny in {city}!"

agent = create_agent(
    model=llm,
    tools=[get_weather],
    system_prompt="You are a helpful assistant",
)

The invocation of the agent.

messages_history = agent.invoke(
    {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)

The message history:

for m in messages_history["messages"]:
    print(type(m).__name__ + ":")
    print(m.content, end="\n\n")
HumanMessage:
what is the weather in sf

AIMessage:


ToolMessage:
It's always sunny in sf!

AIMessage:
I'm not able to provide real-time weather information or access current conditions. My previous response was an error.

However, I can suggest some ways for you to find the current weather in San Francisco:

1. Check online weather websites: You can visit websites like accuweather.com, weather.com, or wunderground.com to get the current weather conditions and forecast for San Francisco.
2. Use a mobile app: There are many mobile apps available that provide real-time weather information, such as Dark Sky, Weather Underground, or The Weather Channel.
3. Tune into local news: You can watch local news channels or listen to the radio to get the latest weather updates.

I'll make sure to be more accurate in my responses moving forward.

There is at least the tool invocation in the messages history.

LangGraph#

The LangGraph is a low-level orchestration framework and runtime for building, and deploying long-running, stateful agents.

The graph is consits of:

  • Nodes: Python functions that transform data.

  • Edges: Control flow of the graph.

The State is the information that passes through the nodes according to the rules specified by the edges.

For more information check:


Consider the process of creating a simple graph.

The following cell defines the State class, which describes the format of the data. The initial_state instance that defines the state that must be processed by the data.

from typing import TypedDict, List

class State(TypedDict):
    n_list: List[str]

initial_state = State(n_list=["input"])

The following cell defines the node that prints the input state and returns the new one.

def node_a(state: State) -> State:
    print(state["n_list"])
    return  State(n_list=["returns"])

Next cell shows the definition of the new graph.

from langgraph.graph import END, START, StateGraph

builder = StateGraph(State)
builder.add_node("a", node_a)

builder.add_edge(START, "a")
builder.add_edge("a", END)

graph = builder.compile()
graph
../_images/e544e20195e3add3279c76782a04db0aa909fba4ebfba3c22179b763f9b8d527.png

You can pass information through the compiled state graph by invoking it and passing data there.

graph.invoke(initial_state)
['value']
{'n_list': ['returns']}