Build your first custom AI-powered application with LangChain

Since the launch of ChatGPT, everyone has been trying to build their own AI platforms, but it needs a lot of programming and a complex environment setup. But fear not! LangChain has arrived to simplify the process and make things more accessible.

Build your first custom AI-powered application with LangChain

About LangChain

Using language models like ChatGPT and Bard is simple, but making full AI applications often means combining different tools, which can be hard. That's where LangChain helps. It's like a toolbox that makes building powerful AI apps easier. You don't have to do a lot of complicated coding or set up complex stuff. LangChain makes it simpler by handling the integration part for you. You can build amazing things like chatbots, document summarization tools, and automated web Research.

What is LangChain?

It is an open-source framework that enables developers to combine large language models, such as GPT-4, with external sources of computation and data. Offered as a Python or JavaScript package.

Capabilities of the applications built ?

It is a framework for Python and JavaScript that simplifies the development of applications powered by generative AI and large language models.

These applications are:

Aware of the context:This means that applications can connect to different sources of information, for example, prompts or content, giving them a way to understand what is going on around them.

Capable of reasoning:The application can be connected to a large language model like OpenAI, Cohere, or Hugging Face. This enables the application to answer based on the context it is given.

Applications created with LangChain, explore

LangChain Framework Overview

It has six components that help to build the application for a specific use case.

Model I/O (Input/Output):

This works as the communication link between the LangChain and the selected language model. It helps in managing the instructions, asking questions, and getting answers.

Data Connection

It's like a bridge between LangChain and the specific data that the application needs.

Chains

Building a simple AI application where LLM is being used in isolation is fine, but more complex applications require the chaining of LLMs either with each other or with other components of the application.

Sequences of steps to achieve tasks using language model tools.

Agents

These are the decision-makers who help the chains decide which tool to use next or what step is needed next to achieve the task.

There are two main types of agents:

  • Action agents: make decisions for the next action at each time step based on previous actions.

  • Plan-and-execute agents: decide on the complete sequence of actions in advance and execute them without modifying the plan.

Memory

This is like a storage space that persists the application state between the run of chains.

Callbacks

These are progress trackers that log the intermediate steps of any chain and can be used to connect to different stages of the application. This feature can be used for activities such as logging, monitoring, and streaming.

Use cases

Examples of use cases include

  1. QA over structured data

  2. Retrieval-augmented generation (RAG)

  3. Interacting with APIs

  4. Chatbots, Extraction

  5. Summarization,

  6. Tagging,

  7. Web scraping,

  8. Synthetic data generation

  9. Graph querying.

Integrations available

LangChain has many integrations to help developers integrate the necessary tools out of the box, such as:

The list contains just some popular platforms, but there is much more it offers. This is the link that takes you to the complete list.

Installation steps

Quick Install for Python

pip install langchain

or

pip install langsmith && conda install langchain -c conda-forge

Quick Install for JavaScript

npm install -S langchain

For more detailed guidance and environment setup you can refer to the official docs for Python and  JavaScript

Getting  Started

After clearing the basics and setting up the development environment, it is time to build something cool and gain some practical experience.

Project Overview: Building an AI Agent

We will be building this cool AI agent using JavaScript. It's like a smart assistant that listens to what you ask. So, let's say you ask it about the current weather in Paris or the latest stock market trends. What it does is, it goes online, finds the latest info on your query, and then uses this advanced AI model to understand and organize that info. After that, it gives you a clear, easy-to-understand answer. It's like having a super-smart friend who can quickly find and explain stuff to you.

Functionality of the AI Agent:

  1. User Query Processing: The AI agent is programmed to accept queries from users.
  2. Data Retrieval: Upon receiving a query, the agent will access the internet to fetch data relevant to the query.
  3. Data Integration with LLM: After retrieving the data, the agent will provide this information to a Large Language Model (LLM).
  4. Response Generation: The LLM will then process this data to generate a comprehensive and informative response, which the agent will deliver to the user.

Setting Up the environment and installing dependencies

Just make a new folder or directory and initialize the package.json.by running the

npm init

Above command will Install the required dependencies “langchain” and “dotenv” by running the command

npm i langchain dotenv

With everything set up, it's time to dive into coding. Create a JavaScript file, for example, myAgent.js, and a .env file that will store the API keys for GPT-4 or GPT-3.5-turbo and SerpAPI.

We will start by importing the necessary modules and libraries from the LangChain package.

// Importing necessary modules and libraries
import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { SerpAPI } from "langchain/tools";
import { Calculator } from "langchain/tools/calculator";
import dotenv from "dotenv";

Configure environment variables for the API keys of OPENAI and SERPAPI  given are the direct links to get your API keys.

// Configuring environment variables
dotenv.config();

Create an array of tools including a calculator and SerpAPI

// Creating an array of tools including a calculator and SerpAPI
const tools = [new Calculator(), new SerpAPI()];

Initialize a ChatOpenAi instance with specified options you can use any model GPT-4 or GPT-3.5-turbo or any other LLM model

// Initializing a ChatOpenAI instance with specified options
const chat = new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0 });

Initialize an agent executor with the specified tools, chat model,  and options

// Initializing an agent executor with the specified tools, chat model, and options
const executor = await initializeAgentExecutorWithOptions(tools, chat, {
  agentType: "openai-functions",
  verbose: true,
});

Invoke the executor with a sample input to get a response

// Invoking the executor with a sample input to get a response
const result = await executor.invoke({
  input: "What is the weather in New York?",
});

Log the result to the console

// Logging the result to the console
console.log(result);

Below is the output that you will see in your console which will contain real-time data fetched from the internet and then generated by the LLM Certainly! Below is the markdown file with explanations added to each step:

Chain Execution Process

Step 1: AgentExecutor Entering Chain run

{
  "input": "What is the weather in New York?",
  "chat_history": []
}

Explanation: The Chain starts with the AgentExecutor receiving a query about the weather in New York.

Step 2: Entering LLM run

{
  "messages": [
    [
      {
        "lc": 1,
        "type": "constructor",
        "id": ["langchain", "schema", "SystemMessage"],
        "kwargs": {
          "content": "You are a helpful AI assistant.",
          "additional_kwargs": {}
        }
      },
      {
        "lc": 1,
        "type": "constructor",
        "id": ["langchain", "schema", "HumanMessage"],
        "kwargs": {
          "content": "What is the weather in New York?",
          "additional_kwargs": {}
        }
      }
    ]
  ]
}

Explanation: The Language Model (LLM) is invoked with a system message and a user's query about the weather in New York.

Step 3: Exiting LLM run with SerpAPI Output

{
  "generations": [
    [
      {
        "text": "",
        "message": {
          "lc": 1,
          "type": "constructor",
          "id": ["langchain", "schema", "AIMessage"],
          "kwargs": {
            "content": "",
            "additional_kwargs": {
              "function_call": {
                "name": "search",
                "arguments": "{\n \"input\": \"current weather in New York\"\n}"
              }
            }
          }
        }
      }
    ]
  ],
  "llmOutput": {
    "tokenUsage": {
      "completionTokens": 18,
      "promptTokens": 121,
      "totalTokens": 139
    }
  }
}

Explanation: The LLM generates an AIMessage with a function call to search for the current weather in New York using SerpAPI.

Step 4: AgentExecutor Selecting Search Action

{
  "tool": "search",
  "toolInput": {
    "input": "current weather in New York"
  },
  "log": ""
}

Explanation: The AgentExecutor selects the search action with the input "current weather in New York."

Step 5: Entering Tool run with SerpAPI

{
  "current weather in New York"
}

Explanation: The search tool, SerpAPI, is invoked to fetch information about the current weather in New York.

Step 6: Tool Exiting Tool run with SerpAPI Output

Output:

{
  "1 am · Feels Like72° · WindSSW 1 mph · Humidity89% · UV Index0 of 11 · Cloud Cover79% · Rain Amount0 in ..."
}

Explanation: SerpAPI returns the weather information for New York.

Step 7: Entering LLM run to Generate Response

{
  "messages": [
    [
      {
        "lc": 1,
        "type": "constructor",
        "id": ["langchain", "schema", "SystemMessage"],
        "kwargs": {
          "content": "You are a helpful AI assistant.",
          "additional_kwargs": {}
        }
      },
      {
        "lc": 1,
        "type": "constructor",
        "id": ["langchain", "schema", "HumanMessage"],
        "kwargs": {
          "content": "What is the weather in New York?",
          "additional_kwargs": {}
        }
      },
      {
        "lc": 1,
        "type": "constructor",
        "id": ["langchain", "schema", "AIMessage"],
        "kwargs": {
          "content": "",
          "additional_kwargs": {
            "function_call": {
              "name": "search",
              "arguments": "{\"input\":\"current weather in New York\"}"
            }
          }
        }
      },
      {
        "lc": 1,
        "type": "constructor",
        "id": ["langchain", "schema", "FunctionMessage"],
        "kwargs": {
          "content": "1 am · Feels Like72° · WindSSW 1 mph · Humidity89% · UV Index0 of 11 · Cloud Cover79% · Rain Amount0 in ...",
          "name": "search",
          "additional_kwargs": {}
        }
      }
    ]
  ]
}

Explanation: The LLM processes the messages and generates a response incorporating the weather information.

Step 8: Chain Execution

At this stage, the Chain has successfully completed its entire sequence of tasks. The AI assistant, having processed all the necessary steps, including data retrieval and interaction with the language model, finalizes the process by generating a response. This response contains the requested information, in this case, the current weather details for New York City.

Output of the query

The final output, presented in JSON format, provides a concise and informative summary of New York's current weather conditions. This output is the result of the Chain's comprehensive processing and represents the end-product of the AI assistant's task execution.

{
  "output": "The current weather in New York is 72°F with a wind speed of 1 mph coming from the SSW. The humidity is at 89% and the UV index is 0 out of 11. The cloud cover is 79% and there has been no rain."
}

This output is a direct result of the Chain's effective management and execution of tasks, showcasing its ability to handle complex queries and return informative, real-time data.

Conclusion

There is no limit to what one can do with AI, and with the help of LangChain, it becomes easy to build applications. You just need to have an idea and a basic understanding of how it is explained in the article, and you are good to go.

LangChain is not the only option in the market, but it is a very good starting point for developers who want to get started with AI development. Bigger and more complex projects are also possible using this framework. The project we built is just a demo of what can be done.

References 

Official Documentation of LangChain (Python)

Official Documentation of LangChain (Js/TS)