Lang Chain Expression Language Overview

Sep 19, 2024

Lang Chain Expression Language Lecture Notes

Introduction

  • Topic: Lang Chain Expression Language (LC)
  • Purpose: Allows minimalist code for building chains within Lang Chain, enabling features like parallel execution, async, and streaming.
  • Key Benefits: Fast development, advanced features, easy integration with other Lang Chain products (Lang Smith and Lang Serve).

Understanding Expression Language

  • Reference: Lang Chain documentation on Expression Language.
  • Components: Streaming, async, parallel execution.

Prerequisites for the Notebook

  • Required Tools:
    • Lang Chain
    • Anthropic Claude 2.1 model
    • Cave for embeddings
    • Doc array for parallel retrieval example.
  • Obtain an API key from Anthropic console (optional: use OpenAI if needed).

Building a Chain

Traditional Approach

  • Typically, a chain is built using LLM Chain:
    • Components: Prompt, LLM, Output Passer.
    • Example: Creating a report on a topic (e.g. Artificial Intelligence).

Using Expression Language

  • Syntax: Utilizes a pipe operator to chain components:
    • Example: prompt | model | output_passer.
    • Advantages: Simple and flexible, though it may be less pythonic.
  • Execution: Instead of run, we use invoke to pass input variables.

Pipe Operator Functionality

  • Concept: The output from the left of each pipe operator is passed to the right.
  • Implementation:
    • Functionality achieved by implementing a Runnable class that defines the __or__ method to allow chaining.

Creating Custom Functions

  • Demonstration: Use custom functions (e.g. add_five, multiply_by_two) within the Runnable framework.
  • Example: Chaining functions and executing them to produce desired results.

Using Runnable Parallel

  • Purpose: Allows running multiple chains/components in parallel and extracting multiple values.
  • Implementation:
    • Example of using RunnableParallel along with RunnablePassThrough to manage multiple inputs and outputs.
  • Case Study: Retrieving information from document stores and answering questions based on the retrieved context.

Running Multiple Components

  • Can utilize RunnableParallel to run different retrievals in parallel and combine results for a final output.
  • Handling multiple inputs requires mapping retrievals to different contexts.

Runnable Lambdas

  • Concept: Lang Chain's abstraction to create runnable functions (similar to custom Runnable classes).
  • Example: Modifying outputs to remove unwanted prefixes or formatting issues using a custom function wrapped in Runnable.

Conclusion

Pros and Cons of Expression Language

  • Pros:
    • Minimalist and clean style.
    • Out of the box support for advanced features.
  • Cons:
    • Added abstraction may lead to confusion.
    • Syntax may not be familiar to Python users, conflicting with Python's Zen.

Final Thoughts

  • Expression Language is worth exploring for prototyping and can speed up development, but may need careful consideration in production code.