LangChain

Introduction

  • LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and concepts necessary to work with language models. These modular components—like functions and object classes—serve as the building blocks of generative AI programs.

  • Components can be “chained” together to create applications for minimizing the amount of code and fine understanding required to execute complex NLP tasks.

  • Importing language models into LangChain is easy, provided you have an API key. The LLM class is designed to provide a standard interface for all models.

Chain

  • Any two runnables can be “chained” together into sequences. The output of the previous runnable’s .invoke() call is passed as input to the next runnable. This can be done using the .pipe() method.

import { ChatOpenAI } from "@langchain/openai";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";

const model = new ChatOpenAI({
  model: "gpt-4o-mini",
  temperature: 0
});


const prompt = ChatPromptTemplate.fromTemplate("tell me a joke about {topic}");

const chain = prompt.pipe(model).pipe(new StringOutputParser());
await chain.invoke({ topic: "bears" });

Prompt Templates

  • To format user input into a format that can be passed to a language model.

Example Selector

  • To select the correct few shot examples to pass to the prompt.

Chat Models

  • Integrate with different engines models that take message in and output a message

Message

  • It is the input and output of chat models. we can use the utilitity functions to control the output

Output Parser

  • It is for taking the output of an LLM and parsing into more structured format.

Document Loader

  • It is responsible for loading documents from a variety of sources.

  • Sample CSV

Text Splitter

  • To split the large text into chunks

Embedding models

  • Take a text as a input , and convert it into vectors

Vector Store

  • To store and search over unstructured data is to embed it and store the resulting embedding vectors, and then at query time to embed the unstructured query and retrieve the embedding vectors that are 'most similar' to the embedded query.

Retrievers

  • For taking a query and returning relevant documents, can be used with vector store for similarity search

Tool

  • Tool contain a description of the tool (to pass to the language model) as well as the implementation of the function to call.

  • Tool calling allows a chat model to respond to a given prompt by “calling a tool”. The model only generates the arguments to a tool, and actually running the tool (or not) is up to the user.

Last updated

Was this helpful?