Go to file
Harrison Chase 1fe3a4f724
extra requires (#129)
add extra requires
2022-11-13 17:34:58 -08:00
.github/workflows Harrison/fix lint (#80) 2022-11-07 15:22:37 -08:00
docs Harrison/fix docs (#115) 2022-11-10 08:59:51 -08:00
examples huggingface tokenizer (#75) 2022-11-13 09:37:44 -08:00
langchain Fix a few typos and wrapped f-strings (#128) 2022-11-13 13:16:19 -08:00
tests Prompt from file proof of concept using plain text (#127) 2022-11-13 13:15:30 -08:00
.flake8 initial commit 2022-10-24 14:51:15 -07:00
.gitignore Fix pip install issue due to FAISS (#102) 2022-11-09 13:23:17 -08:00
LICENSE add license (#50) 2022-11-01 21:12:02 -07:00
Makefile initial commit 2022-10-24 14:51:15 -07:00
MANIFEST.in Add py.typed marker to package (#121) 2022-11-12 11:22:32 -08:00
pyproject.toml initial commit 2022-10-24 14:51:15 -07:00
README.md Readme: Fix link to embeddings example and use python markup for code examples (#123) 2022-11-12 11:26:08 -08:00
readthedocs.yml Bumping python version for read the docs (#122) 2022-11-12 13:43:39 -08:00
requirements.txt extra requires (#129) 2022-11-13 17:34:58 -08:00
setup.py extra requires (#129) 2022-11-13 17:34:58 -08:00
test_requirements.txt Harrison/lintai21 (#114) 2022-11-10 08:46:35 -08:00

🦜🔗 LangChain

Building applications with LLMs through composability

lint test License: MIT Twitter

Quick Install

pip install langchain

🤔 What is this?

Large language models (LLMs) are emerging as a transformative technology, enabling developers to build applications that they previously could not. But using these LLMs in isolation is often not enough to create a truly powerful app - the real power comes when you are able to combine them with other sources of computation or knowledge.

This library is aimed at assisting in the development of those types of applications. It aims to create:

  1. a comprehensive collection of pieces you would ever want to combine
  2. a flexible interface for combining pieces into a single comprehensive "chain"
  3. a schema for easily saving and sharing those chains

🔧 Setting up your environment

Besides the installation of this python package, you will also need to install packages and set environment variables depending on which chains you want to use.

Note: the reason these packages are not included in the dependencies by default is that as we imagine scaling this package, we do not want to force dependencies that are not needed.

The following use cases require specific installs and api keys:

  • OpenAI:
    • Install requirements with pip install openai
    • Get an OpenAI api key and either set it as an environment variable (OPENAI_API_KEY) or pass it to the LLM constructor as openai_api_key.
  • Cohere:
    • Install requirements with pip install cohere
    • Get a Cohere api key and either set it as an environment variable (COHERE_API_KEY) or pass it to the LLM constructor as cohere_api_key.
  • HuggingFace Hub
    • Install requirements with pip install huggingface_hub
    • Get a HuggingFace Hub api token and either set it as an environment variable (HUGGINGFACEHUB_API_TOKEN) or pass it to the LLM constructor as huggingfacehub_api_token.
  • SerpAPI:
    • Install requirements with pip install google-search-results
    • Get a SerpAPI api key and either set it as an environment variable (SERPAPI_API_KEY) or pass it to the LLM constructor as serpapi_api_key.
  • NatBot:
    • Install requirements with pip install playwright
  • Wikipedia:
    • Install requirements with pip install wikipedia
  • Elasticsearch:
    • Install requirements with pip install elasticsearch
    • Set up Elasticsearch backend. If you want to do locally, this is a good guide.
  • FAISS:
    • Install requirements with pip install faiss for Python 3.7 and pip install faiss-cpu for Python 3.10+.
  • Manifest:
    • Install requirements with pip install manifest-ml (Note: this is only available in Python 3.8+ currently).

If you are using the NLTKTextSplitter or the SpacyTextSplitter, you will also need to install the appropriate models. For example, if you want to use the SpacyTextSplitter, you will need to install the en_core_web_sm model with python -m spacy download en_core_web_sm. Similarly, if you want to use the NLTKTextSplitter, you will need to install the punkt model with python -m nltk.downloader punkt.

🚀 What can I do with this

This project was largely inspired by a few projects seen on Twitter for which we thought it would make sense to have more explicit tooling. A lot of the initial functionality was done in an attempt to recreate those. Those are:

Self-ask-with-search

To recreate this paper, use the following code snippet or checkout the example notebook.

from langchain import SelfAskWithSearchChain, OpenAI, SerpAPIChain

llm = OpenAI(temperature=0)
search = SerpAPIChain()

self_ask_with_search = SelfAskWithSearchChain(llm=llm, search_chain=search)

self_ask_with_search.run("What is the hometown of the reigning men's U.S. Open champion?")

LLM Math

To recreate this example, use the following code snippet or check out the example notebook.

from langchain import OpenAI, LLMMathChain

llm = OpenAI(temperature=0)
llm_math = LLMMathChain(llm=llm)

llm_math.run("How many of the integers between 0 and 99 inclusive are divisible by 8?")

Generic Prompting

You can also use this for simple prompting pipelines, as in the below example and this example notebook.

from langchain import Prompt, OpenAI, LLMChain

template = """Question: {question}

Answer: Let's think step by step."""
prompt = Prompt(template=template, input_variables=["question"])
llm = OpenAI(temperature=0)
llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"

llm_chain.predict(question=question)

Embed & Search Documents

We support two vector databases to store and search embeddings -- FAISS and Elasticsearch. Here's a code snippet showing how to use FAISS to store embeddings and search for text similar to a query. Both database backends are featured in this example notebook.

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.faiss import FAISS
from langchain.text_splitter import CharacterTextSplitter

with open('state_of_the_union.txt') as f:
    state_of_the_union = f.read()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_text(state_of_the_union)

embeddings = OpenAIEmbeddings()

docsearch = FAISS.from_texts(texts, embeddings)

query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query)

📖 Documentation

The above examples are probably the most user friendly documentation that exists, but full API docs can be found here.

🤖 Developer Guide

To begin developing on this project, first clone to the repo locally. To install requirements, run pip install -r requirements.txt. This will install all requirements for running the package, examples, linting, formatting, and tests.

Formatting for this project is a combination of Black and isort. To run formatting for this project, run make format.

Linting for this project is a combination of Black, isort, flake8, and mypy. To run linting for this project, run make lint. We recognize linting can be annoying - if you do not want to do it, please contact a project maintainer and they can help you with it. We do not want this to be a blocker for good code getting contributed.

Unit tests cover modular logic that does not require calls to outside apis. To run unit tests, run make tests. If you add new logic, please add a unit test.

Integration tests cover logic that requires making calls to outside APIs (often integration with other services). To run integration tests, run make integration_tests. If you add support for a new external API, please add a new integration test.

If you are adding a Jupyter notebook example, you can run pip install -e . to build the langchain package from your local changes, so your new logic can be imported into the notebook.

Docs are largely autogenerated by sphinx from the code. For that reason, we ask that you add good documentation to all classes and methods. Similar to linting, we recognize documentation can be annoying - if you do not want to do it, please contact a project maintainer and they can help you with it. We do not want this to be a blocker for good code getting contributed.