How to Create LCEL Chains in LangChain
Tags: langchain, lcel, python
Published: 2024-03-21 19:57:00

---
title: How to Create LCEL Chains in LangChain
published: true
description: 
tags:
- LangChain
- LCEL
- Python
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
published_at: 2024-03-21 19:57 +0000
---
![Blog post cover image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/timoxgx1r9lbl88809tl.jpg)

[LangChain](https://python.langchain.com/docs/get_started/introduction) is a framework designed to enhance the capabilities of large language models (LLMs) by integrating external tools and data sources directly into the workflow. It addresses two main limitations of LLMs: the inability to access real-time data and the lack of tools to interact with the external world. By using LangChain, developers can create applications that leverage the intelligence of LLMs while overcoming these restrictions.

This guide will walk you through creating a simple app to fetch current stock prices and calculate the percentage difference between them using a LangChain [chain](https://python.langchain.com/docs/modules/chains). I'll also demonstrate LangChain's Expression Language ([LCEL](https://python.langchain.com/docs/expression_language/)), showcasing how to create chains of operations with less syntax than before LCEL was introduced.

All of the code for this tutorial is available on [GitHub](https://github.com/stephenc222/example-langchain-chain).

### Project Overview

Our project involves creating a LangChain chain that:

1. Fetches the current prices of two stocks.
2. Passes these prices to a Python Read-Eval-Print Loop (REPL) to calculate the percent difference between them.

These tasks require using two separate tools that we pass to our LLM in order to overcome LLM limitations.

### Prerequisites

Before starting, ensure you have Python 3.9 or greater installed on your system along with the following packages:

- `requests` for making HTTP requests.
- `python-dotenv` for loading environment variables.
- `langchain` for creating LangChain Chains, agents, and retrieval strategies.
- `langchain_openai` for integrating OpenAI models.
- `langchain_core` for LangChain core functionalities.

In addition, you need to have the following environment variables:

- An [OpenAI API key](https://platform.openai.com/account/api-keys) set in the `OPENAI_API_KEY` variable.
- A [SERP API key](https://serpapi.com/) set in the `SERP_API_KEY` variable.

See the `example.env` file for an example of how to set up your environment variables.

### Step-by-Step Implementation

#### Step 1: Setup and Initialization

First, we install the necessary libraries from the `requirements.txt` file:

```bash
pip3 install -r requirements.txt
```

Next, import the necessary modules and set up the environment:

```python
import os
import requests
from langchain.output_parsers import JsonOutputKeyToolsParser
from langchain_core.tools import tool
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain.globals import set_debug
from dotenv import load_dotenv

# Enable debug mode to see intermediate chain outputs
set_debug(True)

load_dotenv()

OPENAI_CHAT_MODEL = "gpt-3.5-turbo-1106"
```

Here, we enable debug mode for visibility into intermediate chain steps and load environment variables, including our OpenAI model choice.

#### Step 2: Define Tools

We define two tools: `repl_tool` and `stock_search`.

- **`repl_tool`**: Executes a Python command and returns the result. This tool allows us to perform precise calculations that LLMs can't directly execute.

```python
@tool
def repl_tool(command: str) -> str:
    """Run a Python REPL command. Return the final value set to a variable called "result" """
    exec_globals = {}
    exec(command, None, exec_globals)
    return exec_globals['result']
```

- **`stock_search`**: Fetches stock prices using the SERP API. This tool overcomes the LLM's limitation of not accessing real-time data.

```python
@tool
def stock_search(stock_tickers: list) -> list:
    """Search for multiple stocks with their ticker symbols, including the stock exchange, like AAPL:NASDAQ or GOOGL:NASDAQ, returning a list of stock summaries using the SERP API."""
    api_key = os.getenv('SERP_API_KEY')
    if not api_key:
        raise ValueError("SERP API key not found in environment variables.")
    # Fetching logic here...
```

#### Step 3: Create LangChain Chain

We construct a chain using LCEL syntax, demonstrating the integration of [prompts](https://python.langchain.com/docs/modules/model_io/prompts/), [models](https://python.langchain.com/docs/modules/model_io/models/), [output parsers](https://python.langchain.com/docs/modules/model_io/output_parsers/), and [tools](https://python.langchain.com/docs/modules/agents/tools/).

```python
chain = (
    get_stock_prices_prompt
    | model
    | JsonOutputKeyToolsParser(key_name="stock_search", first_tool_only=True, return_single=True)
    | stock_search
    | calculate_percent_difference_prompt
    | model
    | JsonOutputKeyToolsParser(key_name="repl_tool", first_tool_only=True, return_single=True)
    | repl_tool
)
```

This chain starts with a prompt to get stock prices, fetches them using `stock_search`, calculates the percent difference with `repl_tool`, and returns the result.

#### Step 4: Invoke the Chain

Finally, we invoke the chain with specific stock tickers and print the result:

```python
result = chain.invoke({"stockA": "AAPL", "stockB": "GOOGL"})
# Returns the percent difference between the two stock prices
print(result)
```

### Criticisms of LangChain

This example project took quite a bit of effort digging into LangChain's source code because of how outdated the documentation is.

Additionally, the documentation's examples are not very well explained, often missing critical parts that prevent you from copying and pasting the code snippets to try out yourself.

If you have to use LangChain, I hope this guide helps you.

However - my take? I would recommend _not_ using LangChain if you want to build production-ready applications unless you are prepared to dig through the source code to understand how it all works.

I'll continue to write about LangChain and provide more up-to-date and helpful guides than their documentation, if there's interest in more LangChain tutorials.

Hopefully in the near future, they take a serious effort to updating their documentation, so LangChain can be easier to use.

### Conclusion

By following this tutorial, you've learned how to use LangChain to fetch real-time data and perform calculations by integrating external tools with LLMs. This example project serves as a foundation for building more complex applications that leverage LangChain and LLMs. Whether you're interested in financial analysis or any other domain, LangChain offers a way to extend the capabilities of your LLM-based applications.