This repository contains code examples (in python and javascript) from each chapter of the book "Learning LangChain".
To run the examples, you can clone the repository and run the examples in your preferred language.
First, we need the environment variables required to run the examples in this repository.
You can find the full list in the .env.example
file. Copy this file to a .env
and fill in the values.
cp .env.example .env
OPENAI_API_KEY
: You can get your key here. This is will enable you to run the examples that require an openai model.LANGCHAIN_API_KEY
: You can get your key by creating an account here. This is will enable you to interact with the langsmith tracing and debugging tools.LANGCHAIN_TRACING_V2=true
: This is required to enable visual tracing and debugging in langsmith for the examples.
If you want to run the production example in chapter 9, you need a supabase account and a supabase api key.
- To register for a supabase account, go to supabase.com and sign up.
- Once you have an account, create a new project then navigate to the settings section.
- In the settings section, navigate to the API section to see your keys.
- Copy the project url and
service_role
key and add them to the.env
file as values forSUPABASE_URL
andSUPABASE_SERVICE_ROLE_KEY
.
For python examples:
If you haven't installed python on your system, install it first as per the instructions here.
- Create a virtual environment:
This command creates a directory named .venv
containing the virtual environment.
python -m venv .venv
- Activate the virtual environment:
- MacOs/Linux:
source .venv/bin/activate
- Windows:
.venv\Scripts\activate
After activation, your terminal prompt should prefix with (venv), indicating that the virtual environment is active.
- Install the dependencies in the
pyproject.toml
file:
pip install -e .
- Verify the installation:
pip list
- Run an example to see the output:
python ch2/py/a-text-loader.py
For javascript examples:
If you haven't installed node on your system, install it first as per the instructions here.
- Install the dependencies in the
package.json
file:
npm install
- Run the example to see the output:
node ch2/js/a-text-loader.js
- PgVector Vector Store Installation or Connection Errors:
Errors for python:
- Can't find
psycopg
orpsycopg_binary
: Try to reinstallpsycopg
with the [binary] extra, which includes pre-compiled binaries and necessary dependencies.
pip install psycopg[binary]
Then run the file again.
If you're having issues connecting to postgres via docker, you can use try some alernative vector stores:
-
Use the memory vector store instead: This is a simple vector store that stores vectors in memory. It is not persistent and will be lost when the program is terminated. Here's the API for Python and docs for Javascript.
-
You can also use
Chroma
-- an AI-native open-source vector database. You can installChroma
as per the instructions for Python or Javascript.