Asshley Gozar

Back to all posts
AI Engineering

Langchain Fundamentals

Get started with langchain by first understanding its fundamentals!

Learning Langchain

I just finished learning langchain and before starting out it was overwhelming but if you learn the building blocks and core components on what makes langchain good to use then I think it would be easy to visualize the concept and flow.

Applications of Langchain

If you are wandering where you can use langchain, here are the common use case where you might want to use langchain:

  • Retrieval Augmented Generation (RAG)
  • Dynamic Model Provider (When you want to switch llm or ai model without changing too much code)
  • AI Agents and more!

Langchain

At its core, langchain itself is the abstraction of using different ai model providers and connecting them to your applications. It allows you to bridge the gap between AI and your application. For instance you might want to use Gemini AI Model then after sometime you want to try other AI models without changing too much code, Langchain allows you to change model smoothly.

Core Building blocks

To help myself visualize the operations and underlying work of langchain I break down its components into different parts such as:

  • LLM (Large Language Model) or Hugging Face Model-
  • Prompt Templates
  • LCEL (Langchain Expression Language)

Then anything else is their sub operations I like to think as branch or extensions of their inner working.

Large Language Model and Hugging Face Model

AI Models are the brain of the AI application. When working with them using Langchain you have options to use either Online AI Models like Gemini API, OpenAI API, Claude or Models from Hugging Face Community. Setting them up is very straightforward and Langchain documentation is helpful in this kind of settings. Just keep in mind that you can use these two types of AI models when implementing your AI application.

Prompt Templates

In langchain ecosystem there are a lots of different types of prompt templates you can use including:

  • Prompt Template
  • Chat Template
  • Few Shot Prompt Template

These templates have different use cases and limitations. Understanding the pros and cons of each templates allows you to strategically decide which prompt template would be suitable to your needs.

Langchain Expression Language (LCEL)

Langchain Expression Language or also know as LCEL is one of the most fun and familiar feature to me in terms of programming thinking. So basically LCEL work like chain or pipeline. What I mean is it works by first passing the value from one to the next until it reaches the end. LCEL has a lot of unique and awesome feature like preserving user input as your context later on if you want to feed it to your LLM. It has syntax of pipe operator ( | ) or you may be familiar with vertical bar character. At first I thought it was some kind of logical operator but it works differently compare to what I expected.

I think that the most important thing when learning is knowing when to learn and unlearn what you have know in order to digest and understand new things.

Integrating Document Loaders

Langchain has also this capability to ingest text or information from document using its rich library such as:

  • PyPDFLoader (For PDF related documents)
  • TextLoader (For .txt extension related files)
  • CSVLoader (For .csv extension related files)
  • JSONLoader (For JSON like format)
  • UnstructuredFileLoader (For unstructured documents)
  • And more!

In this case what I have listed are just standard documents loaders that you might need in your endeavor but there's a lot more to explore.

Splitting

Splitting is also one of the most important part when developing AI applications. When you try to ingest a large documents like PDF files, you will need to break down its information or texts into smaller and manageable segments so AI models can proccess your request smoothly. In case you did not know, AI models has strict limit on how much they can processed. This can also be known as Context Window. When AI Models tried to read or process large amount of data at once they might lose some important details or worse you might hit your AI model tier depending on your subscription.

In Langchain, you can split your documents into different startegy using the following methods such as:

  • Text Structured Based
    • CharacterTextSplitter
    • RecursiveCharacterTextSplitter
    • and more!

When selecting splitting startegy, it is very important to understand what type of document you are dealing with. For instance CharacterTextSplitter works very well if you know that the document you are processing contains specific separator where it can splitted efficiently, but it fails when pair with documents that has no separators resulting in poor rag results.

Vector Database and Vector Stores

Langchain supports wide variety of vector databases and vector stores.

  • Pinecone
  • Qdrant
  • Weaviate
  • Chroma
  • and more!

When considering what type of vector database you are gonna use you have to consider whether your project will work effectively with standalone or extended, lightweight or performative, and whether you are okay learning those tools or something you are familiar with.

Wrap Up!

Langchain is one those frameworks or tools that will be helpful when integrating AI to your application. I tried to learned this tool because I got interested working on integrating Artificial Intelligence to my software projects.

In regular basis, you can actually integrate AI models to your software application using its built-in SDK providers. But that would require setups and configuration and once you decided to change AI Model providers you also have to change all underlying code and structure integrated to your application. Langchain provides these tools and abstraction out of the box.

I hope this short summary of what I have learned helps you at least understand some of the langchain fundamentals that will help you in your AI Engineering journey. As I always believed learning is endless and fulfilling so we must strive for it.