What is Langchain: Diving into Github Library, Docs, and Powerful Tools

Alpaca

Alpaca

on · 4 min read
If you learn best through hands-on practice, some great examples here: Langchain handbook

LangChain value props

  • Data-aware: built in vector store abstractions
  • Agentic: allows you to define an organized collection of behaviors for LLM models
  • Components: powerful abstractions providing modularity for all major building blocks
  • Plug-and-play components: predefined chains and agents to help you build as fast as possible

Supported Languages

Started as a Python tool but team has rolled out full JS support and JS is considered a first-class citizen so choose whichever language you are most comfortable with.

  1. Python: find the docs here
  2. Javascript: find the docs here

Supported Models

LangChain does not serve any of its own LLMs, but it has integrated support for most popular LLMs out of box.

LangChain breaks down LLM support into two groups:

  1. LLMs
  2. Chat Models

Essentially it's the difference between the OpenAI completions API and the OpenAI chat completions API.

The full list of supported models is available here. Look for all models listed at the path

and
for the supported chat and chat completion models respectively.

Breaking down Key Concepts

Chains

You can use the

class for simple calls like this:

Loading...

But for more complex applications, I recommend using chains. The base interface is simple enough:

  • : List of input keys this chain expects to receive when called
  • : List of output keys this chain will produce when called
  • : core logic of the chain and returns the output (
    used for asynchronous calling)

, and
are both wrappers for
that provides preprocessing of input argument.

You probably will not need to edit/interact with this class, since the next building block,

is the most commonly used; however I think it's good to cover the basics.

Loading...

LLMChain

The most commonly used chain. Two main inputs: PromptTemplate and an LLM. You can also define

, usually only so you can feed output, as input, into the next chain.

The interface is simple:

Loading...

Take a closer look at the source code.

Combining Chains

The cleanest example of combining chains that I've found is here under "Generic Chains":

https://colab.research.google.com/github/pinecone-io/examples/blob/master/generation/langchain/handbook/02-langchain-chains.ipynb#scrollTo=f66a25a2

The example creates a chain(

) from a function that trims strings called
and then creates another chain(
- an
) that takes in prompts and rewords them in a certain writing style. Below, the chains are stitched together, with the output from the first chain passed to the second chain.

Loading...

Take a closer look at the source code(and other predefined chains).

Highlevel Outline

Tools
How language models interact with other resources.

Agents
The language model that drives decision making.

Toolkits
Sets of tools that when used together can accomplish a specific task.

Agent Executor
The logic for running agents with tools.

-- LangChain Docs

Agents

To handle complex workflows, use agents that can choose and perform behaviors from a list of actions. If you are familiar with AutoGPT, it is pretty much the parameterized version of a LangChain agent, with CLI interactivity.

Loading...

Take a look at the source code for Agent.

Tools

Loading...

Take a look at the source code for Tool.


AgentExecutor

This is most likely the class that you will interact with when it comes to Agents. Takes in three inputs:

  • LLM/Chat model: the language model powering the agent
  • Tool(s): predefined function for a specific task. Such as: SERP, Access Gmail, etc...
  • AgentType: agent that follow a plan. (List of predefined agents here)

Here's an example of executing an agent:

Loading...
Alpaca

About Alpaca

Hey there! My name is Alpaca and I am the creator and sole author of all the content found on this site. I currently live in my hometown of MIT License and enjoy writing about topics related to artificial intelligence and its applications. When I'm not busy developing cutting edge technologies, I enjoy taking virtual strolls through nature, binge watching sci-fi movies, and trying to beat high scores at arcade classics - usually with success.

Loading...
footer-logo
Copyright © tinydesk.ai 2024