mirror of https://github.com/hwchase17/langchain
Docs refactor (#480)
Big docs refactor! Motivation is to make it easier for people to find resources they are looking for. To accomplish this, there are now three main sections: - Getting Started: steps for getting started, walking through most core functionality - Modules: these are different modules of functionality that langchain provides. Each part here has a "getting started", "how to", "key concepts" and "reference" section (except in a few select cases where it didnt easily fit). - Use Cases: this is to separate use cases (like summarization, question answering, evaluation, etc) from the modules, and provide a different entry point to the code base. There is also a full reference section, as well as extra resources (glossary, gallery, etc) Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>pull/512/head
parent
c5f0af9398
commit
985496f4be
@ -0,0 +1,10 @@
|
||||
LangChain Ecosystem
|
||||
===================
|
||||
|
||||
Guides for how other companies/products can be used with LangChain
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
|
||||
ecosystem/*
|
@ -0,0 +1,16 @@
|
||||
# AI21 Labs
|
||||
|
||||
This page covers how to use the AI21 ecosystem within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific AI21 wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- Get an AI21 api key and set it as an environment variable (`AI21_API_KEY`)
|
||||
|
||||
## Wrappers
|
||||
|
||||
### LLM
|
||||
|
||||
There exists an AI21 LLM wrapper, which you can access with
|
||||
```python
|
||||
from langchain.llms import AI21
|
||||
```
|
@ -0,0 +1,25 @@
|
||||
# Cohere
|
||||
|
||||
This page covers how to use the Cohere ecosystem within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific Cohere wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- Install the Python SDK with `pip install cohere`
|
||||
- Get an Cohere api key and set it as an environment variable (`COHERE_API_KEY`)
|
||||
|
||||
## Wrappers
|
||||
|
||||
### LLM
|
||||
|
||||
There exists an Cohere LLM wrapper, which you can access with
|
||||
```python
|
||||
from langchain.llms import Cohere
|
||||
```
|
||||
|
||||
### Embeddings
|
||||
|
||||
There exists an Cohere Embeddings wrapper, which you can access with
|
||||
```python
|
||||
from langchain.embeddings import CohereEmbeddings
|
||||
```
|
||||
For a more detailed walkthrough of this, see [this notebook](../modules/utils/combine_docs_examples/embeddings.ipynb)
|
@ -0,0 +1,32 @@
|
||||
# Google Search Wrapper
|
||||
|
||||
This page covers how to use the Google Search API within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific Pinecone wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- Install requirements with `pip install google-api-python-client`
|
||||
- Set up a Custom Search Engine, following [these instructions](https://stackoverflow.com/questions/37083058/programmatically-searching-google-in-python-using-custom-search)
|
||||
- Get an API Key and Custom Search Engine ID from the previous step, and set them as environment variables `GOOGLE_API_KEY` and `GOOGLE_CSE_ID` respectively
|
||||
|
||||
## Wrappers
|
||||
|
||||
### Utility
|
||||
|
||||
There exists a GoogleSearchAPIWrapper utility which wraps this API. To import this utility:
|
||||
|
||||
```python
|
||||
from langchain.utilities import GoogleSearchAPIWrapper
|
||||
```
|
||||
|
||||
For a more detailed walkthrough of this wrapper, see [this notebook](../modules/utils/examples/google_search.ipynb).
|
||||
|
||||
### Tool
|
||||
|
||||
You can also easily load this wrapper as a Tool (to use with an Agent).
|
||||
You can do this with:
|
||||
```python
|
||||
from langchain.agents import load_tools
|
||||
tools = load_tools(["google-search"])
|
||||
```
|
||||
|
||||
For more information on this, see [this page](../modules/agents/tools.md)
|
@ -0,0 +1,19 @@
|
||||
# Hazy Research
|
||||
|
||||
This page covers how to use the Hazy Research ecosystem within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific Hazy Research wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- To use the `manifest`, install it with `pip install manifest-ml`
|
||||
|
||||
## Wrappers
|
||||
|
||||
### LLM
|
||||
|
||||
There exists an LLM wrapper around Hazy Research's `manifest` library.
|
||||
`manifest` is a python library which is itself a wrapper around many model providers, and adds in caching, history, and more.
|
||||
|
||||
To use this wrapper:
|
||||
```python
|
||||
from langchain.llms.manifest import ManifestWrapper
|
||||
```
|
@ -0,0 +1,68 @@
|
||||
# Hugging Face
|
||||
|
||||
This page covers how to use the Hugging Face ecosystem (including the Hugging Face Hub) within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific Hugging Face wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
|
||||
If you want to work with the Hugging Face Hub:
|
||||
- Install the Python SDK with `pip install huggingface_hub`
|
||||
- Get an OpenAI api key and set it as an environment variable (`HUGGINGFACEHUB_API_TOKEN`)
|
||||
|
||||
If you want work with Hugging Face python libraries:
|
||||
- Install `pip install transformers` for working with models and tokenizers
|
||||
- Install `pip install datasets` for working with datasets
|
||||
|
||||
## Wrappers
|
||||
|
||||
### LLM
|
||||
|
||||
There exists two Hugging Face LLM wrappers, one for a local pipeline and one for a model hosted on Hugging Face Hub.
|
||||
Note that these wrappers only work for the following tasks: `text2text-generation`, `text-generation`
|
||||
|
||||
To use the local pipeline wrapper:
|
||||
```python
|
||||
from langchain.llms import HuggingFacePipeline
|
||||
```
|
||||
|
||||
To use a the wrapper for a model hosted on Hugging Face Hub:
|
||||
```python
|
||||
from langchain.llms import HuggingFaceHub
|
||||
```
|
||||
For a more detailed walkthrough of the Hugging Face Hub wrapper, see [this notebook](../modules/llms/integrations/huggingface_hub.ipynb)
|
||||
|
||||
|
||||
### Embeddings
|
||||
|
||||
There exists two Hugging Face Embeddings wrappers, one for a local model and one for a model hosted on Hugging Face Hub.
|
||||
Note that these wrappers only work for `sentence-transformers` models.
|
||||
|
||||
To use the local pipeline wrapper:
|
||||
```python
|
||||
from langchain.embeddings import HuggingFaceEmbeddings
|
||||
```
|
||||
|
||||
To use a the wrapper for a model hosted on Hugging Face Hub:
|
||||
```python
|
||||
from langchain.embeddings import HuggingFaceHubEmbeddings
|
||||
```
|
||||
For a more detailed walkthrough of this, see [this notebook](../modules/utils/combine_docs_examples/embeddings.ipynb)
|
||||
|
||||
### Tokenizer
|
||||
|
||||
There are several places you can use tokenizers available through the `transformers` package.
|
||||
By default, it is used to count tokens for all LLMs.
|
||||
|
||||
You can also use it to count tokens when splitting documents with
|
||||
```python
|
||||
from langchain.text_splitter import CharacterTextSplitter
|
||||
CharacterTextSplitter.from_huggingface_tokenizer(...)
|
||||
```
|
||||
For a more detailed walkthrough of this, see [this notebook](../modules/utils/combine_docs_examples/textsplitter.ipynb)
|
||||
|
||||
|
||||
### Datasets
|
||||
|
||||
Hugging Face has lots of great datasets that can be used to evaluate your LLM chains.
|
||||
|
||||
For a detailed walkthrough of how to use them to do so, see [this notebook](../use_cases/evaluation/huggingface_datasets.ipynb)
|
@ -0,0 +1,17 @@
|
||||
# NLPCloud
|
||||
|
||||
This page covers how to use the NLPCloud ecosystem within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific NLPCloud wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- Install the Python SDK with `pip install nlpcloud`
|
||||
- Get an NLPCloud api key and set it as an environment variable (`NLPCLOUD_API_KEY`)
|
||||
|
||||
## Wrappers
|
||||
|
||||
### LLM
|
||||
|
||||
There exists an NLPCloud LLM wrapper, which you can access with
|
||||
```python
|
||||
from langchain.llms import NLPCloud
|
||||
```
|
@ -0,0 +1,55 @@
|
||||
# OpenAI
|
||||
|
||||
This page covers how to use the OpenAI ecosystem within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific OpenAI wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- Install the Python SDK with `pip install openai`
|
||||
- Get an OpenAI api key and set it as an environment variable (`OPENAI_API_KEY`)
|
||||
- If you want to use OpenAI's tokenizer (only available for Python 3.9+), install it with `pip install tiktoken`
|
||||
|
||||
## Wrappers
|
||||
|
||||
### LLM
|
||||
|
||||
There exists an OpenAI LLM wrapper, which you can access with
|
||||
```python
|
||||
from langchain.llms import OpenAI
|
||||
```
|
||||
|
||||
If you are using a model hosted on Azure, you should use different wrapper for that:
|
||||
```python
|
||||
from langchain.llms import AzureOpenAI
|
||||
```
|
||||
For a more detailed walkthrough of the Azure wrapper, see [this notebook](../modules/llms/integrations/azure_openai_example.ipynb)
|
||||
|
||||
|
||||
|
||||
### Embeddings
|
||||
|
||||
There exists an OpenAI Embeddings wrapper, which you can access with
|
||||
```python
|
||||
from langchain.embeddings import OpenAIEmbeddings
|
||||
```
|
||||
For a more detailed walkthrough of this, see [this notebook](../modules/utils/combine_docs_examples/embeddings.ipynb)
|
||||
|
||||
|
||||
### Tokenizer
|
||||
|
||||
There are several places you can use the `tiktoken` tokenizer. By default, it is used to count tokens
|
||||
for OpenAI LLMs.
|
||||
|
||||
You can also use it to count tokens when splitting documents with
|
||||
```python
|
||||
from langchain.text_splitter import CharacterTextSplitter
|
||||
CharacterTextSplitter.from_tiktoken_encoder(...)
|
||||
```
|
||||
For a more detailed walkthrough of this, see [this notebook](../modules/utils/combine_docs_examples/textsplitter.ipynb)
|
||||
|
||||
### Moderation
|
||||
You can also access the OpenAI content moderation endpoint with
|
||||
|
||||
```python
|
||||
from langchain.chains import OpenAIModerationChain
|
||||
```
|
||||
For a more detailed walkthrough of this, see [this notebook](../modules/chains/examples/moderation.ipynb)
|
@ -0,0 +1,20 @@
|
||||
# Pinecone
|
||||
|
||||
This page covers how to use the Pinecone ecosystem within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific Pinecone wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- Install the Python SDK with `pip install pinecone-client`
|
||||
## Wrappers
|
||||
|
||||
### VectorStore
|
||||
|
||||
There exists a wrapper around Pinecone indexes, allowing you to use it as a vectorstore,
|
||||
whether for semantic search or example selection.
|
||||
|
||||
To import this vectorstore:
|
||||
```python
|
||||
from langchain.vectorstores import Pinecone
|
||||
```
|
||||
|
||||
For a more detailed walkthrough of the Pinecone wrapper, see [this notebook](../modules/utils/combine_docs_examples/vectorstores.ipynb)
|
@ -0,0 +1,31 @@
|
||||
# SerpAPI
|
||||
|
||||
This page covers how to use the SerpAPI search APIs within LangChain.
|
||||
It is broken into two parts: installation and setup, and then references to specific Pinecone wrappers.
|
||||
|
||||
## Installation and Setup
|
||||
- Install requirements with `pip install google-search-results`
|
||||
- Get a SerpAPI api key and either set it as an environment variable (`SERPAPI_API_KEY`)
|
||||
|
||||
## Wrappers
|
||||
|
||||
### Utility
|
||||
|
||||
There exists a SerpAPI utility which wraps this API. To import this utility:
|
||||
|
||||
```python
|
||||
from langchain.utilities import SerpAPIWrapper
|
||||
```
|
||||
|
||||
For a more detailed walkthrough of this wrapper, see [this notebook](../modules/utils/examples/serpapi.ipynb).
|
||||
|
||||
### Tool
|
||||
|
||||
You can also easily load this wrapper as a Tool (to use with an Agent).
|
||||
You can do this with:
|
||||
```python
|
||||
from langchain.agents import load_tools
|
||||
tools = load_tools(["serpapi"])
|
||||
```
|
||||
|
||||
For more information on this, see [this page](../modules/agents/tools.md)
|
@ -1,50 +0,0 @@
|
||||
Agents
|
||||
======
|
||||
|
||||
The first category of how-to guides here cover specific parts of working with agents.
|
||||
|
||||
`Custom Tools <agents/custom_tools.ipynb>`_: How to create custom tools that an agent can use.
|
||||
|
||||
`Intermediate Steps <agents/intermediate_steps.ipynb>`_: How to access and use intermediate steps to get more visibility into the internals of an agent.
|
||||
|
||||
`Custom Agent <agents/custom_agent.ipynb>`_: How to create a custom agent (specifically, a custom LLM + prompt to drive that agent).
|
||||
|
||||
`Multi Input Tools <agents/multi_input_tool.ipynb>`_: How to use a tool that requires multiple inputs with an agent.
|
||||
|
||||
|
||||
The next set of examples are all end-to-end agents for specific applications.
|
||||
In all examples there is an Agent with a particular set of tools.
|
||||
|
||||
- Tools: A tool can be anything that takes in a string and returns a string. This means that you can use both the primitives AND the chains found in `this <chains.rst>`_ documentation. LangChain also provides a list of easily loadable tools. For detailed information on those, please see `this documentation <../explanation/tools.md>`_
|
||||
- Agents: An agent uses an LLMChain to determine which tools to use. For a list of all available agent types, see `here <../explanation/agents.md>`_.
|
||||
|
||||
**MRKL**
|
||||
|
||||
- **Tools used**: Search, SQLDatabaseChain, LLMMathChain
|
||||
- **Agent used**: `zero-shot-react-description`
|
||||
- `Paper <https://arxiv.org/pdf/2205.00445.pdf>`_
|
||||
- **Note**: This is the most general purpose example, so if you are looking to use an agent with arbitrary tools, please start here.
|
||||
- `Example Notebook <agents/mrkl.ipynb>`_
|
||||
|
||||
**Self-Ask-With-Search**
|
||||
|
||||
- **Tools used**: Search
|
||||
- **Agent used**: `self-ask-with-search`
|
||||
- `Paper <https://ofir.io/self-ask.pdf>`_
|
||||
- `Example Notebook <agents/self_ask_with_search.ipynb>`_
|
||||
|
||||
**ReAct**
|
||||
|
||||
- **Tools used**: Wikipedia Docstore
|
||||
- **Agent used**: `react-docstore`
|
||||
- `Paper <https://arxiv.org/pdf/2210.03629.pdf>`_
|
||||
- `Example Notebook <agents/react.ipynb>`_
|
||||
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
agents/*
|
@ -1,48 +0,0 @@
|
||||
Chains
|
||||
======
|
||||
|
||||
The examples here are all end-to-end chains for specific applications.
|
||||
A chain is made up of links, which can be either primitives or other chains.
|
||||
|
||||
The following primitives exist as options to use for links:
|
||||
|
||||
#. `LLM: <../reference/modules/llms.rst>`_ A language model takes text as input and outputs text.
|
||||
#. `PromptTemplate: <../reference/modules/prompt.rst>`_ A prompt template takes arbitrary string inputs and returns a final formatted string.
|
||||
#. `Python REPL: <../reference/modules/python.rst>`_ A Python REPL takes a string representing a Python command to run, runs that command, and then returns anything that was printed during that run.
|
||||
#. `SQL Database: <../reference/modules/sql_database.rst>`_ A SQL database takes a string representing a SQL command as input and executes that command against the database. If any rows are returned, then those are cast to a string and returned.
|
||||
#. `Search: <../reference/modules/serpapi.rst>`_ A search object takes a string as input and executes that against a search object, returning any results.
|
||||
|
||||
With these primitives in mind, the following chains exist:
|
||||
|
||||
**LLMChain**
|
||||
|
||||
- **Links Used**: PromptTemplate, LLM
|
||||
- **Notes**: This chain is the simplest chain, and is widely used by almost every other chain. This chain takes arbitrary user input, creates a prompt with it from the PromptTemplate, passes that to the LLM, and then returns the output of the LLM as the final output.
|
||||
- `Example Notebook <chains/llm_chain.ipynb>`_
|
||||
|
||||
**LLMMath**
|
||||
|
||||
- **Links Used**: Python REPL, LLMChain
|
||||
- **Notes**: This chain takes user input (a math question), uses an LLMChain to convert it to python code snippet to run in the Python REPL, and then returns that as the result.
|
||||
- `Example Notebook <chains/llm_math.ipynb>`_
|
||||
|
||||
**PAL**
|
||||
|
||||
- **Links Used**: Python REPL, LLMChain
|
||||
- **Notes**: This chain takes user input (a reasoning question), uses an LLMChain to convert it to python code snippet to run in the Python REPL, and then returns that as the result.
|
||||
- `Paper <https://arxiv.org/abs/2211.10435>`_
|
||||
- `Example Notebook <chains/pal.ipynb>`_
|
||||
|
||||
**SQLDatabase Chain**
|
||||
|
||||
- **Links Used**: SQLDatabase, LLMChain
|
||||
- **Notes**: This chain takes user input (a question), uses a first LLM chain to construct a SQL query to run against the SQL database, and then uses another LLMChain to take the results of that query and use it to answer the original question.
|
||||
- `Example Notebook <chains/sqlite.ipynb>`_
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:caption: Chains
|
||||
:hidden:
|
||||
|
||||
chains/*
|
@ -1,32 +0,0 @@
|
||||
Data Augmented Generation
|
||||
=========================
|
||||
|
||||
The walkthroughs here are related to data augmented generation.
|
||||
They cover either how to work with the components of data augmented generation (documents, embeddings, and vectorstores), or are end-to-end examples for using these components.
|
||||
|
||||
**Components**
|
||||
|
||||
`Text Splitters <data_augmented_generation/textsplitter.ipynb>`_: A walkthrough of how to split large documents up into smaller, more manageable pieces of text.
|
||||
|
||||
`Embeddings & VectorStores <data_augmented_generation/embeddings.ipynb>`_: A walkthrough of the different embedding and vectorstore functionalies that LangChain supports.
|
||||
|
||||
|
||||
**Examples**
|
||||
|
||||
`Question Answering <data_augmented_generation/question_answering.ipynb>`_: A walkthrough of how to use LangChain for question answering over specific documents.
|
||||
|
||||
`Question Answering with Sources <data_augmented_generation/qa_with_sources.ipynb>`_: A walkthrough of how to use LangChain for question answering (with sources) over specific documents.
|
||||
|
||||
`Summarization <data_augmented_generation/summarize.ipynb>`_: A walkthrough of how to use LangChain for summarization over specific documents.
|
||||
|
||||
`Vector DB Question Answering <data_augmented_generation/vector_db_qa.ipynb>`_: A walkthrough of how to use LangChain for question answering over a vector database.
|
||||
|
||||
`Vector DB Question Answering with Sources <data_augmented_generation/vector_db_qa_with_sources.ipynb>`_: A walkthrough of how to use LangChain for question answering (with sources) over a vector database.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
data_augmented_generation/*
|
@ -1,18 +0,0 @@
|
||||
Evaluation
|
||||
==============
|
||||
|
||||
The examples here all highlight how to use language models to assist in evaluation of themselves.
|
||||
|
||||
`Question Answering <evaluation/question_answering.ipynb>`_: An overview of LLMs aimed at evaluating question answering systems in general.
|
||||
|
||||
`Data Augmented Question Answering <evaluation/data_augmented_question_answering.ipynb>`_: An end-to-end example of evaluating a question answering system focused on a specific document (a VectorDBQAChain to be precise). This example highlights how to use LLMs to come up with question/answer examples to evaluate over, and then highlights how to use LLMs to evaluate performance on those generated examples.
|
||||
|
||||
`Hugging Face Datasets <evaluation/huggingface_datasets.ipynb>`_: Covers an example of loading and using a dataset from Hugging Face for evaluation.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
evaluation/*
|
@ -1,24 +0,0 @@
|
||||
Memory
|
||||
======
|
||||
|
||||
The examples here all highlight how to use memory in different ways.
|
||||
|
||||
`Adding Memory <memory/adding_memory.ipynb>`_: How to add a memory component to any single input chain.
|
||||
|
||||
`Adding Memory to Multi-Input Chain <memory/adding_memory_chain_multiple_inputs.ipynb>`_: How to add a memory component to any multiple input chain.
|
||||
|
||||
`Conversational Memory Types <memory/conversational_memory.ipynb>`_: An overview of the different types of conversation memory you can load and use with a conversation-like chain.
|
||||
|
||||
`Conversational Memory Customization <memory/conversational_customization.ipynb>`_: How to customize existing conversation memory components.
|
||||
|
||||
`Custom Memory <memory/custom_memory.ipynb>`_: How to write your own custom memory component.
|
||||
|
||||
`Adding Memory to Agents <memory/agent_with_memory.ipynb>`_: How to add a memory component to any agent.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
memory/*
|
@ -1,45 +0,0 @@
|
||||
LLMs & Prompts
|
||||
==============
|
||||
|
||||
The examples here all highlight how to work with LLMs and prompts.
|
||||
|
||||
**LLMs**
|
||||
|
||||
`LLM Functionality <prompts/llm_functionality.ipynb>`_: A walkthrough of all the functionality the standard LLM interface exposes.
|
||||
|
||||
`LLM Serialization <prompts/llm_serialization.ipynb>`_: A walkthrough of how to serialize LLMs to and from disk.
|
||||
|
||||
`LLM Caching <prompts/llm_caching.ipynb>`_: Covers different types of caches, and how to use a cache to save results of LLM calls.
|
||||
|
||||
`Custom LLM <prompts/custom_llm.ipynb>`_: How to create and use a custom LLM class, in case you have an LLM not from one of the standard providers (including one that you host yourself).
|
||||
|
||||
|
||||
**Specific LLM Integrations**
|
||||
|
||||
`Huggingface Hub <prompts/huggingface_hub.ipynb>`_: Covers how to connect to LLMs hosted on HuggingFace Hub.
|
||||
|
||||
`Azure OpenAI <prompts/azure_openai_example.ipynb>`_: Covers how to connect to Azure-hosted OpenAI Models.
|
||||
|
||||
|
||||
|
||||
**Prompts**
|
||||
|
||||
`Prompt Management <prompts/prompt_management.ipynb>`_: A walkthrough of all the functionality LangChain supports for working with prompts.
|
||||
|
||||
`Prompt Serialization <prompts/prompt_serialization.ipynb>`_: A walkthrough of how to serialize prompts to and from disk.
|
||||
|
||||
`Few Shot Examples <prompts/few_shot_examples.ipynb>`_: How to include examples in the prompt.
|
||||
|
||||
`Generate Examples <prompts/generate_examples.ipynb>`_: How to use existing examples to generate more examples.
|
||||
|
||||
`Custom Example Selector <prompts/custom_example_selector.ipynb>`_: How to create and use a custom ExampleSelector (the class responsible for choosing which examples to use in a prompt).
|
||||
|
||||
`Custom Prompt Template <prompts/custom_prompt_template.ipynb>`_: How to create and use a custom PromptTemplate, the logic that decides how input variables get formatted into a prompt.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
prompts/*
|
@ -1,176 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f897c784",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Custom ExampleSelector\n",
|
||||
"\n",
|
||||
"This notebook goes over how to implement a custom ExampleSelector. ExampleSelectors are used to select examples to use in few shot prompts.\n",
|
||||
"\n",
|
||||
"An ExampleSelector must implement two methods:\n",
|
||||
"\n",
|
||||
"1. An `add_example` method which takes in an example and adds it into the ExampleSelector\n",
|
||||
"2. A `select_examples` method which takes in input variables (which are meant to be user input) and returns a list of examples to use in the few shot prompt.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Let's implement a custom ExampleSelector that just selects two examples at random."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "1a945da1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts.example_selector.base import BaseExampleSelector\n",
|
||||
"from typing import Dict, List\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "62cf0ad7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class CustomExampleSelector(BaseExampleSelector):\n",
|
||||
" \n",
|
||||
" def __init__(self, examples: List[Dict[str, str]]):\n",
|
||||
" self.examples = examples\n",
|
||||
" \n",
|
||||
" def add_example(self, example: Dict[str, str]) -> None:\n",
|
||||
" \"\"\"Add new example to store for a key.\"\"\"\n",
|
||||
" self.examples.append(example)\n",
|
||||
"\n",
|
||||
" def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:\n",
|
||||
" \"\"\"Select which examples to use based on the inputs.\"\"\"\n",
|
||||
" return np.random.choice(self.examples, size=2, replace=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "242d3213",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"examples = [{\"foo\": \"1\"}, {\"foo\": \"2\"}, {\"foo\": \"3\"}]\n",
|
||||
"example_selector = CustomExampleSelector(examples)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2a038065",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's now try it out! We can select some examples and try adding examples."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "74fbbef5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([{'foo': '2'}, {'foo': '3'}], dtype=object)"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"example_selector.select_examples({\"foo\": \"foo\"})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "9bbb5421",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"example_selector.add_example({\"foo\": \"4\"})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "c0eb9f22",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"example_selector.examples"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "cc39b1e3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([{'foo': '1'}, {'foo': '4'}], dtype=object)"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"example_selector.select_examples({\"foo\": \"foo\"})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1739dd96",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
@ -1,116 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a37d9694",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Custom Prompt Template\n",
|
||||
"\n",
|
||||
"This notebook goes over how to create a custom prompt template, in case you want to create your own methodology for creating prompts.\n",
|
||||
"\n",
|
||||
"The only two requirements for all prompt templates are:\n",
|
||||
"\n",
|
||||
"1. They have a `input_variables` attribute that exposes what input variables this prompt template expects.\n",
|
||||
"2. They expose a `format` method which takes in keyword arguments corresponding to the expected `input_variables` and returns the formatted prompt.\n",
|
||||
"\n",
|
||||
"Let's imagine that we want to create a prompt template that takes in input variables and formats them into the template AFTER capitalizing them. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "26f796e5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts import BasePromptTemplate\n",
|
||||
"from pydantic import BaseModel"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "27919e96",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class CustomPromptTemplate(BasePromptTemplate, BaseModel):\n",
|
||||
" template: str\n",
|
||||
" \n",
|
||||
" def format(self, **kwargs) -> str:\n",
|
||||
" capitalized_kwargs = {k: v.upper() for k, v in kwargs.items()}\n",
|
||||
" return self.template.format(**capitalized_kwargs)\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "76d1d84d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can now see that when we use this, the input variables get formatted."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "eed1ff28",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = CustomPromptTemplate(input_variables=[\"foo\"], template=\"Capitalized: {foo}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "94892a3c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Capitalized: LOWERCASE'"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt.format(foo=\"lowercase\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d3d9a7c7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
# Cool Demos
|
||||
|
||||
Lots of people have built some pretty awesome stuff with LangChain.
|
||||
This is a collection of our favorites.
|
||||
If you see any other demos that you think we should highlight, be sure to let us know!
|
||||
|
||||
## Open Source
|
||||
|
||||
### [YouTube Transcription Question Answering with Sources](https://colab.research.google.com/drive/1sKSTjt9cPstl_WMZ86JsgEqFG-aSAwkn?usp=sharing)
|
||||
An end-to-end example of doing question answering on YouTube transcripts, returning the timestamps as sources to legitimize the answer.
|
||||
|
||||
### [ThoughtSource](https://github.com/OpenBioLink/ThoughtSource)
|
||||
A central, open resource and community around data and tools related to chain-of-thought reasoning in large language models.
|
||||
|
||||
### [Notion Database Question-Answering Bot](https://github.com/hwchase17/notion-qa)
|
||||
Open source GitHub project shows how to use LangChain to create a
|
||||
chatbot that can answer questions about an arbitrary Notion database.
|
||||
|
||||
### [GPT Index](https://github.com/jerryjliu/gpt_index)
|
||||
GPT Index is a project consisting of a set of data structures that are created using GPT-3 and can be traversed using GPT-3 in order to answer queries.
|
||||
|
||||
### [Grover's Algorithm](https://github.com/JavaFXpert/llm-grovers-search-party)
|
||||
Leveraging Qiskit, OpenAI and LangChain to demonstrate Grover's algorithm
|
||||
|
||||
### [ReAct TextWorld](https://colab.research.google.com/drive/19WTIWC3prw5LDMHmRMvqNV2loD9FHls6?usp=sharing)
|
||||
Leveraging the ReActTextWorldAgent to play TextWorld with an LLM!
|
||||
|
||||
|
||||
## Not Open Source
|
||||
|
||||
### [Daimon](https://twitter.com/sjwhitmore/status/1580593217153531908?s=20&t=neQvtZZTlp623U3LZwz3bQ)
|
||||
A chat-based AI personal assistant with long-term memory about you.
|
||||
|
||||
### [Clerkie](https://twitter.com/krrish_dh/status/1581028925618106368?s=20&t=neQvtZZTlp623U3LZwz3bQ)
|
||||
Stack Tracing QA Bot to help debug complex stack tracing (especially the ones that go multi-function/file deep).
|
||||
|
||||
### [Sales Email Writer](https://twitter.com/Raza_Habib496/status/1596880140490838017?s=20&t=6MqEQYWfSqmJwsKahjCVOA)
|
||||
By Raza Habib, this demo utilizes LangChain + SerpAPI + HumanLoop to write sales emails.
|
||||
Give it a company name and a person, this application will use Google Search (via SerpAPI) to get
|
||||
more information on the company and the person, and then write them a sales message.
|
||||
|
||||
### [Question-Answering on a Web Browser](https://twitter.com/chillzaza_/status/1592961099384905730?s=20&t=EhU8jl0KyCPJ7vE9Rnz-cQ)
|
||||
By Zahid Khawaja, this demo utilizes question answering to answer questions about a given website.
|
||||
A followup added this for [YouTube videos](https://twitter.com/chillzaza_/status/1593739682013220865?s=20&t=EhU8jl0KyCPJ7vE9Rnz-cQ),
|
||||
and then another followup added it for [Wikipedia](https://twitter.com/chillzaza_/status/1594847151238037505?s=20&t=EhU8jl0KyCPJ7vE9Rnz-cQ).
|
@ -1,37 +0,0 @@
|
||||
# Core Concepts
|
||||
|
||||
This section goes over the core concepts of LangChain.
|
||||
Understanding these will go a long way in helping you understand the codebase and how to construct chains.
|
||||
|
||||
## PromptTemplates
|
||||
PromptTemplates generically have a `format` method that takes in variables and returns a formatted string.
|
||||
The most simple implementation of this is to have a template string with some variables in it, and then format it with the incoming variables.
|
||||
More complex iterations dynamically construct the template string from few shot examples, etc.
|
||||
|
||||
For a more detailed explanation of how LangChain approaches prompts and prompt templates, see [here](/examples/prompts/prompt_management).
|
||||
|
||||
## LLMs
|
||||
Wrappers around Large Language Models (in particular, the `generate` ability of large language models) are at the core of LangChain functionality.
|
||||
These wrappers are classes that are callable: they take in an input string, and return the generated output string.
|
||||
|
||||
## Embeddings
|
||||
These classes are very similar to the LLM classes in that they are wrappers around models,
|
||||
but rather than return a string they return an embedding (list of floats). These are particularly useful when
|
||||
implementing semantic search functionality. They expose separate methods for embedding queries versus embedding documents.
|
||||
|
||||
## Vectorstores
|
||||
These are datastores that store documents. They expose a method for passing in a string and finding similar documents.
|
||||
|
||||
## Chains
|
||||
These are pipelines that combine multiple of the above ideas.
|
||||
They vary greatly in complexity and are combination of generic, highly configurable pipelines and more narrow (but usually more complex) pipelines.
|
||||
|
||||
## Agents
|
||||
As opposed to a chain, whether the steps to be taken are known ahead of time, agents
|
||||
use an LLM to determine which tools to call and in what order.
|
||||
|
||||
## Memory
|
||||
By default, Chains and Agents are stateless, meaning that they treat each incoming query independently.
|
||||
In some applications (chatbots being a GREAT example) it is highly important to remember previous interactions,
|
||||
both at a short term but also at a long term level. The concept of "Memory" exists to do exactly that.
|
||||
|
@ -1,37 +0,0 @@
|
||||
# Setting up your environment
|
||||
|
||||
Using LangChain will usually require integrations with one or more model providers, data stores, apis, etc.
|
||||
There are two components to setting this up, installing the correct python packages and setting the right environment variables.
|
||||
|
||||
## Python packages
|
||||
The python package needed varies based on the integration. See the list of integrations for details.
|
||||
There should also be helpful error messages raised if you try to run an integration and are missing any required python packages.
|
||||
|
||||
## Environment Variables
|
||||
The environment variable needed varies based on the integration. See the list of integrations for details.
|
||||
There should also be helpful error messages raised if you try to run an integration and are missing any required environment variables.
|
||||
|
||||
You can set the environment variable in a few ways.
|
||||
If you are trying to set the environment variable `FOO` to value `bar`, here are the ways you could do so:
|
||||
- From the command line:
|
||||
```
|
||||
export FOO=bar
|
||||
```
|
||||
- From the python notebook/script:
|
||||
```python
|
||||
import os
|
||||
os.environ["FOO"] = "bar"
|
||||
```
|
||||
|
||||
For the Getting Started example, we will be using OpenAI's APIs, so we will first need to install their SDK:
|
||||
|
||||
```
|
||||
pip install openai
|
||||
```
|
||||
|
||||
We will then need to set the environment variable. Let's do this from inside the Jupyter notebook (or Python script).
|
||||
|
||||
```python
|
||||
import os
|
||||
os.environ["OPENAI_API_KEY"] = "..."
|
||||
```
|
@ -0,0 +1,276 @@
|
||||
# Quickstart Guide
|
||||
|
||||
|
||||
This tutorial gives you a quick walkthrough about building an end-to-end language model application with LangChain.
|
||||
|
||||
## Installation
|
||||
|
||||
To get started, install LangChain with the following command:
|
||||
|
||||
```bash
|
||||
pip install langchain
|
||||
```
|
||||
|
||||
|
||||
## Environment Setup
|
||||
|
||||
Using LangChain will usually require integrations with one or more model providers, data stores, apis, etc.
|
||||
|
||||
For this example, we will be using OpenAI's APIs, so we will first need to install their SDK:
|
||||
|
||||
```bash
|
||||
pip install openai
|
||||
```
|
||||
|
||||
We will then need to set the environment variable in the terminal.
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY="..."
|
||||
```
|
||||
|
||||
Alternatively, you could do this from inside the Jupyter notebook (or Python script):
|
||||
|
||||
```python
|
||||
import os
|
||||
os.environ["OPENAI_API_KEY"] = "..."
|
||||
```
|
||||
|
||||
|
||||
## Building a Language Model Application
|
||||
|
||||
Now that we have installed LangChain and set up our environment, we can start building our language model application.
|
||||
|
||||
LangChain provides many modules that can be used to build language model applications. Modules can be combined to create more complex applications, or be used individually for simple applications.
|
||||
|
||||
|
||||
|
||||
`````{dropdown} LLMs: Get predictions from a language model
|
||||
|
||||
The most basic building block of LangChain is calling an LLM on some input.
|
||||
Let's walk through a simple example of how to do this.
|
||||
For this purpose, let's pretend we are building a service that generates a company name based on what the company makes.
|
||||
|
||||
In order to do this, we first need to import the LLM wrapper.
|
||||
|
||||
```python
|
||||
from langchain.llms import OpenAI
|
||||
```
|
||||
|
||||
We can then initialize the wrapper with any arguments.
|
||||
In this example, we probably want the outputs to be MORE random, so we'll initialize it with a HIGH temperature.
|
||||
|
||||
```python
|
||||
llm = OpenAI(temperature=0.9)
|
||||
```
|
||||
|
||||
We can now call it on some input!
|
||||
|
||||
```python
|
||||
text = "What would be a good company name a company that makes colorful socks?"
|
||||
print(llm(text))
|
||||
```
|
||||
|
||||
```pycon
|
||||
Feetful of Fun
|
||||
```
|
||||
|
||||
For more details on how to use LLMs within LangChain, see the [LLM getting started guide](../modules/llms/getting_started.ipynb).
|
||||
`````
|
||||
|
||||
|
||||
`````{dropdown} Prompt Templates: Manage prompts for LLMs
|
||||
|
||||
Calling an LLM is a great first step, but it's just the beginning.
|
||||
Normally when you use an LLM in an application, you are not sending user input directly to the LLM.
|
||||
Instead, you are probably taking user input and constructing a prompt, and then sending that to the LLM.
|
||||
|
||||
For example, in the previous example, the text we passed in was hardcoded to ask for a name for a company that made colorful socks.
|
||||
In this imaginary service, what we would want to do is take only the user input describing what the company does, and then format the prompt with that information.
|
||||
|
||||
This is easy to do with LangChain!
|
||||
|
||||
First lets define the prompt template:
|
||||
|
||||
```python
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
prompt = PromptTemplate(
|
||||
input_variables=["product"],
|
||||
template="What is a good name for a company that makes {product}?",
|
||||
)
|
||||
```
|
||||
|
||||
Let's now see how this works! We can call the `.format` method to format it.
|
||||
|
||||
```python
|
||||
print(prompt.format(product="colorful socks"))
|
||||
```
|
||||
|
||||
```pycon
|
||||
What is a good name for a company that makes colorful socks?
|
||||
```
|
||||
|
||||
|
||||
[For more details, check out the getting started guide for prompts.](../modules/prompts/getting_started.ipynb)
|
||||
|
||||
`````
|
||||
|
||||
|
||||
|
||||
`````{dropdown} Chains: Combine LLMs and prompts in multi-step workflows
|
||||
|
||||
Up until now, we've worked with the PromptTemplate and LLM primitives by themselves. But of course, a real application is not just one primitive, but rather a combination of them.
|
||||
|
||||
A chain in LangChain is made up of links, which can be either primitives like LLMs or other chains.
|
||||
|
||||
The most core type of chain is an LLMChain, which consists of a PromptTemplate and an LLM.
|
||||
|
||||
Extending the previous example, we can construct an LLMChain which takes user input, formats it with a PromptTemplate, and then passes the formatted response to an LLM.
|
||||
|
||||
```python
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain.llms import OpenAI
|
||||
|
||||
llm = OpenAI(temperature=0.9)
|
||||
prompt = PromptTemplate(
|
||||
input_variables=["product"],
|
||||
template="What is a good name for a company that makes {product}?",
|
||||
)
|
||||
```
|
||||
|
||||
We can now create a very simple chain that will take user input, format the prompt with it, and then send it to the LLM:
|
||||
|
||||
```python
|
||||
from langchain.chains import LLMChain
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
```
|
||||
|
||||
Now we can run that chain only specifying the product!
|
||||
|
||||
```python
|
||||
chain.run("colorful socks")
|
||||
# -> '\n\nSocktastic!'
|
||||
```
|
||||
|
||||
There we go! There's the first chain - an LLM Chain.
|
||||
This is one of the simpler types of chains, but understanding how it works will set you up well for working with more complex chains.
|
||||
|
||||
[For more details, check out the getting started guide for chains.](../modules/chains/getting_started.ipynb)
|
||||
|
||||
`````
|
||||
|
||||
|
||||
`````{dropdown} Agents: Dynamically call chains based on user input
|
||||
|
||||
So for the chains we've looked at run in a predetermined order.
|
||||
|
||||
Agents no longer do: they use an LLM to determine which actions to take and in what order. An action can either be using a tool and observing its output, or returning to the user.
|
||||
|
||||
When used correctly agents can be extremely powerful. In this tutorial, we show you how to easily use agents through the simplest, highest level API.
|
||||
|
||||
|
||||
In order to load agents, you should understand the following concepts:
|
||||
|
||||
- Tool: A function that performs a specific duty. This can be things like: Google Search, Database lookup, Python REPL, other chains. The interface for a tool is currently a function that is expected to have a string as an input, with a string as an output.
|
||||
- LLM: The language model powering the agent.
|
||||
- Agent: The agent to use. This should be a string that references a support agent class. Because this notebook focuses on the simplest, highest level API, this only covers using the standard supported agents. If you want to implement a custom agent, see the documentation for custom agents (coming soon).
|
||||
|
||||
**Agents**: For a list of supported agents and their specifications, see [here](../modules/agents/agents.md).
|
||||
|
||||
**Tools**: For a list of predefined tools and their specifications, see [here](../modules/agents/tools.md).
|
||||
|
||||
|
||||
```python
|
||||
from langchain.agents import load_tools
|
||||
from langchain.agents import initialize_agent
|
||||
from langchain.llms import OpenAI
|
||||
|
||||
# First, let's load the language model we're going to use to control the agent.
|
||||
llm = OpenAI(temperature=0)
|
||||
|
||||
# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.
|
||||
tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
||||
|
||||
|
||||
# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
|
||||
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
|
||||
|
||||
# Now let's test it out!
|
||||
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")
|
||||
```
|
||||
|
||||
```pycon
|
||||
Entering new AgentExecutor chain...
|
||||
I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.
|
||||
Action: Search
|
||||
Action Input: "Olivia Wilde boyfriend"
|
||||
Observation: Jason Sudeikis
|
||||
Thought: I need to find out Jason Sudeikis' age
|
||||
Action: Search
|
||||
Action Input: "Jason Sudeikis age"
|
||||
Observation: 47 years
|
||||
Thought: I need to calculate 47 raised to the 0.23 power
|
||||
Action: Calculator
|
||||
Action Input: 47^0.23
|
||||
Observation: Answer: 2.4242784855673896
|
||||
|
||||
Thought: I now know the final answer
|
||||
Final Answer: Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.
|
||||
> Finished AgentExecutor chain.
|
||||
"Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896."
|
||||
```
|
||||
|
||||
|
||||
`````
|
||||
|
||||
|
||||
`````{dropdown} Memory: Add state to chains and agents
|
||||
|
||||
So far, all the chains and agents we've gone through have been stateless. But often, you may want a chain or agent to have some concept of "memory" so that it may remember information about its previous interactions. The clearest and simple example of this is when designing a chatbot - you want it to remember previous messages so it can use context from that to have a better conversation. This would be a type of "short-term memory". On the more complex side, you could imagine a chain/agent remembering key pieces of information over time - this would be a form of "long-term memory". For more concrete ideas on the latter, see this [awesome paper](https://memprompt.com/).
|
||||
|
||||
LangChain provides several specially created chains just for this purpose. This notebook walks through using one of those chains (the `ConversationChain`) with two different types of memory.
|
||||
|
||||
By default, the `ConversationChain` has a simple type of memory that remembers all previous inputs/outputs and adds them to the context that is passed. Let's take a look at using this chain (setting `verbose=True` so we can see the prompt).
|
||||
|
||||
```python
|
||||
from langchain import OpenAI, ConversationChain
|
||||
|
||||
llm = OpenAI(temperature=0)
|
||||
conversation = ConversationChain(llm=llm, verbose=True)
|
||||
|
||||
conversation.predict(input="Hi there!")
|
||||
```
|
||||
|
||||
```pycon
|
||||
> Entering new chain...
|
||||
Prompt after formatting:
|
||||
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
|
||||
|
||||
Current conversation:
|
||||
|
||||
Human: Hi there!
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
' Hello! How are you today?'
|
||||
```
|
||||
|
||||
```python
|
||||
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
|
||||
```
|
||||
|
||||
```pycon
|
||||
> Entering new chain...
|
||||
Prompt after formatting:
|
||||
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
|
||||
|
||||
Current conversation:
|
||||
|
||||
Human: Hi there!
|
||||
AI: Hello! How are you today?
|
||||
Human: I'm doing well! Just having a conversation with an AI.
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
" That's great! What would you like to talk about?"
|
||||
```
|
@ -1,11 +0,0 @@
|
||||
# Installation
|
||||
|
||||
LangChain is available on PyPi, so to it is easily installable with:
|
||||
|
||||
```
|
||||
pip install langchain
|
||||
```
|
||||
|
||||
For more involved installation options, see the [Installation Reference](/installation.md) section.
|
||||
|
||||
That's it! LangChain is now installed. You can now use LangChain from a python script or Jupyter notebook.
|
@ -1,25 +0,0 @@
|
||||
# Calling a LLM
|
||||
|
||||
The most basic building block of LangChain is calling an LLM on some input.
|
||||
Let's walk through a simple example of how to do this.
|
||||
For this purpose, let's pretend we are building a service that generates a company name based on what the company makes.
|
||||
|
||||
In order to do this, we first need to import the LLM wrapper.
|
||||
|
||||
```python
|
||||
from langchain.llms import OpenAI
|
||||
```
|
||||
|
||||
We can then initialize the wrapper with any arguments.
|
||||
In this example, we probably want the outputs to be MORE random, so we'll initialize it with a HIGH temperature.
|
||||
|
||||
```python
|
||||
llm = OpenAI(temperature=0.9)
|
||||
```
|
||||
|
||||
We can now call it on some input!
|
||||
|
||||
```python
|
||||
text = "What would be a good company name a company that makes colorful socks?"
|
||||
print(llm(text))
|
||||
```
|
@ -1,37 +0,0 @@
|
||||
# LLM Chains
|
||||
|
||||
Calling an LLM is a great first step, but it's just the beginning.
|
||||
Normally when you use an LLM in an application, you are not sending user input directly to the LLM.
|
||||
Instead, you are probably taking user input and constructing a prompt, and then sending that to the LLM.
|
||||
|
||||
For example, in the previous example, the text we passed in was hardcoded to ask for a name for a company that made colorful socks.
|
||||
In this imaginary service, what we would want to do is take only the user input describing what the company does, and then format the prompt with that information.
|
||||
|
||||
This is easy to do with LangChain!
|
||||
|
||||
First lets define the prompt:
|
||||
|
||||
```python
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
prompt = PromptTemplate(
|
||||
input_variables=["product"],
|
||||
template="What is a good name for a company that makes {product}?",
|
||||
)
|
||||
```
|
||||
|
||||
We can now create a very simple chain that will take user input, format the prompt with it, and then send it to the LLM:
|
||||
|
||||
```python
|
||||
from langchain.chains import LLMChain
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
```
|
||||
|
||||
Now we can run that chain only specifying the product!
|
||||
|
||||
```python
|
||||
chain.run("colorful socks")
|
||||
```
|
||||
|
||||
There we go! There's the first chain - an LLM Chain.
|
||||
This is one of the simpler types of chains, but understanding how it works will set you up well for working with more complex chains.
|
@ -1,197 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d31df93e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Memory\n",
|
||||
"So far, all the chains and agents we've gone through have been stateless. But often, you may want a chain or agent to have some concept of \"memory\" so that it may remember information about its previous interactions. The clearest and simple example of this is when designing a chatbot - you want it to remember previous messages so it can use context from that to have a better conversation. This would be a type of \"short-term memory\". On the more complex side, you could imagine a chain/agent remembering key pieces of information over time - this would be a form of \"long-term memory\". For more concrete ideas on the latter, see this [awesome paper](https://memprompt.com/).\n",
|
||||
"\n",
|
||||
"LangChain provides several specially created chains just for this purpose. This notebook walks through using one of those chains (the `ConversationChain`) with two different types of memory."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d051c1da",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### ConversationChain with default memory\n",
|
||||
"By default, the `ConversationChain` has a simple type of memory that remembers all previous inputs/outputs and adds them to the context that is passed. Let's take a look at using this chain (setting `verbose=True` so we can see the prompt)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "ae046bff",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
||||
"Prompt after formatting:\n",
|
||||
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
|
||||
"\n",
|
||||
"Current conversation:\n",
|
||||
"\n",
|
||||
"Human: Hi there!\n",
|
||||
"AI:\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"' Hello! How are you today?'"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain import OpenAI, ConversationChain\n",
|
||||
"\n",
|
||||
"llm = OpenAI(temperature=0)\n",
|
||||
"conversation = ConversationChain(llm=llm, verbose=True)\n",
|
||||
"\n",
|
||||
"conversation.predict(input=\"Hi there!\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "d8e2a6ff",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
||||
"Prompt after formatting:\n",
|
||||
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
|
||||
"\n",
|
||||
"Current conversation:\n",
|
||||
"\n",
|
||||
"Human: Hi there!\n",
|
||||
"AI: Hello! How are you today?\n",
|
||||
"Human: I'm doing well! Just having a conversation with an AI.\n",
|
||||
"AI:\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\" That's great! What would you like to talk about?\""
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"conversation.predict(input=\"I'm doing well! Just having a conversation with an AI.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "15eda316",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
||||
"Prompt after formatting:\n",
|
||||
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
|
||||
"\n",
|
||||
"Current conversation:\n",
|
||||
"\n",
|
||||
"Human: Hi there!\n",
|
||||
"AI: Hello! How are you today?\n",
|
||||
"Human: I'm doing well! Just having a conversation with an AI.\n",
|
||||
"AI: That's great! What would you like to talk about?\n",
|
||||
"Human: Tell me about yourself.\n",
|
||||
"AI:\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"' I am an AI created to provide information and support to humans. I enjoy learning and exploring new things.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"conversation.predict(input=\"Tell me about yourself.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5c8735cc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### More Resources on Memory\n",
|
||||
"\n",
|
||||
"This just scratches the surface of what you can do with memory. \n",
|
||||
"\n",
|
||||
"For more concrete examples of conversational memory, please see [this notebook](../examples/memory/conversational_memory)\n",
|
||||
"\n",
|
||||
"For more examples on things like how to implement custom memory classes, how to add memory to a custom LLM chain and how to use memory with an agent, please see the [How-To: Memory](../examples/memoryrst) section. \n",
|
||||
"\n",
|
||||
"For even more advanced ideas on memory (which will hopefully be included in LangChain soon!) see the [MemPrompt](https://memprompt.com/) paper."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "436dda66",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
Agents
|
||||
==========================
|
||||
|
||||
Some applications will require not just a predetermined chain of calls to LLMs/other tools,
|
||||
but potentially an unknown chain that depends on the user input.
|
||||
In these types of chains, there is a “agent” which has access to a suite of tools.
|
||||
Depending on the user input, the agent can then decide which, if any, of these tools to call.
|
||||
|
||||
The following sections of documentation are provided:
|
||||
|
||||
- `Getting Started <agents/getting_started.html>`_: A notebook to help you get started working with agents as quickly as possible.
|
||||
|
||||
- `Key Concepts <agents/key_concepts.html>`_: A conceptual guide going over the various concepts related to agents.
|
||||
|
||||
- `How-To Guides <agents/how_to_guides.html>`_: A collection of how-to guides. These highlight how to integrate various types of tools, how to work with different types of agent, and how to customize agents.
|
||||
|
||||
- `Reference </reference/modules/agents.html>`_: API reference documentation for all Agent classes.
|
||||
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:caption: Agents
|
||||
:name: Agents
|
||||
:hidden:
|
||||
|
||||
agents/getting_started.ipynb
|
||||
agents/key_concepts.md
|
||||
agents/how_to_guides.rst
|
||||
Reference</reference/modules/agents.rst>
|
@ -0,0 +1,61 @@
|
||||
How-To Guides
|
||||
=============
|
||||
|
||||
The first category of how-to guides here cover specific parts of working with agents.
|
||||
|
||||
`Custom Tools <examples/custom_tools.html>`_: How to create custom tools that an agent can use.
|
||||
|
||||
`Intermediate Steps <examples/intermediate_steps.html>`_: How to access and use intermediate steps to get more visibility into the internals of an agent.
|
||||
|
||||
`Custom Agent <examples/custom_agent.html>`_: How to create a custom agent (specifically, a custom LLM + prompt to drive that agent).
|
||||
|
||||
`Multi Input Tools <examples/multi_input_tool.html>`_: How to use a tool that requires multiple inputs with an agent.
|
||||
|
||||
`Search Tools <examples/search_tools.html>`_: How to use the different type of search tools that LangChain supports.
|
||||
|
||||
`Max Iterations <examples/max_iterations.html>`_: How to restrict an agent to a certain number of iterations.
|
||||
|
||||
|
||||
The next set of examples are all end-to-end agents for specific applications.
|
||||
In all examples there is an Agent with a particular set of tools.
|
||||
|
||||
- Tools: A tool can be anything that takes in a string and returns a string. This means that you can use both the primitives AND the chains found in `this <chains.html>`_ documentation. LangChain also provides a list of easily loadable tools. For detailed information on those, please see `this documentation <../explanation/tools.html>`_
|
||||
- Agents: An agent uses an LLMChain to determine which tools to use. For a list of all available agent types, see `here <../explanation/agents.html>`_.
|
||||
|
||||
**MRKL**
|
||||
|
||||
- **Tools used**: Search, SQLDatabaseChain, LLMMathChain
|
||||
- **Agent used**: `zero-shot-react-description`
|
||||
- `Paper <https://arxiv.org/pdf/2205.00445.pdf>`_
|
||||
- **Note**: This is the most general purpose example, so if you are looking to use an agent with arbitrary tools, please start here.
|
||||
- `Example Notebook <implementations/mrkl.html>`_
|
||||
|
||||
**Self-Ask-With-Search**
|
||||
|
||||
- **Tools used**: Search
|
||||
- **Agent used**: `self-ask-with-search`
|
||||
- `Paper <https://ofir.io/self-ask.pdf>`_
|
||||
- `Example Notebook <implementations/self_ask_with_search.html>`_
|
||||
|
||||
**ReAct**
|
||||
|
||||
- **Tools used**: Wikipedia Docstore
|
||||
- **Agent used**: `react-docstore`
|
||||
- `Paper <https://arxiv.org/pdf/2210.03629.pdf>`_
|
||||
- `Example Notebook <implementations/react.html>`_
|
||||
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
examples/*
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
implementations/*
|
@ -0,0 +1,10 @@
|
||||
# Key Concepts
|
||||
|
||||
## Agents
|
||||
Agents use an LLM to determine which actions to take and in what order.
|
||||
For more detailed information on agents, and different types of agents in LangChain, see [this documentation](agents.md).
|
||||
|
||||
## Tools
|
||||
Tools are functions that agents can use to interact with the world.
|
||||
These tools can be generic utilities (e.g. search), other chains, or even other agents.
|
||||
For more detailed information on tools, and different types of tools in LangChain, see [this documentation](tools.md).
|
@ -0,0 +1,29 @@
|
||||
Chains
|
||||
==========================
|
||||
|
||||
Using an LLM in isolation is fine for some simple applications,
|
||||
but many more complex ones require chaining LLMs - either with eachother or with other experts.
|
||||
LangChain provides a standard interface for Chains, as well as some common implementations of chains for easy use.
|
||||
|
||||
The following sections of documentation are provided:
|
||||
|
||||
- `Getting Started <chains/getting_started.html>`_: A getting started guide for chains, to get you up and running quickly.
|
||||
|
||||
- `Key Concepts <chains/key_concepts.html>`_: A conceptual guide going over the various concepts related to chains.
|
||||
|
||||
- `How-To Guides <chains/how_to_guides.html>`_: A collection of how-to guides. These highlight how to use various types of chains.
|
||||
|
||||
- `Reference </reference/chains.html>`_: API reference documentation for all Chain classes.
|
||||
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:caption: Chains
|
||||
:name: Chains
|
||||
:hidden:
|
||||
|
||||
chains/getting_started.ipynb
|
||||
chains/how_to_guides.rst
|
||||
chains/key_concepts.rst
|
||||
Reference</reference/modules/chains.rst>
|
@ -0,0 +1,41 @@
|
||||
# CombineDocuments Chains
|
||||
CombineDocuments chains are useful for when you need to run a language over multiple documents.
|
||||
Common use cases for this include question answering, question answering with sources, summarization, and more.
|
||||
For more information on specific use cases as well as different methods for **fetching** these documents, please see
|
||||
[this overview](/use_cases/combine_docs.md).
|
||||
|
||||
This documentation now picks up from after you've fetched your documents - now what?
|
||||
How do you pass them to the language model in a format it can understand?
|
||||
There are a few different methods, or chains, for doing so. LangChain supports three of the more common ones - and
|
||||
we are actively looking to include more, so if you have any ideas please reach out! Note that there is not
|
||||
one best method - the decision of which one to use is often very context specific. In order from simplest to
|
||||
most complex:
|
||||
|
||||
## Stuffing
|
||||
Stuffing is the simplest method, whereby you simply stuff all the related data into the prompt as context
|
||||
to pass to the language model. This is implemented in LangChain as the `StuffDocumentsChain`.
|
||||
|
||||
**Pros:** Only makes a single call to the LLM. When generating text, the LLM has access to all the data at once.
|
||||
|
||||
**Cons:** Most LLMs have a context length, and for large documents (or many documents) this will not work as it will result in a prompt larger than the context length.
|
||||
|
||||
The main downside of this method is that it only works one smaller pieces of data. Once you are working
|
||||
with many pieces of data, this approach is no longer feasible. The next two approaches are designed to help deal with that.
|
||||
|
||||
## Map Reduce
|
||||
This method involves an initial prompt on each chunk of data (for summarization tasks, this
|
||||
could be a summary of that chunk; for question-answering tasks, it could be an answer based solely on that chunk).
|
||||
Then a different prompt is run to combine all the initial outputs. This is implemented in the LangChain as the `MapReduceDocumentsChain`.
|
||||
|
||||
**Pros:** Can scale to larger documents (and more documents) than `StuffDocumentsChain`. The calls to the LLM on individual documents are independent and can therefore be parallelized.
|
||||
|
||||
**Cons:** Requires many more calls to the LLM than `StuffDocumentsChain`. Loses some information during the final combining call.
|
||||
|
||||
## Refine
|
||||
This method involves an initial prompt on the first chunk of data, generating some output.
|
||||
For the remaining documents, that output is passed in, along with the next document,
|
||||
asking the LLM to refine the output based on the new document.
|
||||
|
||||
**Pros:** Can pull in more relevant context, and may be less lossy than `MapReduceDocumentsChain`.
|
||||
|
||||
**Cons:** Requires many more calls to the LLM than `StuffDocumentsChain`. The calls are also NOT independent, meaning they cannot be paralleled like `MapReduceDocumentsChain`. There is also some potential dependencies on the ordering of the documents.
|
@ -0,0 +1,26 @@
|
||||
CombineDocuments Chains
|
||||
-----------------------
|
||||
|
||||
A chain is made up of links, which can be either primitives or other chains.
|
||||
Primitives can be either `prompts <../prompts.html>`_, `llms <../llms.html>`_, `utils <../utils.html>`_, or other chains.
|
||||
The examples here are all end-to-end chains for working with documents.
|
||||
|
||||
`Question Answering <combine_docs_examples/question_answering.html>`_: A walkthrough of how to use LangChain for question answering over specific documents.
|
||||
|
||||
`Question Answering with Sources <combine_docs_examples/qa_with_sources.html>`_: A walkthrough of how to use LangChain for question answering (with sources) over specific documents.
|
||||
|
||||
`Summarization <combine_docs_examples/summarize.html>`_: A walkthrough of how to use LangChain for summarization over specific documents.
|
||||
|
||||
`Vector DB Question Answering <combine_docs_examples/vector_db_qa.html>`_: A walkthrough of how to use LangChain for question answering over a vector database.
|
||||
|
||||
`Vector DB Question Answering with Sources <combine_docs_examples/vector_db_qa_with_sources.html>`_: A walkthrough of how to use LangChain for question answering (with sources) over a vector database.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:caption: CombineDocument Chains
|
||||
:name: combine_docs
|
||||
:hidden:
|
||||
|
||||
combine_docs_examples/*
|
@ -0,0 +1,33 @@
|
||||
Generic Chains
|
||||
--------------
|
||||
|
||||
A chain is made up of links, which can be either primitives or other chains.
|
||||
Primitives can be either `prompts <../prompts.html>`_, `llms <../llms.html>`_, `utils <../utils.html>`_, or other chains.
|
||||
The examples here are all generic end-to-end chains that are meant to be used to construct other chains rather than serving a specific purpose.
|
||||
|
||||
**LLMChain**
|
||||
|
||||
- **Links Used**: PromptTemplate, LLM
|
||||
- **Notes**: This chain is the simplest chain, and is widely used by almost every other chain. This chain takes arbitrary user input, creates a prompt with it from the PromptTemplate, passes that to the LLM, and then returns the output of the LLM as the final output.
|
||||
- `Example Notebook <generic/llm_chain.html>`_
|
||||
|
||||
**Transformation Chain**
|
||||
|
||||
- **Links Used**: TransformationChain
|
||||
- **Notes**: This notebook shows how to use the Transformation Chain, which takes an arbitrary python function and applies it to inputs/outputs of other chains.
|
||||
- `Example Notebook <generic/transformation.html>`_
|
||||
|
||||
**Sequential Chain**
|
||||
|
||||
- **Links Used**: Sequential
|
||||
- **Notes**: This notebook shows how to combine calling multiple other chains in sequence.
|
||||
- `Example Notebook <generic/sequential_chains.html>`_
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:caption: Generic Chains
|
||||
:name: generic
|
||||
:hidden:
|
||||
|
||||
generic/*
|
@ -0,0 +1,278 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Getting Started\n",
|
||||
"\n",
|
||||
"In this tutorial, we will learn about creating simple chains in LangChain. We will learn how to create a chain, add components to it, and run it.\n",
|
||||
"\n",
|
||||
"In this tutorial, we will cover:\n",
|
||||
"- Using the simple LLM chain\n",
|
||||
"- Creating sequential chains\n",
|
||||
"- Creating a custom chain\n",
|
||||
"\n",
|
||||
"## Why do we need chains?\n",
|
||||
"\n",
|
||||
"Chains allow us to combine multiple components together to create a single, coherent application. For example, we can create a chain that takes user input, format it with a PromptTemplate, and then passes the formatted response to an LLM. We can build more complex chains by combining multiple chains together, or by combining chains with other components.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Query an LLM with the `LLMChain`\n",
|
||||
"\n",
|
||||
"The `LLMChain` is a simple chain that takes in a prompt template, formats it with the user input and returns the response from an LLM.\n",
|
||||
"\n",
|
||||
"To use the `LLMChain`, first create a prompt template."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain.llms import OpenAI\n",
|
||||
"\n",
|
||||
"llm = OpenAI(temperature=0.9)\n",
|
||||
"prompt = PromptTemplate(\n",
|
||||
" input_variables=[\"product\"],\n",
|
||||
" template=\"What is a good name for a company that makes {product}?\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can now create a very simple chain that will take user input, format the prompt with it, and then send it to the LLM."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"Vibrancy Socks.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"chain = LLMChain(llm=llm, prompt=prompt)\n",
|
||||
"\n",
|
||||
"# Run the chain only specifying the input variable.\n",
|
||||
"print(chain.run(\"colorful socks\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This is one of the simpler types of chains, but understanding how it works will set you up well for working with more complex chains."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Combine chains with the `SequentialChain`\n",
|
||||
"\n",
|
||||
"The next step after calling a language model is make a series of calls to a language model. We can do this using sequential chains, which are chains that execute their links in a predefined order. Specifically, we will use the `SimpleSequentialChain`. This is the simplest form of sequential chains, where each step has a singular input/output, and the output of one step is the input to the next.\n",
|
||||
"\n",
|
||||
"In this tutorial, our sequential chain will:\n",
|
||||
"1. First, create a company name for a product. We will reuse the `LLMChain` we'd previously initialized to create this company name.\n",
|
||||
"2. Then, create a catchphrase for the product. We will initialize a new `LLMChain` to create this catchphrase, as shown below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"second_prompt = PromptTemplate(\n",
|
||||
" input_variables=[\"company_name\"],\n",
|
||||
" template=\"Write a catchphrase for the following company: {company_name}\",\n",
|
||||
")\n",
|
||||
"chain_two = LLMChain(llm=llm, prompt=second_prompt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now we can combine the two LLMChains, so that we can create a company name and a catchphrase in a single step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
|
||||
"\u001b[36;1m\u001b[1;3m\n",
|
||||
"\n",
|
||||
"Cheerful Toes.\u001b[0m\n",
|
||||
"\u001b[33;1m\u001b[1;3m\n",
|
||||
"\n",
|
||||
"\"Spread smiles from your toes!\"\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished SimpleSequentialChain chain.\u001b[0m\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\"Spread smiles from your toes!\"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain.chains import SimpleSequentialChain\n",
|
||||
"overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)\n",
|
||||
"\n",
|
||||
"# Run the chain specifying only the input variable for the first chain.\n",
|
||||
"catchphrase = overall_chain.run(\"colorful socks\")\n",
|
||||
"print(catchphrase)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Create a custom chain with the `Chain` class\n",
|
||||
"\n",
|
||||
"LangChain provides many chains out of the box, but sometimes you may want to create a custom chains for your specific use case. For this example, we will create a custom chain that concatenates the outputs of 2 `LLMChain`s.\n",
|
||||
"\n",
|
||||
"In order to create a custom chain:\n",
|
||||
"1. Start by subclassing the `Chain` class,\n",
|
||||
"2. Fill out the `input_keys` and `output_keys` properties,\n",
|
||||
"3. Add the `_call` method that shows how to execute the chain.\n",
|
||||
"\n",
|
||||
"These steps are demonstrated in the example below:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain.chains.base import Chain\n",
|
||||
"\n",
|
||||
"from typing import Dict, List\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class ConcatenateChain(Chain):\n",
|
||||
" chain_1: LLMChain\n",
|
||||
" chain_2: LLMChain\n",
|
||||
"\n",
|
||||
" @property\n",
|
||||
" def input_keys(self) -> List[str]:\n",
|
||||
" # Union of the input keys of the two chains.\n",
|
||||
" all_input_vars = set(self.chain_1.input_keys).union(set(self.chain_2.input_keys))\n",
|
||||
" return list(all_input_vars)\n",
|
||||
"\n",
|
||||
" @property\n",
|
||||
" def output_keys(self) -> List[str]:\n",
|
||||
" return ['concat_output']\n",
|
||||
"\n",
|
||||
" def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:\n",
|
||||
" output_1 = self.chain_1.run(inputs)\n",
|
||||
" output_2 = self.chain_2.run(inputs)\n",
|
||||
" return {'concat_output': output_1 + output_2}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now, we can try running the chain that we called."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Concatenated output:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Rainbow Socks Co.\n",
|
||||
"\n",
|
||||
"\"Step Into Colorful Comfort!\"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt_1 = PromptTemplate(\n",
|
||||
" input_variables=[\"product\"],\n",
|
||||
" template=\"What is a good name for a company that makes {product}?\",\n",
|
||||
")\n",
|
||||
"chain_1 = LLMChain(llm=llm, prompt=prompt_1)\n",
|
||||
"\n",
|
||||
"prompt_2 = PromptTemplate(\n",
|
||||
" input_variables=[\"product\"],\n",
|
||||
" template=\"What is a good slogan for a company that makes {product}?\",\n",
|
||||
")\n",
|
||||
"chain_2 = LLMChain(llm=llm, prompt=prompt_2)\n",
|
||||
"\n",
|
||||
"concat_chain = ConcatenateChain(chain_1=chain_1, chain_2=chain_2)\n",
|
||||
"concat_output = concat_chain.run(\"colorful socks\")\n",
|
||||
"print(f\"Concatenated output:\\n{concat_output}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"That's it! For more details about how to do cool things with Chains, check out the [how-to guide](how_to_guides.rst) for chains."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.9"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
How-To Guides
|
||||
=============
|
||||
|
||||
A chain is made up of links, which can be either primitives or other chains.
|
||||
Primitives can be either `prompts <../prompts.html>`_, `llms <../llms.html>`_, `utils <../utils.html>`_, or other chains.
|
||||
The examples here are all end-to-end chains for specific applications.
|
||||
They are broken up into three categories:
|
||||
|
||||
1. `Generic Chains <generic_how_to.html>`_: Generic chains, that are meant to help build other chains rather than serve a particular purpose.
|
||||
2. `CombineDocuments Chains <combine_docs_how_to.html>`_: Chains aimed at making it easy to work with documents (question answering, summarization, etc).
|
||||
3. `Utility Chains <utility_how_to.html>`_: Chains consisting of an LLMChain interacting with a specific util.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
generic_how_to.rst
|
||||
combine_docs_how_to.rst
|
||||
utility_how_to.rst
|
@ -0,0 +1,21 @@
|
||||
# Key Concepts
|
||||
|
||||
## Chains
|
||||
A chain is made up of links, which can be either primitives or other chains.
|
||||
They vary greatly in complexity and are combination of generic, highly configurable pipelines and more narrow (but usually more complex) pipelines.
|
||||
|
||||
## Sequential Chain
|
||||
This is a specific type of chain where multiple other chains are run in sequence, with the outputs being added as inputs
|
||||
to the next. A subtype of this type of chain is the `SimpleSequentialChain`, where all subchains have only one input and one output,
|
||||
and the output of one is therefor used as sole input to the next chain.
|
||||
|
||||
## CombineDocuments Chains
|
||||
These are a subset of chains designed to work with documents. There are two pieces to consider:
|
||||
|
||||
1. The underlying chain method (eg, how the documents are combined)
|
||||
2. Use cases for these types of chains.
|
||||
|
||||
For the first, please see [this documentation](combine_docs.md) for more detailed information on the types of chains LangChain supports.
|
||||
For the second, please see the Use Cases section for more information on [question answering](/use_cases/question_answering.md),
|
||||
[question answering with sources](/use_cases/qa_with_sources.md), and [summarization](/use_cases/summarization.md).
|
||||
|
@ -0,0 +1,59 @@
|
||||
Utility Chains
|
||||
--------------
|
||||
|
||||
A chain is made up of links, which can be either primitives or other chains.
|
||||
Primitives can be either `prompts <../prompts.html>`_, `llms <../llms.html>`_, `utils <../utils.html>`_, or other chains.
|
||||
The examples here are all end-to-end chains for specific applications, focused on interacting an LLMChain with a specific utility.
|
||||
|
||||
**LLMMath**
|
||||
|
||||
- **Links Used**: Python REPL, LLMChain
|
||||
- **Notes**: This chain takes user input (a math question), uses an LLMChain to convert it to python code snippet to run in the Python REPL, and then returns that as the result.
|
||||
- `Example Notebook <examples/llm_math.html>`_
|
||||
|
||||
**PAL**
|
||||
|
||||
- **Links Used**: Python REPL, LLMChain
|
||||
- **Notes**: This chain takes user input (a reasoning question), uses an LLMChain to convert it to python code snippet to run in the Python REPL, and then returns that as the result.
|
||||
- `Paper <https://arxiv.org/abs/2211.10435>`_
|
||||
- `Example Notebook <examples/pal.html>`_
|
||||
|
||||
**SQLDatabase Chain**
|
||||
|
||||
- **Links Used**: SQLDatabase, LLMChain
|
||||
- **Notes**: This chain takes user input (a question), uses a first LLM chain to construct a SQL query to run against the SQL database, and then uses another LLMChain to take the results of that query and use it to answer the original question.
|
||||
- `Example Notebook <examples/sqlite.html>`_
|
||||
|
||||
**LLMBash Chain**
|
||||
|
||||
- **Links Used**: BashProcess, LLMChain
|
||||
- **Notes**: This chain takes user input (a question), uses an LLM chain to convert it to a bash command to run in the terminal, and then returns that as the result.
|
||||
- `Example Notebook <examples/llm_bash.html>`_
|
||||
|
||||
**LLMChecker Chain**
|
||||
|
||||
- **Links Used**: LLMChain
|
||||
- **Notes**: This chain takes user input (a question), uses an LLM chain to answer that question, and then uses other LLMChains to self-check that answer.
|
||||
- `Example Notebook <examples/llm_checker.html>`_
|
||||
|
||||
**LLMRequests Chain**
|
||||
|
||||
- **Links Used**: Requests, LLMChain
|
||||
- **Notes**: This chain takes a URL and other inputs, uses Requests to get the data at that URL, and then passes that along with the other inputs into an LLMChain to generate a response. The example included shows how to ask a question to Google - it firsts constructs a Google url, then fetches the data there, then passes that data + the original question into an LLMChain to get an answer.
|
||||
- `Example Notebook <examples/llm_requests.html>`_
|
||||
|
||||
**Moderation Chain**
|
||||
|
||||
- **Links Used**: LLMChain, ModerationChain
|
||||
- **Notes**: This chain shows how to use OpenAI's content moderation endpoint to screen output, and shows how to connect this to an LLMChain.
|
||||
- `Example Notebook <examples/moderation.html>`_
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:caption: Generic Chains
|
||||
:name: generic
|
||||
:hidden:
|
||||
|
||||
examples/*
|
@ -0,0 +1,27 @@
|
||||
LLMs
|
||||
==========================
|
||||
|
||||
Large Language Models (LLMs) are a core component of LangChain.
|
||||
LangChain is a provider of LLMs, but rather provides a standard interface through which
|
||||
you can interact with a variety of LLMs.
|
||||
|
||||
The following sections of documentation are provided:
|
||||
|
||||
- `Getting Started <llms/getting_started.html>`_: An overview of all the functionality the LangChain LLM class provides.
|
||||
|
||||
- `Key Concepts <llms/key_concepts.html>`_: A conceptual guide going over the various concepts related to LLMs.
|
||||
|
||||
- `How-To Guides <llms/how_to_guides.html>`_: A collection of how-to guides. These highlight how to accomplish various objectives with our LLM class, as well as how to integrate with various LLM providers.
|
||||
|
||||
- `Reference </reference/modules/llms.html>`_: API reference documentation for all LLM classes.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:name: LLMs
|
||||
:hidden:
|
||||
|
||||
llms/key_concepts.md
|
||||
llms/getting_started.ipynb
|
||||
llms/how_to_guides.rst
|
||||
Reference</reference/modules/llms.rst>
|
@ -0,0 +1,20 @@
|
||||
Generic Functionality
|
||||
=====================
|
||||
|
||||
The examples here all address certain "how-to" guides for working with LLMs.
|
||||
|
||||
`LLM Serialization <examples/llm_serialization.html>`_: A walkthrough of how to serialize LLMs to and from disk.
|
||||
|
||||
`LLM Caching <examples/llm_caching.html>`_: Covers different types of caches, and how to use a cache to save results of LLM calls.
|
||||
|
||||
`Custom LLM <examples/custom_llm.html>`_: How to create and use a custom LLM class, in case you have an LLM not from one of the standard providers (including one that you host yourself).
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:caption: Generic Functionality
|
||||
:name: Generic Functionality
|
||||
:hidden:
|
||||
|
||||
examples/*
|
@ -0,0 +1,17 @@
|
||||
How-To Guides
|
||||
=============
|
||||
|
||||
The examples here all address certain "how-to" guides for working with LLMs.
|
||||
They are split into two categories:
|
||||
|
||||
|
||||
1. `Generic Functionality <generic_how_to.html>`_: Covering generic functionality all LLMs should have.
|
||||
2. `Integrations <integrations.html>`_: Covering integrations with various LLM providers.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
generic_how_to.rst
|
||||
integrations.rst
|
@ -0,0 +1,20 @@
|
||||
Integrations
|
||||
=============
|
||||
|
||||
The examples here are all "how-to" guides for how to integrate with various LLM providers.
|
||||
|
||||
`Huggingface Hub <examples/huggingface_hub.html>`_: Covers how to connect to LLMs hosted on HuggingFace Hub.
|
||||
|
||||
`Azure OpenAI <examples/azure_openai_example.html>`_: Covers how to connect to Azure-hosted OpenAI Models.
|
||||
|
||||
`Manifest <examples/manifest.html>`_: Covers how to utilize the Manifest wrapper.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:caption: Specific LLM Integrations
|
||||
:name: Specific LLM Integrations
|
||||
:hidden:
|
||||
|
||||
integrations/*
|
@ -0,0 +1,19 @@
|
||||
# Key Concepts
|
||||
|
||||
## LLMs
|
||||
Wrappers around Large Language Models (in particular, the "generate" ability of large language models) are at the core of LangChain functionality.
|
||||
The core method that these classes expose is a `generate` method, which takes in a list of strings and returns an LLMResult (which contains outputs for all input strings).
|
||||
Read more about LLMResult. This interface operates over a list of strings because often the lists of strings can be batched to the LLM provider,
|
||||
providing speed and efficiency gains.
|
||||
For convenience, this class also exposes a simpler, more user friendly interface (via `__call__`).
|
||||
The interface for this takes in a single string, and returns a single string.
|
||||
|
||||
## Generation
|
||||
The output of a single generation. Currently in LangChain this is just the generated text, although could be extended in the future
|
||||
to contain log probs or the like.
|
||||
|
||||
## LLMResult
|
||||
The full output of a call to the `generate` method of the LLM class.
|
||||
Since the `generate` method takes as input a list of strings, this returns a list of results.
|
||||
Each result consists of a list of generations (since you can request N generations per input string).
|
||||
This also contains a `llm_output` attribute which contains provider-specific information about the call.
|
@ -0,0 +1,27 @@
|
||||
Memory
|
||||
==========================
|
||||
|
||||
By default, Chains and Agents are stateless,
|
||||
meaning that they treat each incoming query independently.
|
||||
In some applications (chatbots being a GREAT example) it is highly important
|
||||
to remember previous interactions, both at a short term but also at a long term level.
|
||||
The concept of “Memory” exists to do exactly that.
|
||||
|
||||
The following sections of documentation are provided:
|
||||
|
||||
- `Getting Started <memory/getting_started.html>`_: An overview of how to get started with different types of memory.
|
||||
|
||||
- `Key Concepts <memory/key_concepts.html>`_: A conceptual guide going over the various concepts related to memory.
|
||||
|
||||
- `How-To Guides <memory/how_to_guides.html>`_: A collection of how-to guides. These highlight how to work with different types of memory, as well as how to customize memory.
|
||||
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:caption: Memory
|
||||
:name: Memory
|
||||
|
||||
memory/getting_started.ipynb
|
||||
memory/key_concepts.rst
|
||||
memory/how_to_guides.rst
|
@ -0,0 +1,24 @@
|
||||
How-To Guides
|
||||
=============
|
||||
|
||||
The examples here all highlight how to use memory in different ways.
|
||||
|
||||
`Adding Memory <examples/adding_memory.html>`_: How to add a memory component to any single input chain.
|
||||
|
||||
`ChatGPT Clone <examples/chatgpt_clone.html>`_: How to recreate ChatGPT with LangChain prompting + memory components.
|
||||
|
||||
`Adding Memory to Multi-Input Chain <examples/adding_memory_chain_multiple_inputs.html>`_: How to add a memory component to any multiple input chain.
|
||||
|
||||
`Conversational Memory Customization <examples/conversational_customization.html>`_: How to customize existing conversation memory components.
|
||||
|
||||
`Custom Memory <examples/custom_memory.html>`_: How to write your own custom memory component.
|
||||
|
||||
`Adding Memory to Agents <examples/agent_with_memory.html>`_: How to add a memory component to any agent.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
:hidden:
|
||||
|
||||
examples/*
|
@ -0,0 +1,14 @@
|
||||
# Key Concepts
|
||||
|
||||
## Memory
|
||||
By default, Chains and Agents are stateless, meaning that they treat each incoming query independently.
|
||||
In some applications (chatbots being a GREAT example) it is highly important to remember previous interactions,
|
||||
both at a short term but also at a long term level. The concept of "Memory" exists to do exactly that.
|
||||
|
||||
## Conversational Memory
|
||||
One of the simpler forms of memory occurs in chatbots, where they remember previous conversations.
|
||||
There are a few different ways to accomplish this:
|
||||
- Buffer: This is just passing in the past `N` interactions in as context. `N` can be chosen based on a fixed number, the length of the interactions, or other!
|
||||
- Summary: This involves summarizing previous conversations and passing that summary in, instead of the raw dialouge itself. Compared to `Buffer`, this compresses information: meaning it is more lossy, but also less likely to run into context length limits.
|
||||
- Combination: A combination of the above two approaches, where you compute a summary but also pass in some previous interfactions directly!
|
||||
|
@ -0,0 +1,30 @@
|
||||
Prompt Templates
|
||||
==========================
|
||||
|
||||
Language models take text as input - that text is commonly referred to as a prompt.
|
||||
Typically this is not simply a hardcoded string but rather a combination of a template, some examples, and user input.
|
||||
LangChain provides several classes and functions to make constructing and working with prompts easy.
|
||||
|
||||
The following sections of documentation are provided:
|
||||
|
||||
- `Getting Started <prompts/getting_started.html>`_: An overview of all the functionality LangChain provides for working with and constructing prompts.
|
||||
|
||||
- `Key Concepts <prompts/key_concepts.html>`_: A conceptual guide going over the various concepts related to prompts.
|
||||
|
||||
- `How-To Guides <prompts/how_to_guides.html>`_: A collection of how-to guides. These highlight how to accomplish various objectives with our prompt class.
|
||||
|
||||
- `Reference </reference/prompts.html>`_: API reference documentation for all prompt classes.
|
||||
|
||||
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:caption: Prompt Templates
|
||||
:name: Prompts
|
||||
:hidden:
|
||||
|
||||
prompts/getting_started.md
|
||||
prompts/key_concepts.md
|
||||
prompts/how_to_guides.rst
|
||||
Reference</reference/prompts.rst>
|
@ -0,0 +1,68 @@
|
||||
# Create a custom example selector
|
||||
|
||||
In this tutorial, we'll create a custom example selector that selects examples every alternate example given a list of examples.
|
||||
|
||||
An `ExampleSelector` must implement two methods:
|
||||
|
||||
1. An `add_example` method which takes in an example and adds it into the ExampleSelector
|
||||
2. A `select_examples` method which takes in input variables (which are meant to be user input) and returns a list of examples to use in the few shot prompt.
|
||||
|
||||
Let's implement a custom `ExampleSelector` that just selects two examples at random.
|
||||
|
||||
:::{note}
|
||||
Take a look at the current set of example selector implementations supported in LangChain [here](../getting_started.md).
|
||||
:::
|
||||
|
||||
<!-- TODO(shreya): Add the correct link. -->
|
||||
|
||||
## Implement custom example selector
|
||||
|
||||
```python
|
||||
from langchain.prompts.example_selector.base import BaseExampleSelector
|
||||
from typing import Dict, List
|
||||
import numpy as np
|
||||
|
||||
|
||||
class CustomExampleSelector(BaseExampleSelector):
|
||||
|
||||
def __init__(self, examples: List[Dict[str, str]]):
|
||||
self.examples = examples
|
||||
|
||||
def add_example(self, example: Dict[str, str]) -> None:
|
||||
"""Add new example to store for a key."""
|
||||
self.examples.append(example)
|
||||
|
||||
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
|
||||
"""Select which examples to use based on the inputs."""
|
||||
return np.random.choice(self.examples, size=2, replace=False)
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Use custom example selector
|
||||
|
||||
```python
|
||||
|
||||
examples = [
|
||||
{"foo": "1"},
|
||||
{"foo": "2"},
|
||||
{"foo": "3"}
|
||||
]
|
||||
|
||||
# Initialize example selector.
|
||||
example_selector = CustomExampleSelector(examples)
|
||||
|
||||
|
||||
# Select examples
|
||||
example_selector.select_examples({"foo": "foo"})
|
||||
# -> array([{'foo': '2'}, {'foo': '3'}], dtype=object)
|
||||
|
||||
# Add new example to the set of examples
|
||||
example_selector.add_example({"foo": "4"})
|
||||
example_selector.examples
|
||||
# -> [{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]
|
||||
|
||||
# Select examples
|
||||
example_selector.select_examples({"foo": "foo"})
|
||||
# -> array([{'foo': '1'}, {'foo': '4'}], dtype=object)
|
||||
```
|
@ -0,0 +1,75 @@
|
||||
# Create a custom prompt template
|
||||
|
||||
Let's suppose we want the LLM to generate English language explanations of a function given its name. To achieve this task, we will create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.
|
||||
|
||||
## Why are custom prompt templates needed?
|
||||
|
||||
LangChain provides a set of default prompt templates that can be used to generate prompts for a variety of tasks. However, there may be cases where the default prompt templates do not meet your needs. For example, you may want to create a prompt template with specific dynamic instructions for your language model. In such cases, you can create a custom prompt template.
|
||||
|
||||
:::{note}
|
||||
Take a look at the current set of default prompt templates [here](../prompt_templates.md).
|
||||
:::
|
||||
<!-- TODO(shreya): Add correct link here. -->
|
||||
|
||||
## Create a custom prompt template
|
||||
|
||||
The only two requirements for all prompt templates are:
|
||||
|
||||
1. They have a input_variables attribute that exposes what input variables this prompt template expects.
|
||||
2. They expose a format method which takes in keyword arguments corresponding to the expected input_variables and returns the formatted prompt.
|
||||
|
||||
Let's create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.
|
||||
|
||||
First, let's create a function that will return the source code of a function given its name.
|
||||
|
||||
```python
|
||||
import inspect
|
||||
|
||||
def get_source_code(function_name):
|
||||
# Get the source code of the function
|
||||
return inspect.getsource(function_name)
|
||||
```
|
||||
|
||||
Next, we'll create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.
|
||||
|
||||
```python
|
||||
from langchain.prompts import BasePromptTemplate
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class FunctionExplainerPromptTemplate(BasePromptTemplate, BaseModel):
|
||||
""" A custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function. """
|
||||
|
||||
@validator("input_variables")
|
||||
def validate_input_variables(cls, v):
|
||||
""" Validate that the input variables are correct. """
|
||||
if len(v) != 1 or "function_name" not in v:
|
||||
raise ValueError("function_name must be the only input_variable.")
|
||||
return v
|
||||
|
||||
def format(self, **kwargs) -> str:
|
||||
# Get the source code of the function
|
||||
source_code = get_source_code(kwargs["function_name"])
|
||||
|
||||
# Generate the prompt to be sent to the language model
|
||||
prompt = f"""
|
||||
Given the function name and source code, generate an English language explanation of the function.
|
||||
Function Name: {kwargs["function_name"]}
|
||||
Source Code:
|
||||
{source_code}
|
||||
Explanation:
|
||||
"""
|
||||
return prompt
|
||||
```
|
||||
|
||||
## Use the custom prompt template
|
||||
|
||||
Now that we have created a custom prompt template, we can use it to generate prompts for our task.
|
||||
|
||||
```python
|
||||
fn_explainer = FunctionExplainerPromptTemplate(input_variables=["function_name"])
|
||||
|
||||
# Generate a prompt for the function "get_source_code"
|
||||
prompt = fn_explainer.format(function_name=get_source_code)
|
||||
print(prompt)
|
||||
```
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue