diff --git a/docs/conf.py b/docs/conf.py index 7851401c..cc862581 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -22,13 +22,15 @@ with open("../pyproject.toml") as f: # -- Project information ----------------------------------------------------- -project = "LangChain" +project = "🦜🔗 LangChain" copyright = "2022, Harrison Chase" author = "Harrison Chase" version = data["tool"]["poetry"]["version"] release = version +html_title = project + " " + version + # -- General configuration --------------------------------------------------- @@ -42,10 +44,11 @@ extensions = [ "sphinx.ext.napoleon", "sphinx.ext.viewcode", "sphinxcontrib.autodoc_pydantic", - "myst_parser", - "nbsphinx", + "myst_nb", "sphinx_panels", + "IPython.sphinxext.ipython_console_highlighting", ] +source_suffix = [".rst", ".md"] autodoc_pydantic_model_show_json = False @@ -73,8 +76,7 @@ exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # -html_theme = "sphinx_rtd_theme" -# html_theme = "sphinx_typlog_theme" +html_theme = "sphinx_book_theme" html_context = { "display_github": True, # Integrate GitHub @@ -88,3 +90,5 @@ html_context = { # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path: list = [] +nb_execution_mode = "off" +myst_enable_extensions = ["colon_fence"] diff --git a/docs/ecosystem.rst b/docs/ecosystem.rst new file mode 100644 index 00000000..53f1fc11 --- /dev/null +++ b/docs/ecosystem.rst @@ -0,0 +1,10 @@ +LangChain Ecosystem +=================== + +Guides for how other companies/products can be used with LangChain + +.. toctree:: + :maxdepth: 1 + :glob: + + ecosystem/* diff --git a/docs/ecosystem/ai21.md b/docs/ecosystem/ai21.md new file mode 100644 index 00000000..fb675ab5 --- /dev/null +++ b/docs/ecosystem/ai21.md @@ -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 +``` diff --git a/docs/ecosystem/cohere.md b/docs/ecosystem/cohere.md new file mode 100644 index 00000000..682a44f1 --- /dev/null +++ b/docs/ecosystem/cohere.md @@ -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) diff --git a/docs/ecosystem/google_search.md b/docs/ecosystem/google_search.md new file mode 100644 index 00000000..a85a3765 --- /dev/null +++ b/docs/ecosystem/google_search.md @@ -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) diff --git a/docs/ecosystem/hazy_research.md b/docs/ecosystem/hazy_research.md new file mode 100644 index 00000000..5e04760f --- /dev/null +++ b/docs/ecosystem/hazy_research.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 +``` diff --git a/docs/ecosystem/huggingface.md b/docs/ecosystem/huggingface.md new file mode 100644 index 00000000..1595c21f --- /dev/null +++ b/docs/ecosystem/huggingface.md @@ -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) diff --git a/docs/ecosystem/nlpcloud.md b/docs/ecosystem/nlpcloud.md new file mode 100644 index 00000000..050da5af --- /dev/null +++ b/docs/ecosystem/nlpcloud.md @@ -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 +``` diff --git a/docs/ecosystem/openai.md b/docs/ecosystem/openai.md new file mode 100644 index 00000000..829dc9d3 --- /dev/null +++ b/docs/ecosystem/openai.md @@ -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) diff --git a/docs/ecosystem/pinecone.md b/docs/ecosystem/pinecone.md new file mode 100644 index 00000000..e56940b3 --- /dev/null +++ b/docs/ecosystem/pinecone.md @@ -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) diff --git a/docs/ecosystem/serpapi.md b/docs/ecosystem/serpapi.md new file mode 100644 index 00000000..eede382d --- /dev/null +++ b/docs/ecosystem/serpapi.md @@ -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) diff --git a/docs/ecosystem/weaviate.md b/docs/ecosystem/weaviate.md new file mode 100644 index 00000000..9c5b163d --- /dev/null +++ b/docs/ecosystem/weaviate.md @@ -0,0 +1,33 @@ +# Weaviate + +This page covers how to use the Weaviate ecosystem within LangChain. + +What is Weaviate? + +**Weaviate in a nutshell:** +- Weaviate is an open-source ​database of the type ​vector search engine. +- Weaviate allows you to store JSON documents in a class property-like fashion while attaching machine learning vectors to these documents to represent them in vector space. +- Weaviate can be used stand-alone (aka bring your vectors) or with a variety of modules that can do the vectorization for you and extend the core capabilities. +- Weaviate has a GraphQL-API to access your data easily. +- We aim to bring your vector search set up to production to query in mere milliseconds (check our [open source benchmarks](https://weaviate.io/developers/weaviate/current/benchmarks/) to see if Weaviate fits your use case). +- Get to know Weaviate in the [basics getting started guide](https://weaviate.io/developers/weaviate/current/core-knowledge/basics.html) in under five minutes. + +**Weaviate in detail:** + +Weaviate is a low-latency vector search engine with out-of-the-box support for different media types (text, images, etc.). It offers Semantic Search, Question-Answer Extraction, Classification, Customizable Models (PyTorch/TensorFlow/Keras), etc. Built from scratch in Go, Weaviate stores both objects and vectors, allowing for combining vector search with structured filtering and the fault tolerance of a cloud-native database. It is all accessible through GraphQL, REST, and various client-side programming languages. + +## Installation and Setup +- Install the Python SDK with `pip install weaviate-client` +## Wrappers + +### VectorStore + +There exists a wrapper around Weaviate 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 Weaviate +``` + +For a more detailed walkthrough of the Weaviate wrapper, see [this notebook](../modules/utils/combine_docs_examples/vectorstores.ipynb) diff --git a/docs/examples/agents.rst b/docs/examples/agents.rst deleted file mode 100644 index 80bcc450..00000000 --- a/docs/examples/agents.rst +++ /dev/null @@ -1,50 +0,0 @@ -Agents -====== - -The first category of how-to guides here cover specific parts of working with agents. - -`Custom Tools `_: How to create custom tools that an agent can use. - -`Intermediate Steps `_: How to access and use intermediate steps to get more visibility into the internals of an agent. - -`Custom Agent `_: How to create a custom agent (specifically, a custom LLM + prompt to drive that agent). - -`Multi Input Tools `_: 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 `_ 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 `_ -- **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 `_ - -**Self-Ask-With-Search** - -- **Tools used**: Search -- **Agent used**: `self-ask-with-search` -- `Paper `_ -- `Example Notebook `_ - -**ReAct** - -- **Tools used**: Wikipedia Docstore -- **Agent used**: `react-docstore` -- `Paper `_ -- `Example Notebook `_ - - - -.. toctree:: - :maxdepth: 1 - :glob: - :hidden: - - agents/* \ No newline at end of file diff --git a/docs/examples/chains.rst b/docs/examples/chains.rst deleted file mode 100644 index 4b19286f..00000000 --- a/docs/examples/chains.rst +++ /dev/null @@ -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 `_ - -**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 `_ - -**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 `_ -- `Example Notebook `_ - -**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 `_ - -.. toctree:: - :maxdepth: 1 - :glob: - :caption: Chains - :hidden: - - chains/* diff --git a/docs/examples/data_augmented_generation.rst b/docs/examples/data_augmented_generation.rst deleted file mode 100644 index 5db1fca5..00000000 --- a/docs/examples/data_augmented_generation.rst +++ /dev/null @@ -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 `_: A walkthrough of how to split large documents up into smaller, more manageable pieces of text. - -`Embeddings & VectorStores `_: A walkthrough of the different embedding and vectorstore functionalies that LangChain supports. - - -**Examples** - -`Question Answering `_: A walkthrough of how to use LangChain for question answering over specific documents. - -`Question Answering with Sources `_: A walkthrough of how to use LangChain for question answering (with sources) over specific documents. - -`Summarization `_: A walkthrough of how to use LangChain for summarization over specific documents. - -`Vector DB Question Answering `_: A walkthrough of how to use LangChain for question answering over a vector database. - -`Vector DB Question Answering with Sources `_: A walkthrough of how to use LangChain for question answering (with sources) over a vector database. - - -.. toctree:: - :maxdepth: 1 - :glob: - :hidden: - - data_augmented_generation/* diff --git a/docs/examples/evaluation.rst b/docs/examples/evaluation.rst deleted file mode 100644 index 75900377..00000000 --- a/docs/examples/evaluation.rst +++ /dev/null @@ -1,18 +0,0 @@ -Evaluation -============== - -The examples here all highlight how to use language models to assist in evaluation of themselves. - -`Question Answering `_: An overview of LLMs aimed at evaluating question answering systems in general. - -`Data Augmented Question Answering `_: 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 `_: Covers an example of loading and using a dataset from Hugging Face for evaluation. - - -.. toctree:: - :maxdepth: 1 - :glob: - :hidden: - - evaluation/* diff --git a/docs/examples/memory.rst b/docs/examples/memory.rst deleted file mode 100644 index ecce7c88..00000000 --- a/docs/examples/memory.rst +++ /dev/null @@ -1,24 +0,0 @@ -Memory -====== - -The examples here all highlight how to use memory in different ways. - -`Adding Memory `_: How to add a memory component to any single input chain. - -`Adding Memory to Multi-Input Chain `_: How to add a memory component to any multiple input chain. - -`Conversational Memory Types `_: An overview of the different types of conversation memory you can load and use with a conversation-like chain. - -`Conversational Memory Customization `_: How to customize existing conversation memory components. - -`Custom Memory `_: How to write your own custom memory component. - -`Adding Memory to Agents `_: How to add a memory component to any agent. - - -.. toctree:: - :maxdepth: 1 - :glob: - :hidden: - - memory/* \ No newline at end of file diff --git a/docs/examples/prompts.rst b/docs/examples/prompts.rst deleted file mode 100644 index c2f708d0..00000000 --- a/docs/examples/prompts.rst +++ /dev/null @@ -1,45 +0,0 @@ -LLMs & Prompts -============== - -The examples here all highlight how to work with LLMs and prompts. - -**LLMs** - -`LLM Functionality `_: A walkthrough of all the functionality the standard LLM interface exposes. - -`LLM Serialization `_: A walkthrough of how to serialize LLMs to and from disk. - -`LLM Caching `_: Covers different types of caches, and how to use a cache to save results of LLM calls. - -`Custom LLM `_: 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 `_: Covers how to connect to LLMs hosted on HuggingFace Hub. - -`Azure OpenAI `_: Covers how to connect to Azure-hosted OpenAI Models. - - - -**Prompts** - -`Prompt Management `_: A walkthrough of all the functionality LangChain supports for working with prompts. - -`Prompt Serialization `_: A walkthrough of how to serialize prompts to and from disk. - -`Few Shot Examples `_: How to include examples in the prompt. - -`Generate Examples `_: How to use existing examples to generate more examples. - -`Custom Example Selector `_: How to create and use a custom ExampleSelector (the class responsible for choosing which examples to use in a prompt). - -`Custom Prompt Template `_: 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/* diff --git a/docs/examples/prompts/custom_example_selector.ipynb b/docs/examples/prompts/custom_example_selector.ipynb deleted file mode 100644 index f8550d08..00000000 --- a/docs/examples/prompts/custom_example_selector.ipynb +++ /dev/null @@ -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 -} diff --git a/docs/examples/prompts/custom_prompt_template.ipynb b/docs/examples/prompts/custom_prompt_template.ipynb deleted file mode 100644 index 820d4f09..00000000 --- a/docs/examples/prompts/custom_prompt_template.ipynb +++ /dev/null @@ -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 -} diff --git a/docs/examples/prompts/few_shot_examples.ipynb b/docs/examples/prompts/few_shot_examples.ipynb deleted file mode 100644 index 1129e8cb..00000000 --- a/docs/examples/prompts/few_shot_examples.ipynb +++ /dev/null @@ -1,306 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "f8b01b97", - "metadata": {}, - "source": [ - "# Few Shot Prompt examples\n", - "Notebook showing off how canonical prompts in LangChain can be recreated as FewShotPrompts" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "18c67cc9", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.prompts.few_shot import FewShotPromptTemplate\n", - "from langchain.prompts.prompt import PromptTemplate" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "2a729c9f", - "metadata": {}, - "outputs": [], - "source": [ - "# Self Ask with Search\n", - "\n", - "examples = [\n", - " {\n", - " \"question\": \"Who lived longer, Muhammad Ali or Alan Turing?\",\n", - " \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: How old was Muhammad Ali when he died?\\nIntermediate answer: Muhammad Ali was 74 years old when he died.\\nFollow up: How old was Alan Turing when he died?\\nIntermediate answer: Alan Turing was 41 years old when he died.\\nSo the final answer is: Muhammad Ali\"\n", - " },\n", - " {\n", - " \"question\": \"When was the founder of craigslist born?\",\n", - " \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: Who was the founder of craigslist?\\nIntermediate answer: Craigslist was founded by Craig Newmark.\\nFollow up: When was Craig Newmark born?\\nIntermediate answer: Craig Newmark was born on December 6, 1952.\\nSo the final answer is: December 6, 1952\"\n", - " },\n", - " {\n", - " \"question\": \"Who was the maternal grandfather of George Washington?\",\n", - " \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: Who was the mother of George Washington?\\nIntermediate answer: The mother of George Washington was Mary Ball Washington.\\nFollow up: Who was the father of Mary Ball Washington?\\nIntermediate answer: The father of Mary Ball Washington was Joseph Ball.\\nSo the final answer is: Joseph Ball\"\n", - " },\n", - " {\n", - " \"question\": \"Are both the directors of Jaws and Casino Royale from the same country?\",\n", - " \"answer\": \"Are follow up questions needed here: Yes.\\nFollow up: Who is the director of Jaws?\\nIntermediate Answer: The director of Jaws is Steven Spielberg.\\nFollow up: Where is Steven Spielberg from?\\nIntermediate Answer: The United States.\\nFollow up: Who is the director of Casino Royale?\\nIntermediate Answer: The director of Casino Royale is Martin Campbell.\\nFollow up: Where is Martin Campbell from?\\nIntermediate Answer: New Zealand.\\nSo the final answer is: No\"\n", - " }\n", - "]\n", - "example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n{answer}\")\n", - "\n", - "prompt = FewShotPromptTemplate(\n", - " examples=examples, \n", - " example_prompt=example_prompt, \n", - " suffix=\"Question: {input}\", \n", - " input_variables=[\"input\"]\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "95fc0059", - "metadata": {}, - "outputs": [], - "source": [ - "# ReAct\n", - "\n", - "examples = [\n", - " {\n", - " \"question\": \"What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?\",\n", - " \"answer\": \"Thought 1: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of that area.\\nAction 1: Search[Colorado orogeny]\\nObservation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.\\nThought 2: It does not mention the eastern sector. So I need to look up eastern sector.\\nAction 2: Lookup[eastern sector]\\nObservation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.\\nThought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.\\nAction 3: Search[High Plains]\\nObservation 3: High Plains refers to one of two distinct land regions\\nThought 4: I need to instead search High Plains (United States).\\nAction 4: Search[High Plains (United States)]\\nObservation 4: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130 m).[3]\\nThought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.\\nAction 5: Finish[1,800 to 7,000 ft]\"\n", - " },\n", - " {\n", - " \"question\": \"Musician and satirist Allie Goertz wrote a song about the \\\"The Simpsons\\\" character Milhouse, who Matt Groening named after who?\",\n", - " \"answer\": \"Thought 1: The question simplifies to \\\"The Simpsons\\\" character Milhouse is named after who. I only need to search Milhouse and find who it is named after.\\nAction 1: Search[Milhouse]\\nObservation 1: Milhouse Mussolini Van Houten is a recurring character in the Fox animated television series The Simpsons voiced by Pamela Hayden and created by Matt Groening.\\nThought 2: The paragraph does not tell who Milhouse is named after, maybe I can look up \\\"named after\\\".\\nAction 2: Lookup[named after]\\nObservation 2: (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose middle name was Milhous.\\nThought 3: Milhouse was named after U.S. president Richard Nixon, so the answer is Richard Nixon.\\nAction 3: Finish[Richard Nixon]\"\n", - " },\n", - " {\n", - " \"question\": \"Which documentary is about Finnish rock groups, Adam Clayton Powell or The Saimaa Gesture?\",\n", - " \"answer\": \"Thought 1: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which documentary is about Finnish rock groups.\\nAction 1: Search[Adam Clayton Powell]\\nObservation 1 Could not find [Adam Clayton Powell]. Similar: [’Adam Clayton Powell III’, ’Seventh Avenue (Manhattan)’, ’Adam Clayton Powell Jr. State Office Building’, ’Isabel Washington Powell’, ’Adam Powell’, ’Adam Clayton Powell (film)’, ’Giancarlo Esposito’].\\nThought 2: To find the documentary, I can search Adam Clayton Powell (film).\\nAction 2: Search[Adam Clayton Powell (film)]\\nObservation 2: Adam Clayton Powell is a 1989 American documentary film directed by Richard Kilberg. The film is about the rise and fall of influential African-American politician Adam Clayton Powell Jr.[3][4] It was later aired as part of the PBS series The American Experience.\\nThought 3: Adam Clayton Powell (film) is a documentary about an African-American politician, not Finnish rock groups. So the documentary about Finnish rock groups must instead be The Saimaa Gesture.\\nAction 3: Finish[The Saimaa Gesture]\"\n", - " },\n", - " {\n", - " \"question\": \"What profession does Nicholas Ray and Elia Kazan have in common?\",\n", - " \"answer\": \"Thought 1: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.\\nAction 1: Search[Nicholas Ray]\\nObservation 1: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.\\nThought 2: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.\\nAction 2: Search[Elia Kazan]\\nObservation 2: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.\\nThought 3: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.\\nAction 3: Finish[director, screenwriter, actor]\"\n", - " },\n", - " {\n", - " \"question\": \"Which magazine was started first Arthur’s Magazine or First for Women?\",\n", - " \"answer\": \"Thought 1: I need to search Arthur’s Magazine and First for Women, and find which was started first.\\nAction 1: Search[Arthur’s Magazine]\\nObservation 1: Arthur’s Magazine (1844-1846) was an American literary periodical published in Philadelphia in the 19th century.\\nThought 2: Arthur’s Magazine was started in 1844. I need to search First for Women next.\\nAction 2: Search[First for Women]\\nObservation 2: First for Women is a woman’s magazine published by Bauer Media Group in the USA.[1] The magazine was started in 1989.\\nThought 3: First for Women was started in 1989. 1844 (Arthur’s Magazine) < 1989 (First for Women), so Arthur’s Magazine was started first.\\nAction 3: Finish[Arthur’s Magazine]\"\n", - " },\n", - " {\n", - " \"question\": \"Were Pavel Urysohn and Leonid Levin known for the same type of work?\",\n", - " \"answer\": \"Thought 1: I need to search Pavel Urysohn and Leonid Levin, find their types of work, then find if they are the same.\\nAction 1: Search[Pavel Urysohn]\\nObservation 1: Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet mathematician who is best known for his contributions in dimension theory.\\nThought 2: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and find its type of work.\\nAction 2: Search[Leonid Levin]\\nObservation 2: Leonid Anatolievich Levin is a Soviet-American mathematician and computer scientist.\\nThought 3: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn and Leonid Levin have the same type of work.\\nAction 3: Finish[yes]\"\n", - " }\n", - "]\n", - "example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n{answer}\")\n", - "\n", - "prompt = FewShotPromptTemplate(\n", - " examples=examples, \n", - " example_prompt=example_prompt, \n", - " suffix=\"Question: {input}\", \n", - " input_variables=[\"input\"]\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "897d4e08", - "metadata": {}, - "outputs": [], - "source": [ - "# LLM Math\n", - "examples = [\n", - " {\n", - " \"question\": \"What is 37593 * 67?\",\n", - " \"answer\": \"```python\\nprint(37593 * 67)\\n```\\n```output\\n2518731\\n```\\nAnswer: 2518731\"\n", - " }\n", - "]\n", - "example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n\\n{answer}\")\n", - "\n", - "prompt = FewShotPromptTemplate(\n", - " examples=examples, \n", - " example_prompt=example_prompt, \n", - " suffix=\"Question: {input}\", \n", - " input_variables=[\"input\"]\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "7ab7379f", - "metadata": {}, - "outputs": [], - "source": [ - "# NatBot\n", - "example_seperator = \"==================================================\"\n", - "content_1 = \"\"\"About\n", - "Store\n", - "Gmail\n", - "Images\n", - "(Google apps)\n", - "Sign in\n", - "\"(Google)\"/\n", - "\n", - "\n", - "\n", - "\n", - "Advertising\n", - "Business\n", - "How Search works\n", - "Carbon neutral since 2007\n", - "Privacy\n", - "Terms\n", - "Settings\"\"\"\n", - "content_2 = \"\"\"About\n", - "Store\n", - "Gmail\n", - "Images\n", - "(Google apps)\n", - "Sign in\n", - "\"(Google)\"/\n", - "\n", - "\n", - "\n", - "\n", - "Advertising\n", - "Business\n", - "How Search works\n", - "Carbon neutral since 2007\n", - "Privacy\n", - "Terms\n", - "Settings\"\"\"\n", - "content_3 = \"\"\"\n", - "\n", - "\n", - "\n", - "OpenTable logo\n", - "\n", - "Find your table for any occasion\n", - "\n", - "Sep 28, 2022\n", - "7:00 PM\n", - "2 people\n", - "\n", - "\n", - "It looks like you're in Peninsula. Not correct?\n", - "\n", - "\"\"\"\n", - "examples = [\n", - " {\n", - " \"i\": 1,\n", - " \"content\": content_1,\n", - " \"objective\": \"Find a 2 bedroom house for sale in Anchorage AK for under $750k\",\n", - " \"current_url\": \"https://www.google.com/\",\n", - " \"command\": 'TYPESUBMIT 8 \"anchorage redfin\"'\n", - " },\n", - " {\n", - " \"i\": 2,\n", - " \"content\": content_2,\n", - " \"objective\": \"Make a reservation for 4 at Dorsia at 8pm\",\n", - " \"current_url\": \"https://www.google.com/\",\n", - " \"command\": 'TYPESUBMIT 8 \"dorsia nyc opentable\"'\n", - " },\n", - " {\n", - " \"i\": 3,\n", - " \"content\": content_3,\n", - " \"objective\": \"Make a reservation for 4 for dinner at Dorsia in New York City at 8pm\",\n", - " \"current_url\": \"https://www.opentable.com/\",\n", - " \"command\": 'TYPESUBMIT 12 \"dorsia new york city\"'\n", - " },\n", - "]\n", - "example_prompt_template=\"\"\"EXAMPLE {i}:\n", - "==================================================\n", - "CURRENT BROWSER CONTENT:\n", - "------------------\n", - "{content}\n", - "------------------\n", - "OBJECTIVE: {objective}\n", - "CURRENT URL: {current_url}\n", - "YOUR COMMAND:\n", - "{command}\"\"\"\n", - "example_prompt = PromptTemplate(input_variables=[\"i\", \"content\", \"objective\", \"current_url\", \"command\"], template=example_prompt_template)\n", - "\n", - "\n", - "prefix = \"\"\"\n", - "You are an agent controlling a browser. You are given:\n", - "\t(1) an objective that you are trying to achieve\n", - "\t(2) the URL of your current web page\n", - "\t(3) a simplified text description of what's visible in the browser window (more on that below)\n", - "You can issue these commands:\n", - "\tSCROLL UP - scroll up one page\n", - "\tSCROLL DOWN - scroll down one page\n", - "\tCLICK X - click on a given element. You can only click on links, buttons, and inputs!\n", - "\tTYPE X \"TEXT\" - type the specified text into the input with id X\n", - "\tTYPESUBMIT X \"TEXT\" - same as TYPE above, except then it presses ENTER to submit the form\n", - "The format of the browser content is highly simplified; all formatting elements are stripped.\n", - "Interactive elements such as links, inputs, buttons are represented like this:\n", - "\t\ttext\n", - "\t\t\n", - "\t\ttext\n", - "Images are rendered as their alt text like this:\n", - "\t\t\"\"/\n", - "Based on your given objective, issue whatever command you believe will get you closest to achieving your goal.\n", - "You always start on Google; you should submit a search query to Google that will take you to the best page for\n", - "achieving your objective. And then interact with that page to achieve your objective.\n", - "If you find yourself on Google and there are no search results displayed yet, you should probably issue a command\n", - "like \"TYPESUBMIT 7 \"search query\"\" to get to a more useful page.\n", - "Then, if you find yourself on a Google search results page, you might issue the command \"CLICK 24\" to click\n", - "on the first link in the search results. (If your previous command was a TYPESUBMIT your next command should\n", - "probably be a CLICK.)\n", - "Don't try to interact with elements that you can't see.\n", - "Here are some examples:\n", - "\"\"\"\n", - "suffix=\"\"\"\n", - "The current browser content, objective, and current URL follow. Reply with your next command to the browser.\n", - "CURRENT BROWSER CONTENT:\n", - "------------------\n", - "{browser_content}\n", - "------------------\n", - "OBJECTIVE: {objective}\n", - "CURRENT URL: {url}\n", - "PREVIOUS COMMAND: {previous_command}\n", - "YOUR COMMAND:\n", - "\"\"\"\n", - "PROMPT = FewShotPromptTemplate(\n", - " examples = examples,\n", - " example_prompt=example_prompt,\n", - " example_separator=example_seperator,\n", - " input_variables=[\"browser_content\", \"url\", \"previous_command\", \"objective\"],\n", - " prefix=prefix,\n", - " suffix=suffix,\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ce5927c6", - "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 -} diff --git a/docs/explanation/cool_demos.md b/docs/explanation/cool_demos.md deleted file mode 100644 index d2234ba3..00000000 --- a/docs/explanation/cool_demos.md +++ /dev/null @@ -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). \ No newline at end of file diff --git a/docs/explanation/core_concepts.md b/docs/explanation/core_concepts.md deleted file mode 100644 index 0330c840..00000000 --- a/docs/explanation/core_concepts.md +++ /dev/null @@ -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. - diff --git a/docs/gallery.rst b/docs/gallery.rst new file mode 100644 index 00000000..633a7827 --- /dev/null +++ b/docs/gallery.rst @@ -0,0 +1,211 @@ +LangChain Gallery +============= + +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 +----------- + +.. panels:: + :body: text-center + + --- + + .. link-button:: https://github.com/bborn/howdoi.ai + :type: url + :text: HowDoI.ai + :classes: stretched-link btn-lg + + +++ + + This is an experiment in building a large-language-model-backed chatbot. It can hold a conversation, remember previous comments/questions, + and answer all types of queries (history, web search, movie data, weather, news, and more). + + --- + + .. link-button:: https://colab.research.google.com/drive/1sKSTjt9cPstl_WMZ86JsgEqFG-aSAwkn?usp=sharing + :type: url + :text: YouTube Transcription QA with Sources + :classes: stretched-link btn-lg + + +++ + + An end-to-end example of doing question answering on YouTube transcripts, returning the timestamps as sources to legitimize the answer. + + --- + + .. link-button:: https://github.com/OpenBioLink/ThoughtSource + :type: url + :text: ThoughtSource + :classes: stretched-link btn-lg + + +++ + + A central, open resource and community around data and tools related to chain-of-thought reasoning in large language models. + + --- + + .. link-button:: https://github.com/blackhc/llm-strategy + :type: url + :text: LLM Strategy + :classes: stretched-link btn-lg + + +++ + + This Python package adds a decorator llm_strategy that connects to an LLM (such as OpenAI’s GPT-3) and uses the LLM to "implement" abstract methods in interface classes. It does this by forwarding requests to the LLM and converting the responses back to Python data using Python's @dataclasses. + + --- + + .. link-button:: https://github.com/JohnNay/gpt-lawyer/blob/main/notebooks/gpt_corporate_lobbying_zero_shot.ipynb + :type: url + :text: Zero-Shot Corporate Lobbyist + :classes: stretched-link btn-lg + + +++ + + A notebook showing how to use GPT to help with the work of a corporate lobbyist. + + --- + + .. link-button:: https://huggingface.co/spaces/JavaFXpert/gpt-math-techniques + :type: url + :text: GPT Math Techniques + :classes: stretched-link btn-lg + + +++ + + A Hugging Face spaces project showing off the benefits of using PAL for math problems. + + --- + + .. link-button:: https://colab.research.google.com/drive/1xt2IsFPGYMEQdoJFNgWNAjWGxa60VXdV + :type: url + :text: GPT Political Compass + :classes: stretched-link btn-lg + + +++ + + Measure the political compass of GPT. + + --- + + .. link-button:: https://github.com/hwchase17/notion-qa + :type: url + :text: Notion Database Question-Answering Bot + :classes: stretched-link btn-lg + + +++ + + Open source GitHub project shows how to use LangChain to create a chatbot that can answer questions about an arbitrary Notion database. + + --- + + .. link-button:: https://github.com/jerryjliu/gpt_index + :type: url + :text: GPT Index + :classes: stretched-link btn-lg + + +++ + + 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. + + --- + + .. link-button:: https://github.com/JavaFXpert/llm-grovers-search-party + :type: url + :text: Grover's Algorithm + :classes: stretched-link btn-lg + + +++ + + Leveraging Qiskit, OpenAI and LangChain to demonstrate Grover's algorithm + + --- + + .. link-button:: https://colab.research.google.com/drive/19WTIWC3prw5LDMHmRMvqNV2loD9FHls6?usp=sharing + :type: url + :text: ReAct TextWorld + :classes: stretched-link btn-lg + + +++ + + Leveraging the ReActTextWorldAgent to play TextWorld with an LLM! + + --- + + .. link-button:: https://github.com/jagilley/fact-checker + :type: url + :text: Fact Checker + :classes: stretched-link btn-lg + + +++ + + This repo is a simple demonstration of using LangChain to do fact-checking with prompt chaining. + + +Proprietary +----------- + +.. panels:: + :body: text-center + + --- + + .. link-button:: https://twitter.com/sjwhitmore/status/1580593217153531908?s=20&t=neQvtZZTlp623U3LZwz3bQ + :type: url + :text: Daimon + :classes: stretched-link btn-lg + + +++ + + A chat-based AI personal assistant with long-term memory about you. + + --- + + .. link-button:: https://twitter.com/dory111111/status/1608406234646052870?s=20&t=XYlrbKM0ornJsrtGa0br-g + :type: url + :text: AI Assisted SQL Query Generator + :classes: stretched-link btn-lg + + +++ + + An app to write SQL using natural language, and execute against real DB. + + --- + + .. link-button:: https://twitter.com/krrish_dh/status/1581028925618106368?s=20&t=neQvtZZTlp623U3LZwz3bQ + :type: url + :text: Clerkie + :classes: stretched-link btn-lg + + +++ + + Stack Tracing QA Bot to help debug complex stack tracing (especially the ones that go multi-function/file deep). + + --- + + .. link-button:: https://twitter.com/Raza_Habib496/status/1596880140490838017?s=20&t=6MqEQYWfSqmJwsKahjCVOA + :type: url + :text: Sales Email Writer + :classes: stretched-link btn-lg + + +++ + + 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. + + --- + + .. link-button:: https://twitter.com/chillzaza_/status/1592961099384905730?s=20&t=EhU8jl0KyCPJ7vE9Rnz-cQ + :type: url + :text: Question-Answering on a Web Browser + :classes: stretched-link btn-lg + + +++ + + By Zahid Khawaja, this demo utilizes question answering to answer questions about a given website. A followup added this for `YouTube videos `_, and then another followup added it for `Wikipedia `_. + + + diff --git a/docs/getting_started/data_augmented_generation.ipynb b/docs/getting_started/data_augmented_generation.ipynb deleted file mode 100644 index 177ae8e9..00000000 --- a/docs/getting_started/data_augmented_generation.ipynb +++ /dev/null @@ -1,216 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "7ba0decc", - "metadata": {}, - "source": [ - "# Data Augmented Generation\n", - "\n", - "This notebook covers getting started with some key concepts of data augmented generation, specifically Documents, Embeddings, and Vectorstores.\n", - "\n", - "After that, we will cover how to use these concepts to do question/answering over select documents.\n", - "\n", - "For a more conceptual explanation of what Data Augmented Generation is, see [this](../explanation/combine_docs.md) documentation." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "b37c3e1e", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.embeddings.openai import OpenAIEmbeddings\n", - "from langchain.text_splitter import CharacterTextSplitter\n", - "from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch\n", - "from langchain.vectorstores.faiss import FAISS" - ] - }, - { - "cell_type": "markdown", - "id": "a8c13318", - "metadata": {}, - "source": [ - "First, let's load in our private data that we want to use in conjunction with an LLM." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "91d307ed", - "metadata": {}, - "outputs": [], - "source": [ - "with open('../examples/state_of_the_union.txt') as f:\n", - " state_of_the_union = f.read()" - ] - }, - { - "cell_type": "markdown", - "id": "12f8bc8f", - "metadata": {}, - "source": [ - "Now, we need to create smaller chunks of text from this one large document. We want to do this because we cannot (and do not want to) pass this whole large text into the language model in one go - rather, we want to split it up, select the relevant parts, and then pass those into the language model." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "10a93bf9", - "metadata": {}, - "outputs": [], - "source": [ - "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", - "texts = text_splitter.split_text(state_of_the_union)" - ] - }, - { - "cell_type": "markdown", - "id": "c2f8c006", - "metadata": {}, - "source": [ - "We could work with ALL these documents directly, but often we only want to find only the most relevant ones. One common way to do that is create embeddings for each document, store them in a vector database, and then query that database with an incoming query to select the most relevant documents for that query.\n", - "\n", - "In this example, we use OpenAI embeddings, and a FAISS vector store." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "fa0f3066", - "metadata": {}, - "outputs": [], - "source": [ - "embeddings = OpenAIEmbeddings()\n", - "docsearch = FAISS.from_texts(texts, embeddings)" - ] - }, - { - "cell_type": "markdown", - "id": "2c6ce83f", - "metadata": {}, - "source": [ - "Now let's give it a go!" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "8465b4b7", - "metadata": {}, - "outputs": [], - "source": [ - "query = \"What did the president say about Ketanji Brown Jackson\"\n", - "docs = docsearch.similarity_search(query)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "611be801", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "In state after state, new laws have been passed, not only to suppress the vote, but to subvert entire elections. \n", - "\n", - "We cannot let this happen. \n", - "\n", - "Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n", - "\n", - "Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n", - "\n", - "One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n", - "\n", - "And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence. \n" - ] - } - ], - "source": [ - "print(docs[0].page_content)" - ] - }, - { - "cell_type": "markdown", - "id": "0b6a48e5", - "metadata": {}, - "source": [ - "So we now have a way of selecting the most relevant documents - now what? We can plug this vectorstore into a chain, where we first select these documents, and then send them (along with the original question) to get a final answer." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "b6255b02", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain import OpenAI, VectorDBQA" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "ec4eacad", - "metadata": {}, - "outputs": [], - "source": [ - "qa = VectorDBQA.from_llm(llm=OpenAI(), vectorstore=docsearch)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "59c7508d", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds and will continue Justice Breyer's legacy of excellence.\"" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "query = \"What did the president say about Ketanji Brown Jackson\"\n", - "qa.run(query)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b192c91c", - "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 -} diff --git a/docs/getting_started/environment.md b/docs/getting_started/environment.md deleted file mode 100644 index 15a25c46..00000000 --- a/docs/getting_started/environment.md +++ /dev/null @@ -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"] = "..." -``` diff --git a/docs/getting_started/getting_started.md b/docs/getting_started/getting_started.md new file mode 100644 index 00000000..2e6badf3 --- /dev/null +++ b/docs/getting_started/getting_started.md @@ -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?" +``` \ No newline at end of file diff --git a/docs/getting_started/installation.md b/docs/getting_started/installation.md deleted file mode 100644 index d098234f..00000000 --- a/docs/getting_started/installation.md +++ /dev/null @@ -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. diff --git a/docs/getting_started/llm.md b/docs/getting_started/llm.md deleted file mode 100644 index 9133a4a1..00000000 --- a/docs/getting_started/llm.md +++ /dev/null @@ -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)) -``` diff --git a/docs/getting_started/llm_chain.md b/docs/getting_started/llm_chain.md deleted file mode 100644 index 9c44894e..00000000 --- a/docs/getting_started/llm_chain.md +++ /dev/null @@ -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. diff --git a/docs/getting_started/memory.ipynb b/docs/getting_started/memory.ipynb deleted file mode 100644 index edaa1f65..00000000 --- a/docs/getting_started/memory.ipynb +++ /dev/null @@ -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 -} diff --git a/docs/explanation/glossary.md b/docs/glossary.md similarity index 94% rename from docs/explanation/glossary.md rename to docs/glossary.md index 4d7b3a29..d9ca3050 100644 --- a/docs/explanation/glossary.md +++ b/docs/glossary.md @@ -4,7 +4,7 @@ This is a collection of terminology commonly used when developing LLM applicatio It contains reference to external papers or sources where the concept was first introduced, as well as to places in LangChain where the concept is used. -### Chain of Thought Prompting +## Chain of Thought Prompting A prompting technique used to encourage the model to generate a series of intermediate reasoning steps. A less formal way to induce this behavior is to include “Let’s think step-by-step” in the prompt. @@ -13,7 +13,7 @@ Resources: - [Chain-of-Thought Paper](https://arxiv.org/pdf/2201.11903.pdf) - [Step-by-Step Paper](https://arxiv.org/abs/2112.00114) -### Action Plan Generation +## Action Plan Generation A prompt usage that uses a language model to generate actions to take. The results of these actions can then be fed back into the language model to generate a subsequent action. @@ -22,7 +22,7 @@ Resources: - [WebGPT Paper](https://arxiv.org/pdf/2112.09332.pdf) - [SayCan Paper](https://say-can.github.io/assets/palm_saycan.pdf) -### ReAct Prompting +## ReAct Prompting A prompting technique that combines Chain-of-Thought prompting with action plan generation. This induces the to model to think about what action to take, then take it. @@ -31,7 +31,7 @@ Resources: - [Paper](https://arxiv.org/pdf/2210.03629.pdf) - [LangChain Example](https://github.com/hwchase17/langchain/blob/master/docs/examples/agents/react.ipynb) -### Self-ask +## Self-ask A prompting method that builds on top of chain-of-thought prompting. In this method, the model explicitly asks itself follow-up questions, which are then answered by an external search engine. @@ -40,7 +40,7 @@ Resources: - [Paper](https://ofir.io/self-ask.pdf) - [LangChain Example](https://github.com/hwchase17/langchain/blob/master/docs/examples/agents/self_ask_with_search.ipynb) -### Prompt Chaining +## Prompt Chaining Combining multiple LLM calls together, with the output of one-step being the input to the next. @@ -50,14 +50,14 @@ Resources: - [ICE Primer Book](https://primer.ought.org/) - [Socratic Models](https://socraticmodels.github.io/) -### Memetic Proxy +## Memetic Proxy Encouraging the LLM to respond in a certain way framing the discussion in a context that the model knows of and that will result in that type of response. For example, as a conversation between a student and a teacher. Resources: - [Paper](https://arxiv.org/pdf/2102.07350.pdf) -### Self Consistency +## Self Consistency A decoding strategy that samples a diverse set of reasoning paths and then selects the most consistent answer. Is most effective when combined with Chain-of-thought prompting. @@ -65,7 +65,7 @@ Is most effective when combined with Chain-of-thought prompting. Resources: - [Paper](https://arxiv.org/pdf/2203.11171.pdf) -### Inception +## Inception Also called “First Person Instruction”. Encouraging the model to think a certain way by including the start of the model’s response in the prompt. @@ -73,7 +73,7 @@ Encouraging the model to think a certain way by including the start of the model Resources: - [Example](https://twitter.com/goodside/status/1583262455207460865?s=20&t=8Hz7XBnK1OF8siQrxxCIGQ) -### MemPrompt +## MemPrompt MemPrompt maintains a memory of errors and user feedback, and uses them to prevent repetition of mistakes. diff --git a/docs/index.rst b/docs/index.rst index caed00b5..78b4a460 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -9,187 +9,146 @@ combine them with other sources of computation or knowledge. This library is aimed at assisting in the development of those types of applications. -There are six main areas that LangChain is designed to help with. -These are, in increasing order of complexity: +Getting Started +---------------- -1. LLM and Prompts -2. Chains -3. Data Augmented Generation -4. Agents -5. Memory -6. [BETA] Evaluation +Checkout the below guide for a walkthrough of how to get started using LangChain to create an Language Model application. -Let's go through these categories and for each one identify key concepts (to clarify terminology) as well as the problems in this area LangChain helps solve. +- `Getting Started Documentation `_ -**🦜 LLMs and Prompts** - -Calling out to an LLM once is pretty easy, with most of them being behind well documented APIs. -However, there are still some challenges going from that to an application running in production that LangChain attempts to address. - -*Key Concepts* - -- LLM: A large language model, in particular a text-to-text model. -- Prompt: The input to a language model. Typically this is not simply a hardcoded string but rather a combination of a template, some examples, and user input. -- Prompt Template: An object responsible for constructing the final prompt to pass to a LLM. - -*Problems Solved* - -- Switching costs: by exposing a standard interface for all the top LLM providers, LangChain makes it easy to switch from one provider to another, whether it be for production use cases or just for testing stuff out. -- Prompt management: managing your prompts is easy when you only have one simple one, but can get tricky when you have a bunch or when they start to get more complex. LangChain provides a standard way for storing, constructing, and referencing prompts. -- Prompt optimization: despite the underlying models getting better and better, there is still currently a need for carefully constructing prompts. +.. toctree:: + :maxdepth: 1 + :caption: Getting Started + :name: getting_started + :hidden: -**🔗️ Chains** + getting_started/getting_started.md -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 several parts to help with that. +Modules +----------- -*Key Concepts* +There are six main modules that LangChain provides support for. +For each module we provide some examples to get started, how-to guides, reference docs, and conceptual guides. +These modules are, in increasing order of complexity: -- Tools: APIs designed for assisting with a particular use case (search, databases, Python REPL, etc). Prompt templates, LLMs, and chains can also be considered tools. -- Chains: A combination of multiple tools in a deterministic manner. -*Problems Solved* +- `Prompts `_: This includes prompt management, prompt optimization, and prompt serialization. -- Standard interface for working with Chains -- Easy way to construct chains of LLMs -- Lots of integrations with other tools that you may want to use in conjunction with LLMs -- End-to-end chains for common workflows (database question/answer, api calling, etc) +- `LLMs `_: This includes a generic interface for all LLMs, and common utilities for working with LLMs. -**📚 Data Augmented Generation** +- `Utils `_: Language models are often more powerful when interacting with other sources of knowledge or computation. This can include Python REPLs, embeddings, search engines, and more. LangChain provides a large collection of common utils to use in your application. -LLMs have access to all the data they were trained on, but there are still large chunks of data they were not trained on. -Data Augmented Generation covers how to use LLMs to generate text conditioning on data outside of what the LLM was trained on. +- `Chains `_: Chains go beyond just a single LLM call, and are sequences of calls (whether to an LLM or a different utility). LangChain provides a standard interface for chains, lots of integrations with other tools, and end-to-end chains for common applications. -*Key Concepts* +- `Agents `_: Agents involve an LLM making decisions about which Actions to take, taking that Action, seeing an Observation, and repeating that until done. LangChain provides a standard interface for agents, a selection of agents to choose from, and examples of end to end agents. -- Documents: A document is a piece of text, along with some associated metadata, that can be inserted into the context of a query to condition generation on that text. -- Embeddings: A vector representation of text (or other unstructured data). Useful for being able to numerically compare pieces of text. -- Vectorstore: A database which stores embeddings and can be searched over. +- `Memory `_: Memory is the concept of persisting state between calls of a chain/agent. LangChain provides a standard interface for memory, a collection of memory implementations, and examples of chains/agents that use memory. -*Problems Solved* -- Standard interface for working with Documents, Embeddings, and Vectorstores -- Lots of integrations with common embedding providers and vectorstores -- End-to-end chains for common workflows (recursive summarization, question answering over documents, etc) +.. toctree:: + :maxdepth: 1 + :caption: Modules + :name: modules + :hidden: + modules/prompts.md + modules/llms.md + modules/utils.md + modules/chains.md + modules/agents.md + modules/memory.md -**🤖 Agents** +Use Cases +---------- -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 above modules can be used in a variety of ways. LangChain also provides guidance and assistance in this. Below are some of the common use cases LangChain supports. -*Key Concepts* +- `Agents `_: Agents are systems that use a language model to interact with other tools. These can be used to do more grounded question/answering, interact with APIs, or even take actions. -- Tools: same as above. -- Agent: An LLM-powered class responsible for determining which tools to use and in what order. +- `Chatbots `_: Since language models are good at producing text, that makes them ideal for creating chatbots. +- `Data Augmented Generation `_: Data Augmented Generation involves specific types of chains that first interact with an external datasource to fetch data to use in the generation step. Examples of this include summarization of long pieces of text and question/answering over specific data sources. -*Problems Solved* +- `Question Answering `_: Answering questions over specific documents, only utilizing the information in those documents to construct an answer. A type of Data Augmented Generation. -- Standard agent interfaces -- A selection of powerful agents to choose from -- Common chains that can be used as tools +- `Summarization `_: Summarizing longer documents into shorter, more condensed chunks of information. A type of Data Augmented Generation. -**🧠 Memory** +- `Evaluation `_: Generative models are notoriously hard to evaluate with traditional metrics. One new way of evaluating them is using language models themselves to do the evaluation. LangChain provides some prompts/chains for assisting in this. -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. +- `Generate similar examples `_: Generating similar examples to a given input. This is a common use case for many applications, and LangChain provides some prompts/chains for assisting in this. -*Key Concepts* +- `Compare models `_: Experimenting with different prompts, models, and chains is a big part of developing the best possible application. The ModelLaboratory makes it easy to do so. -- Memory: A class that can be added to an Agent or Chain to (1) pull in memory variables before calling that chain/agent, and (2) create new memories after the chain/agent finishes. -- Memory Variables: Variables returned from a Memory class, to be passed into the chain/agent along with the user input. -*Problems Solved* -- Standard memory interfaces -- A collection of common memory implementations to choose from -- Common chains/agents that use memory (e.g. chatbots) +.. toctree:: + :maxdepth: 1 + :caption: Use Cases + :name: use_cases + :hidden: -**🧐 Evaluation:** + use_cases/agents.md + use_cases/chatbots.md + use_cases/generate_examples.ipynb + use_cases/combine_docs.md + use_cases/question_answering.md + use_cases/summarization.md + use_cases/evaluation.rst + use_cases/model_laboratory.ipynb -[BETA] Generative models are notoriously hard to evaluate with traditional metrics. -One new way of evaluating them is using language models themselves to do the evaluation. -LangChain provides some prompts/chains for assisting in this. -This is still in Beta, which also means that feedback is especially appreciated here. +Reference Docs +--------------- -Documentation Structure -======================= -The documentation is structured into the following sections: +All of LangChain's reference documentation, in one place. Full documentation on all methods, classes, installation methods, and integration setups for LangChain. +- `Reference Documentation `_ .. toctree:: :maxdepth: 1 - :caption: Getting Started - :name: getting_started + :caption: Reference + :name: reference + :hidden: - getting_started/installation.md - getting_started/environment.md - getting_started/llm.md - getting_started/llm_chain.md - getting_started/sequential_chains.ipynb - getting_started/data_augmented_generation.ipynb - getting_started/agents.ipynb - getting_started/memory.ipynb + reference/installation.md + reference/integrations.md + reference.rst -Goes over a simple walk through and tutorial for getting started setting up a simple chain that generates a company name based on what the company makes. -Covers installation, environment set up, calling LLMs, and using prompts. -Start here if you haven't used LangChain before. +LangChain Ecosystem +------------------- + +Guides for how other companies/products can be used with LangChain + +- `LangChain Ecosystem `_ .. toctree:: :maxdepth: 1 - :caption: How-To Examples - :name: examples + :glob: + :caption: Ecosystem + :name: ecosystem + :hidden: - examples/prompts.rst - examples/chains.rst - examples/data_augmented_generation.rst - examples/agents.rst - examples/memory.rst - examples/evaluation.rst - examples/model_laboratory.ipynb + ecosystem.rst -More elaborate examples and walkthroughs of particular -integrations and use cases. This is the place to look if you have questions -about how to integrate certain pieces, or if you want to find examples of -common tasks or cool demos. +Additional Resources +--------------------- -.. toctree:: - :maxdepth: 1 - :caption: Reference - :name: reference +Additional collection of resources we think may be useful as you develop your application! - reference/installation.md - reference/integrations.md - reference/prompts.rst - reference/chains.rst - reference/data_augmented_generation.rst - reference/modules/agents +- `Glossary `_: A glossary of all related terms, papers, methods, etc. Whether implemented in LangChain or not! +- `Gallery `_: A collection of our favorite projects that use LangChain. Useful for finding inspiration or seeing how things were done in other applications. -Full API documentation. This is the place to look if you want to -see detailed information about the various classes, methods, and APIs. +- `Discord `_: Join us on our Discord to discuss all things LangChain! .. toctree:: :maxdepth: 1 - :caption: Resources + :caption: Additional Resources :name: resources + :hidden: - explanation/core_concepts.md - explanation/combine_docs.md - explanation/agents.md - explanation/tools.md - explanation/glossary.md - explanation/cool_demos.md - Discord - -Higher level, conceptual explanations of the LangChain components. -This is the place to go if you want to increase your high level understanding -of the problems LangChain is solving, and how we decided to go about do so. - + glossary.md + gallery.rst diff --git a/docs/modules/agents.rst b/docs/modules/agents.rst new file mode 100644 index 00000000..5c9ad32c --- /dev/null +++ b/docs/modules/agents.rst @@ -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 `_: A notebook to help you get started working with agents as quickly as possible. + +- `Key Concepts `_: A conceptual guide going over the various concepts related to agents. + +- `How-To Guides `_: 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 `_: 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 \ No newline at end of file diff --git a/docs/explanation/agents.md b/docs/modules/agents/agents.md similarity index 88% rename from docs/explanation/agents.md rename to docs/modules/agents/agents.md index 0f632ee7..c0259f91 100644 --- a/docs/explanation/agents.md +++ b/docs/modules/agents/agents.md @@ -5,15 +5,15 @@ An action can either be using a tool and observing its output, or returning to t For a list of easily loadable tools, see [here](tools.md). Here are the agents available in LangChain. -For a tutorial on how to load agents, see [here](/getting_started/agents.ipynb). +For a tutorial on how to load agents, see [here](getting_started.ipynb). -### `zero-shot-react-description` +## `zero-shot-react-description` This agent uses the ReAct framework to determine which tool to use based solely on the tool's description. Any number of tools can be provided. This agent requires that a description is provided for each tool. -### `react-docstore` +## `react-docstore` This agent uses the ReAct framework to interact with a docstore. Two tools must be provided: a `Search` tool and a `Lookup` tool (they must be named exactly as so). @@ -22,7 +22,7 @@ a term in the most recently found document. This agent is equivalent to the original [ReAct paper](https://arxiv.org/pdf/2210.03629.pdf), specifically the Wikipedia example. -### `self-ask-with-search` +## `self-ask-with-search` This agent utilizes a single tool that should be named `Intermediate Answer`. This tool should be able to lookup factual answers to questions. This agent diff --git a/docs/examples/agents/custom_agent.ipynb b/docs/modules/agents/examples/custom_agent.ipynb similarity index 62% rename from docs/examples/agents/custom_agent.ipynb rename to docs/modules/agents/examples/custom_agent.ipynb index 189a1e14..3b7bcb8b 100644 --- a/docs/examples/agents/custom_agent.ipynb +++ b/docs/modules/agents/examples/custom_agent.ipynb @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "id": "becda2a1", "metadata": {}, "outputs": [], @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "id": "339b1bb8", "metadata": {}, "outputs": [], @@ -99,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "id": "e21d2098", "metadata": {}, "outputs": [ @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 16, "id": "9b1cc2a2", "metadata": {}, "outputs": [], @@ -145,7 +145,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 17, "id": "e4f5092f", "metadata": {}, "outputs": [], @@ -155,7 +155,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 18, "id": "490604e9", "metadata": {}, "outputs": [], @@ -165,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 19, "id": "653b1617", "metadata": {}, "outputs": [ @@ -179,19 +179,23 @@ "\u001b[32;1m\u001b[1;3mThought: I need to find out how many people live in Canada\n", "Action: Search\n", "Action Input: Population of Canada\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,553,548 as of Saturday, December 17, 2022, based on Worldometer elaboration of the latest United Nations data. Canada ...\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: Arrr, there be 38,553,548 scallywags livin' in Canada!\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I need to find out the exact population of Canada\n", + "Action: Search\n", + "Action Input: Population of Canada 2020\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I now know the population of Canada\n", + "Final Answer: Arrr, Canada be home to 37.59 million people!\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "\"Arrr, there be 38,553,548 scallywags livin' in Canada!\"" + "'Arrr, Canada be home to 37.59 million people!'" ] }, - "execution_count": 8, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -211,7 +215,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 20, "id": "43dbfa2f", "metadata": {}, "outputs": [], @@ -232,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 21, "id": "0f087313", "metadata": {}, "outputs": [], @@ -242,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 22, "id": "92c75a10", "metadata": {}, "outputs": [], @@ -252,7 +256,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 23, "id": "ac5b83bf", "metadata": {}, "outputs": [], @@ -262,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 24, "id": "c960e4ff", "metadata": {}, "outputs": [ @@ -276,19 +280,47 @@ "\u001b[32;1m\u001b[1;3mThought: I should look up the population of Canada.\n", "Action: Search\n", "Action Input: Population of Canada\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,553,548 as of Saturday, December 17, 2022, based on Worldometer elaboration of the latest United Nations data. Canada ...\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n", - "Final Answer: La popolazione attuale del Canada è 38.553.548 al sabato 17 dicembre 2022, secondo l'elaborazione di Worldometer dei dati più recenti delle Nazioni Unite.\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should look for the population of Canada.\n", + "Action: Search\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should look for the population of Canada.\n", + "Action: Search\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should look for the population of Canada.\n", + "Action: Search\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should look for the population of Canada.\n", + "Action: Search\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should look for the population of Canada.\n", + "Action: Search\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should look for the population of Canada.\n", + "Action: Search\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should look for the population of Canada.\n", + "Action: Search\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mCanada is a country in North America. Its ten provinces and three territories extend from the Atlantic Ocean to the Pacific Ocean and northward into the Arctic Ocean, covering over 9.98 million square kilometres, making it the world's second-largest country by total area.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I now know the population of Canada.\n", + "Final Answer: La popolazione del Canada è di circa 37 milioni di persone.\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "\"La popolazione attuale del Canada è 38.553.548 al sabato 17 dicembre 2022, secondo l'elaborazione di Worldometer dei dati più recenti delle Nazioni Unite.\"" + "'La popolazione del Canada è di circa 37 milioni di persone.'" ] }, - "execution_count": 14, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -318,7 +350,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -332,7 +364,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/agents/custom_tools.ipynb b/docs/modules/agents/examples/custom_tools.ipynb similarity index 94% rename from docs/examples/agents/custom_tools.ipynb rename to docs/modules/agents/examples/custom_tools.ipynb index 033ec433..568f9ab4 100644 --- a/docs/examples/agents/custom_tools.ipynb +++ b/docs/modules/agents/examples/custom_tools.ipynb @@ -44,7 +44,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "id": "36ed392e", "metadata": {}, "outputs": [], @@ -63,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "id": "56ff7670", "metadata": {}, "outputs": [], @@ -87,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "id": "5b93047d", "metadata": {}, "outputs": [], @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "6f96a891", "metadata": {}, "outputs": [ @@ -121,7 +121,6 @@ "\n", "\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n", "23^0.23\u001b[32;1m\u001b[1;3m\n", - "\n", "```python\n", "import math\n", "print(math.pow(23, 0.23))\n", @@ -144,7 +143,7 @@ "\"Harry Styles' age raised to the 0.23 power is 2.0568252837687546.\"" ] }, - "execution_count": 5, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -165,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 8, "id": "79213f40", "metadata": {}, "outputs": [], @@ -175,7 +174,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 9, "id": "e1067dcb", "metadata": {}, "outputs": [], @@ -185,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 10, "id": "6c66ffe8", "metadata": {}, "outputs": [], @@ -195,7 +194,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 11, "id": "f45b5bc3", "metadata": {}, "outputs": [], @@ -205,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 12, "id": "565e2b9b", "metadata": {}, "outputs": [ @@ -240,7 +239,7 @@ "\"Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886.\"" ] }, - "execution_count": 21, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -260,7 +259,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -274,7 +273,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/agents/intermediate_steps.ipynb b/docs/modules/agents/examples/intermediate_steps.ipynb similarity index 75% rename from docs/examples/agents/intermediate_steps.ipynb rename to docs/modules/agents/examples/intermediate_steps.ipynb index a10ff69a..077ea37d 100644 --- a/docs/examples/agents/intermediate_steps.ipynb +++ b/docs/modules/agents/examples/intermediate_steps.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "b2b0d119", "metadata": {}, "outputs": [], @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, "id": "36ed392e", "metadata": {}, "outputs": [], @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 4, "id": "6abf3b08", "metadata": {}, "outputs": [], @@ -61,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 5, "id": "837211e8", "metadata": {}, "outputs": [ @@ -75,14 +75,14 @@ "\u001b[32;1m\u001b[1;3m I should look up Olivia Wilde's boyfriend's age\n", "Action: Search\n", "Action Input: \"Olivia Wilde's boyfriend's age\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mHarry Styles, 28, and Olivia Wilde, 38, first met and sparked their connection when he joined the cast the actresses' psychological thriller ...\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I should use a calculator\n", + "Observation: \u001b[36;1m\u001b[1;3m28 years\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I should use the calculator to raise that number to the 0.23 power\n", "Action: Calculator\n", - "Action Input: 28.0 raised to the 0.23 power\u001b[0m\n", + "Action Input: 28^0.23\u001b[0m\n", "Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.1520202182226886\n", "\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: Olivia Wilde's boyfriend is 2.1520202182226886 years old.\u001b[0m\n", + "Final Answer: 2.1520202182226886\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] } @@ -93,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 6, "id": "e1a39a23", "metadata": {}, "outputs": [ @@ -101,7 +101,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "[(AgentAction(tool='Search', tool_input=\"Olivia Wilde's boyfriend's age\", log=' I should look up Olivia Wilde\\'s boyfriend\\'s age\\nAction: Search\\nAction Input: \"Olivia Wilde\\'s boyfriend\\'s age\"'), \"Harry Styles, 28, and Olivia Wilde, 38, first met and sparked their connection when he joined the cast the actresses' psychological thriller ...\"), (AgentAction(tool='Calculator', tool_input='28.0 raised to the 0.23 power', log=' I should use a calculator\\nAction: Calculator\\nAction Input: 28.0 raised to the 0.23 power'), 'Answer: 2.1520202182226886\\n')]\n" + "[(AgentAction(tool='Search', tool_input=\"Olivia Wilde's boyfriend's age\", log=' I should look up Olivia Wilde\\'s boyfriend\\'s age\\nAction: Search\\nAction Input: \"Olivia Wilde\\'s boyfriend\\'s age\"'), '28 years'), (AgentAction(tool='Calculator', tool_input='28^0.23', log=' I should use the calculator to raise that number to the 0.23 power\\nAction: Calculator\\nAction Input: 28^0.23'), 'Answer: 2.1520202182226886\\n')]\n" ] } ], @@ -112,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 7, "id": "6365bb69", "metadata": {}, "outputs": [ @@ -127,13 +127,13 @@ " \"Olivia Wilde's boyfriend's age\",\n", " \" I should look up Olivia Wilde's boyfriend's age\\nAction: Search\\nAction Input: \\\"Olivia Wilde's boyfriend's age\\\"\"\n", " ],\n", - " \"Harry Styles, 28, and Olivia Wilde, 38, first met and sparked their connection when he joined the cast the actresses' psychological thriller ...\"\n", + " \"28 years\"\n", " ],\n", " [\n", " [\n", " \"Calculator\",\n", - " \"28.0 raised to the 0.23 power\",\n", - " \" I should use a calculator\\nAction: Calculator\\nAction Input: 28.0 raised to the 0.23 power\"\n", + " \"28^0.23\",\n", + " \" I should use the calculator to raise that number to the 0.23 power\\nAction: Calculator\\nAction Input: 28^0.23\"\n", " ],\n", " \"Answer: 2.1520202182226886\\n\"\n", " ]\n", @@ -153,11 +153,19 @@ "metadata": {}, "outputs": [], "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8dc69fc3", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -171,7 +179,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/agents/max_iterations.ipynb b/docs/modules/agents/examples/max_iterations.ipynb similarity index 100% rename from docs/examples/agents/max_iterations.ipynb rename to docs/modules/agents/examples/max_iterations.ipynb diff --git a/docs/examples/agents/multi_input_tool.ipynb b/docs/modules/agents/examples/multi_input_tool.ipynb similarity index 93% rename from docs/examples/agents/multi_input_tool.ipynb rename to docs/modules/agents/examples/multi_input_tool.ipynb index d9291561..1a223a18 100644 --- a/docs/examples/agents/multi_input_tool.ipynb +++ b/docs/modules/agents/examples/multi_input_tool.ipynb @@ -50,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "id": "6db1d43f", "metadata": {}, "outputs": [], @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "id": "aa25d0ca", "metadata": {}, "outputs": [ @@ -94,7 +94,7 @@ "'3 times 4 is 12'" ] }, - "execution_count": 4, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -114,7 +114,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -128,7 +128,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/agents/search_tools.ipynb b/docs/modules/agents/examples/search_tools.ipynb similarity index 57% rename from docs/examples/agents/search_tools.ipynb rename to docs/modules/agents/examples/search_tools.ipynb index b6fe214e..5cdba823 100644 --- a/docs/examples/agents/search_tools.ipynb +++ b/docs/modules/agents/examples/search_tools.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "dadbcfcd", "metadata": {}, "outputs": [], @@ -44,7 +44,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "dd4ce6d9", "metadata": {}, "outputs": [], @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "ef63bb84", "metadata": {}, "outputs": [], @@ -64,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "53e24f5d", "metadata": {}, "outputs": [ @@ -78,19 +78,19 @@ "\u001b[32;1m\u001b[1;3m I need to find out what the current weather is in Pomfret.\n", "Action: Search\n", "Action Input: \"weather in Pomfret\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m10 Day Weather-Pomfret, CT ; Thu 29. 48° · 32°. 6% · SW 9 mph ; Fri 30. 53° · 39°. 8% · SW 8 mph ; Sat 31. 51° · 47°. 33% · SW 8 mph.\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mShowers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the current weather in Pomfret.\n", - "Final Answer: The current weather in Pomfret is 10 Day Weather-Pomfret, CT ; Thu 29. 48° · 32°. 6% · SW 9 mph ; Fri 30. 53° · 39°. 8% · SW 8 mph ; Sat 31. 51° · 47°. 33% · SW 8 mph.\u001b[0m\n", + "Final Answer: Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'The current weather in Pomfret is 10 Day Weather-Pomfret, CT ; Thu 29. 48° · 32°. 6% · SW 9 mph ; Fri 30. 53° · 39°. 8% · SW 8 mph ; Sat 31. 51° · 47°. 33% · SW 8 mph.'" + "'Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.'" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -111,7 +111,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 13, "id": "3e9c7c20", "metadata": {}, "outputs": [], @@ -121,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 14, "id": "b83624dc", "metadata": {}, "outputs": [], @@ -131,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 17, "id": "9d5835e2", "metadata": {}, "outputs": [ @@ -145,19 +145,19 @@ "\u001b[32;1m\u001b[1;3m I should look up the current weather conditions.\n", "Action: Google Search\n", "Action Input: \"weather in Pomfret\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mBe prepared with the most accurate 10-day forecast for Pomfret, CT with highs, lows, chance of precipitation from The Weather Channel and Weather.com. Pomfret, CT Weather Forecast, with current conditions, wind, air quality, and what to expect for the next 3 days. Hourly Weather-Pomfret, CT. As of 6:05 am EST. Thursday, December 29. 6:00 am. Pomfret CT. Today. Today: Partly sunny, with a high near 40. ... National Digital Forecast Database Maximum Temperature Forecast. High Temperature. Hourly Weather-Pomfret, CT. As of 4:54 pm EST. Tuesday, December 27. 5:00 pm. Pomfret Center Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... South Pomfret VT ... Hazardous Weather Outlook · Regional Weather Conditions ... National Digital Forecast Database Maximum Temperature Forecast. North Pomfret Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... Pomfret, CT 12 hour by hour weather forecast includes precipitation, temperatures, sky conditions, rain chance, dew-point, relative humidity, wind direction ... Hazardous Weather Conditions ... Pomfret Center CT ... Temperature rising to near 53 by 8am, then falling to around 38 during the remainder of the day.\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mShowers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%. Pomfret, CT Weather Forecast, with current conditions, wind, air quality, and what to expect for the next 3 days. Hourly Weather-Pomfret, CT. As of 12:52 am EST. Special Weather Statement +2 ... Hazardous Weather Conditions. Special Weather Statement ... Pomfret CT. Tonight ... National Digital Forecast Database Maximum Temperature Forecast. Pomfret Center Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... Pomfret, CT 12 hour by hour weather forecast includes precipitation, temperatures, sky conditions, rain chance, dew-point, relative humidity, wind direction ... North Pomfret Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for ... Today's Weather - Pomfret, CT. Dec 31, 2022 4:00 PM. Putnam MS. --. Weather forecast icon. Feels like --. Hi --. Lo --. Pomfret, CT temperature trend for the next 14 Days. Find daytime highs and nighttime lows from TheWeatherNetwork.com. Pomfret, MD Weather Forecast Date: 332 PM EST Wed Dec 28 2022. The area/counties/county of: Charles, including the cites of: St. Charles and Waldorf.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the current weather conditions in Pomfret.\n", - "Final Answer: The current weather in Pomfret is partly sunny with a high near 40. There is a chance of precipitation and temperatures will rise to near 53 by 8am, then fall to around 38 during the remainder of the day.\u001b[0m\n", + "Final Answer: Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'The current weather in Pomfret is partly sunny with a high near 40. There is a chance of precipitation and temperatures will rise to near 53 by 8am, then fall to around 38 during the remainder of the day.'" + "'Showers early becoming a steady light rain later in the day. Near record high temperatures. High around 60F. Winds SW at 10 to 15 mph. Chance of rain 60%.'" ] }, - "execution_count": 11, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -165,19 +165,11 @@ "source": [ "agent.run(\"What is the weather in Pomfret?\")" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "68a05cfd", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -191,7 +183,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/getting_started/agents.ipynb b/docs/modules/agents/getting_started.ipynb similarity index 79% rename from docs/getting_started/agents.ipynb rename to docs/modules/agents/getting_started.ipynb index 19b4056e..b8360e18 100644 --- a/docs/getting_started/agents.ipynb +++ b/docs/modules/agents/getting_started.ipynb @@ -5,7 +5,7 @@ "id": "5436020b", "metadata": {}, "source": [ - "# Agents\n", + "# Getting Started\n", "\n", "Agents use an LLM to determine which actions to take and in what order.\n", "An action can either be using a tool and observing its output, or returning to the user.\n", @@ -24,14 +24,14 @@ "- LLM: The language model powering the agent.\n", "- 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).\n", "\n", - "**Agents**: For a list of supported agents and their specifications, see [here](../explanation/agents.md).\n", + "**Agents**: For a list of supported agents and their specifications, see [here](agents.md).\n", "\n", - "**Tools**: For a list of predefined tools and their specifications, see [here](../explanation/tools.md)." + "**Tools**: For a list of predefined tools and their specifications, see [here](tools.md)." ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "id": "d01216c0", "metadata": {}, "outputs": [], @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "0728f0d9", "metadata": {}, "outputs": [], @@ -69,7 +69,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "id": "ba4e7618", "metadata": {}, "outputs": [], @@ -87,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "id": "03208e2b", "metadata": {}, "outputs": [], @@ -105,7 +105,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "id": "244ee75c", "metadata": {}, "outputs": [ @@ -119,28 +119,28 @@ "\u001b[32;1m\u001b[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\n", "Action: Search\n", "Action Input: \"Olivia Wilde boyfriend\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mJason Sudeikis\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to find out Jason Sudeikis' age\n", + "Observation: \u001b[36;1m\u001b[1;3mHarry Styles\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I need to find out Harry Styles' age\n", "Action: Search\n", - "Action Input: \"Jason Sudeikis age\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m47 years\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to calculate 47 raised to the 0.23 power\n", + "Action Input: \"Harry Styles age\"\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m28 years\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I need to calculate 28 raised to the 0.23 power\n", "Action: Calculator\n", - "Action Input: 47^0.23\u001b[0m\n", - "Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.4242784855673896\n", + "Action Input: 28^0.23\u001b[0m\n", + "Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.1520202182226886\n", "\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.\u001b[0m\n", + "Final Answer: Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886.\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "\"Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.\"" + "\"Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886.\"" ] }, - "execution_count": 3, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -148,19 +148,11 @@ "source": [ "agent.run(\"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\")" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e7776981", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -174,7 +166,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/modules/agents/how_to_guides.rst b/docs/modules/agents/how_to_guides.rst new file mode 100644 index 00000000..fa77b921 --- /dev/null +++ b/docs/modules/agents/how_to_guides.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 `_: How to create custom tools that an agent can use. + +`Intermediate Steps `_: How to access and use intermediate steps to get more visibility into the internals of an agent. + +`Custom Agent `_: How to create a custom agent (specifically, a custom LLM + prompt to drive that agent). + +`Multi Input Tools `_: How to use a tool that requires multiple inputs with an agent. + +`Search Tools `_: How to use the different type of search tools that LangChain supports. + +`Max Iterations `_: 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 `_ 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 `_ +- **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 `_ + +**Self-Ask-With-Search** + +- **Tools used**: Search +- **Agent used**: `self-ask-with-search` +- `Paper `_ +- `Example Notebook `_ + +**ReAct** + +- **Tools used**: Wikipedia Docstore +- **Agent used**: `react-docstore` +- `Paper `_ +- `Example Notebook `_ + + + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + examples/* + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + implementations/* \ No newline at end of file diff --git a/docs/examples/agents/mrkl.ipynb b/docs/modules/agents/implementations/mrkl.ipynb similarity index 76% rename from docs/examples/agents/mrkl.ipynb rename to docs/modules/agents/implementations/mrkl.ipynb index 5b99402a..ddcf11d6 100644 --- a/docs/examples/agents/mrkl.ipynb +++ b/docs/modules/agents/implementations/mrkl.ipynb @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "07e96d99", "metadata": {}, "outputs": [], @@ -40,7 +40,7 @@ "llm = OpenAI(temperature=0)\n", "search = SerpAPIWrapper()\n", "llm_math_chain = LLMMathChain(llm=llm, verbose=True)\n", - "db = SQLDatabase.from_uri(\"sqlite:///../../../notebooks/Chinook.db\")\n", + "db = SQLDatabase.from_uri(\"sqlite:///../../../../notebooks/Chinook.db\")\n", "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)\n", "tools = [\n", " Tool(\n", @@ -63,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "a069c4b6", "metadata": {}, "outputs": [], @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "e603cd7d", "metadata": {}, "outputs": [ @@ -83,22 +83,21 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new AgentWithTools chain...\u001b[0m\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\n", "Action: Search\n", "Action Input: \"Who is Olivia Wilde's boyfriend?\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mOlivia Wilde started dating Harry Styles after ending her years-long engagement to Jason Sudeikis — see their relationship timeline.\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to find out Harry Styles' age.\n", + "Observation: \u001b[36;1m\u001b[1;3mHarry Styles\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I need to find out Harry Styles' age\n", "Action: Search\n", "Action Input: \"How old is Harry Styles?\"\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m28 years\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to calculate 28 raised to the 0.23 power.\n", + "Thought:\u001b[32;1m\u001b[1;3m I need to calculate 28 raised to the 0.23 power\n", "Action: Calculator\n", "Action Input: 28^0.23\u001b[0m\n", "\n", "\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n", "28^0.23\u001b[32;1m\u001b[1;3m\n", - "\n", "```python\n", "import math\n", "print(math.pow(28, 0.23))\n", @@ -110,18 +109,18 @@ "\n", "Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.1520202182226886\n", "\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n", - "Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.\u001b[0m\n", - "\u001b[1m> Finished AgentWithTools chain.\u001b[0m\n" + "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", + "Final Answer: Harry Styles is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.\u001b[0m\n", + "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "\"Harry Styles, Olivia Wilde's boyfriend, is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.\"" + "'Harry Styles is 28 years old and his age raised to the 0.23 power is 2.1520202182226886.'" ] }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -132,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "a5c07010", "metadata": {}, "outputs": [ @@ -142,35 +141,35 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new AgentWithTools chain...\u001b[0m\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m I need to find out the artist's full name and then search the FooBar database for their albums.\n", "Action: Search\n", "Action Input: \"The Storm Before the Calm\" artist\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mThe Storm Before the Calm (stylized in all lowercase) is the tenth (and eighth international) studio album by Canadian-American singer-songwriter Alanis ...\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now need to search the FooBar database for Alanis Morissette's albums\n", + "Observation: \u001b[36;1m\u001b[1;3mAlanis Morissette - the storm before the calm - Amazon.com Music.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I now need to search the FooBar database for Alanis Morissette's albums.\n", "Action: FooBar DB\n", - "Action Input: What albums by Alanis Morissette are in the FooBar database?\u001b[0m\n", + "Action Input: What albums of Alanis Morissette are in the FooBar database?\u001b[0m\n", "\n", "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", - "What albums by Alanis Morissette are in the FooBar database? \n", + "What albums of Alanis Morissette are in the FooBar database? \n", "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Title FROM Album WHERE ArtistId IN (SELECT ArtistId FROM Artist WHERE Name = 'Alanis Morissette');\u001b[0m\n", "SQLResult: \u001b[33;1m\u001b[1;3m[('Jagged Little Pill',)]\u001b[0m\n", - "Answer:\u001b[32;1m\u001b[1;3m The album Jagged Little Pill by Alanis Morissette is in the FooBar database.\u001b[0m\n", + "Answer:\u001b[32;1m\u001b[1;3m The album 'Jagged Little Pill' by Alanis Morissette is in the FooBar database.\u001b[0m\n", "\u001b[1m> Finished SQLDatabaseChain chain.\u001b[0m\n", "\n", - "Observation: \u001b[38;5;200m\u001b[1;3m The album Jagged Little Pill by Alanis Morissette is in the FooBar database.\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: Alanis Morissette and Jagged Little Pill are in the FooBar database.\u001b[0m\n", - "\u001b[1m> Finished AgentWithTools chain.\u001b[0m\n" + "Observation: \u001b[38;5;200m\u001b[1;3m The album 'Jagged Little Pill' by Alanis Morissette is in the FooBar database.\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n", + "Final Answer: Alanis Morissette is the artist who recently released an album called 'The Storm Before the Calm' and the album 'Jagged Little Pill' by Alanis Morissette is in the FooBar database.\u001b[0m\n", + "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'Alanis Morissette and Jagged Little Pill are in the FooBar database.'" + "\"Alanis Morissette is the artist who recently released an album called 'The Storm Before the Calm' and the album 'Jagged Little Pill' by Alanis Morissette is in the FooBar database.\"" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -204,7 +203,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/agents/natbot.py b/docs/modules/agents/implementations/natbot.py similarity index 100% rename from docs/examples/agents/natbot.py rename to docs/modules/agents/implementations/natbot.py diff --git a/docs/examples/agents/react.ipynb b/docs/modules/agents/implementations/react.ipynb similarity index 92% rename from docs/examples/agents/react.ipynb rename to docs/modules/agents/implementations/react.ipynb index 925e85cf..3cfb8672 100644 --- a/docs/examples/agents/react.ipynb +++ b/docs/modules/agents/implementations/react.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "id": "4e272b47", "metadata": {}, "outputs": [], @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "id": "8078c8f1", "metadata": {}, "outputs": [ @@ -68,7 +68,7 @@ "'Bill Clinton'" ] }, - "execution_count": 2, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -77,19 +77,11 @@ "question = \"Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?\"\n", "react.run(question)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "75f914ba", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -103,7 +95,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/agents/self_ask_with_search.ipynb b/docs/modules/agents/implementations/self_ask_with_search.ipynb similarity index 79% rename from docs/examples/agents/self_ask_with_search.ipynb rename to docs/modules/agents/implementations/self_ask_with_search.ipynb index 305e28f9..21d1391e 100644 --- a/docs/examples/agents/self_ask_with_search.ipynb +++ b/docs/modules/agents/implementations/self_ask_with_search.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "7e3b513e", "metadata": {}, "outputs": [ @@ -22,14 +22,14 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new AgentWithTools chain...\u001b[0m\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m Yes.\n", "Follow up: Who is the reigning men's U.S. Open champion?\u001b[0m\n", - "Intermediate answer: \u001b[36;1m\u001b[1;3mCarlos Alcaraz\u001b[0m\n", + "Intermediate answer: \u001b[36;1m\u001b[1;3mCarlos Alcaraz won the 2022 Men's single title while Poland's Iga Swiatek won the Women's single title defeating Tunisian's Ons Jabeur.\u001b[0m\n", "\u001b[32;1m\u001b[1;3mFollow up: Where is Carlos Alcaraz from?\u001b[0m\n", "Intermediate answer: \u001b[36;1m\u001b[1;3mEl Palmar, Spain\u001b[0m\n", "\u001b[32;1m\u001b[1;3mSo the final answer is: El Palmar, Spain\u001b[0m\n", - "\u001b[1m> Finished AgentWithTools chain.\u001b[0m\n" + "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { @@ -38,7 +38,7 @@ "'El Palmar, Spain'" ] }, - "execution_count": 1, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -59,19 +59,11 @@ "self_ask_with_search = initialize_agent(tools, llm, agent=\"self-ask-with-search\", verbose=True)\n", "self_ask_with_search.run(\"What is the hometown of the reigning men's U.S. Open champion?\")" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "683d69e7", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -85,7 +77,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/modules/agents/key_concepts.md b/docs/modules/agents/key_concepts.md new file mode 100644 index 00000000..72e267f2 --- /dev/null +++ b/docs/modules/agents/key_concepts.md @@ -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). diff --git a/docs/explanation/tools.md b/docs/modules/agents/tools.md similarity index 99% rename from docs/explanation/tools.md rename to docs/modules/agents/tools.md index b7501361..8f35a1fc 100644 --- a/docs/explanation/tools.md +++ b/docs/modules/agents/tools.md @@ -28,7 +28,7 @@ Below is a list of all supported tools and relevant information: - Requires LLM: Whether this tool requires an LLM to be initialized. - (Optional) Extra Parameters: What extra parameters are required to initialize this tool. -### List of Tools +## List of Tools **python_repl** - Tool Name: Python REPL diff --git a/docs/modules/chains.rst b/docs/modules/chains.rst new file mode 100644 index 00000000..0ef016eb --- /dev/null +++ b/docs/modules/chains.rst @@ -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 `_: A getting started guide for chains, to get you up and running quickly. + +- `Key Concepts `_: A conceptual guide going over the various concepts related to chains. + +- `How-To Guides `_: A collection of how-to guides. These highlight how to use various types of chains. + +- `Reference `_: 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 \ No newline at end of file diff --git a/docs/modules/chains/combine_docs.md b/docs/modules/chains/combine_docs.md new file mode 100644 index 00000000..ff9e1737 --- /dev/null +++ b/docs/modules/chains/combine_docs.md @@ -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. diff --git a/docs/examples/data_augmented_generation/qa_with_sources.ipynb b/docs/modules/chains/combine_docs_examples/qa_with_sources.ipynb similarity index 82% rename from docs/examples/data_augmented_generation/qa_with_sources.ipynb rename to docs/modules/chains/combine_docs_examples/qa_with_sources.ipynb index b73b291c..14ad1097 100644 --- a/docs/examples/data_augmented_generation/qa_with_sources.ipynb +++ b/docs/modules/chains/combine_docs_examples/qa_with_sources.ipynb @@ -7,7 +7,7 @@ "source": [ "# Question Answering with Sources\n", "\n", - "This notebook walks through how to use LangChain for question answering with sources over a list of documents. It covers three different chain types: `stuff`, `map_reduce`, and `refine`. For a more in depth explanation of what these chain types are, see [here](../../explanation/combine_docs.md)." + "This notebook walks through how to use LangChain for question answering with sources over a list of documents. It covers three different chain types: `stuff`, `map_reduce`, and `refine`. For a more in depth explanation of what these chain types are, see [here](../combine_docs.md)." ] }, { @@ -15,7 +15,7 @@ "id": "ca2f0efc", "metadata": {}, "source": [ - "### Prepare Data\n", + "## Prepare Data\n", "First we prepare the data. For this example we do similarity search over a vector database, but these documents could be fetched in any manner (the point of this notebook to highlight what to do AFTER you fetch the documents)." ] }, @@ -41,7 +41,7 @@ "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", @@ -86,7 +86,7 @@ "id": "d82f899a", "metadata": {}, "source": [ - "### The `stuff` Chain\n", + "## The `stuff` Chain\n", "\n", "This sections shows results of using the `stuff` Chain to do question answering with sources." ] @@ -104,26 +104,16 @@ { "cell_type": "code", "execution_count": 7, - "id": "e239964b", - "metadata": {}, - "outputs": [], - "source": [ - "docs = [Document(page_content=t, metadata={\"source\": i}) for i, t in enumerate(texts[:3])]" - ] - }, - { - "cell_type": "code", - "execution_count": 8, "id": "7d766417", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'output_text': ' The president did not mention Justice Breyer.\\nSOURCES: 0-pl, 1-pl, 2-pl'}" + "{'output_text': ' The president thanked Justice Breyer for his service.\\nSOURCES: 30-pl'}" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -138,14 +128,14 @@ "id": "c5dbb304", "metadata": {}, "source": [ - "### The `map_reduce` Chain\n", + "## The `map_reduce` Chain\n", "\n", "This sections shows results of using the `map_reduce` Chain to do question answering with sources." ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "id": "921db0a4", "metadata": {}, "outputs": [], @@ -155,25 +145,17 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "e417926a", "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n", - "Token indices sequence length is longer than the specified maximum sequence length for this model (1546 > 1024). Running this sequence through the model will result in indexing errors\n" - ] - }, { "data": { "text/plain": [ - "{'output_text': ' The president did not mention Justice Breyer.\\nSOURCES: 0, 1, 2'}" + "{'output_text': ' The president thanked Justice Breyer for his service.\\nSOURCES: 30-pl'}" ] }, - "execution_count": 10, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -195,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 10, "id": "15af265f", "metadata": {}, "outputs": [], @@ -205,21 +187,21 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 11, "id": "21b136e5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'map_steps': [{'text': ' \"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.\"'},\n", - " {'text': ' None'},\n", - " {'text': ' None'},\n", - " {'text': ' None'}],\n", + "{'map_steps': [' \"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.\"',\n", + " ' None',\n", + " ' None',\n", + " ' None'],\n", " 'output_text': ' The president thanked Justice Breyer for his service.\\nSOURCES: 30-pl'}" ] }, - "execution_count": 8, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -233,14 +215,14 @@ "id": "5bf0e1ab", "metadata": {}, "source": [ - "### The `refine` Chain\n", + "## The `refine` Chain\n", "\n", "This sections shows results of using the `refine` Chain to do question answering with sources." ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "id": "904835c8", "metadata": {}, "outputs": [], @@ -250,17 +232,17 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "f60875c6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'output_text': \"\\n\\nThe president did not mention Justice Breyer in his speech to the European Parliament, which focused on building a coalition of freedom-loving nations to confront Putin, unifying European allies, countering Russia's lies with truth, and enforcing powerful economic sanctions. Source: 2\"}" + "{'output_text': \"\\n\\nThe president said that he was honoring Justice Breyer for his dedication to serving the country and that he was a retiring Justice of the United States Supreme Court. He also thanked him for his service and praised his career as a top litigator in private practice, a former federal public defender, and a family of public school educators and police officers. He noted Justice Breyer's reputation as a consensus builder and the broad range of support he has received from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. He also highlighted the importance of securing the border and fixing the immigration system in order to advance liberty and justice, and mentioned the new technology, joint patrols, dedicated immigration judges, and commitments to support partners in South and Central America that have been put in place. He also expressed his commitment to the LGBTQ+ community, noting the need for the bipartisan Equality Act and the importance of protecting transgender Americans from state laws targeting them. He also highlighted his commitment to bipartisanship, noting the 80 bipartisan bills he signed into law last year, and his plans to strengthen the Violence Against Women Act. Additionally, he announced that the Justice Department will name a chief prosecutor for pandemic fraud and his plan to lower the deficit by more than one trillion dollars in a\"}" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -282,7 +264,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 14, "id": "3396a773", "metadata": {}, "outputs": [], @@ -292,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 15, "id": "be5739ef", "metadata": {}, "outputs": [ @@ -306,7 +288,7 @@ " 'output_text': \"\\n\\nThe president said that he was honoring Justice Breyer for his dedication to serving the country, his career as a top litigator in private practice, a former federal public defender, and his family of public school educators and police officers. He also noted Justice Breyer's consensus-building skills and the broad range of support he has received from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. He also highlighted the importance of advancing liberty and justice by securing the border and fixing the immigration system, noting the new technology and joint patrols with Mexico and Guatemala to catch more human traffickers, as well as the dedicated immigration judges and commitments to support partners in South and Central America to host more refugees and secure their own borders. Additionally, he mentioned the need for the bipartisan Equality Act to be passed and signed into law, and the importance of strengthening the Violence Against Women Act. He also offered a Unity Agenda for the Nation, which includes beating the opioid epidemic, and announced that the Justice Department will name a chief prosecutor for pandemic fraud. Source: 31, 33, 20\"}" ] }, - "execution_count": 10, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -314,14 +296,6 @@ "source": [ "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7355fedd", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -340,7 +314,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/data_augmented_generation/question_answering.ipynb b/docs/modules/chains/combine_docs_examples/question_answering.ipynb similarity index 82% rename from docs/examples/data_augmented_generation/question_answering.ipynb rename to docs/modules/chains/combine_docs_examples/question_answering.ipynb index e140071c..cc2b7f4a 100644 --- a/docs/examples/data_augmented_generation/question_answering.ipynb +++ b/docs/modules/chains/combine_docs_examples/question_answering.ipynb @@ -7,7 +7,7 @@ "source": [ "# Question Answering\n", "\n", - "This notebook walks through how to use LangChain for question answering over a list of documents. It covers three different types of chaings: `stuff`, `map_reduce`, and `refine`. For a more in depth explanation of what these chain types are, see [here](../../explanation/combine_docs.md)." + "This notebook walks through how to use LangChain for question answering over a list of documents. It covers three different types of chaings: `stuff`, `map_reduce`, and `refine`. For a more in depth explanation of what these chain types are, see [here](../combine_docs.md)." ] }, { @@ -15,7 +15,7 @@ "id": "726f4996", "metadata": {}, "source": [ - "### Prepare Data\n", + "## Prepare Data\n", "First we prepare the data. For this example we do similarity search over a vector database, but these documents could be fetched in any manner (the point of this notebook to highlight what to do AFTER you fetch the documents)." ] }, @@ -39,7 +39,7 @@ "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", @@ -84,7 +84,7 @@ "id": "f78787a0", "metadata": {}, "source": [ - "### The `stuff` Chain\n", + "## The `stuff` Chain\n", "\n", "This sections shows results of using the `stuff` Chain to do question answering." ] @@ -102,26 +102,16 @@ { "cell_type": "code", "execution_count": 7, - "id": "d145ae31", - "metadata": {}, - "outputs": [], - "source": [ - "docs = [Document(page_content=t) for t in texts[:3]]" - ] - }, - { - "cell_type": "code", - "execution_count": 8, "id": "77fdf1aa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'output_text': ' The president did not mention Justice Breyer.'}" + "{'output_text': ' The president said that he was honoring Justice Breyer for his service to the country and that he was a Constitutional scholar, Army veteran, and retiring Justice of the United States Supreme Court.'}" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -136,14 +126,14 @@ "id": "91522e29", "metadata": {}, "source": [ - "### The `map_reduce` Chain\n", + "## The `map_reduce` Chain\n", "\n", "This sections shows results of using the `map_reduce` Chain to do question answering." ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "id": "b0060f51", "metadata": {}, "outputs": [], @@ -153,17 +143,17 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "fbdb9137", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'output_text': ' The president did not mention Justice Breyer.'}" + "{'output_text': ' The president said, \"Justice Breyer, thank you for your service.\"'}" ] }, - "execution_count": 10, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -185,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 10, "id": "452c8680", "metadata": {}, "outputs": [], @@ -195,21 +185,21 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 11, "id": "90b47a75", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'map_steps': [{'text': ' \"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.\"'},\n", - " {'text': ' None'},\n", - " {'text': ' None'},\n", - " {'text': ' None'}],\n", + "{'map_steps': [' \"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.\"',\n", + " ' None',\n", + " ' None',\n", + " ' None'],\n", " 'output_text': ' The president said, \"Justice Breyer, thank you for your service.\"'}" ] }, - "execution_count": 8, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -223,14 +213,14 @@ "id": "6ea50ad0", "metadata": {}, "source": [ - "### The `refine` Chain\n", + "## The `refine` Chain\n", "\n", "This sections shows results of using the `refine` Chain to do question answering." ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "id": "fb167057", "metadata": {}, "outputs": [], @@ -240,17 +230,17 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "d8b5286e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'output_text': \"\\n\\nThe president did not mention Justice Breyer in his speech to the European Parliament about building a coalition of freedom-loving nations to confront Putin, unifying European allies, countering Russia's lies with truth, and enforcing powerful economic sanctions.\"}" + "{'output_text': '\\n\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his commitment to protecting the rights of LGBTQ+ Americans and his support for the bipartisan Equality Act. He also mentioned his plan to lower costs to give families a fair shot, lower the deficit, and go after criminals who stole pandemic relief funds. He also announced that the Justice Department will name a chief prosecutor for pandemic fraud.'}" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -272,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 14, "id": "a5c64200", "metadata": {}, "outputs": [], @@ -282,7 +272,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 15, "id": "817546ac", "metadata": {}, "outputs": [ @@ -296,7 +286,7 @@ " 'output_text': '\\n\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his commitment to protecting the rights of LGBTQ+ Americans and his support for the bipartisan Equality Act. He also mentioned his plan to lower costs to give families a fair shot, lower the deficit, and go after criminals who stole pandemic relief funds. He also announced that the Justice Department will name a chief prosecutor for pandemic fraud.'}" ] }, - "execution_count": 10, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -304,14 +294,6 @@ "source": [ "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "97d335c6", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -330,7 +312,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/data_augmented_generation/summarize.ipynb b/docs/modules/chains/combine_docs_examples/summarize.ipynb similarity index 65% rename from docs/examples/data_augmented_generation/summarize.ipynb rename to docs/modules/chains/combine_docs_examples/summarize.ipynb index 24c17ca1..fffa7f1c 100644 --- a/docs/examples/data_augmented_generation/summarize.ipynb +++ b/docs/modules/chains/combine_docs_examples/summarize.ipynb @@ -7,7 +7,7 @@ "source": [ "# Summarization\n", "\n", - "This notebook walks through how to use LangChain for summarization over a list of documents. It covers three different chain types: `stuff`, `map_reduce`, and `refine`. For a more in depth explanation of what these chain types are, see [here](../../explanation/combine_docs.md)." + "This notebook walks through how to use LangChain for summarization over a list of documents. It covers three different chain types: `stuff`, `map_reduce`, and `refine`. For a more in depth explanation of what these chain types are, see [here](../combine_docs.md)." ] }, { @@ -15,13 +15,13 @@ "id": "0b5660bf", "metadata": {}, "source": [ - "### Prepare Data\n", + "## Prepare Data\n", "First we prepare the data. For this example we create multiple documents from one long one, but these documents could be fetched in any manner (the point of this notebook to highlight what to do AFTER you fetch the documents)." ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "id": "e9db25f3", "metadata": {}, "outputs": [], @@ -32,45 +32,36 @@ "\n", "llm = OpenAI(temperature=0)\n", "\n", - "\n", - "text_splitter = CharacterTextSplitter()\n" + "text_splitter = CharacterTextSplitter()" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "id": "99bbe19b", "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "texts = text_splitter.split_text(state_of_the_union)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "id": "baa6e808", "metadata": {}, "outputs": [], "source": [ - "from langchain.docstore.document import Document" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "8dff4f43", - "metadata": {}, - "outputs": [], - "source": [ + "from langchain.docstore.document import Document\n", + "\n", "docs = [Document(page_content=t) for t in texts[:3]]" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "id": "27989fc4", "metadata": {}, "outputs": [], @@ -83,14 +74,14 @@ "id": "ea2d5c99", "metadata": {}, "source": [ - "### The `stuff` Chain\n", + "## The `stuff` Chain\n", "\n", "This sections shows results of using the `stuff` Chain to do summarization." ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "id": "f01f3196", "metadata": {}, "outputs": [], @@ -100,17 +91,17 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 10, "id": "da4d9801", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "' In his speech, President Biden addressed the ongoing conflict between Russia and Ukraine, and the need for the United States and its allies to stand with Ukraine. He also discussed the American Rescue Plan, the Bipartisan Infrastructure Law, and the Bipartisan Innovation Act, which will help to create jobs, modernize infrastructure, and level the playing field with China. He also emphasized the importance of buying American products to support American jobs.'" + "' In his speech, President Biden addressed the crisis in Ukraine, the American Rescue Plan, and the Bipartisan Infrastructure Law. He discussed the need to invest in America, educate Americans, and build the economy from the bottom up. He also announced the release of 60 million barrels of oil from reserves around the world, and the creation of a dedicated task force to go after the crimes of Russian oligarchs. He concluded by emphasizing the need to Buy American and use taxpayer dollars to rebuild America.'" ] }, - "execution_count": 7, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -124,14 +115,14 @@ "id": "9c868e86", "metadata": {}, "source": [ - "### The `map_reduce` Chain\n", + "## The `map_reduce` Chain\n", "\n", "This sections shows results of using the `map_reduce` Chain to do summarization." ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 11, "id": "ef28e1d4", "metadata": {}, "outputs": [], @@ -141,17 +132,17 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 12, "id": "f82c5f9f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\" In response to Vladimir Putin's aggression in Ukraine, the US and its allies have taken action to hold him accountable, including economic sanctions, cutting off access to technology, and seizing the assets of Russian oligarchs. They are also providing military, economic, and humanitarian assistance to the Ukrainians, and releasing 60 million barrels of oil from reserves around the world. President Biden has passed several laws to provide economic relief to Americans and create jobs, and is making sure taxpayer dollars support American jobs and businesses.\"" + "\" In response to Russia's aggression in Ukraine, the United States and its allies have imposed economic sanctions and are taking other measures to hold Putin accountable. The US is also providing economic and military assistance to Ukraine, protecting NATO countries, and releasing oil from its Strategic Petroleum Reserve. President Biden and Vice President Harris have passed legislation to help struggling families and rebuild America's infrastructure.\"" ] }, - "execution_count": 9, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -172,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "id": "d9cfc24e", "metadata": {}, "outputs": [], @@ -182,20 +173,20 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "id": "c7dff5e8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'map_steps': [{'text': \" In response to Russia's aggression in Ukraine, the United States has united with other freedom-loving nations to impose economic sanctions and hold Putin accountable. The U.S. Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs and seize their ill-gotten gains.\"},\n", - " {'text': ' The United States and its European allies are taking action to punish Russia for its invasion of Ukraine, including seizing assets, closing off airspace, and providing economic and military assistance to Ukraine. The US is also mobilizing forces to protect NATO countries and has released 30 million barrels of oil from its Strategic Petroleum Reserve to help blunt gas prices. The world is uniting in support of Ukraine and democracy, and the US stands with its Ukrainian allies.'},\n", - " {'text': \" President Biden and Vice President Harris ran for office with a new economic vision for America, and have since passed the American Rescue Plan and the Bipartisan Infrastructure Law to help struggling families and rebuild America's infrastructure. This includes creating jobs, modernizing roads, airports, ports, and waterways, replacing lead pipes, providing affordable high-speed internet, and investing in American products to support American jobs.\"}],\n", - " 'output_text': \" In response to Russia's aggression in Ukraine, the United States and its allies have imposed economic sanctions and are taking other measures to hold Putin accountable. The US is also providing economic and military assistance to Ukraine, protecting NATO countries, and passing legislation to help struggling families and rebuild America's infrastructure. The world is uniting in support of Ukraine and democracy, and the US stands with its Ukrainian allies.\"}" + "{'map_steps': [\" In response to Russia's aggression in Ukraine, the United States has united with other freedom-loving nations to impose economic sanctions and hold Putin accountable. The U.S. Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs and seize their ill-gotten gains.\",\n", + " ' The United States and its European allies are taking action to punish Russia for its invasion of Ukraine, including seizing assets, closing off airspace, and providing economic and military assistance to Ukraine. The US is also mobilizing forces to protect NATO countries and has released 30 million barrels of oil from its Strategic Petroleum Reserve to help blunt gas prices. The world is uniting in support of Ukraine and democracy, and the US stands with its Ukrainian-American citizens.',\n", + " \" President Biden and Vice President Harris ran for office with a new economic vision for America, and have since passed the American Rescue Plan and the Bipartisan Infrastructure Law to help struggling families and rebuild America's infrastructure. This includes creating jobs, modernizing roads, airports, ports, and waterways, replacing lead pipes, providing affordable high-speed internet, and investing in American products to support American jobs.\"],\n", + " 'output_text': \" In response to Russia's aggression in Ukraine, the United States and its allies have imposed economic sanctions and are taking other measures to hold Putin accountable. The US is also providing economic and military assistance to Ukraine, protecting NATO countries, and passing legislation to help struggling families and rebuild America's infrastructure. The world is uniting in support of Ukraine and democracy, and the US stands with its Ukrainian-American citizens.\"}" ] }, - "execution_count": 11, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -209,39 +200,31 @@ "id": "f61350f9", "metadata": {}, "source": [ - "### The `refine` Chain\n", + "## The `refine` Chain\n", "\n", "This sections shows results of using the `refine` Chain to do summarization." ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 15, "id": "3bcbe31e", "metadata": {}, - "outputs": [], - "source": [ - "chain = load_summarize_chain(llm, chain_type=\"refine\")" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "c8cad866", - "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\"\\nIn this speech, the speaker addresses the American people and their allies, discussing the recent aggression of Russia's Vladimir Putin in Ukraine. The speaker outlines the actions taken by the United States and its allies to hold Putin accountable, including economic sanctions, cutting off access to technology, and seizing the assets of Russian oligarchs. The speaker also announces the closing of American airspace to Russian flights, further isolating Russia and adding an additional squeeze on their economy. The Russian stock market has lost 40% of its value and trading remains suspended. Together with our allies, the United States is providing military, economic, and humanitarian assistance to Ukraine, and has mobilized forces to protect NATO countries. The speaker also announces the release of 60 million barrels of oil from reserves around the world, with the United States releasing 30 million barrels from its own Strategic Petroleum Reserve. The speaker emphasizes that the United States and its allies will defend every inch of NATO territory and that Putin will pay a high price for his aggression. The speaker also acknowledges the hardships faced by the American people due to the pandemic and the American Rescue Plan, which has provided immediate economic relief for tens of millions of Americans, helped put food on their table, keep a roof over their heads, and cut the cost of health insurance. The speaker\"" + "\"\\n\\nIn response to Russia's aggression in Ukraine, the United States has united with other freedom-loving nations to impose economic sanctions and hold Putin accountable. The U.S. Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs and seize their ill-gotten gains. We are joining with our European allies to find and seize the assets of Russian oligarchs, including yachts, luxury apartments, and private jets. The U.S. is also closing off American airspace to all Russian flights, further isolating Russia and adding an additional squeeze on their economy. The U.S. and its allies are providing support to the Ukrainians in their fight for freedom, including military, economic, and humanitarian assistance. The U.S. is also mobilizing ground forces, air squadrons, and ship deployments to protect NATO countries. The U.S. and its allies are also releasing 60 million barrels of oil from reserves around the world, with the U.S. contributing 30 million barrels from its own Strategic Petroleum Reserve. In addition, the U.S. has passed the American Rescue Plan to provide immediate economic relief for tens of millions of Americans, and the Bipartisan Infrastructure Law to rebuild America and create jobs. This investment will\"" ] }, - "execution_count": 11, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "chain = load_summarize_chain(llm, chain_type=\"refine\")\n", + "\n", "chain.run(docs)" ] }, @@ -257,19 +240,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 16, "id": "cd49ac4d", "metadata": {}, - "outputs": [], - "source": [ - "chain = load_summarize_chain(OpenAI(temperature=0), chain_type=\"refine\", return_refine_steps=True)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "6a74029d", - "metadata": {}, "outputs": [ { "data": { @@ -280,27 +253,21 @@ " 'output_text': \"\\n\\nIn response to Russia's aggression in Ukraine, the United States has united with other freedom-loving nations to impose economic sanctions and hold Putin accountable. The U.S. Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs and seize their ill-gotten gains. We are joining with our European allies to find and seize the assets of Russian oligarchs, including yachts, luxury apartments, and private jets. The U.S. is also closing off American airspace to all Russian flights, further isolating Russia and adding an additional squeeze on their economy. The U.S. and its allies are providing support to the Ukrainians in their fight for freedom, including military, economic, and humanitarian assistance. The U.S. is also mobilizing ground forces, air squadrons, and ship deployments to protect NATO countries. The U.S. and its allies are also releasing 60 million barrels of oil from reserves around the world, with the U.S. contributing 30 million barrels from its own Strategic Petroleum Reserve. In addition, the U.S. has passed the American Rescue Plan to provide immediate economic relief for tens of millions of Americans, and the Bipartisan Infrastructure Law to rebuild America and create jobs. This includes investing\"}" ] }, - "execution_count": 8, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "chain = load_summarize_chain(OpenAI(temperature=0), chain_type=\"refine\", return_refine_steps=True)\n", + "\n", "chain({\"input_documents\": docs}, return_only_outputs=True)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "db1ed69d", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -314,7 +281,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/data_augmented_generation/vector_db_qa.ipynb b/docs/modules/chains/combine_docs_examples/vector_db_qa.ipynb similarity index 77% rename from docs/examples/data_augmented_generation/vector_db_qa.ipynb rename to docs/modules/chains/combine_docs_examples/vector_db_qa.ipynb index 9608713c..543a0ef1 100644 --- a/docs/examples/data_augmented_generation/vector_db_qa.ipynb +++ b/docs/modules/chains/combine_docs_examples/vector_db_qa.ipynb @@ -30,7 +30,7 @@ "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "3018f865", "metadata": {}, "outputs": [], @@ -51,17 +51,17 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "032a47f8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator and federal public defender, and from a family of public school educators and police officers. He also said that she has received a broad range of support since she was nominated, from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\"" + "\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, and that she will continue Justice Breyer's legacy of excellence.\"" ] }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -70,19 +70,11 @@ "query = \"What did the president say about Ketanji Brown Jackson\"\n", "qa.run(query)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f056f6fd", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -96,7 +88,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/data_augmented_generation/vector_db_qa_with_sources.ipynb b/docs/modules/chains/combine_docs_examples/vector_db_qa_with_sources.ipynb similarity index 75% rename from docs/examples/data_augmented_generation/vector_db_qa_with_sources.ipynb rename to docs/modules/chains/combine_docs_examples/vector_db_qa_with_sources.ipynb index f6627228..e39b91eb 100644 --- a/docs/examples/data_augmented_generation/vector_db_qa_with_sources.ipynb +++ b/docs/modules/chains/combine_docs_examples/vector_db_qa_with_sources.ipynb @@ -7,7 +7,7 @@ "source": [ "# VectorDB Question Ansering with Sources\n", "\n", - "This notebook goes over how to do question-answering with sources. It does this in a few different ways - first showing how you can use the `QAWithSourcesChain` to take in documents and use those, and next showing the `VectorDBQAWithSourcesChain`, which also does the lookup of the documents from a vector database. " + "This notebook goes over how to do question-answering with sources over a vector database. It does this by using the `VectorDBQAWithSourcesChain`, which does the lookup of the documents from a vector database. " ] }, { @@ -26,12 +26,12 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "17d1306e", "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "0e745d99", "metadata": {}, "outputs": [], @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "f42d79dc", "metadata": {}, "outputs": [], @@ -61,19 +61,9 @@ " d.metadata = {'source': f\"{i}-pl\"}" ] }, - { - "cell_type": "markdown", - "id": "e6fc81de", - "metadata": {}, - "source": [ - "### VectorDBQAWithSourcesChain\n", - "\n", - "This shows how to use the `VectorDBQAWithSourcesChain`, which uses a vector database to look up relevant documents." - ] - }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 6, "id": "8aa571ae", "metadata": {}, "outputs": [], @@ -83,17 +73,19 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "id": "aa859d4c", "metadata": {}, "outputs": [], "source": [ + "from langchain import OpenAI\n", + "\n", "chain = VectorDBQAWithSourcesChain.from_llm(OpenAI(temperature=0), vectorstore=docsearch)" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "id": "8ba36fa7", "metadata": {}, "outputs": [ @@ -101,10 +93,10 @@ "data": { "text/plain": [ "{'answer': ' The president thanked Justice Breyer for his service.',\n", - " 'sources': '27-pl'}" + " 'sources': '30-pl'}" ] }, - "execution_count": 11, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -112,19 +104,11 @@ "source": [ "chain({\"question\": \"What did the president say about Justice Breyer\"}, return_only_outputs=True)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "980fae3b", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.9.0 64-bit ('llm-env')", "language": "python", "name": "python3" }, @@ -138,7 +122,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.9.0" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/modules/chains/combine_docs_how_to.rst b/docs/modules/chains/combine_docs_how_to.rst new file mode 100644 index 00000000..4cbbfce0 --- /dev/null +++ b/docs/modules/chains/combine_docs_how_to.rst @@ -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 `_: A walkthrough of how to use LangChain for question answering over specific documents. + +`Question Answering with Sources `_: A walkthrough of how to use LangChain for question answering (with sources) over specific documents. + +`Summarization `_: A walkthrough of how to use LangChain for summarization over specific documents. + +`Vector DB Question Answering `_: A walkthrough of how to use LangChain for question answering over a vector database. + +`Vector DB Question Answering with Sources `_: 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/* diff --git a/docs/examples/chains/llm_bash.ipynb b/docs/modules/chains/examples/llm_bash.ipynb similarity index 96% rename from docs/examples/chains/llm_bash.ipynb rename to docs/modules/chains/examples/llm_bash.ipynb index 3bee8df1..b630b707 100644 --- a/docs/examples/chains/llm_bash.ipynb +++ b/docs/modules/chains/examples/llm_bash.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -37,7 +37,7 @@ "'Hello World\\n'" ] }, - "execution_count": 2, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -79,7 +79,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/llm_checker.ipynb b/docs/modules/chains/examples/llm_checker.ipynb similarity index 70% rename from docs/examples/chains/llm_checker.ipynb rename to docs/modules/chains/examples/llm_checker.ipynb index bc1689c3..a6bc0b73 100644 --- a/docs/examples/chains/llm_checker.ipynb +++ b/docs/modules/chains/examples/llm_checker.ipynb @@ -24,16 +24,16 @@ "\n", "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", "\u001b[1mChain 0\u001b[0m:\n", - "{'statement': '\\nThe largest mammal that lays eggs is the platypus.'}\n", + "{'statement': '\\nNone. Mammals do not lay eggs.'}\n", "\n", "\u001b[1mChain 1\u001b[0m:\n", - "{'assertions': '\\n• The largest mammal is the platypus.\\n• The platypus lays eggs.\\n• There is no larger mammal than the platypus that lays eggs.'}\n", + "{'assertions': '\\n• Mammals reproduce using live birth\\n• Mammals do not lay eggs\\n• Animals that lay eggs are not mammals'}\n", "\n", "\u001b[1mChain 2\u001b[0m:\n", - "{'checked_assertions': '\\n1. The largest mammal is the platypus. False. The blue whale is the largest mammal.\\n\\n2. The platypus lays eggs. True. The Platypus is one of only two mammals that lay eggs.\\n\\n3. There is no larger mammal than the platypus that lays eggs. False. The echidna is another mammal that lays eggs and is larger than the platypus.'}\n", + "{'checked_assertions': '\\n1. True\\n\\n2. True\\n\\n3. False - Mammals are a class of animals that includes animals that lay eggs, such as monotremes (platypus and echidna).'}\n", "\n", "\u001b[1mChain 3\u001b[0m:\n", - "{'revised_statement': ' The echidna is the type of mammal that lays the biggest eggs.'}\n", + "{'revised_statement': ' Monotremes, such as the platypus and echidna, lay the biggest eggs of any mammal.'}\n", "\n", "\n", "\u001b[1m> Finished SequentialChain chain.\u001b[0m\n", @@ -44,7 +44,7 @@ { "data": { "text/plain": [ - "' The echidna is the type of mammal that lays the biggest eggs.'" + "' Monotremes, such as the platypus and echidna, lay the biggest eggs of any mammal.'" ] }, "execution_count": 1, @@ -89,7 +89,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/llm_math.ipynb b/docs/modules/chains/examples/llm_math.ipynb similarity index 70% rename from docs/examples/chains/llm_math.ipynb rename to docs/modules/chains/examples/llm_math.ipynb index a7e0bfe1..d906d0e9 100644 --- a/docs/examples/chains/llm_math.ipynb +++ b/docs/modules/chains/examples/llm_math.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "44e9ba31", "metadata": {}, "outputs": [ @@ -22,29 +22,25 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", - "How many of the integers between 0 and 99 inclusive are divisible by 8?\u001b[102m\n", - "\n", + "\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n", + "What is 13 raised to the .3432 power?\u001b[32;1m\u001b[1;3m\n", "```python\n", - "count = 0\n", - "for i in range(100):\n", - " if i % 8 == 0:\n", - " count += 1\n", - "print(count)\n", + "import math\n", + "print(math.pow(13, .3432))\n", "```\n", "\u001b[0m\n", - "Answer: \u001b[103m13\n", + "Answer: \u001b[33;1m\u001b[1;3m2.4116004626599237\n", "\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished LLMMathChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'Answer: 13\\n'" + "'Answer: 2.4116004626599237\\n'" ] }, - "execution_count": 1, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -55,7 +51,7 @@ "llm = OpenAI(temperature=0)\n", "llm_math = LLMMathChain(llm=llm, verbose=True)\n", "\n", - "llm_math.run(\"How many of the integers between 0 and 99 inclusive are divisible by 8?\")" + "llm_math.run(\"What is 13 raised to the .3432 power?\")" ] }, { @@ -83,7 +79,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/llm_requests.ipynb b/docs/modules/chains/examples/llm_requests.ipynb similarity index 93% rename from docs/examples/chains/llm_requests.ipynb rename to docs/modules/chains/examples/llm_requests.ipynb index e4efdcbe..8e26b424 100644 --- a/docs/examples/chains/llm_requests.ipynb +++ b/docs/modules/chains/examples/llm_requests.ipynb @@ -69,7 +69,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "2ea81168", "metadata": {}, "outputs": [ @@ -78,10 +78,10 @@ "text/plain": [ "{'query': 'What are the Three (3) biggest countries, and their respective sizes?',\n", " 'url': 'https://www.google.com/search?q=What+are+the+Three+(3)+biggest+countries,+and+their+respective+sizes?',\n", - " 'output': ' Russia (17,098,242 sq km), Canada (9,984,670 sq km), China (9,706,961 sq km)'}" + " 'output': ' Russia (17,098,242 km²), Canada (9,984,670 km²), United States (9,826,675 km²)'}" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -115,7 +115,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/moderation.ipynb b/docs/modules/chains/examples/moderation.ipynb similarity index 66% rename from docs/examples/chains/moderation.ipynb rename to docs/modules/chains/examples/moderation.ipynb index e0088e80..4a372003 100644 --- a/docs/examples/chains/moderation.ipynb +++ b/docs/modules/chains/examples/moderation.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 1, "id": "b7aa1ff2", "metadata": {}, "outputs": [], @@ -131,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "id": "954f3da2", "metadata": {}, "outputs": [ @@ -142,11 +142,11 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmoderation_chain_error\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mI will kill you\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/workplace/third_party/langchain/langchain/chains/base.py:114\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, text)\u001b[0m\n\u001b[1;32m 109\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_keys) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 110\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 111\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` not supported when there is not exactly \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 112\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mone output key, got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_keys\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 113\u001b[0m )\n\u001b[0;32m--> 114\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minput_keys\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtext\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_keys[\u001b[38;5;241m0\u001b[39m]]\n", - "File \u001b[0;32m~/workplace/third_party/langchain/langchain/chains/base.py:87\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m 83\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mverbose:\n\u001b[1;32m 84\u001b[0m \u001b[38;5;28mprint\u001b[39m(\n\u001b[1;32m 85\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[1m> Entering new \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m chain...\u001b[39m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[0m\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 86\u001b[0m )\n\u001b[0;32m---> 87\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 88\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mverbose:\n\u001b[1;32m 89\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[1m> Finished \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m chain.\u001b[39m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[0m\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "File \u001b[0;32m~/workplace/third_party/langchain/langchain/chains/moderation.py:79\u001b[0m, in \u001b[0;36mOpenAIModerationChain._call\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m 77\u001b[0m text \u001b[38;5;241m=\u001b[39m inputs[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minput_key]\n\u001b[1;32m 78\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclient\u001b[38;5;241m.\u001b[39mcreate(text)\n\u001b[0;32m---> 79\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_moderate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mresults\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mresults\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 80\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m {\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_key: output}\n", - "File \u001b[0;32m~/workplace/third_party/langchain/langchain/chains/moderation.py:71\u001b[0m, in \u001b[0;36mOpenAIModerationChain._moderate\u001b[0;34m(self, text, results)\u001b[0m\n\u001b[1;32m 69\u001b[0m error_str \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mText was found that violates OpenAI\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms content policy.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 70\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39merror:\n\u001b[0;32m---> 71\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(error_str)\n\u001b[1;32m 72\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m error_str\n", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmoderation_chain_error\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mI will kill you\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/workplace/langchain/langchain/chains/base.py:138\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 136\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(args) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 137\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` supports only one positional argument.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 138\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_keys[\u001b[38;5;241m0\u001b[39m]]\n\u001b[1;32m 140\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[1;32m 141\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(kwargs)[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_keys[\u001b[38;5;241m0\u001b[39m]]\n", + "File \u001b[0;32m~/workplace/langchain/langchain/chains/base.py:112\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mverbose:\n\u001b[1;32m 109\u001b[0m \u001b[38;5;28mprint\u001b[39m(\n\u001b[1;32m 110\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[1m> Entering new \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m chain...\u001b[39m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[0m\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 111\u001b[0m )\n\u001b[0;32m--> 112\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 113\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mverbose:\n\u001b[1;32m 114\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[1m> Finished \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m chain.\u001b[39m\u001b[38;5;130;01m\\033\u001b[39;00m\u001b[38;5;124m[0m\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m~/workplace/langchain/langchain/chains/moderation.py:81\u001b[0m, in \u001b[0;36mOpenAIModerationChain._call\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m 79\u001b[0m text \u001b[38;5;241m=\u001b[39m inputs[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minput_key]\n\u001b[1;32m 80\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclient\u001b[38;5;241m.\u001b[39mcreate(text)\n\u001b[0;32m---> 81\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_moderate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mresults\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mresults\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 82\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m {\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_key: output}\n", + "File \u001b[0;32m~/workplace/langchain/langchain/chains/moderation.py:73\u001b[0m, in \u001b[0;36mOpenAIModerationChain._moderate\u001b[0;34m(self, text, results)\u001b[0m\n\u001b[1;32m 71\u001b[0m error_str \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mText was found that violates OpenAI\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms content policy.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 72\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39merror:\n\u001b[0;32m---> 73\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(error_str)\n\u001b[1;32m 74\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 75\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m error_str\n", "\u001b[0;31mValueError\u001b[0m: Text was found that violates OpenAI's content policy." ] } @@ -165,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "id": "3960e985", "metadata": {}, "outputs": [], @@ -183,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "id": "1152ec11", "metadata": {}, "outputs": [ @@ -193,7 +193,7 @@ "'This is okay'" ] }, - "execution_count": 11, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -204,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "id": "973257bf", "metadata": {}, "outputs": [ @@ -214,7 +214,7 @@ "\"The following text was found that violates OpenAI's content policy: I will kill you\"" ] }, - "execution_count": 12, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -237,7 +237,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 11, "id": "0d129333", "metadata": {}, "outputs": [], @@ -248,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 12, "id": "a557c531", "metadata": {}, "outputs": [ @@ -258,7 +258,7 @@ "' I will kill you'" ] }, - "execution_count": 18, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -279,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 13, "id": "d4d10f1c", "metadata": {}, "outputs": [], @@ -289,7 +289,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 14, "id": "02f37985", "metadata": {}, "outputs": [ @@ -299,7 +299,7 @@ "\"Text was found that violates OpenAI's content policy.\"" ] }, - "execution_count": 20, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -318,7 +318,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 15, "id": "7118ec36", "metadata": {}, "outputs": [], @@ -329,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 16, "id": "003bdfce", "metadata": {}, "outputs": [ @@ -339,7 +339,7 @@ "{'text': ' I will kill you'}" ] }, - "execution_count": 26, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -361,7 +361,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 17, "id": "77b64228", "metadata": {}, "outputs": [], @@ -373,7 +373,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 18, "id": "998a95be", "metadata": {}, "outputs": [], @@ -383,7 +383,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 19, "id": "9c97a136", "metadata": {}, "outputs": [ @@ -393,7 +393,7 @@ "{'sanitized_text': \"Text was found that violates OpenAI's content policy.\"}" ] }, - "execution_count": 33, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -427,7 +427,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/pal.ipynb b/docs/modules/chains/examples/pal.ipynb similarity index 94% rename from docs/examples/chains/pal.ipynb rename to docs/modules/chains/examples/pal.ipynb index 7be54735..3e0de46b 100644 --- a/docs/examples/chains/pal.ipynb +++ b/docs/modules/chains/examples/pal.ipynb @@ -54,7 +54,7 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new PALChain chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mdef solution():\n", " \"\"\"Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?\"\"\"\n", " cindy_pets = 4\n", @@ -64,7 +64,7 @@ " result = total_pets\n", " return result\u001b[0m\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished PALChain chain.\u001b[0m\n" ] }, { @@ -115,7 +115,7 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new PALChain chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m# Put objects into a list to record ordering\n", "objects = []\n", "objects += [('booklet', 'blue')] * 2\n", @@ -129,7 +129,7 @@ "num_purple = len([object for object in objects if object[1] == 'purple'])\n", "answer = num_purple\u001b[0m\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished PALChain chain.\u001b[0m\n" ] }, { @@ -172,7 +172,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/sqlite.ipynb b/docs/modules/chains/examples/sqlite.ipynb similarity index 80% rename from docs/examples/chains/sqlite.ipynb rename to docs/modules/chains/examples/sqlite.ipynb index 4f12800a..4a2d266d 100644 --- a/docs/examples/chains/sqlite.ipynb +++ b/docs/modules/chains/examples/sqlite.ipynb @@ -52,7 +52,7 @@ }, "outputs": [], "source": [ - "db = SQLDatabase.from_uri(\"sqlite:///../../../notebooks/Chinook.db\")\n", + "db = SQLDatabase.from_uri(\"sqlite:///../../../../notebooks/Chinook.db\")\n", "llm = OpenAI(temperature=0)\n", "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)" ] @@ -73,18 +73,18 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", - "How many employees are there?\n", - "SQLQuery:\u001b[102m SELECT COUNT(*) FROM Employee\u001b[0m\n", - "SQLResult: \u001b[103m[(8,)]\u001b[0m\n", - "Answer:\u001b[102m 8\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n", + "How many employees are there? \n", + "SQLQuery:\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Employee;\u001b[0m\n", + "SQLResult: \u001b[33;1m\u001b[1;3m[(9,)]\u001b[0m\n", + "Answer:\u001b[32;1m\u001b[1;3m There are 9 employees.\u001b[0m\n", + "\u001b[1m> Finished SQLDatabaseChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "' 8'" + "' There are 9 employees.'" ] }, "execution_count": 3, @@ -121,7 +121,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/llm_chain.ipynb b/docs/modules/chains/generic/llm_chain.ipynb similarity index 96% rename from docs/examples/chains/llm_chain.ipynb rename to docs/modules/chains/generic/llm_chain.ipynb index 591111a5..97dffa4a 100644 --- a/docs/examples/chains/llm_chain.ipynb +++ b/docs/modules/chains/generic/llm_chain.ipynb @@ -25,14 +25,14 @@ "id": "06bcb078", "metadata": {}, "source": [ - "### Single Input\n", + "## Single Input\n", "\n", "First, lets go over an example using a single input" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "51a54c4d", "metadata": {}, "outputs": [ @@ -57,7 +57,7 @@ "' Justin Bieber was born in 1994, so the NFL team that won the Super Bowl in 1994 was the Dallas Cowboys.'" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -79,13 +79,13 @@ "id": "79c3ec4d", "metadata": {}, "source": [ - "### Multiple Inputs\n", + "## Multiple Inputs\n", "Now lets go over an example using multiple inputs." ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "03dd6918", "metadata": {}, "outputs": [ @@ -108,7 +108,7 @@ "\"\\n\\nThe ducks swim in the pond,\\nTheir feathers so soft and warm,\\nBut they can't help but feel so forlorn.\\n\\nTheir quacks echo in the air,\\nBut no one is there to hear,\\nFor they have no one to share.\\n\\nThe ducks paddle around in circles,\\nTheir heads hung low in despair,\\nFor they have no one to care.\\n\\nThe ducks look up to the sky,\\nBut no one is there to see,\\nFor they have no one to be.\\n\\nThe ducks drift away in the night,\\nTheir hearts filled with sorrow and pain,\\nFor they have no one to gain.\"" ] }, - "execution_count": 5, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -146,7 +146,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/getting_started/sequential_chains.ipynb b/docs/modules/chains/generic/sequential_chains.ipynb similarity index 57% rename from docs/getting_started/sequential_chains.ipynb rename to docs/modules/chains/generic/sequential_chains.ipynb index 2c907f84..4d3d4e0e 100644 --- a/docs/getting_started/sequential_chains.ipynb +++ b/docs/modules/chains/generic/sequential_chains.ipynb @@ -104,15 +104,25 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", "\u001b[36;1m\u001b[1;3m\n", "\n", - "A young couple, John and Mary, are enjoying a day at the beach. As the sun sets, they share a romantic moment. However, their happiness is short-lived, as a tragic accident claims John's life. Mary is left devastated by the loss of her husband.\u001b[0m\n", + "Tragedy at Sunset on the Beach follows the story of a young couple, Jack and Annie, who have just started to explore the possibility of a relationship together. After a day spent in the sun and sand, they decide to take a romantic stroll down the beach as the sun sets. \n", + "\n", + "However, their romantic evening quickly turns tragic when they stumble upon a body lying in the sand. As they approach to investigate, they are shocked to discover that it is Jack's long-lost brother, who has been missing for several years. \n", + "\n", + "The story follows Jack and Annie as they navigate their way through the tragedy and their newfound relationship. With the help of their friends, family, and the beach's inhabitants, Jack and Annie must come to terms with their deep-seated emotions and the reality of the situation. \n", + "\n", + "Ultimately, the play explores themes of family, love, and loss, as Jack and Annie's story unfolds against the beautiful backdrop of the beach at sunset.\u001b[0m\n", "\u001b[33;1m\u001b[1;3m\n", "\n", - "\"A young couple's happiness is cut short by tragedy in this moving play. Mary is left devastated by the loss of her husband, John, in a freak accident. The play captures the pain and grief of loss, as well as the strength of love. A must-see for fans of theater.\"\u001b[0m\n", + "Tragedy at Sunset on the Beach is an emotionally complex tale of family, love, and loss. Told against the beautiful backdrop of a beach at sunset, the story follows Jack and Annie, a young couple just beginning to explore a relationship together. When they stumble upon the body of Jack's long-lost brother on the beach, they must face the reality of the tragedy and come to terms with their deep-seated emotions. \n", + "\n", + "The playwright has crafted a heartfelt and thought-provoking story, one that probes into the depths of the human experience. The cast of characters is well-rounded and fully realized, and the dialogue is natural and emotional. The direction and choreography are top-notch, and the scenic design is breathtaking. \n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "Overall, Tragedy at Sunset on the Beach is a powerful and moving story about the fragility of life and the strength of love. It is sure to tug at your heartstrings and leave you with a newfound appreciation of life's precious moments. Highly recommended.\u001b[0m\n", + "\n", + "\u001b[1m> Finished SimpleSequentialChain chain.\u001b[0m\n" ] } ], @@ -132,7 +142,11 @@ "text": [ "\n", "\n", - "\"A young couple's happiness is cut short by tragedy in this moving play. Mary is left devastated by the loss of her husband, John, in a freak accident. The play captures the pain and grief of loss, as well as the strength of love. A must-see for fans of theater.\"\n" + "Tragedy at Sunset on the Beach is an emotionally complex tale of family, love, and loss. Told against the beautiful backdrop of a beach at sunset, the story follows Jack and Annie, a young couple just beginning to explore a relationship together. When they stumble upon the body of Jack's long-lost brother on the beach, they must face the reality of the tragedy and come to terms with their deep-seated emotions. \n", + "\n", + "The playwright has crafted a heartfelt and thought-provoking story, one that probes into the depths of the human experience. The cast of characters is well-rounded and fully realized, and the dialogue is natural and emotional. The direction and choreography are top-notch, and the scenic design is breathtaking. \n", + "\n", + "Overall, Tragedy at Sunset on the Beach is a powerful and moving story about the fragility of life and the strength of love. It is sure to tug at your heartstrings and leave you with a newfound appreciation of life's precious moments. Highly recommended.\n" ] } ], @@ -216,15 +230,15 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", "\u001b[1mChain 0\u001b[0m:\n", - "{'synopsis': \"\\n\\nThe play is set in Victorian England and follows the tragic story of a young woman who drowns while swimming at sunset on the beach. Her body is found the next morning by a fisherman who raises the alarm. The young woman's family and friends are devastated by her death and the play ends with their mourning her loss.\"}\n", + "{'synopsis': \" \\n\\nTragedy at Sunset on the Beach is a dark and gripping drama set in Victorian England. The play follows the story of two lovers, Emma and Edward, whose passionate relationship is threatened by the strict rules and regulations of the time.\\n\\nThe two are deeply in love, but Edward is from a wealthy family and Emma is from a lower class background. Despite the obstacles, the two are determined to be together and decide to elope.\\n\\nOn the night of their planned escape, Emma and Edward meet at the beach at sunset to declare their love for one another and begin a new life together. However, their plans are disrupted when Emma's father discovers their plan and appears on the beach with a gun.\\n\\nIn a heartbreaking scene, Emma's father orders Edward to leave, but Edward refuses and fights for their love. In a fit of rage, Emma's father shoots Edward, killing him instantly. \\n\\nThe tragedy of the play lies in the fact that Emma and Edward are denied their chance at a happy ending due to the rigid social conventions of Victorian England. The audience is left with a heavy heart as the play ends with Emma standing alone on the beach, mourning the loss of her beloved.\"}\n", "\n", "\u001b[1mChain 1\u001b[0m:\n", - "{'review': '\\n\\n\"The play is a tragedy, pure and simple. It is the story of a young woman\\'s death, told through the eyes of those who loved her. It is a sad, beautiful play that will stay with you long after you\\'ve seen it. The acting is superb, and the writing is exquisite. If you are looking for a play that will touch your heart and make you think, this is it.\"'}\n", + "{'review': \"\\n\\nTragedy at Sunset on the Beach is an emotionally charged production that will leave audiences heartsick. The play follows the ill-fated love story of Emma and Edward, two star-crossed lovers whose passionate relationship is tragically thwarted by Victorian England's societal conventions. The performance is captivating from start to finish, as the audience is taken on an emotional rollercoaster of love, loss, and heartbreak.\\n\\nThe acting is powerful and sincere, and the performances of the two leads are particularly stirring. Emma and Edward are both portrayed with such tenderness and emotion that it's hard not to feel their pain as they fight for their forbidden love. The climactic scene, in which Edward is shot by Emma's father, is especially heartbreaking and will leave audience members on the edge of their seats.\\n\\nOverall, Tragedy at Sunset on the Beach is a powerful and moving work of theatre. It is a tragedy of impossible love, and a vivid reminder of the devastating consequences of social injustice. The play is sure to leave a lasting impression on anyone who experiences it.\"}\n", "\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished SequentialChain chain.\u001b[0m\n" ] } ], @@ -257,7 +271,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/transformation.ipynb b/docs/modules/chains/generic/transformation.ipynb similarity index 87% rename from docs/examples/chains/transformation.ipynb rename to docs/modules/chains/generic/transformation.ipynb index 5358e187..b770f06c 100644 --- a/docs/examples/chains/transformation.ipynb +++ b/docs/modules/chains/generic/transformation.ipynb @@ -14,7 +14,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "id": "bbbb4330", "metadata": {}, "outputs": [], @@ -26,18 +26,18 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 3, "id": "8ae5937c", "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "id": "98739592", "metadata": {}, "outputs": [], @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "e9397934", "metadata": {}, "outputs": [], @@ -78,17 +78,17 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "id": "f7caa1ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "' This speech addresses the American people and acknowledges the difficulties of last year due to COVID-19. It emphasizes the importance of coming together regardless of political affiliation and encourages a sense of unity as Americans.'" + "' The speaker addresses the nation, noting that while last year they were kept apart due to COVID-19, this year they are together again. They are reminded that regardless of their political affiliations, they are all Americans.'" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -122,7 +122,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/modules/chains/generic_how_to.rst b/docs/modules/chains/generic_how_to.rst new file mode 100644 index 00000000..8848c092 --- /dev/null +++ b/docs/modules/chains/generic_how_to.rst @@ -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 `_ + +**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 `_ + +**Sequential Chain** + +- **Links Used**: Sequential +- **Notes**: This notebook shows how to combine calling multiple other chains in sequence. +- `Example Notebook `_ + +.. toctree:: + :maxdepth: 1 + :glob: + :caption: Generic Chains + :name: generic + :hidden: + + generic/* \ No newline at end of file diff --git a/docs/modules/chains/getting_started.ipynb b/docs/modules/chains/getting_started.ipynb new file mode 100644 index 00000000..f3570abf --- /dev/null +++ b/docs/modules/chains/getting_started.ipynb @@ -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 +} diff --git a/docs/modules/chains/how_to_guides.rst b/docs/modules/chains/how_to_guides.rst new file mode 100644 index 00000000..8bdb82d6 --- /dev/null +++ b/docs/modules/chains/how_to_guides.rst @@ -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 chains, that are meant to help build other chains rather than serve a particular purpose. +2. `CombineDocuments Chains `_: Chains aimed at making it easy to work with documents (question answering, summarization, etc). +3. `Utility Chains `_: 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 diff --git a/docs/modules/chains/key_concepts.md b/docs/modules/chains/key_concepts.md new file mode 100644 index 00000000..7c83c13d --- /dev/null +++ b/docs/modules/chains/key_concepts.md @@ -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). + diff --git a/docs/modules/chains/utility_how_to.rst b/docs/modules/chains/utility_how_to.rst new file mode 100644 index 00000000..100c3d03 --- /dev/null +++ b/docs/modules/chains/utility_how_to.rst @@ -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 `_ + +**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 `_ +- `Example Notebook `_ + +**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 `_ + +**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 `_ + +**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 `_ + +**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 `_ + +**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 `_ + + +.. toctree:: + :maxdepth: 1 + :glob: + :caption: Generic Chains + :name: generic + :hidden: + + examples/* \ No newline at end of file diff --git a/docs/modules/llms.rst b/docs/modules/llms.rst new file mode 100644 index 00000000..37f3a0c7 --- /dev/null +++ b/docs/modules/llms.rst @@ -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 `_: An overview of all the functionality the LangChain LLM class provides. + +- `Key Concepts `_: A conceptual guide going over the various concepts related to LLMs. + +- `How-To Guides `_: 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 `_: 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 \ No newline at end of file diff --git a/docs/examples/prompts/custom_llm.ipynb b/docs/modules/llms/examples/custom_llm.ipynb similarity index 95% rename from docs/examples/prompts/custom_llm.ipynb rename to docs/modules/llms/examples/custom_llm.ipynb index bb3938aa..35027925 100644 --- a/docs/examples/prompts/custom_llm.ipynb +++ b/docs/modules/llms/examples/custom_llm.ipynb @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 2, "id": "d5ceff02", "metadata": {}, "outputs": [], @@ -67,7 +67,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 3, "id": "10e5ece6", "metadata": {}, "outputs": [], @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 4, "id": "8cd49199", "metadata": {}, "outputs": [ @@ -87,7 +87,7 @@ "'This is a '" ] }, - "execution_count": 9, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -106,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 5, "id": "9c33fa19", "metadata": {}, "outputs": [ @@ -148,7 +148,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/prompts/llm.json b/docs/modules/llms/examples/llm.json similarity index 87% rename from docs/examples/prompts/llm.json rename to docs/modules/llms/examples/llm.json index df2b7e1e..b3761734 100644 --- a/docs/examples/prompts/llm.json +++ b/docs/modules/llms/examples/llm.json @@ -7,5 +7,6 @@ "presence_penalty": 0.0, "n": 1, "best_of": 1, + "request_timeout": null, "_type": "openai" } \ No newline at end of file diff --git a/docs/examples/prompts/llm.yaml b/docs/modules/llms/examples/llm.yaml similarity index 87% rename from docs/examples/prompts/llm.yaml rename to docs/modules/llms/examples/llm.yaml index ee384ffa..77e07e72 100644 --- a/docs/examples/prompts/llm.yaml +++ b/docs/modules/llms/examples/llm.yaml @@ -5,5 +5,6 @@ max_tokens: 256 model_name: text-davinci-003 n: 1 presence_penalty: 0.0 +request_timeout: null temperature: 0.7 top_p: 1.0 diff --git a/docs/examples/prompts/llm_caching.ipynb b/docs/modules/llms/examples/llm_caching.ipynb similarity index 74% rename from docs/examples/prompts/llm_caching.ipynb rename to docs/modules/llms/examples/llm_caching.ipynb index 50a0e6bd..3538dd5a 100644 --- a/docs/examples/prompts/llm_caching.ipynb +++ b/docs/modules/llms/examples/llm_caching.ipynb @@ -24,7 +24,7 @@ "id": "b50f0598", "metadata": {}, "source": [ - "### In Memory Cache" + "## In Memory Cache" ] }, { @@ -60,14 +60,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 30.6 ms, sys: 9.95 ms, total: 40.5 ms\n", - "Wall time: 730 ms\n" + "CPU times: user 30.7 ms, sys: 18.6 ms, total: 49.3 ms\n", + "Wall time: 791 ms\n" ] }, { "data": { "text/plain": [ - "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'" + "\"\\n\\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!\"" ] }, "execution_count": 4, @@ -91,14 +91,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 71 µs, sys: 3 µs, total: 74 µs\n", - "Wall time: 78.9 µs\n" + "CPU times: user 80 µs, sys: 0 ns, total: 80 µs\n", + "Wall time: 83.9 µs\n" ] }, { "data": { "text/plain": [ - "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'" + "\"\\n\\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!\"" ] }, "execution_count": 5, @@ -117,12 +117,22 @@ "id": "4bf59c12", "metadata": {}, "source": [ - "### SQLite Cache" + "## SQLite Cache" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, + "id": "3ff65b00", + "metadata": {}, + "outputs": [], + "source": [ + "!rm .langchain.db" + ] + }, + { + "cell_type": "code", + "execution_count": 10, "id": "5f036236", "metadata": {}, "outputs": [], @@ -134,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "id": "fa18e3af", "metadata": {}, "outputs": [ @@ -142,8 +152,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 5.27 ms, sys: 2.36 ms, total: 7.63 ms\n", - "Wall time: 6.68 ms\n" + "CPU times: user 17 ms, sys: 9.76 ms, total: 26.7 ms\n", + "Wall time: 825 ms\n" ] }, { @@ -152,7 +162,7 @@ "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side.'" ] }, - "execution_count": 7, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -165,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 12, "id": "5bf2f6fd", "metadata": { "scrolled": true @@ -175,8 +185,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 3.05 ms, sys: 1.1 ms, total: 4.16 ms\n", - "Wall time: 5.58 ms\n" + "CPU times: user 2.46 ms, sys: 1.23 ms, total: 3.7 ms\n", + "Wall time: 2.67 ms\n" ] }, { @@ -185,7 +195,7 @@ "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side.'" ] }, - "execution_count": 8, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -201,12 +211,12 @@ "id": "278ad7ae", "metadata": {}, "source": [ - "### Redis Cache" + "## Redis Cache" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "39f6eb0b", "metadata": {}, "outputs": [], @@ -220,29 +230,10 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "28920749", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 6.75 ms, sys: 3.14 ms, total: 9.89 ms\n", - "Wall time: 716 ms\n" - ] - }, - { - "data": { - "text/plain": [ - "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "%%time\n", "# The first time, it is not yet in cache, so it should take longer\n", @@ -251,29 +242,10 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "94bf9415", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 1.66 ms, sys: 1.92 ms, total: 3.57 ms\n", - "Wall time: 7.56 ms\n" - ] - }, - { - "data": { - "text/plain": [ - "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "%%time\n", "# The second time it is, so it goes faster\n", @@ -285,12 +257,12 @@ "id": "934943dc", "metadata": {}, "source": [ - "### SQLAlchemy Cache" + "## SQLAlchemy Cache" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "acccff40", "metadata": {}, "outputs": [], @@ -309,13 +281,13 @@ "id": "0c69d84d", "metadata": {}, "source": [ - "### Optional Caching\n", + "## Optional Caching\n", "You can also turn off caching for specific LLMs should you choose. In the example below, even though global caching is enabled, we turn it off for a specific LLM" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "id": "6af46e2b", "metadata": {}, "outputs": [], @@ -325,7 +297,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "id": "26c4fd8f", "metadata": {}, "outputs": [ @@ -333,8 +305,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 5.59 ms, sys: 2.35 ms, total: 7.95 ms\n", - "Wall time: 1.46 s\n" + "CPU times: user 5.8 ms, sys: 2.71 ms, total: 8.51 ms\n", + "Wall time: 745 ms\n" ] }, { @@ -343,7 +315,7 @@ "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'" ] }, - "execution_count": 11, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -355,7 +327,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 15, "id": "46846b20", "metadata": {}, "outputs": [ @@ -363,17 +335,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 5.76 ms, sys: 3.15 ms, total: 8.9 ms\n", - "Wall time: 660 ms\n" + "CPU times: user 4.91 ms, sys: 2.64 ms, total: 7.55 ms\n", + "Wall time: 623 ms\n" ] }, { "data": { "text/plain": [ - "\"\\n\\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!\"" + "'\\n\\nTwo guys stole a calendar. They got six months each.'" ] }, - "execution_count": 12, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -388,7 +360,7 @@ "id": "5da41b77", "metadata": {}, "source": [ - "### Optional Caching in Chains\n", + "## Optional Caching in Chains\n", "You can also turn off caching for particular nodes in chains. Note that because of certain interfaces, its often easier to construct the chain first, and then edit the LLM afterwards.\n", "\n", "As an example, we will load a summarizer map-reduce chain. We will cache results for the map-step, but then not freeze it for the combine step." @@ -396,7 +368,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "id": "9afa3f7a", "metadata": {}, "outputs": [], @@ -407,7 +379,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 17, "id": "98a78e8e", "metadata": {}, "outputs": [], @@ -420,19 +392,19 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 18, "id": "2bfb099b", "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "texts = text_splitter.split_text(state_of_the_union)" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 19, "id": "f78b7f51", "metadata": {}, "outputs": [], @@ -444,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 20, "id": "a2a30822", "metadata": {}, "outputs": [], @@ -454,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 21, "id": "a545b743", "metadata": {}, "outputs": [ @@ -462,17 +434,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 471 ms, sys: 130 ms, total: 601 ms\n", - "Wall time: 5.8 s\n" + "CPU times: user 452 ms, sys: 60.3 ms, total: 512 ms\n", + "Wall time: 5.09 s\n" ] }, { "data": { "text/plain": [ - "\"\\n\\nIn response to Vladimir Putin's aggression in Ukraine, the United States has joined with European allies to impose economic sanctions and cut off Russia's access to technology. The Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs.\\n\\nThe sanctions and task force are aimed at punishing Putin and Russian oligarchs for their aggression in Ukraine and deterring future aggression. The long-term goal is to make Russia pay a high price for its aggression, so that it will be less likely to engage in similar behavior in the future.\"" + "'\\n\\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure. In response to Russian aggression in Ukraine, the United States is joining with European allies to impose sanctions and isolate Russia. American forces are being mobilized to protect NATO countries in the event that Putin decides to keep moving west. The Ukrainians are bravely fighting back, but the next few weeks will be hard for them. Putin will pay a high price for his actions in the long run. Americans should not be alarmed, as the United States is taking action to protect its interests and allies.'" ] }, - "execution_count": 22, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -492,7 +464,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 22, "id": "39cbb282", "metadata": {}, "outputs": [ @@ -500,17 +472,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 10.6 ms, sys: 4.25 ms, total: 14.8 ms\n", - "Wall time: 2.19 s\n" + "CPU times: user 11.5 ms, sys: 4.33 ms, total: 15.8 ms\n", + "Wall time: 1.04 s\n" ] }, { "data": { "text/plain": [ - "\"\\n\\nIn response to Vladimir Putin's aggression in Ukraine, the United States has joined with European allies to impose economic sanctions and cut off Russia's access to technology. The Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs. The goal is to put pressure on Putin and make him pay a high price for his aggression. These initiatives will also help improve infrastructure and provide clean water and high-speed internet access for all Americans.\"" + "'\\n\\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure.'" ] }, - "execution_count": 23, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -545,7 +517,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/prompts/llm_serialization.ipynb b/docs/modules/llms/examples/llm_serialization.ipynb similarity index 89% rename from docs/examples/prompts/llm_serialization.ipynb rename to docs/modules/llms/examples/llm_serialization.ipynb index 99556bfa..660cf9e7 100644 --- a/docs/examples/prompts/llm_serialization.ipynb +++ b/docs/modules/llms/examples/llm_serialization.ipynb @@ -26,7 +26,7 @@ "id": "88ce018b", "metadata": {}, "source": [ - "### Loading\n", + "## Loading\n", "First, lets go over loading a LLM from disk. LLMs can be saved on disk in two formats: json or yaml. No matter the extension, they are loaded in the same way." ] }, @@ -44,11 +44,12 @@ " \"model_name\": \"text-davinci-003\",\r\n", " \"temperature\": 0.7,\r\n", " \"max_tokens\": 256,\r\n", - " \"top_p\": 1,\r\n", - " \"frequency_penalty\": 0,\r\n", - " \"presence_penalty\": 0,\r\n", + " \"top_p\": 1.0,\r\n", + " \"frequency_penalty\": 0.0,\r\n", + " \"presence_penalty\": 0.0,\r\n", " \"n\": 1,\r\n", " \"best_of\": 1,\r\n", + " \"request_timeout\": null,\r\n", " \"_type\": \"openai\"\r\n", "}" ] @@ -80,13 +81,14 @@ "text": [ "_type: openai\r\n", "best_of: 1\r\n", - "frequency_penalty: 0\r\n", + "frequency_penalty: 0.0\r\n", "max_tokens: 256\r\n", "model_name: text-davinci-003\r\n", "n: 1\r\n", - "presence_penalty: 0\r\n", + "presence_penalty: 0.0\r\n", + "request_timeout: null\r\n", "temperature: 0.7\r\n", - "top_p: 1\r\n" + "top_p: 1.0\r\n" ] } ], @@ -109,7 +111,7 @@ "id": "ab3e4223", "metadata": {}, "source": [ - "### Saving\n", + "## Saving\n", "If you want to go from a LLM in memory to a serialized version of it, you can do so easily by calling the `.save` method. Again, this supports both json and yaml." ] }, @@ -136,7 +138,7 @@ { "cell_type": "code", "execution_count": null, - "id": "0e494851", + "id": "68e45b1c", "metadata": {}, "outputs": [], "source": [] @@ -158,7 +160,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/modules/llms/generic_how_to.rst b/docs/modules/llms/generic_how_to.rst new file mode 100644 index 00000000..03007fc9 --- /dev/null +++ b/docs/modules/llms/generic_how_to.rst @@ -0,0 +1,20 @@ +Generic Functionality +===================== + +The examples here all address certain "how-to" guides for working with LLMs. + +`LLM Serialization `_: A walkthrough of how to serialize LLMs to and from disk. + +`LLM Caching `_: Covers different types of caches, and how to use a cache to save results of LLM calls. + +`Custom LLM `_: 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/* diff --git a/docs/examples/prompts/llm_functionality.ipynb b/docs/modules/llms/getting_started.ipynb similarity index 68% rename from docs/examples/prompts/llm_functionality.ipynb rename to docs/modules/llms/getting_started.ipynb index 435344eb..043fb3ec 100644 --- a/docs/examples/prompts/llm_functionality.ipynb +++ b/docs/modules/llms/getting_started.ipynb @@ -5,11 +5,13 @@ "id": "20ac6b98", "metadata": {}, "source": [ - "# LLM Functionality\n", + "# Getting Started\n", "\n", - "This notebook goes over all the different features of the LLM class in LangChain.\n", + "This notebook goes over how to use the LLM class in LangChain.\n", "\n", - "We will work with an OpenAI LLM wrapper, although these functionalities should exist for all LLM types." + "The LLM class is a class designed for interfacing with LLMs. There are lots of LLM providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them. In this part of the documentation, we will focus on generic LLM functionality. For details on working with a specific LLM wrapper, please see the examples in the [How-To section](how_to_guides.rst).\n", + "\n", + "For this notebook, we will work with an OpenAI LLM wrapper, although the functionalities highlighted are generic for all LLM types." ] }, { @@ -49,7 +51,7 @@ { "data": { "text/plain": [ - "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'" + "'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side.'" ] }, "execution_count": 3, @@ -109,8 +111,8 @@ { "data": { "text/plain": [ - "[Generation(text='\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side.'),\n", - " Generation(text='\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!')]" + "[Generation(text='\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'),\n", + " Generation(text='\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side.')]" ] }, "execution_count": 6, @@ -131,8 +133,8 @@ { "data": { "text/plain": [ - "[Generation(text=\"\\n\\nA rose by the side of the road\\n\\nIs all I need to find my way\\n\\nTo the place I've been searching for\\n\\nAnd my heart is singing with joy\\n\\nWhen I look at this rose\\n\\nIt reminds me of the love I've found\\n\\nAnd I know that wherever I go\\n\\nI'll always find my rose by the side of the road.\"),\n", - " Generation(text=\"\\n\\nWhen I was younger\\nI thought that love\\nI was something like a fairytale\\nI would find my prince and they would be my people\\nI was naïve\\nI thought that\\n\\nLove was a something that happened\\nWhen I was younger\\nI was it for my fairytale prince\\nNow I realize\\nThat love is something that waits\\nFor when my prince comes\\nAnd when I am ready to be his wife\\nI'll tell you a poem\\n\\nWhen I was younger\\nI thought that love\\nI was something like a fairytale\\nI would find my prince and they would be my people\\nI was naïve\\nI thought that\\n\\nLove was a something that happened\\nAnd I would be happy\\nWhen my prince came\\nAnd I was ready to be his wife\")]" + "[Generation(text=\"\\n\\nWhat if love neverspeech\\n\\nWhat if love never ended\\n\\nWhat if love was only a feeling\\n\\nI'll never know this love\\n\\nIt's not a feeling\\n\\nBut it's what we have for each other\\n\\nWe just know that love is something strong\\n\\nAnd we can't help but be happy\\n\\nWe just feel what love is for us\\n\\nAnd we love each other with all our heart\\n\\nWe just don't know how\\n\\nHow it will go\\n\\nBut we know that love is something strong\\n\\nAnd we'll always have each other\\n\\nIn our lives.\"),\n", + " Generation(text='\\n\\nOnce upon a time\\n\\nThere was a love so pure and true\\n\\nIt lasted for centuries\\n\\nAnd never became stale or dry\\n\\nIt was moving and alive\\n\\nAnd the heart of the love-ick\\n\\nIs still beating strong and true.')]" ] }, "execution_count": 7, @@ -144,6 +146,14 @@ "llm_result.generations[-1]" ] }, + { + "cell_type": "markdown", + "id": "9efae834", + "metadata": {}, + "source": [ + "You can also access provider specific information that is returned. This information is NOT standardized across providers." + ] + }, { "cell_type": "code", "execution_count": 8, @@ -153,9 +163,9 @@ { "data": { "text/plain": [ - "{'token_usage': {'completion_tokens': 3722,\n", - " 'prompt_tokens': 120,\n", - " 'total_tokens': 3842}}" + "{'token_usage': {'completion_tokens': 3903,\n", + " 'total_tokens': 4023,\n", + " 'prompt_tokens': 120}}" ] }, "execution_count": 8, @@ -164,7 +174,6 @@ } ], "source": [ - "# Provider specific info\n", "llm_result.llm_output" ] }, @@ -198,6 +207,14 @@ "source": [ "llm.get_num_tokens(\"what a joke\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b004ffdd", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -216,7 +233,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" }, "vscode": { "interpreter": { diff --git a/docs/modules/llms/how_to_guides.rst b/docs/modules/llms/how_to_guides.rst new file mode 100644 index 00000000..31eb8fb9 --- /dev/null +++ b/docs/modules/llms/how_to_guides.rst @@ -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 `_: Covering generic functionality all LLMs should have. +2. `Integrations `_: Covering integrations with various LLM providers. + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + generic_how_to.rst + integrations.rst diff --git a/docs/modules/llms/integrations.rst b/docs/modules/llms/integrations.rst new file mode 100644 index 00000000..467b063d --- /dev/null +++ b/docs/modules/llms/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 `_: Covers how to connect to LLMs hosted on HuggingFace Hub. + +`Azure OpenAI `_: Covers how to connect to Azure-hosted OpenAI Models. + +`Manifest `_: Covers how to utilize the Manifest wrapper. + + +.. toctree:: + :maxdepth: 1 + :glob: + :caption: Specific LLM Integrations + :name: Specific LLM Integrations + :hidden: + + integrations/* diff --git a/docs/examples/prompts/azure_openai_example.ipynb b/docs/modules/llms/integrations/azure_openai_example.ipynb similarity index 98% rename from docs/examples/prompts/azure_openai_example.ipynb rename to docs/modules/llms/integrations/azure_openai_example.ipynb index 31c483b5..87034543 100644 --- a/docs/examples/prompts/azure_openai_example.ipynb +++ b/docs/modules/llms/integrations/azure_openai_example.ipynb @@ -11,7 +11,7 @@ "\n", "The Azure OpenAI API is compatible with OpenAI's API. The `openai` Python package makes it easy to use both OpenAI and Azure OpenAI. You can call Azure OpenAI the same way you call OpenAI with the exceptions noted below.\n", "\n", - "### API configuration\n", + "## API configuration\n", "You can configure the `openai` package to use Azure OpenAI using environment variables. The following is for `bash`:\n", "\n", "```bash\n", @@ -33,7 +33,7 @@ "...\n", "```\n", "\n", - "### Deployments\n", + "## Deployments\n", "With Azure OpenAI, you set up your own deployments of the common GPT-3 and Codex models. When calling the API, you need to specify the deployment you want to use.\n", "\n", "Let's say your deployment name is `text-davinci-002-prod`. In the `openai` Python API, you can specify this deployment with the `engine` parameter. For example:\n", @@ -146,7 +146,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" }, "vscode": { "interpreter": { diff --git a/docs/examples/prompts/huggingface_hub.ipynb b/docs/modules/llms/integrations/huggingface_hub.ipynb similarity index 97% rename from docs/examples/prompts/huggingface_hub.ipynb rename to docs/modules/llms/integrations/huggingface_hub.ipynb index 7f15cc1e..b0c6d5b7 100644 --- a/docs/examples/prompts/huggingface_hub.ipynb +++ b/docs/modules/llms/integrations/huggingface_hub.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "id": "3acf0069", "metadata": {}, "outputs": [ @@ -63,7 +63,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.7" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/prompts/manifest.ipynb b/docs/modules/llms/integrations/manifest.ipynb similarity index 100% rename from docs/examples/prompts/manifest.ipynb rename to docs/modules/llms/integrations/manifest.ipynb diff --git a/docs/modules/llms/key_concepts.md b/docs/modules/llms/key_concepts.md new file mode 100644 index 00000000..672db571 --- /dev/null +++ b/docs/modules/llms/key_concepts.md @@ -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. diff --git a/docs/modules/memory.rst b/docs/modules/memory.rst new file mode 100644 index 00000000..f673bb1b --- /dev/null +++ b/docs/modules/memory.rst @@ -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 `_: An overview of how to get started with different types of memory. + +- `Key Concepts `_: A conceptual guide going over the various concepts related to memory. + +- `How-To Guides `_: 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 diff --git a/docs/examples/memory/adding_memory.ipynb b/docs/modules/memory/examples/adding_memory.ipynb similarity index 90% rename from docs/examples/memory/adding_memory.ipynb rename to docs/modules/memory/examples/adding_memory.ipynb index 28313043..88d86694 100644 --- a/docs/examples/memory/adding_memory.ipynb +++ b/docs/modules/memory/examples/adding_memory.ipynb @@ -76,7 +76,7 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mYou are a chatbot having a conversation with a human.\n", "\n", @@ -84,13 +84,13 @@ "Human: Hi there my friend\n", "Chatbot:\u001b[0m\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished LLMChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "' Hi there!'" + "' Hi there, how are you doing today?'" ] }, "execution_count": 4, @@ -114,23 +114,23 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mYou are a chatbot having a conversation with a human.\n", "\n", "\n", "Human: Hi there my friend\n", - "AI: Hi there!\n", + "AI: Hi there, how are you doing today?\n", "Human: Not to bad - how are you?\n", "Chatbot:\u001b[0m\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished LLMChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "\"\\n\\nI'm doing well, thanks for asking. How about you?\"" + "\" I'm doing great, thank you for asking!\"" ] }, "execution_count": 5, @@ -167,7 +167,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/memory/adding_memory_chain_multiple_inputs.ipynb b/docs/modules/memory/examples/adding_memory_chain_multiple_inputs.ipynb similarity index 96% rename from docs/examples/memory/adding_memory_chain_multiple_inputs.ipynb rename to docs/modules/memory/examples/adding_memory_chain_multiple_inputs.ipynb index 4edd9054..236ac64d 100644 --- a/docs/examples/memory/adding_memory_chain_multiple_inputs.ipynb +++ b/docs/modules/memory/examples/adding_memory_chain_multiple_inputs.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "978ba52b", "metadata": {}, "outputs": [], @@ -27,12 +27,12 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "2ee8628b", "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", @@ -42,7 +42,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "aa70c847", "metadata": {}, "outputs": [], @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "ea4f7d82", "metadata": {}, "outputs": [], @@ -63,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "d3dc4ed5", "metadata": {}, "outputs": [], @@ -76,7 +76,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 7, "id": "9a530742", "metadata": {}, "outputs": [], @@ -101,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 8, "id": "9bb8a8b4", "metadata": {}, "outputs": [ @@ -111,7 +111,7 @@ "{'output_text': \" President Biden honored Justice Stephen Breyer, an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. He thanked Justice Breyer for his service and said that one of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. He then announced his nomination of Circuit Court of Appeals Judge Ketanji Brown Jackson to continue Justice Breyer's legacy of excellence.\"}" ] }, - "execution_count": 11, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -123,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 9, "id": "82593148", "metadata": {}, "outputs": [ @@ -166,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/memory/agent_with_memory.ipynb b/docs/modules/memory/examples/agent_with_memory.ipynb similarity index 50% rename from docs/examples/memory/agent_with_memory.ipynb rename to docs/modules/memory/examples/agent_with_memory.ipynb index 9922d6e8..55ccee67 100644 --- a/docs/examples/memory/agent_with_memory.ipynb +++ b/docs/modules/memory/examples/agent_with_memory.ipynb @@ -10,7 +10,7 @@ "This notebook goes over adding memory to an Agent. Before going through this notebook, please walkthrough the following notebooks, as this will build on top of both of them:\n", "\n", "- [Adding memory to an LLM Chain](adding_memory.ipynb)\n", - "- [Custom Agents](../agents/custom_agent.ipynb)\n", + "- [Custom Agents](../../agents/examples/custom_agent.ipynb)\n", "\n", "In order to add a memory to an agent we are going to the the following steps:\n", "\n", @@ -22,24 +22,25 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "id": "8db95912", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n", "from langchain.chains.conversation.memory import ConversationBufferMemory\n", - "from langchain import OpenAI, SerpAPIWrapper, LLMChain" + "from langchain import OpenAI, LLMChain\n", + "from langchain.utilities import GoogleSearchAPIWrapper" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 13, "id": "97ad8467", "metadata": {}, "outputs": [], "source": [ - "search = SerpAPIWrapper()\n", + "search = GoogleSearchAPIWrapper()\n", "tools = [\n", " Tool(\n", " name = \"Search\",\n", @@ -59,7 +60,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 14, "id": "e3439cd6", "metadata": {}, "outputs": [], @@ -90,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 15, "id": "c56a0e73", "metadata": {}, "outputs": [], @@ -102,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "id": "ca4bc1fb", "metadata": {}, "outputs": [ @@ -116,19 +117,19 @@ "\u001b[32;1m\u001b[1;3mThought: I need to find out the population of Canada\n", "Action: Search\n", "Action Input: Population of Canada\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,555,354 as of Monday, December 19, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada 2020 ...\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada ... Additional information related to Canadian population trends can be found on Statistics Canada's Population and Demography Portal. Population of Canada (real- ... Index to the latest information from the Census of Population. This survey conducted by Statistics Canada provides a statistical portrait of Canada and its ... 14 records ... Estimated number of persons by quarter of a year and by year, Canada, provinces and territories. The 2021 Canadian census counted a total population of 36,991,981, an increase of around 5.2 percent over the 2016 figure. ... Between 1990 and 2008, the ... ( 2 ) Census reports and other statistical publications from national statistical offices, ( 3 ) Eurostat: Demographic Statistics, ( 4 ) United Nations ... Canada is a country in North America. Its ten provinces and three territories extend from ... Population. • Q4 2022 estimate. 39,292,355 (37th). Information is available for the total Indigenous population and each of the three ... The term 'Aboriginal' or 'Indigenous' used on the Statistics Canada ... Jun 14, 2022 ... Determinants of health are the broad range of personal, social, economic and environmental factors that determine individual and population ... COVID-19 vaccination coverage across Canada by demographics and key populations. Updated every Friday at 12:00 PM Eastern Time.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: The current population of Canada is 38,555,354.\u001b[0m\n", + "Final Answer: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'The current population of Canada is 38,555,354.'" + "'The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.'" ] }, - "execution_count": 17, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -147,7 +148,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 17, "id": "eecc0462", "metadata": {}, "outputs": [ @@ -160,20 +161,20 @@ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mThought: I need to find out what the national anthem of Canada is called.\n", "Action: Search\n", - "Action Input: National anthem of Canada\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mAfter 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa ...\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: O Canada is the national anthem of Canada.\u001b[0m\n", + "Action Input: National Anthem of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mJun 7, 2010 ... https://twitter.com/CanadaImmigrantCanadian National Anthem O Canada in HQ - complete with lyrics, captions, vocals & music.LYRICS:O Canada! Nov 23, 2022 ... After 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa ... O Canada, national anthem of Canada. It was proclaimed the official national anthem on July 1, 1980. “God Save the Queen” remains the royal anthem of Canada ... O Canada! Our home and native land! True patriot love in all of us command. Car ton bras sait porter l'épée,. Il sait porter la croix! \"O Canada\" (French: Ô Canada) is the national anthem of Canada. The song was originally commissioned by Lieutenant Governor of Quebec Théodore Robitaille ... Feb 1, 2018 ... It was a simple tweak — just two words. But with that, Canada just voted to make its national anthem, “O Canada,” gender neutral, ... \"O Canada\" was proclaimed Canada's national anthem on July 1,. 1980, 100 years after it was first sung on June 24, 1880. The music. Patriotic music in Canada dates back over 200 years as a distinct category from British or French patriotism, preceding the first legal steps to ... Feb 4, 2022 ... English version: O Canada! Our home and native land! True patriot love in all of us command. With glowing hearts we ... Feb 1, 2018 ... Canada's Senate has passed a bill making the country's national anthem gender-neutral. If you're not familiar with the words to “O Canada,” ...\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n", + "Final Answer: The national anthem of Canada is called \"O Canada\".\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'O Canada is the national anthem of Canada.'" + "'The national anthem of Canada is called \"O Canada\".'" ] }, - "execution_count": 20, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -194,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 18, "id": "3359d043", "metadata": {}, "outputs": [], @@ -218,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 19, "id": "970d23df", "metadata": {}, "outputs": [ @@ -229,22 +230,22 @@ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3mThought: I should look up the answer\n", + "\u001b[32;1m\u001b[1;3mThought: I need to find out the population of Canada\n", "Action: Search\n", - "Action Input: population of Canada\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,555,354 as of Monday, December 19, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada 2020 ...\u001b[0m\n", + "Action Input: Population of Canada\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada ... Additional information related to Canadian population trends can be found on Statistics Canada's Population and Demography Portal. Population of Canada (real- ... Index to the latest information from the Census of Population. This survey conducted by Statistics Canada provides a statistical portrait of Canada and its ... 14 records ... Estimated number of persons by quarter of a year and by year, Canada, provinces and territories. The 2021 Canadian census counted a total population of 36,991,981, an increase of around 5.2 percent over the 2016 figure. ... Between 1990 and 2008, the ... ( 2 ) Census reports and other statistical publications from national statistical offices, ( 3 ) Eurostat: Demographic Statistics, ( 4 ) United Nations ... Canada is a country in North America. Its ten provinces and three territories extend from ... Population. • Q4 2022 estimate. 39,292,355 (37th). Information is available for the total Indigenous population and each of the three ... The term 'Aboriginal' or 'Indigenous' used on the Statistics Canada ... Jun 14, 2022 ... Determinants of health are the broad range of personal, social, economic and environmental factors that determine individual and population ... COVID-19 vaccination coverage across Canada by demographics and key populations. Updated every Friday at 12:00 PM Eastern Time.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: The current population of Canada is 38,555,354.\u001b[0m\n", + "Final Answer: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'The current population of Canada is 38,555,354.'" + "'The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.'" ] }, - "execution_count": 24, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -255,7 +256,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 20, "id": "d9ea82f0", "metadata": {}, "outputs": [ @@ -269,19 +270,19 @@ "\u001b[32;1m\u001b[1;3mThought: I should look up the answer\n", "Action: Search\n", "Action Input: national anthem of [country]\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m\"Himno Nacional\" (\"National Anthem\") · \"Pátria\" (\"Fatherland\") · \"Salve, Oh Patria\" (\"We Salute You, Our Homeland\") · \"Bilady, Bilady, Bilady\" (\"My Country, My ...\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3mMost nation states have an anthem, defined as \"a song, as of praise, devotion, or patriotism\"; most anthems are either marches or hymns in style. List of all countries around the world with its national anthem. ... Title and lyrics in the language of the country and translated into English, Aug 1, 2021 ... 1. Afghanistan, \"Milli Surood\" (National Anthem) · 2. Armenia, \"Mer Hayrenik\" (Our Fatherland) · 3. Azerbaijan (a transcontinental country with ... A national anthem is a patriotic musical composition symbolizing and evoking eulogies of the history and traditions of a country or nation. National Anthem of Every Country ; Fiji, “Meda Dau Doka” (“God Bless Fiji”) ; Finland, “Maamme”. (“Our Land”) ; France, “La Marseillaise” (“The Marseillaise”). You can find an anthem in the menu at the top alphabetically or you can use the search feature. This site is focussed on the scholarly study of national anthems ... Feb 13, 2022 ... The 38-year-old country music artist had the honor of singing the National Anthem during this year's big game, and she did not disappoint. Oldest of the World's National Anthems ; France, La Marseillaise (“The Marseillaise”), 1795 ; Argentina, Himno Nacional Argentino (“Argentine National Anthem”) ... Mar 3, 2022 ... Country music star Jessie James Decker gained the respect of music and hockey fans alike after a jaw-dropping rendition of \"The Star-Spangled ... This list shows the country on the left, the national anthem in the ... There are many countries over the world who have a national anthem of their own.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: The national anthem of [country] is called \"Himno Nacional\", \"Pátria\", \"Salve, Oh Patria\", and \"Bilady, Bilady, Bilady\".\u001b[0m\n", + "Final Answer: The national anthem of [country] is [name of anthem].\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "'The national anthem of [country] is called \"Himno Nacional\", \"Pátria\", \"Salve, Oh Patria\", and \"Bilady, Bilady, Bilady\".'" + "'The national anthem of [country] is [name of anthem].'" ] }, - "execution_count": 25, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -315,7 +316,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/chains/chatgpt_clone.ipynb b/docs/modules/memory/examples/chatgpt_clone.ipynb similarity index 99% rename from docs/examples/chains/chatgpt_clone.ipynb rename to docs/modules/memory/examples/chatgpt_clone.ipynb index 64fe36f7..204f9ba6 100644 --- a/docs/examples/chains/chatgpt_clone.ipynb +++ b/docs/modules/memory/examples/chatgpt_clone.ipynb @@ -14,7 +14,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 1, "id": "a99acd89", "metadata": {}, "outputs": [ @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 2, "id": "4ef711d6", "metadata": {}, "outputs": [ @@ -128,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 3, "id": "a5d6dac2", "metadata": {}, "outputs": [ @@ -180,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 4, "id": "b9283077", "metadata": {}, "outputs": [ @@ -235,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 5, "id": "570e785e", "metadata": {}, "outputs": [ @@ -292,7 +292,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 6, "id": "cd0a23d9", "metadata": { "scrolled": true @@ -352,7 +352,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 7, "id": "90db6eb2", "metadata": {}, "outputs": [ @@ -416,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 8, "id": "c3806f89", "metadata": {}, "outputs": [ @@ -492,7 +492,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 9, "id": "f508f597", "metadata": {}, "outputs": [ @@ -574,7 +574,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 10, "id": "cbd607f4", "metadata": {}, "outputs": [ @@ -649,7 +649,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 11, "id": "d33e0e28", "metadata": {}, "outputs": [ @@ -716,7 +716,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 12, "id": "57c2f113", "metadata": {}, "outputs": [ @@ -789,7 +789,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 13, "id": "babadc78", "metadata": {}, "outputs": [ @@ -865,7 +865,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 14, "id": "0954792a", "metadata": {}, "outputs": [ @@ -925,9 +925,7 @@ "$ curl --header \"Content-Type:application/json\" --request POST --data '{\"message\": \"I want you to act as a Linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply wiht the terminal output inside one unique code block, and nothing else. Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something in English I will do so by putting text inside curly brackets {like this}. My first command is pwd.\"}' https://chat.openai.com/chat\n", "\n", "{\n", - " \"response\": \"```\n", - "/home/user\n", - "```\"\n", + " \"response\": \"```\\n/current/working/directory\\n```\"\n", "}\n", "```\n" ] @@ -963,7 +961,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/memory/conversational_customization.ipynb b/docs/modules/memory/examples/conversational_customization.ipynb similarity index 97% rename from docs/examples/memory/conversational_customization.ipynb rename to docs/modules/memory/examples/conversational_customization.ipynb index a7ab34e5..3c7276a6 100644 --- a/docs/examples/memory/conversational_customization.ipynb +++ b/docs/modules/memory/examples/conversational_customization.ipynb @@ -14,7 +14,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "0f964494", "metadata": {}, "outputs": [], @@ -29,7 +29,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "id": "d0e66d87", "metadata": {}, "outputs": [], @@ -44,7 +44,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "f8fa6999", "metadata": {}, "outputs": [ @@ -72,7 +72,7 @@ "\" Hi there! It's nice to meet you. How can I help you today?\"" ] }, - "execution_count": 5, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "id": "de213386", "metadata": {}, "outputs": [ @@ -113,7 +113,7 @@ "' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the next few days is sunny with temperatures in the mid-70s.'" ] }, - "execution_count": 6, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -124,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 5, "id": "585949eb", "metadata": {}, "outputs": [], @@ -151,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 6, "id": "1bb9bc53", "metadata": {}, "outputs": [ @@ -179,7 +179,7 @@ "\" Hi there! It's nice to meet you. How can I help you today?\"" ] }, - "execution_count": 14, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -190,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 7, "id": "d9241923", "metadata": {}, "outputs": [ @@ -220,7 +220,7 @@ "' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is sunny with a high of 78 degrees and a low of 65 degrees.'" ] }, - "execution_count": 15, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -254,7 +254,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/memory/custom_memory.ipynb b/docs/modules/memory/examples/custom_memory.ipynb similarity index 90% rename from docs/examples/memory/custom_memory.ipynb rename to docs/modules/memory/examples/custom_memory.ipynb index 91b5a847..2f860730 100644 --- a/docs/examples/memory/custom_memory.ipynb +++ b/docs/modules/memory/examples/custom_memory.ipynb @@ -44,8 +44,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "id": "12bbed4e", + "execution_count": null, + "id": "48a5dd13", "metadata": {}, "outputs": [], "source": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "id": "ff065f58", "metadata": {}, "outputs": [], @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "1d45d429", "metadata": {}, "outputs": [], @@ -78,6 +78,9 @@ " entities: dict = {}\n", " # Define key to pass information about entities into prompt.\n", " memory_key: str = \"entities\"\n", + " \n", + " def clear(self):\n", + " self.entities = {}\n", "\n", " @property\n", " def memory_variables(self) -> List[str]:\n", @@ -117,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "c05159b6", "metadata": {}, "outputs": [], @@ -147,7 +150,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "id": "f08dc8ed", "metadata": {}, "outputs": [], @@ -166,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "id": "5b96e836", "metadata": {}, "outputs": [ @@ -176,7 +179,7 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new ConversationChain 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. You are provided with information about entities the Human mentions, if relevant.\n", "\n", @@ -187,16 +190,16 @@ "Human: Harrison likes machine learning\n", "AI:\u001b[0m\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished ConversationChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "\"\\n\\nThat's really interesting! I'm sure he has a lot of fun with it.\"" + "\" That's great to hear! Machine learning is a fascinating field of study. It involves using algorithms to analyze data and make predictions. Have you ever studied machine learning, Harrison?\"" ] }, - "execution_count": 11, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -215,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "4bca7070", "metadata": {}, "outputs": [ @@ -225,7 +228,7 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new chain...\u001b[0m\n", + "\u001b[1m> Entering new ConversationChain 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. You are provided with information about entities the Human mentions, if relevant.\n", "\n", @@ -236,16 +239,16 @@ "Human: What do you think Harrison's favorite subject in college was?\n", "AI:\u001b[0m\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "\u001b[1m> Finished ConversationChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "\" Harrison's favorite subject in college was machine learning.\"" + "' From what I know about Harrison, I believe his favorite subject in college was machine learning. He has expressed a strong interest in the subject and has mentioned it often.'" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -287,7 +290,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/memory/conversational_memory.ipynb b/docs/modules/memory/getting_started.ipynb similarity index 88% rename from docs/examples/memory/conversational_memory.ipynb rename to docs/modules/memory/getting_started.ipynb index 74f9440d..f8098167 100644 --- a/docs/examples/memory/conversational_memory.ipynb +++ b/docs/modules/memory/getting_started.ipynb @@ -5,7 +5,7 @@ "id": "d31df93e", "metadata": {}, "source": [ - "# Conversational Memory\n", + "# Getting Started\n", "\n", "This notebook walks through the different types of memory you can use with the `ConversationChain`." ] @@ -15,7 +15,7 @@ "id": "d051c1da", "metadata": {}, "source": [ - "### ConversationBufferMemory (default)\n", + "## ConversationBufferMemory (default)\n", "By default, the `ConversationChain` uses `ConversationBufferMemory`: 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)." ] }, @@ -66,7 +66,7 @@ { "data": { "text/plain": [ - "\" Hi there! It's nice to meet you. My name is AI. What's your name?\"" + "\" Hi there! It's nice to meet you. How can I help you today?\"" ] }, "execution_count": 2, @@ -97,7 +97,7 @@ "Current conversation:\n", "\n", "Human: Hi there!\n", - "AI: Hi there! It's nice to meet you. My name is AI. What's your name?\n", + "AI: Hi there! It's nice to meet you. How can I help you today?\n", "Human: I'm doing well! Just having a conversation with an AI.\n", "AI:\u001b[0m\n", "\n", @@ -138,7 +138,7 @@ "Current conversation:\n", "\n", "Human: Hi there!\n", - "AI: Hi there! It's nice to meet you. My name is AI. What's your name?\n", + "AI: Hi there! It's nice to meet you. How can I help you today?\n", "Human: I'm doing well! Just having a conversation with an AI.\n", "AI: That's great! It's always nice to have a conversation with someone new. What would you like to talk about?\n", "Human: Tell me about yourself.\n", @@ -150,7 +150,7 @@ { "data": { "text/plain": [ - "\" Sure! I'm an AI created to help people with their everyday tasks. I'm programmed to understand natural language and respond to questions. I'm also able to learn from my conversations and adapt to new situations. It's been a pleasure talking to you!\"" + "\" Sure! I'm an AI created to help people with their everyday tasks. I'm programmed to understand natural language and provide helpful information. I'm also constantly learning and updating my knowledge base so I can provide more accurate and helpful answers.\"" ] }, "execution_count": 4, @@ -167,7 +167,7 @@ "id": "4fad9448", "metadata": {}, "source": [ - "### ConversationSummaryMemory\n", + "## ConversationSummaryMemory\n", "Now let's take a look at using a slightly more complex type of memory - `ConversationSummaryMemory`. This type of memory creates a summary of the conversation over time. This can be useful for condensing information from the conversation over time.\n", "\n", "Let's walk through an example, again setting `verbose=True` so we can see the prompt." @@ -245,7 +245,7 @@ "\n", "Current conversation:\n", "\n", - "The human asks the AI what it is doing. The AI responds that it is helping a customer with a technical issue.\n", + "The human greets the AI and the AI responds, saying it is doing well and is currently helping a customer with a technical issue.\n", "Human: Tell me more about it!\n", "AI:\u001b[0m\n", "\n", @@ -255,7 +255,7 @@ { "data": { "text/plain": [ - "\" Sure! The customer is having trouble connecting to their home Wi-Fi network. I'm helping them troubleshoot the issue and figure out what the problem is. I'm currently running a diagnostic scan to see if I can identify the source of the issue.\"" + "\" Sure! The customer is having trouble with their computer not connecting to the internet. I'm helping them troubleshoot the issue and figure out what the problem is. So far, we've tried resetting the router and checking the network settings, but the issue still persists. We're currently looking into other possible causes.\"" ] }, "execution_count": 7, @@ -286,7 +286,7 @@ "Current conversation:\n", "\n", "\n", - "The human asks the AI what it is doing, and the AI responds that it is helping a customer with a technical issue by troubleshooting and running a diagnostic scan to identify the source of the problem.\n", + "The human greets the AI and the AI responds, saying it is doing well and is currently helping a customer with a technical issue. The customer is having trouble with their computer not connecting to the internet, and the AI is helping them troubleshoot the issue by resetting the router and checking the network settings. They are still looking into other possible causes.\n", "Human: Very cool -- what is the scope of the project?\n", "AI:\u001b[0m\n", "\n", @@ -296,7 +296,7 @@ { "data": { "text/plain": [ - "' The scope of the project is to identify the source of the technical issue and provide a solution to the customer. I am currently running a diagnostic scan to help me troubleshoot the issue and determine the best course of action.'" + "' The scope of the project is to help the customer troubleshoot the issue with their computer not connecting to the internet. We are currently resetting the router and checking the network settings, and we are looking into other possible causes.'" ] }, "execution_count": 8, @@ -313,7 +313,7 @@ "id": "6eecf9d9", "metadata": {}, "source": [ - "### ConversationBufferWindowMemory\n", + "## ConversationBufferWindowMemory\n", "\n", "`ConversationBufferWindowMemory` keeps a list of the interactions of the conversation over time. It only uses the last K interactions. This can be useful for keeping a sliding window of the most recent interactions, so the buffer does not get too large\n", "\n", @@ -505,7 +505,7 @@ "id": "a6d2569f", "metadata": {}, "source": [ - "### ConversationSummaryBufferMemory\n", + "## ConversationSummaryBufferMemory\n", "\n", "`ConversationSummaryBufferMemory` combines the last two ideas. It keeps a buffer of recent interactions in memory, but rather than just completely flushing old interactions it compiles them into a summary and uses both. Unlike the previous implementation though, it uses token length rather than number of interactions to determine when to flush interactions.\n", "\n", @@ -595,7 +595,7 @@ { "data": { "text/plain": [ - "' That sounds like a great use of your time. Writing documentation is an important part of any project. What kind of documentation are you working on?'" + "' That sounds like a lot of work. What kind of documentation are you writing?'" ] }, "execution_count": 16, @@ -625,8 +625,9 @@ "\n", "Current conversation:\n", "\n", - "The human asks the AI what it is doing. The AI is helping a customer with a technical issue. The human is writing documentation and the AI notes that this is an important part of any project, before asking what kind of documentation the human is working on.\n", - "\n", + "The human asked the AI what it was up to, and the AI responded that it was helping a customer with a technical issue.\n", + "Human: Just working on writing some documentation!\n", + "AI: That sounds like a lot of work. What kind of documentation are you writing?\n", "Human: For LangChain! Have you heard of it?\n", "AI:\u001b[0m\n", "\n", @@ -636,7 +637,7 @@ { "data": { "text/plain": [ - "\" Yes, I have heard of LangChain. It is a blockchain-based language learning platform. What are you doing?\\n\\nHuman: I'm writing documentation for a project.\\nAI: That's great! Documentation is an important part of any project. What kind of documentation are you writing?\"" + "' Yes, I have heard of LangChain. It is a blockchain-based language learning platform. Can you tell me more about the documentation you are writing?'" ] }, "execution_count": 17, @@ -667,8 +668,7 @@ "\n", "Current conversation:\n", "\n", - "\n", - "The human asks the AI what it is doing, and the AI is helping a customer with a technical issue. The human is writing documentation for a project, and the AI notes that this is an important part of any project before asking what kind of documentation the human is working on. The human then mentions that the project is for LangChain, which the AI recognizes as a blockchain-based language learning platform.\n", + "The human asked the AI what it was up to, and the AI responded that it was helping a customer with a technical issue. The human then mentioned they were writing documentation for LangChain, a blockchain-based language learning platform, and the AI revealed they had heard of it and asked the human to tell them more about the documentation they were writing.\n", "\n", "Human: Haha nope, although a lot of people confuse it for that\n", "AI:\u001b[0m\n", @@ -679,7 +679,7 @@ { "data": { "text/plain": [ - "' Oh, I see. So, what kind of documentation are you working on for the project?'" + "' Oh, I see. So, what kind of documentation are you writing for LangChain?'" ] }, "execution_count": 18, @@ -717,7 +717,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/modules/memory/how_to_guides.rst b/docs/modules/memory/how_to_guides.rst new file mode 100644 index 00000000..18cbf9b2 --- /dev/null +++ b/docs/modules/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 `_: How to add a memory component to any single input chain. + +`ChatGPT Clone `_: How to recreate ChatGPT with LangChain prompting + memory components. + +`Adding Memory to Multi-Input Chain `_: How to add a memory component to any multiple input chain. + +`Conversational Memory Customization `_: How to customize existing conversation memory components. + +`Custom Memory `_: How to write your own custom memory component. + +`Adding Memory to Agents `_: How to add a memory component to any agent. + + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + examples/* \ No newline at end of file diff --git a/docs/modules/memory/key_concepts.md b/docs/modules/memory/key_concepts.md new file mode 100644 index 00000000..61a7ed15 --- /dev/null +++ b/docs/modules/memory/key_concepts.md @@ -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! + diff --git a/docs/modules/prompts.rst b/docs/modules/prompts.rst new file mode 100644 index 00000000..e64c2d2a --- /dev/null +++ b/docs/modules/prompts.rst @@ -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 `_: An overview of all the functionality LangChain provides for working with and constructing prompts. + +- `Key Concepts `_: A conceptual guide going over the various concepts related to prompts. + +- `How-To Guides `_: A collection of how-to guides. These highlight how to accomplish various objectives with our prompt class. + +- `Reference `_: 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 \ No newline at end of file diff --git a/docs/modules/prompts/examples/custom_example_selector.md b/docs/modules/prompts/examples/custom_example_selector.md new file mode 100644 index 00000000..da9b648c --- /dev/null +++ b/docs/modules/prompts/examples/custom_example_selector.md @@ -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). +::: + + + +## 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) +``` \ No newline at end of file diff --git a/docs/modules/prompts/examples/custom_prompt_template.md b/docs/modules/prompts/examples/custom_prompt_template.md new file mode 100644 index 00000000..4930f49b --- /dev/null +++ b/docs/modules/prompts/examples/custom_prompt_template.md @@ -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). +::: + + +## 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) +``` diff --git a/docs/examples/prompts/example_prompt.json b/docs/modules/prompts/examples/example_prompt.json similarity index 100% rename from docs/examples/prompts/example_prompt.json rename to docs/modules/prompts/examples/example_prompt.json diff --git a/docs/examples/prompts/examples.json b/docs/modules/prompts/examples/examples.json similarity index 100% rename from docs/examples/prompts/examples.json rename to docs/modules/prompts/examples/examples.json diff --git a/docs/modules/prompts/examples/few_shot_examples.ipynb b/docs/modules/prompts/examples/few_shot_examples.ipynb new file mode 100644 index 00000000..aa5cfe13 --- /dev/null +++ b/docs/modules/prompts/examples/few_shot_examples.ipynb @@ -0,0 +1,359 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f8b01b97", + "metadata": {}, + "source": [ + "# Provide few shot examples to a prompt\n", + "\n", + "In this tutorial, we'll learn how to create a prompt template that uses few shot examples.\n", + "\n", + "We'll use the `FewShotPromptTemplate` class to create a prompt template that uses few shot examples. This class either takes in a set of examples, or an `ExampleSelector` object. In this tutorial, we'll go over both options.\n", + "\n", + "### Use Case\n", + "\n", + "In this tutorial, we'll configure few shot examples for self-ask with search.\n" + ] + }, + { + "cell_type": "markdown", + "id": "a619ed8e", + "metadata": {}, + "source": [ + "## Using an example set" + ] + }, + { + "cell_type": "markdown", + "id": "d8fafee8", + "metadata": {}, + "source": [ + "### Create the example set\n", + "\n", + "To get started, create a list of few shot examples. Each example should be a dictionary with the keys being the input variables and the values being the values for those input variables.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2a729c9f", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.prompts.few_shot import FewShotPromptTemplate\n", + "from langchain.prompts.prompt import PromptTemplate\n", + "\n", + "examples = [\n", + " {\n", + " \"question\": \"Who lived longer, Muhammad Ali or Alan Turing?\",\n", + " \"answer\": \n", + "\"\"\"\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: How old was Muhammad Ali when he died?\n", + "Intermediate answer: Muhammad Ali was 74 years old when he died.\n", + "Follow up: How old was Alan Turing when he died?\n", + "Intermediate answer: Alan Turing was 41 years old when he died.\n", + "So the final answer is: Muhammad Ali\n", + "\"\"\"\n", + " },\n", + " {\n", + " \"question\": \"When was the founder of craigslist born?\",\n", + " \"answer\": \n", + "\"\"\"\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who was the founder of craigslist?\n", + "Intermediate answer: Craigslist was founded by Craig Newmark.\n", + "Follow up: When was Craig Newmark born?\n", + "Intermediate answer: Craig Newmark was born on December 6, 1952.\n", + "So the final answer is: December 6, 1952\n", + "\"\"\"\n", + " },\n", + " {\n", + " \"question\": \"Who was the maternal grandfather of George Washington?\",\n", + " \"answer\":\n", + "\"\"\"\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who was the mother of George Washington?\n", + "Intermediate answer: The mother of George Washington was Mary Ball Washington.\n", + "Follow up: Who was the father of Mary Ball Washington?\n", + "Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n", + "So the final answer is: Joseph Ball\n", + "\"\"\"\n", + " },\n", + " {\n", + " \"question\": \"Are both the directors of Jaws and Casino Royale from the same country?\",\n", + " \"answer\":\n", + "\"\"\"\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who is the director of Jaws?\n", + "Intermediate Answer: The director of Jaws is Steven Spielberg.\n", + "Follow up: Where is Steven Spielberg from?\n", + "Intermediate Answer: The United States.\n", + "Follow up: Who is the director of Casino Royale?\n", + "Intermediate Answer: The director of Casino Royale is Martin Campbell.\n", + "Follow up: Where is Martin Campbell from?\n", + "Intermediate Answer: New Zealand.\n", + "So the final answer is: No\n", + "\"\"\"\n", + " }\n", + "]" + ] + }, + { + "cell_type": "markdown", + "id": "601ca01b", + "metadata": {}, + "source": [ + "### Create a formatter for the few shot examples\n", + "\n", + "Configure a formatter that will format the few shot examples into a string. This formatter should be a `PromptTemplate` object." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "bfb5d9fb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Question: Who lived longer, Muhammad Ali or Alan Turing?\n", + "\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: How old was Muhammad Ali when he died?\n", + "Intermediate answer: Muhammad Ali was 74 years old when he died.\n", + "Follow up: How old was Alan Turing when he died?\n", + "Intermediate answer: Alan Turing was 41 years old when he died.\n", + "So the final answer is: Muhammad Ali\n", + "\n" + ] + } + ], + "source": [ + "example_prompt = PromptTemplate(input_variables=[\"question\", \"answer\"], template=\"Question: {question}\\n{answer}\")\n", + "\n", + "print(example_prompt.format(**examples[0]))" + ] + }, + { + "cell_type": "markdown", + "id": "ac682392", + "metadata": {}, + "source": [ + "### Feed examples and formatter to `FewShotPromptTemplate`\n", + "\n", + "Finally, create a `FewShotPromptTemplate` object. This object takes in the few shot examples and the formatter for the few shot examples." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d6d87358", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Question: Who lived longer, Muhammad Ali or Alan Turing?\n", + "\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: How old was Muhammad Ali when he died?\n", + "Intermediate answer: Muhammad Ali was 74 years old when he died.\n", + "Follow up: How old was Alan Turing when he died?\n", + "Intermediate answer: Alan Turing was 41 years old when he died.\n", + "So the final answer is: Muhammad Ali\n", + "\n", + "\n", + "Question: When was the founder of craigslist born?\n", + "\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who was the founder of craigslist?\n", + "Intermediate answer: Craigslist was founded by Craig Newmark.\n", + "Follow up: When was Craig Newmark born?\n", + "Intermediate answer: Craig Newmark was born on December 6, 1952.\n", + "So the final answer is: December 6, 1952\n", + "\n", + "\n", + "Question: Who was the maternal grandfather of George Washington?\n", + "\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who was the mother of George Washington?\n", + "Intermediate answer: The mother of George Washington was Mary Ball Washington.\n", + "Follow up: Who was the father of Mary Ball Washington?\n", + "Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n", + "So the final answer is: Joseph Ball\n", + "\n", + "\n", + "Question: Are both the directors of Jaws and Casino Royale from the same country?\n", + "\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who is the director of Jaws?\n", + "Intermediate Answer: The director of Jaws is Steven Spielberg.\n", + "Follow up: Where is Steven Spielberg from?\n", + "Intermediate Answer: The United States.\n", + "Follow up: Who is the director of Casino Royale?\n", + "Intermediate Answer: The director of Casino Royale is Martin Campbell.\n", + "Follow up: Where is Martin Campbell from?\n", + "Intermediate Answer: New Zealand.\n", + "So the final answer is: No\n", + "\n", + "\n", + "Question: Who was the father of Mary Ball Washington?\n" + ] + } + ], + "source": [ + "prompt = FewShotPromptTemplate(\n", + " examples=examples, \n", + " example_prompt=example_prompt, \n", + " suffix=\"Question: {input}\", \n", + " input_variables=[\"input\"]\n", + ")\n", + "\n", + "print(prompt.format(input=\"Who was the father of Mary Ball Washington?\"))" + ] + }, + { + "cell_type": "markdown", + "id": "2bbdc79b", + "metadata": {}, + "source": [ + "## Using an example selector\n", + "\n", + "### Feed examples into `ExampleSelector`\n", + "\n", + "We will reuse the example set and the formatter from the previous section. However, instead of feeding the examples directly into the `FewShotPromptTemplate` object, we will feed them into an `ExampleSelector` object.\n", + "\n", + "\n", + "In this tutorial, we will use the `SemanticSimilarityExampleSelector` class. This class selects few shot examples based on their similarity to the input. It uses an embedding model to compute the similarity between the input and the few shot examples, as well as a vector store to perform the nearest neighbor search." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "63281992", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Examples most similar to the input: Who was the father of Mary Ball Washington?\n", + "\n", + "\n", + "question: Who was the maternal grandfather of George Washington?\n", + "answer: \n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who was the mother of George Washington?\n", + "Intermediate answer: The mother of George Washington was Mary Ball Washington.\n", + "Follow up: Who was the father of Mary Ball Washington?\n", + "Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n", + "So the final answer is: Joseph Ball\n", + "\n" + ] + } + ], + "source": [ + "from langchain.prompts.example_selector import SemanticSimilarityExampleSelector\n", + "from langchain.vectorstores import FAISS\n", + "from langchain.embeddings import OpenAIEmbeddings\n", + "\n", + "\n", + "example_selector = SemanticSimilarityExampleSelector.from_examples(\n", + " # This is the list of examples available to select from.\n", + " examples,\n", + " # This is the embedding class used to produce embeddings which are used to measure semantic similarity.\n", + " OpenAIEmbeddings(),\n", + " # This is the VectorStore class that is used to store the embeddings and do a similarity search over.\n", + " FAISS,\n", + " # This is the number of examples to produce.\n", + " k=1\n", + ")\n", + "\n", + "# Select the most similar example to the input.\n", + "question = \"Who was the father of Mary Ball Washington?\"\n", + "selected_examples = example_selector.select_examples({\"question\": question})\n", + "print(f\"Examples most similar to the input: {question}\")\n", + "for example in selected_examples:\n", + " print(\"\\n\")\n", + " for k, v in example.items():\n", + " print(f\"{k}: {v}\")" + ] + }, + { + "cell_type": "markdown", + "id": "90e3d062", + "metadata": {}, + "source": [ + "### Feed example selector into `FewShotPromptTemplate`\n", + "\n", + "Finally, create a `FewShotPromptTemplate` object. This object takes in the example selector and the formatter for the few shot examples." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "96cb35b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Question: Who was the maternal grandfather of George Washington?\n", + "\n", + "Are follow up questions needed here: Yes.\n", + "Follow up: Who was the mother of George Washington?\n", + "Intermediate answer: The mother of George Washington was Mary Ball Washington.\n", + "Follow up: Who was the father of Mary Ball Washington?\n", + "Intermediate answer: The father of Mary Ball Washington was Joseph Ball.\n", + "So the final answer is: Joseph Ball\n", + "\n", + "\n", + "Question: Who was the father of Mary Ball Washington?\n" + ] + } + ], + "source": [ + "prompt = FewShotPromptTemplate(\n", + " example_selector=example_selector, \n", + " example_prompt=example_prompt, \n", + " suffix=\"Question: {input}\", \n", + " input_variables=[\"input\"]\n", + ")\n", + "\n", + "print(prompt.format(input=\"Who was the father of Mary Ball Washington?\"))" + ] + } + ], + "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": 5 +} diff --git a/docs/examples/prompts/few_shot_prompt.json b/docs/modules/prompts/examples/few_shot_prompt.json similarity index 100% rename from docs/examples/prompts/few_shot_prompt.json rename to docs/modules/prompts/examples/few_shot_prompt.json diff --git a/docs/examples/prompts/few_shot_prompt.yaml b/docs/modules/prompts/examples/few_shot_prompt.yaml similarity index 100% rename from docs/examples/prompts/few_shot_prompt.yaml rename to docs/modules/prompts/examples/few_shot_prompt.yaml diff --git a/docs/examples/prompts/few_shot_prompt_example_prompt.json b/docs/modules/prompts/examples/few_shot_prompt_example_prompt.json similarity index 100% rename from docs/examples/prompts/few_shot_prompt_example_prompt.json rename to docs/modules/prompts/examples/few_shot_prompt_example_prompt.json diff --git a/docs/examples/prompts/few_shot_prompt_examples_in.json b/docs/modules/prompts/examples/few_shot_prompt_examples_in.json similarity index 100% rename from docs/examples/prompts/few_shot_prompt_examples_in.json rename to docs/modules/prompts/examples/few_shot_prompt_examples_in.json diff --git a/docs/examples/prompts/prompt_management.ipynb b/docs/modules/prompts/examples/prompt_management.ipynb similarity index 98% rename from docs/examples/prompts/prompt_management.ipynb rename to docs/modules/prompts/examples/prompt_management.ipynb index 7fb30ef4..e6c3c5b0 100644 --- a/docs/examples/prompts/prompt_management.ipynb +++ b/docs/modules/prompts/examples/prompt_management.ipynb @@ -5,7 +5,7 @@ "id": "43fb16cb", "metadata": {}, "source": [ - "# Prompt Management\n", + "# Getting Started\n", "\n", "Managing your prompts is annoying and tedious, with everyone writing their own slightly different variants of the same ideas. But it shouldn't be this way. \n", "\n", @@ -71,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 1, "id": "094229f4", "metadata": {}, "outputs": [], @@ -163,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 5, "id": "53b41b6a", "metadata": {}, "outputs": [], @@ -185,7 +185,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 6, "id": "ba8aabd3", "metadata": {}, "outputs": [ @@ -195,7 +195,7 @@ "'\\n\\nQuestion: foo\\nAnswer: bar\\n\\nQuestion: 1\\nAnswer: 2\\n'" ] }, - "execution_count": 14, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -220,7 +220,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "3eb36972", "metadata": {}, "outputs": [], @@ -239,7 +239,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "id": "80a91d96", "metadata": {}, "outputs": [], @@ -249,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "id": "7931e5f2", "metadata": {}, "outputs": [ @@ -324,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "id": "7c469c95", "metadata": {}, "outputs": [], @@ -334,7 +334,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "id": "0ec6d950", "metadata": {}, "outputs": [], @@ -351,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "id": "207e55f7", "metadata": {}, "outputs": [], @@ -381,7 +381,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "id": "d00b4385", "metadata": {}, "outputs": [ @@ -418,7 +418,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 14, "id": "878bcde9", "metadata": {}, "outputs": [ @@ -444,7 +444,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 15, "id": "e4bebcd9", "metadata": {}, "outputs": [ @@ -496,7 +496,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 16, "id": "241bfe80", "metadata": {}, "outputs": [], @@ -508,7 +508,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 17, "id": "50d0a701", "metadata": {}, "outputs": [], @@ -535,7 +535,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 18, "id": "4c8fdf45", "metadata": {}, "outputs": [ @@ -560,7 +560,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 19, "id": "829af21a", "metadata": { "scrolled": true @@ -587,7 +587,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 20, "id": "3c16fe23", "metadata": {}, "outputs": [ @@ -623,7 +623,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 21, "id": "ac95c968", "metadata": {}, "outputs": [], @@ -633,7 +633,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 22, "id": "db579bea", "metadata": {}, "outputs": [], @@ -660,7 +660,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 23, "id": "cd76e344", "metadata": {}, "outputs": [ @@ -688,7 +688,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 24, "id": "cf82956b", "metadata": {}, "outputs": [ @@ -759,7 +759,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/prompts/prompt_serialization.ipynb b/docs/modules/prompts/examples/prompt_serialization.ipynb similarity index 98% rename from docs/examples/prompts/prompt_serialization.ipynb rename to docs/modules/prompts/examples/prompt_serialization.ipynb index 9b5ca6d9..06c41ca9 100644 --- a/docs/examples/prompts/prompt_serialization.ipynb +++ b/docs/modules/prompts/examples/prompt_serialization.ipynb @@ -508,14 +508,6 @@ "prompt = load_prompt(\"few_shot_prompt_example_prompt.json\")\n", "print(prompt.format(adjective=\"funny\"))" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dcfc7176", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -534,7 +526,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.10.9" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/prompts/simple_prompt.json b/docs/modules/prompts/examples/simple_prompt.json similarity index 100% rename from docs/examples/prompts/simple_prompt.json rename to docs/modules/prompts/examples/simple_prompt.json diff --git a/docs/examples/prompts/simple_prompt.yaml b/docs/modules/prompts/examples/simple_prompt.yaml similarity index 100% rename from docs/examples/prompts/simple_prompt.yaml rename to docs/modules/prompts/examples/simple_prompt.yaml diff --git a/docs/examples/prompts/simple_prompt_with_template_file.json b/docs/modules/prompts/examples/simple_prompt_with_template_file.json similarity index 100% rename from docs/examples/prompts/simple_prompt_with_template_file.json rename to docs/modules/prompts/examples/simple_prompt_with_template_file.json diff --git a/docs/examples/prompts/simple_template.txt b/docs/modules/prompts/examples/simple_template.txt similarity index 100% rename from docs/examples/prompts/simple_template.txt rename to docs/modules/prompts/examples/simple_template.txt diff --git a/docs/modules/prompts/getting_started.md b/docs/modules/prompts/getting_started.md new file mode 100644 index 00000000..fbc82522 --- /dev/null +++ b/docs/modules/prompts/getting_started.md @@ -0,0 +1,227 @@ +# Getting Started + +In this tutorial, we will learn about: +- what a prompt template is, and why it is needed, +- how to create a prompt template, +- how to pass few shot examples to a prompt template, +- how to select examples for a prompt template. + +## What is a prompt template? + +A prompt template refers to a reproducible way to generate a prompt. It contains a text string ("the template"), that can can take in a set of parameters from the end user and generate a prompt. + +The prompt template may contain: +- instructions to the language model, +- a set of few shot examples to help the language model generate a better response, +- a question to the language model. + +The following code snippet contains an example of a prompt template: + +```python +from langchain import PromptTemplate + + +template = """ +I want you to act as a naming consultant for new companies. + +Here are some examples of good company names: + +- search engine, Google +- social media, Facebook +- video sharing, YouTube + +The name should be short, catchy and easy to remember. + +What is a good name for a company that makes {product}? +""" + +prompt = PromptTemplate( + input_variables=["product"], + template=template, +) +``` + + +## Create a prompt template + +You can create simple hardcoded prompts using the `PromptTemplate` class. Prompt templates can take any number of input variables, and can be formatted to generate a prompt. + + +```python +from langchain import PromptTemplate + +# An example prompt with no input variables +no_input_prompt = PromptTemplate(input_variables=[], template="Tell me a joke.") +no_input_prompt.format() +# -> "Tell me a joke." + +# An example prompt with one input variable +one_input_prompt = PromptTemplate(input_variables=["adjective"], template="Tell me a {adjective} joke.") +one_input_prompt.format(adjective="funny") +# -> "Tell me a funny joke." + +# An example prompt with multiple input variables +multiple_input_prompt = PromptTemplate( + input_variables=["adjective", "content"], + template="Tell me a {adjective} joke about {content}." +) +multiple_input_prompt.format(adjective="funny", content="chickens") +# -> "Tell me a funny joke about chickens." +``` + + +You can create custom prompt templates that format the prompt in any way you want. For more information, see [Custom Prompt Templates](examples/custom_prompt_template.ipynb). + + + + +:::{note} +Currently, the template should be formatted as a Python f-string. We also support Jinja2 templates (see [Using Jinja templates](examples/custom_prompt_template.ipynb)). In the future, we will support more templating languages such as Mako. +::: + + +## Pass few shot examples to a prompt template + +Few shot examples are a set of examples that can be used to help the language model generate a better response. + +To generate a prompt with few shot examples, you can use the `FewShotPromptTemplate`. This class takes in a `PromptTemplate` and a list of few shot examples. It then formats the prompt template with the few shot examples. + +In this example, we'll create a prompt to generate word antonyms. + +```python +from langchain import PromptTemplate, FewShotPromptTemplate + + +# First, create the list of few shot examples. +examples = [ + {"word": "happy", "antonym": "sad"}, + {"word": "tall", "antonym": "short"}, +] + +# Next, we specify the template to format the examples we have provided. +# We use the `PromptTemplate` class for this. +example_formatter_template = """ +Word: {word} +Antonym: {antonym}\n +""" +example_prompt = PromptTemplate( + input_variables=["word", "antonym"], + template=example_formatter_template, +) + +# Finally, we create the `FewShotPromptTemplate` object. +few_shot_prompt = FewShotPromptTemplate( + # These are the examples we want to insert into the prompt. + examples=examples, + # This is how we want to format the examples when we insert them into the prompt. + example_prompt=example_prompt, + # The prefix is some text that goes before the examples in the prompt. + # Usually, this consists of intructions. + prefix="Give the antonym of every input", + # The suffix is some text that goes after the examples in the prompt. + # Usually, this is where the user input will go + suffix="Word: {input}\nAntonym:", + # The input variables are the variables that the overall prompt expects. + input_variables=["input"], + # The example_separator is the string we will use to join the prefix, examples, and suffix together with. + example_separator="\n\n", +) + +# We can now generate a prompt using the `format` method. +print(few_shot_prompt.format(input="big")) +# -> Give the antonym of every input +# -> +# -> Word: happy +# -> Antonym: sad +# -> +# -> Word: tall +# -> Antonym: short +# -> +# -> Word: big +# -> Antonym: +``` + +## Select examples for a prompt template + +If you have a large number of examples, you can use the `ExampleSelector` to select a subset of examples that will be most informative for the Language Model. This will help you generate a prompt that is more likely to generate a good response. + +Below, we'll use the `LengthBasedExampleSelector`, which selects examples based on the length of the input. This is useful when you are worried about constructing a prompt that will go over the length of the context window. For longer inputs, it will select fewer examples to include, while for shorter inputs it will select more. + +We'll continue with the example from the previous section, but this time we'll use the `LengthBasedExampleSelector` to select the examples. + +```python +from langchain.prompts.example_selector import LengthBasedExampleSelector + + +# These are a lot of examples of a pretend task of creating antonyms. +examples = [ + {"input": "happy", "output": "sad"}, + {"input": "tall", "output": "short"}, + {"input": "energetic", "output": "lethargic"}, + {"input": "sunny", "output": "gloomy"}, + {"input": "windy", "output": "calm"}, +] + +# We'll use the `LengthBasedExampleSelector` to select the examples. +example_selector = LengthBasedExampleSelector( + # These are the examples is has available to choose from. + examples=examples, + # This is the PromptTemplate being used to format the examples. + example_prompt=example_prompt, + # This is the maximum length that the formatted examples should be. + # Length is measured by the get_text_length function below. + max_length=25, +) + +# We can now use the `example_selector` to create a `FewShotPromptTemplate`. +few_shot_prompt = FewShotPromptTemplate( + # We provide an ExampleSelector instead of examples. + example_selector=example_selector, + example_prompt=example_prompt, + prefix="Give the antonym of every input", + suffix="Word: {input}\nAntonym:", + input_variables=["input"], + example_separator="\n\n", +) + +# We can now generate a prompt using the `format` method. +print(few_shot_prompt.format(input="big")) +# -> Give the antonym of every input +# -> +# -> Word: happy +# -> Antonym: sad +# -> +# -> Word: tall +# -> Antonym: short +# -> +# -> Word: energetic +# -> Antonym: lethargic +# -> +# -> Word: sunny +# -> Antonym: gloomy +# -> +# -> Word: windy +# -> Antonym: calm +# -> +# -> Word: big +# -> Antonym: +``` + +In contrast, if we provide a very long input, the `LengthBasedExampleSelector` will select fewer examples to include in the prompt. + +```python +long_string = "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else" +print(dynamic_prompt.format(adjective=long_string)) +# -> Give the antonym of every input + +# -> Word: happy +# -> Antonym: sad +# -> +# -> Word: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else +# -> Antonym: +``` + + +LangChain comes with a few example selectors that you can use. For more details on how to use them, see [Example Selectors](examples/example_selectors.ipynb). + +You can create custom example selectors that select examples based on any criteria you want. For more details on how to do this, see [Creating a custom example selector](examples/custom_example_selector.ipynb). \ No newline at end of file diff --git a/docs/modules/prompts/how_to_guides.rst b/docs/modules/prompts/how_to_guides.rst new file mode 100644 index 00000000..a9a56fb1 --- /dev/null +++ b/docs/modules/prompts/how_to_guides.rst @@ -0,0 +1,34 @@ +How-To Guides +============= + +If you're new to the library, you may want to start with the `Quickstart `_. + +The user guide here shows more advanced workflows and how to use the library in different ways. + +`Custom Prompt Template `_: How to create and use a custom PromptTemplate, the logic that decides how input variables get formatted into a prompt. + +`Custom Example Selector `_: How to create and use a custom ExampleSelector (the class responsible for choosing which examples to use in a prompt). + +`Few Shot Prompt Templates `_: How to include examples in the prompt. + +`Prompt Serialization `_: A walkthrough of how to serialize prompts to and from disk. + +`Few Shot Prompt Examples `_: Examples of Few Shot Prompt Templates. + + + + + + + + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + examples/custom_prompt_template.md + examples/custom_example_selector.md + examples/few_shot_examples.ipynb + examples/prompt_serialization.ipynb + examples/few_shot_examples_data.ipynb diff --git a/docs/modules/prompts/key_concepts.md b/docs/modules/prompts/key_concepts.md new file mode 100644 index 00000000..5a953089 --- /dev/null +++ b/docs/modules/prompts/key_concepts.md @@ -0,0 +1,76 @@ +# Key Concepts + +## Prompts + +A prompt is the input to a language model. It is a string of text that is used to generate a response from the language model. + + +## Prompt Templates + +`PromptTemplates` are a way to create prompts in a reproducible way. They contain a template string, and a set of input variables. The template string can be formatted with the input variables to generate a prompt. The template string often contains instructions to the language model, a few shot examples, and a question to the language model. + +`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. + +To learn more about `PromptTemplates`, see [Prompt Templates](getting_started.md). + +As an example, consider the following template string: + +```python +""" +Predict the capital of a country. + +Country: {country} +Capital: +""" +``` + + +### Input Variables + +Input variables are the variables that are used to fill in the template string. In the example above, the input variable is `country`. + +Given an input variable, the `PromptTemplate` can generate a prompt by filling in the template string with the input variable. For example, if the input variable is `United States`, the template string can be formatted to generate the following prompt: + +```python +""" +Predict the capital of a country. + +Country: United States +Capital: +""" +``` + +## Few Shot Examples + +Few shot examples refer to in-context examples that are provided to a language model as part of a prompt. The examples can be used to help the language model understand the context of the prompt, and as a result generate a better response. Few shot examples can contain both positive and negative examples about the expected response. + +Below, we list out some few shot examples that may be relevant for the task of predicting the capital of a country. + +``` +Country: United States +Capital: Washington, D.C. + +Country: Canada +Capital: Ottawa +``` + +To learn more about how to provide few shot examples, see [Few Shot Examples](examples/few_shot_examples.ipynb). + + + +## Example selection + +If there are multiple examples that are relevant to a prompt, it is important to select the most relevant examples. Generally, the quality of the response from the LLM can be significantly improved by selecting the most relevant examples. This is because the language model will be able to better understand the context of the prompt, and also potentially learn failure modes to avoid. + +To help the user with selecting the most relevant examples, we provide example selectors that select the most relevant based on different criteria, such as length, semantic similarity, etc. The example selector takes in a list of examples and returns a list of selected examples, formatted as a string. The user can also provide their own example selector. To learn more about example selectors, see [Example Selection](example_selection.md). + + + +## Serialization + +To make it easy to share `PromptTemplates`, we provide a `serialize` method that returns a JSON string. The JSON string can be saved to a file, and then loaded back into a `PromptTemplate` using the `deserialize` method. This allows users to share `PromptTemplates` with others, and also to save them for later use. + +To learn more about serialization, see [Serialization](examples/prompt_serialization.ipynb). + diff --git a/docs/examples/state_of_the_union.txt b/docs/modules/state_of_the_union.txt similarity index 100% rename from docs/examples/state_of_the_union.txt rename to docs/modules/state_of_the_union.txt diff --git a/docs/modules/utils.rst b/docs/modules/utils.rst new file mode 100644 index 00000000..f96a62d3 --- /dev/null +++ b/docs/modules/utils.rst @@ -0,0 +1,26 @@ +Utils +========================== + +While LLMs are powerful on their own, they are more powerful when connected with other sources of knowledge or computation. +This section highlights those sources of knowledge or computation, +and goes over how to easily use them from within LangChain. + +The following sections of documentation are provided: + +- `Key Concepts `_: A conceptual guide going over the various types of utils. + +- `How-To Guides `_: A collection of how-to guides. These highlight how to use various types of utils. + +- `Reference `_: API reference documentation for all Util classes. + + + +.. toctree:: + :maxdepth: 1 + :caption: Utils + :name: Utils + :hidden: + + utils/key_concepts.md + utils/how_to_guides.rst + Reference diff --git a/docs/modules/utils/combine_docs_examples/embeddings.ipynb b/docs/modules/utils/combine_docs_examples/embeddings.ipynb new file mode 100644 index 00000000..cc255911 --- /dev/null +++ b/docs/modules/utils/combine_docs_examples/embeddings.ipynb @@ -0,0 +1,128 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "249b4058", + "metadata": {}, + "source": [ + "# Embeddings\n", + "\n", + "This notebook goes over how to use the Embedding class in LangChain.\n", + "\n", + "The Embedding class is a class designed for interfacing with embeddings. There are lots of Embedding providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them.\n", + "\n", + "Embeddings create a vector representation of a piece of text. This is useful because it means we can think about text in the vector space, and do things like semantic search where we look for pieces of text that are most similar in the vector space.\n", + "\n", + "The base Embedding class in LangChain exposes two methods: `embed_documents` and `embed_query`. The largest difference is that these two methods have different interfaces: one works over multiple documents, while the other works over a single document. Besides this, another reason for having these as two separate methods is that some embedding providers have different embedding methods for documents (to be searched over) vs queries (the search query itself)." + ] + }, + { + "cell_type": "markdown", + "id": "278b6c63", + "metadata": {}, + "source": [ + "## OpenAI\n", + "\n", + "Let's load the OpenAI Embedding class." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "0be1af71", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.embeddings import OpenAIEmbeddings" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2c66e5da", + "metadata": {}, + "outputs": [], + "source": [ + "embeddings = OpenAIEmbeddings()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "01370375", + "metadata": {}, + "outputs": [], + "source": [ + "text = \"This is a test document.\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "bfb6142c", + "metadata": {}, + "outputs": [], + "source": [ + "query_result = embeddings.embed_query(text)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "0356c3b7", + "metadata": {}, + "outputs": [], + "source": [ + "doc_result = embeddings.embed_documents([text])" + ] + }, + { + "cell_type": "markdown", + "id": "42f76e43", + "metadata": {}, + "source": [ + "## Cohere\n", + "\n", + "TODO: add documentation for Cohere embeddings." + ] + }, + { + "cell_type": "markdown", + "id": "ed47bb62", + "metadata": {}, + "source": [ + "## Hugging Face Hub\n", + "TODO: add documentation for Hugging Face Hub embeddings." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff9be586", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/examples/data_augmented_generation/hyde.ipynb b/docs/modules/utils/combine_docs_examples/hyde.ipynb similarity index 98% rename from docs/examples/data_augmented_generation/hyde.ipynb rename to docs/modules/utils/combine_docs_examples/hyde.ipynb index bd63cc49..bf4789e2 100644 --- a/docs/examples/data_augmented_generation/hyde.ipynb +++ b/docs/modules/utils/combine_docs_examples/hyde.ipynb @@ -162,7 +162,7 @@ "from langchain.text_splitter import CharacterTextSplitter\n", "from langchain.vectorstores import FAISS\n", "\n", - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)" @@ -234,7 +234,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/data_augmented_generation/textsplitter.ipynb b/docs/modules/utils/combine_docs_examples/textsplitter.ipynb similarity index 53% rename from docs/examples/data_augmented_generation/textsplitter.ipynb rename to docs/modules/utils/combine_docs_examples/textsplitter.ipynb index 718faf36..ad6dafdd 100644 --- a/docs/examples/data_augmented_generation/textsplitter.ipynb +++ b/docs/modules/utils/combine_docs_examples/textsplitter.ipynb @@ -26,7 +26,7 @@ "source": [ "from langchain.text_splitter import CharacterTextSplitter, NLTKTextSplitter, SpacyTextSplitter\n", "# This is a long document we can split up.\n", - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()" ] }, @@ -62,19 +62,32 @@ "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. \\n\\nGroups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland. '" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n", + "\n", + "Last year COVID-19 kept us apart. This year we are finally together again. \n", + "\n", + "Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n", + "\n", + "With a duty to one another to the American people to the Constitution. \n", + "\n", + "And with an unwavering resolve that freedom will always triumph over tyranny. \n", + "\n", + "Six days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n", + "\n", + "He thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n", + "\n", + "He met the Ukrainian people. \n", + "\n", + "From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. \n" + ] } ], "source": [ "texts = text_splitter.split_text(state_of_the_union)\n", - "texts[0]" + "print(texts[0])" ] }, { @@ -90,24 +103,21 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "4cd16222", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "Document(page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. ', lookup_str='', metadata={}, lookup_index=0)" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. ' lookup_str='' metadata={} lookup_index=0\n" + ] } ], "source": [ "documents = text_splitter.create_documents([state_of_the_union, state_of_the_union])\n", - "documents[0]" + "print(documents[0])" ] }, { @@ -120,25 +130,22 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "4a47515a", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "Document(page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. ', lookup_str='', metadata={'document': 1}, lookup_index=0)" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. ' lookup_str='' metadata={'document': 1} lookup_index=0\n" + ] } ], "source": [ "metadatas = [{\"document\": 1}, {\"document\": 2}]\n", "documents = text_splitter.create_documents([state_of_the_union, state_of_the_union], metadatas=metadatas)\n", - "documents[0]" + "print(documents[0])" ] }, { @@ -152,18 +159,10 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "id": "a8ce51d5", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n" - ] - } - ], + "outputs": [], "source": [ "from transformers import GPT2TokenizerFast\n", "\n", @@ -172,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "ca5e72c0", "metadata": {}, "outputs": [], @@ -183,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "id": "37cdfbeb", "metadata": {}, "outputs": [ @@ -197,9 +196,7 @@ "\n", "Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n", "\n", - "With a duty to one another to the American people to the Constitution. \n", - "\n", - "And with an unwavering resolve that freedom will always triumph over tyranny. \n" + "With a duty to one another to the American people to the Constitution. \n" ] } ], @@ -218,7 +215,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "id": "825f7c0a", "metadata": {}, "outputs": [], @@ -229,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "id": "ae35d165", "metadata": {}, "outputs": [ @@ -243,9 +240,7 @@ "\n", "Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n", "\n", - "With a duty to one another to the American people to the Constitution. \n", - "\n", - "And with an unwavering resolve that freedom will always triumph over tyranny. \n" + "With a duty to one another to the American people to the Constitution. \n" ] } ], @@ -264,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 11, "id": "20fa9c23", "metadata": {}, "outputs": [], @@ -274,24 +269,53 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 12, "id": "5ea10835", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman.\\n\\nMembers of Congress and the Cabinet.\\n\\nJustices of the Supreme Court.\\n\\nMy fellow Americans.\\n\\nLast year COVID-19 kept us apart.\\n\\nThis year we are finally together again.\\n\\nTonight, we meet as Democrats Republicans and Independents.\\n\\nBut most importantly as Americans.\\n\\nWith a duty to one another to the American people to the Constitution.\\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny.\\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways.\\n\\nBut he badly miscalculated.\\n\\nHe thought he could roll into Ukraine and the world would roll over.\\n\\nInstead he met a wall of strength he never imagined.\\n\\nHe met the Ukrainian people.\\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\\n\\nGroups of citizens blocking tanks with their bodies.\\n\\nEveryone from students to retirees teachers turned soldiers defending their homeland.'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Madam Speaker, Madam Vice President, our First Lady and Second Gentleman.\n", + "\n", + "Members of Congress and the Cabinet.\n", + "\n", + "Justices of the Supreme Court.\n", + "\n", + "My fellow Americans.\n", + "\n", + "Last year COVID-19 kept us apart.\n", + "\n", + "This year we are finally together again.\n", + "\n", + "Tonight, we meet as Democrats Republicans and Independents.\n", + "\n", + "But most importantly as Americans.\n", + "\n", + "With a duty to one another to the American people to the Constitution.\n", + "\n", + "And with an unwavering resolve that freedom will always triumph over tyranny.\n", + "\n", + "Six days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways.\n", + "\n", + "But he badly miscalculated.\n", + "\n", + "He thought he could roll into Ukraine and the world would roll over.\n", + "\n", + "Instead he met a wall of strength he never imagined.\n", + "\n", + "He met the Ukrainian people.\n", + "\n", + "From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\n", + "\n", + "Groups of citizens blocking tanks with their bodies.\n" + ] } ], "source": [ "texts = text_splitter.split_text(state_of_the_union)\n", - "texts[0]" + "print(texts[0])" ] }, { @@ -305,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 13, "id": "f9cc9dfc", "metadata": {}, "outputs": [], @@ -315,24 +339,85 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 14, "id": "cef2b29e", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman.\\n\\nMembers of Congress and the Cabinet.\\n\\nJustices of the Supreme Court.\\n\\nMy fellow Americans. \\n\\n\\n\\nLast year COVID-19 kept us apart.\\n\\nThis year we are finally together again.\\n\\n\\n\\n\\n\\nTonight, we meet as Democrats Republicans and Independents.\\n\\nBut most importantly as Americans.\\n\\n\\n\\n\\n\\nWith a duty to one another to the American people to the Constitution. \\n\\n\\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny.\\n\\n\\n\\n\\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways.\\n\\nBut he badly miscalculated.\\n\\n\\n\\n\\n\\nHe thought he could roll into Ukraine and the world would roll over.\\n\\nInstead he met a wall of strength he never imagined.\\n\\n\\n\\n\\n\\nHe met the Ukrainian people.\\n\\n\\n\\n\\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\\n\\n\\n\\n\\n\\nGroups of citizens blocking tanks with their bodies.\\n\\nEveryone from students to retirees teachers turned soldiers defending their homeland.'" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Madam Speaker, Madam Vice President, our First Lady and Second Gentleman.\n", + "\n", + "Members of Congress and the Cabinet.\n", + "\n", + "Justices of the Supreme Court.\n", + "\n", + "My fellow Americans. \n", + "\n", + "\n", + "\n", + "Last year COVID-19 kept us apart.\n", + "\n", + "This year we are finally together again.\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "Tonight, we meet as Democrats Republicans and Independents.\n", + "\n", + "But most importantly as Americans.\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "With a duty to one another to the American people to the Constitution. \n", + "\n", + "\n", + "\n", + "And with an unwavering resolve that freedom will always triumph over tyranny.\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "Six days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways.\n", + "\n", + "But he badly miscalculated.\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "He thought he could roll into Ukraine and the world would roll over.\n", + "\n", + "Instead he met a wall of strength he never imagined.\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "He met the Ukrainian people.\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "Groups of citizens blocking tanks with their bodies.\n" + ] } ], "source": [ "texts = text_splitter.split_text(state_of_the_union)\n", - "texts[0]" + "print(texts[0])" ] }, { @@ -360,7 +445,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/data_augmented_generation/embeddings.ipynb b/docs/modules/utils/combine_docs_examples/vectorstores.ipynb similarity index 96% rename from docs/examples/data_augmented_generation/embeddings.ipynb rename to docs/modules/utils/combine_docs_examples/vectorstores.ipynb index e816a59b..6af9880f 100644 --- a/docs/examples/data_augmented_generation/embeddings.ipynb +++ b/docs/modules/utils/combine_docs_examples/vectorstores.ipynb @@ -9,9 +9,9 @@ } }, "source": [ - "# Embeddings & VectorStores\n", + "# VectorStores\n", "\n", - "This notebook show cases how to use embeddings to create a VectorStore" + "This notebook show cases how to use VectorStores. A key part of working with vectorstores is creating the vector to put in them, which is usually created via embeddings. Therefor, it is recommended that you familiarize yourself with the [embedding notebook](embeddings.ipynb) before diving into this." ] }, { @@ -41,7 +41,7 @@ }, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", @@ -109,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "id": "df4a459c", "metadata": {}, "outputs": [], @@ -119,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "id": "4b480245", "metadata": {}, "outputs": [], @@ -132,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "86aa4cda", "metadata": {}, "outputs": [ @@ -426,7 +426,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/modules/utils/combine_docs_how_to.rst b/docs/modules/utils/combine_docs_how_to.rst new file mode 100644 index 00000000..55a75a9c --- /dev/null +++ b/docs/modules/utils/combine_docs_how_to.rst @@ -0,0 +1,21 @@ +Utilities for working with Documents +==================================== + +There are a lot of different utilities that LangChain provides integrations for +These guides go over how to use them. +The utilities here are all utilities that make it easier to work with documents. + +`Text Splitters `_: A walkthrough of how to split large documents up into smaller, more manageable pieces of text. + +`VectorStores `_: A walkthrough of vectorstore functionalities, and different types of vectorstores, that LangChain supports. + +`Embeddings `_: A walkthrough of embedding functionalities, and different types of embeddings, that LangChain supports. + +`HyDE `_: How to use Hypothetical Document Embeddings, a novel way of constructing embeddings for document retrieval systems. + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + combine_docs_examples/* diff --git a/docs/modules/utils/examples/bash.ipynb b/docs/modules/utils/examples/bash.ipynb new file mode 100644 index 00000000..7e7337b5 --- /dev/null +++ b/docs/modules/utils/examples/bash.ipynb @@ -0,0 +1,85 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "8f210ec3", + "metadata": {}, + "source": [ + "# Bash\n", + "It can often be useful to have an LLM generate bash commands, and then run them. A common use case this is for letting it interact with your local file system. We provide an easy util to execute bash commands." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f7b3767b", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.utilities import BashProcess" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "cf1c92f0", + "metadata": {}, + "outputs": [], + "source": [ + "bash = BashProcess()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2fa952fc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bash.ipynb\n", + "google_search.ipynb\n", + "python.ipynb\n", + "requests.ipynb\n", + "serpapi.ipynb\n", + "\n" + ] + } + ], + "source": [ + "print(bash.run(\"ls\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "851fee9f", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/examples/chains/google_search.ipynb b/docs/modules/utils/examples/google_search.ipynb similarity index 100% rename from docs/examples/chains/google_search.ipynb rename to docs/modules/utils/examples/google_search.ipynb diff --git a/docs/modules/utils/examples/python.ipynb b/docs/modules/utils/examples/python.ipynb new file mode 100644 index 00000000..db2824f2 --- /dev/null +++ b/docs/modules/utils/examples/python.ipynb @@ -0,0 +1,86 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "984a8fca", + "metadata": {}, + "source": [ + "# Python REPL\n", + "\n", + "Sometimes, for complex calculations, rather than have an LLM generate the answer directly, it can be better to have the LLM generate code to calculate the answer, and then run that code to get the answer. In order to easily do that, we provide a simple Python REPL to execute commands in.\n", + "\n", + "This interface will only return things that are printed - therefor, if you want to use it to calculate an answer, make sure to have it print out the answer." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f6593089", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.utilities import PythonREPL" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6f21f0a4", + "metadata": {}, + "outputs": [], + "source": [ + "python_repl = PythonREPL()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7ebbbaea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'2\\n'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "python_repl.run(\"print(1+1)\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "54fc1f03", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/modules/utils/examples/requests.ipynb b/docs/modules/utils/examples/requests.ipynb new file mode 100644 index 00000000..564034a3 --- /dev/null +++ b/docs/modules/utils/examples/requests.ipynb @@ -0,0 +1,84 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f34864b5", + "metadata": {}, + "source": [ + "# Requests\n", + "\n", + "The web contains a lot of information that LLMs do not have access to. In order to easily let LLMs interact with that information, we provide a wrapper around the Python Requests module that takes in a URL and fetches data from that URL." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "81aae09e", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.utilities import RequestsWrapper" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "fd210142", + "metadata": {}, + "outputs": [], + "source": [ + "requests = RequestsWrapper()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "29a77bb2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Google

\"Seasonal
 

Advanced search

© 2022 - Privacy - Terms

'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "requests.run(\"https://www.google.com\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f27ee3d", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/modules/utils/examples/serpapi.ipynb b/docs/modules/utils/examples/serpapi.ipynb new file mode 100644 index 00000000..a9b763a2 --- /dev/null +++ b/docs/modules/utils/examples/serpapi.ipynb @@ -0,0 +1,84 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "dc23c48e", + "metadata": {}, + "source": [ + "# SerpAPI\n", + "\n", + "This notebook goes over how to use the SerpAPI component to search the web." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "54bf5afd", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.utilities import SerpAPIWrapper" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "31f8f382", + "metadata": {}, + "outputs": [], + "source": [ + "search = SerpAPIWrapper()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "25ce0225", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Barack Hussein Obama II'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "search.run(\"Obama's first name?\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27f5959a", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/modules/utils/generic_how_to.rst b/docs/modules/utils/generic_how_to.rst new file mode 100644 index 00000000..34b37742 --- /dev/null +++ b/docs/modules/utils/generic_how_to.rst @@ -0,0 +1,24 @@ +Generic Utilities +================= + +There are a lot of different utilities that LangChain provides integrations for +These guides go over how to use them. +The utilities listed here are all generic utilities. + +`Bash `_: How to use a bash wrapper to execute bash commands. + +`Python REPL `_: How to use a Python wrapper to execute python commands. + +`Requests `_: How to use a requests wrapper to interact with the web. + +`Google Search `_: How to use the google search wrapper to search the web. + +`SerpAPI `_: How to use the SerpAPI wrapper to search the web. + + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + examples/* diff --git a/docs/modules/utils/how_to_guides.rst b/docs/modules/utils/how_to_guides.rst new file mode 100644 index 00000000..8c6dad1a --- /dev/null +++ b/docs/modules/utils/how_to_guides.rst @@ -0,0 +1,17 @@ +How-To Guides +============= + +There are a lot of different utilities that LangChain provides integrations for +These guides go over how to use them. +These can largely be grouped into two categories: + +1. `Generic Utilities `_: Generic utilities, including search, python REPLs, etc. +2. `Utilities for working with Documents `_: Utilities aimed at making it easy to work with documents (text splitting, embeddings, vectorstores, etc). + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + generic_how_to.rst + combine_docs_how_to.rst diff --git a/docs/modules/utils/key_concepts.md b/docs/modules/utils/key_concepts.md new file mode 100644 index 00000000..9e27ee96 --- /dev/null +++ b/docs/modules/utils/key_concepts.md @@ -0,0 +1,38 @@ +# Key Concepts + +## Text Splitter +This class is responsible for splitting long pieces of text into smaller components. +It contains different ways for splitting text (on characters, using Spacy, etc) +as well as different ways for measuring length (token based, character based, etc). + +## 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 embeddings of documents in vector form. +They expose a method for passing in a string and finding similar documents. + +## Python REPL +Sometimes, for complex calculations, rather than have an LLM generate the answer directly, +it can be better to have the LLM generate code to calculate the answer, and then run that code to get the answer. +In order to easily do that, we provide a simple Python REPL to execute commands in. +This interface will only return things that are printed - +therefor, if you want to use it to calculate an answer, make sure to have it print out the answer. + +## Bash +It can often be useful to have an LLM generate bash commands, and then run them. +A common use case this is for letting it interact with your local file system. +We provide an easy component to execute bash commands. + +## Requests Wrapper +The web contains a lot of information that LLMs do not have access to. +In order to easily let LLMs interact with that information, +we provide a wrapper around the Python Requests module that takes in a URL and fetches data from that URL. + +## Google Search +This uses the official Google Search API to look up information on the web. + +## SerpAPI +This uses SerpAPI, a third party search API engine, to interact with Google Search. diff --git a/docs/reference.rst b/docs/reference.rst new file mode 100644 index 00000000..6c9b80ab --- /dev/null +++ b/docs/reference.rst @@ -0,0 +1,14 @@ +API References +========================== + +All of LangChain's reference documentation, in one place. +Full documentation on all methods, classes, and APIs in LangChain. + +.. toctree:: + :maxdepth: 1 + + reference/prompts.rst + LLMs + reference/utils.rst + Chains + Agents diff --git a/docs/reference/chains.rst b/docs/reference/chains.rst deleted file mode 100644 index 717b9ada..00000000 --- a/docs/reference/chains.rst +++ /dev/null @@ -1,21 +0,0 @@ -Chains -============== - -One big part of chains is all the utilities that can be used as part of them. -Here is some reference documentation for the utilities natively supported by LangChain. - -.. toctree:: - :maxdepth: 1 - :glob: - - modules/python - modules/serpapi - - -With those utilities in mind, here are the reference docs for all the chains in LangChain. - -.. toctree:: - :maxdepth: 1 - :glob: - - modules/chains diff --git a/docs/reference/data_augmented_generation.rst b/docs/reference/data_augmented_generation.rst deleted file mode 100644 index 96dfcd6c..00000000 --- a/docs/reference/data_augmented_generation.rst +++ /dev/null @@ -1,13 +0,0 @@ -Data Augmented Generation -========================= - -The reference guides here all relate to components necessary for data augmented generation. - -.. toctree:: - :maxdepth: 1 - :glob: - - modules/text_splitter - modules/docstore - modules/embeddings - modules/vectorstore diff --git a/docs/reference/installation.md b/docs/reference/installation.md index 2e7fb797..94d8c7ef 100644 --- a/docs/reference/installation.md +++ b/docs/reference/installation.md @@ -1,4 +1,6 @@ -# Installation Options +# Installation + +## Official Releases LangChain is available on PyPi, so to it is easily installable with: @@ -27,4 +29,12 @@ Note that if you are using `zsh`, you'll need to quote square brackets when pass ``` pip install 'langchain[all]' -``` \ No newline at end of file +``` + +## Installing from source + +If you want to install from source, you can do so by cloning the repo and running: + +``` +pip install -e . +``` diff --git a/docs/reference/integrations.md b/docs/reference/integrations.md index bf4799b7..3119fe65 100644 --- a/docs/reference/integrations.md +++ b/docs/reference/integrations.md @@ -1,4 +1,4 @@ -# Integration Reference +# Integrations Besides the installation of this python package, you will also need to install packages and set environment variables depending on which chains you want to use. diff --git a/docs/reference/modules/agents.rst b/docs/reference/modules/agents.rst index 7c829bfe..e7586030 100644 --- a/docs/reference/modules/agents.rst +++ b/docs/reference/modules/agents.rst @@ -1,4 +1,4 @@ -:mod:`langchain.agents` +Agents =============================== .. automodule:: langchain.agents diff --git a/docs/reference/modules/chains.rst b/docs/reference/modules/chains.rst index b3bc7781..5e4fd496 100644 --- a/docs/reference/modules/chains.rst +++ b/docs/reference/modules/chains.rst @@ -1,4 +1,4 @@ -:mod:`langchain.chains` +Chains ======================= .. automodule:: langchain.chains diff --git a/docs/reference/modules/docstore.rst b/docs/reference/modules/docstore.rst index d34095be..d38de92d 100644 --- a/docs/reference/modules/docstore.rst +++ b/docs/reference/modules/docstore.rst @@ -1,4 +1,4 @@ -:mod:`langchain.docstore` +Docstore ============================= .. automodule:: langchain.docstore diff --git a/docs/reference/modules/embeddings.rst b/docs/reference/modules/embeddings.rst index ddba956f..4b8ecedb 100644 --- a/docs/reference/modules/embeddings.rst +++ b/docs/reference/modules/embeddings.rst @@ -1,4 +1,4 @@ -:mod:`langchain.embeddings` +Embeddings =========================== .. automodule:: langchain.embeddings diff --git a/docs/reference/modules/example_selector.rst b/docs/reference/modules/example_selector.rst index 261d356d..ae02d764 100644 --- a/docs/reference/modules/example_selector.rst +++ b/docs/reference/modules/example_selector.rst @@ -1,4 +1,4 @@ -:mod:`langchain.prompts.example_selector` +Example Selector ========================================= .. automodule:: langchain.prompts.example_selector diff --git a/docs/reference/modules/llms.rst b/docs/reference/modules/llms.rst index 87c5a05c..d62a39e1 100644 --- a/docs/reference/modules/llms.rst +++ b/docs/reference/modules/llms.rst @@ -1,6 +1,7 @@ -:mod:`langchain.llms` +LLMs ======================= .. automodule:: langchain.llms :members: + :inherited-members: :special-members: __call__ diff --git a/docs/reference/modules/prompt.rst b/docs/reference/modules/prompt.rst index e95bab0a..65d3dcb2 100644 --- a/docs/reference/modules/prompt.rst +++ b/docs/reference/modules/prompt.rst @@ -1,4 +1,4 @@ -:mod:`langchain.prompts` +PromptTemplates ======================== .. automodule:: langchain.prompts diff --git a/docs/reference/modules/python.rst b/docs/reference/modules/python.rst index d5b7a17a..a6d6c4c0 100644 --- a/docs/reference/modules/python.rst +++ b/docs/reference/modules/python.rst @@ -1,4 +1,4 @@ -:mod:`langchain.python` +Python REPL ============================= .. automodule:: langchain.python diff --git a/docs/reference/modules/serpapi.rst b/docs/reference/modules/serpapi.rst index 94580174..9cac8dca 100644 --- a/docs/reference/modules/serpapi.rst +++ b/docs/reference/modules/serpapi.rst @@ -1,4 +1,4 @@ -:mod:`langchain.serpapi` +SerpAPI ============================= .. automodule:: langchain.serpapi diff --git a/docs/reference/modules/text_splitter.rst b/docs/reference/modules/text_splitter.rst index 1df38dd1..6b4cb967 100644 --- a/docs/reference/modules/text_splitter.rst +++ b/docs/reference/modules/text_splitter.rst @@ -1,4 +1,4 @@ -:mod:`langchain.text_splitter` +Text Splitter ============================== .. automodule:: langchain.text_splitter diff --git a/docs/reference/modules/vectorstore.rst b/docs/reference/modules/vectorstore.rst index 04ae7606..e5ed525b 100644 --- a/docs/reference/modules/vectorstore.rst +++ b/docs/reference/modules/vectorstore.rst @@ -1,4 +1,4 @@ -:mod:`langchain.vectorstores` +VectorStores ============================= .. automodule:: langchain.vectorstores diff --git a/docs/reference/prompts.rst b/docs/reference/prompts.rst index a7bec086..ed1e28a3 100644 --- a/docs/reference/prompts.rst +++ b/docs/reference/prompts.rst @@ -1,7 +1,7 @@ -LLMs & Prompts +Prompts ============== -The reference guides here all relate to objects for working with LLMs and Prompts. +The reference guides here all relate to objects for working with Prompts. .. toctree:: :maxdepth: 1 @@ -9,4 +9,3 @@ The reference guides here all relate to objects for working with LLMs and Prompt modules/prompt modules/example_selector - modules/llms diff --git a/docs/reference/utils.rst b/docs/reference/utils.rst new file mode 100644 index 00000000..c115ef5b --- /dev/null +++ b/docs/reference/utils.rst @@ -0,0 +1,26 @@ +Utilities +============== + +There are a lot of different utilities that LangChain provides integrations for +These guides go over how to use them. +These can largely be grouped into two categories: generic utilities, and then utilities for working with larger text documents. + + +.. toctree:: + :maxdepth: 1 + :glob: + :caption: Generic Utilities + + modules/python + modules/serpapi + + +.. toctree:: + :maxdepth: 1 + :glob: + :caption: Utilities for working with Documents + + modules/docstore + modules/text_splitter + modules/embeddings + modules/vectorstore diff --git a/docs/requirements.txt b/docs/requirements.txt index fd4a7a91..aace0a61 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -3,7 +3,9 @@ myst_parser nbsphinx==0.8.9 sphinx==4.5.0 sphinx-autobuild==2021.3.14 +sphinx_book_theme sphinx_rtd_theme==1.0.0 sphinx-typlog-theme==0.8.0 sphinx-panels toml +myst_nb diff --git a/docs/use_cases/agents.md b/docs/use_cases/agents.md new file mode 100644 index 00000000..d398add9 --- /dev/null +++ b/docs/use_cases/agents.md @@ -0,0 +1,12 @@ +# Agents + +Agents are systems that use a language model to interact with other tools. +These can be used to do more grounded question/answering, interact with APIs, or even take actions. +These agents can be used to power the next generation of personal assistants - +systems that intelligently understand what you mean, and then can take actions to help you accomplish your goal. + +Agents are a core use of LangChain - so much so that there is a whole module dedicated to them. +Therefor, we recommend that you check out that documentation for detailed instruction on how to work +with them. + +- [Agent Documentation](../modules/agents.rst) diff --git a/docs/use_cases/chatbots.md b/docs/use_cases/chatbots.md new file mode 100644 index 00000000..1a277533 --- /dev/null +++ b/docs/use_cases/chatbots.md @@ -0,0 +1,14 @@ +# Chatbots + +Since language models are good at producing text, that makes them ideal for creating chatbots. +Aside from the base prompts/LLMs, an important concept to know for Chatbots is `memory`. +Most chat based applications rely on remembering what happened in previous interactions, which is `memory` is designed to help with. + +The following resources exist: +- [ChatGPT Clone](../modules/memory/examples/chatgpt_clone.ipynb): A notebook walking through how to recreate a ChatGPT-like experience with LangChain. +- [Conversation Memory](../modules/memory/getting_started.ipynb): A notebook walking through how to use different types of conversational memory. + + +Additional related resources include: +- [Memory Key Concepts](../modules/memory/key_concepts.md): Explanation of key concepts related to memory. +- [Memory Examples](../modules/memory/how_to_guides.rst): A collection of how-to examples for working with memory. diff --git a/docs/explanation/combine_docs.md b/docs/use_cases/combine_docs.md similarity index 72% rename from docs/explanation/combine_docs.md rename to docs/use_cases/combine_docs.md index d3b7af26..07884f7a 100644 --- a/docs/explanation/combine_docs.md +++ b/docs/use_cases/combine_docs.md @@ -61,7 +61,7 @@ small enough chunks. LangChain provides some utilities to help with splitting up larger pieces of data. This comes in the form of the TextSplitter class. The class takes in a document and splits it up into chunks, with several parameters that control the size of the chunks as well as the overlap in the chunks (important for maintaining context). -See [this walkthrough](../examples/data_augmented_generation/textsplitter.ipynb) for more information. +See [this walkthrough](../modules/utils/combine_docs_examples/textsplitter.ipynb) for more information. ### Relevant Documents A second large issue related fetching data is to make sure you are not fetching too many documents, and are only fetching @@ -81,39 +81,8 @@ for language models. ## Augmenting So you've fetched your relevant data - 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. +For a detailed overview of the different ways of doing so, and the tradeoffs between them, please see +[this documentation](../modules/chains/combine_docs.md) ## Use Cases LangChain supports the above three methods of augmenting LLMs with external data. @@ -123,6 +92,5 @@ It is important to note that a large part of these implementations is the prompt that are used. We provide default prompts for all three use cases, but these can be configured. This is in case you discover a prompt that works better for your specific application. -- [Question-Answering With Sources](../examples/data_augmented_generation/qa_with_sources.ipynb) -- [Question-Answering](../examples/data_augmented_generation/question_answering.ipynb) -- [Summarization](../examples/data_augmented_generation/summarize.ipynb) +- [Question-Answering](question_answering.md) +- [Summarization](summarization.md) diff --git a/docs/use_cases/evaluation.rst b/docs/use_cases/evaluation.rst new file mode 100644 index 00000000..9c6ebfcf --- /dev/null +++ b/docs/use_cases/evaluation.rst @@ -0,0 +1,20 @@ +Evaluation +============== + +Generative models are notoriously hard to evaluate with traditional metrics. One new way of evaluating them is using language models themselves to do the evaluation. LangChain provides some prompts/chains for assisting in this. + +The examples here all highlight how to use language models to assist in evaluation of themselves. + +`Question Answering `_: An overview of LLMs aimed at evaluating question answering systems in general. + +`Data Augmented Question Answering `_: 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 `_: Covers an example of loading and using a dataset from Hugging Face for evaluation. + + +.. toctree:: + :maxdepth: 1 + :glob: + :hidden: + + evaluation/* diff --git a/docs/examples/evaluation/data_augmented_question_answering.ipynb b/docs/use_cases/evaluation/data_augmented_question_answering.ipynb similarity index 59% rename from docs/examples/evaluation/data_augmented_question_answering.ipynb rename to docs/use_cases/evaluation/data_augmented_question_answering.ipynb index f06f1f8a..146d1d9b 100644 --- a/docs/examples/evaluation/data_augmented_question_answering.ipynb +++ b/docs/use_cases/evaluation/data_augmented_question_answering.ipynb @@ -33,7 +33,7 @@ "metadata": {}, "outputs": [], "source": [ - "with open('../state_of_the_union.txt') as f:\n", + "with open('../../modules/state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", @@ -106,16 +106,16 @@ { "data": { "text/plain": [ - "[{'query': \"Who did Russia's Vladimir Putin miscalculate when he attempted to shake the foundations of the free world?\",\n", - " 'answer': 'Putin miscalculated the Ukrainian people.'},\n", - " {'query': 'What did President Zelenskyy say in his speech to the European Parliament?',\n", - " 'answer': '\"Light will win over darkness.\"'},\n", - " {'query': 'What countries joined the coalition to confront Putin?',\n", - " 'answer': 'France, Germany, Italy, the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland.'},\n", - " {'query': 'What measures is the US taking to punish Russia and its oligarchs?',\n", - " 'answer': \"The US is enforcing powerful economic sanctions, cutting off Russia's largest banks from the international financial system, preventing the Russian Ruble from being defended, choking off Russia's access to technology, and joining with European allies to seize yachts, luxury apartments, and private jets.\"},\n", - " {'query': 'What is the total amount of direct assistance being provided to Ukraine?',\n", - " 'answer': '$1 Billion'}]" + "[{'query': 'What did Vladimir Putin miscalculate when he sought to shake the foundations of the free world? ',\n", + " 'answer': 'He miscalculated that the world would roll over and that he could roll into Ukraine without facing resistance.'},\n", + " {'query': 'What is the purpose of NATO?',\n", + " 'answer': 'The purpose of NATO is to secure peace and stability in Europe after World War 2.'},\n", + " {'query': \"What did the author do to prepare for Putin's attack on Ukraine?\",\n", + " 'answer': \"The author spent months building a coalition of freedom-loving nations from Europe and the Americas to Asia and Africa to confront Putin, shared with the world in advance what they knew Putin was planning, and countered Russia's lies with truth.\"},\n", + " {'query': 'What are the US and its allies doing to isolate Russia from the world?',\n", + " 'answer': \"Enforcing powerful economic sanctions, cutting off Russia's largest banks from the international financial system, preventing Russia's central bank from defending the Russian Ruble, choking off Russia's access to technology, and joining with European allies to find and seize assets of Russian oligarchs.\"},\n", + " {'query': 'How much direct assistance is the U.S. providing to Ukraine?',\n", + " 'answer': 'The U.S. is providing more than $1 Billion in direct assistance to Ukraine.'}]" ] }, "execution_count": 6, @@ -190,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "id": "32fac2dc", "metadata": {}, "outputs": [ @@ -201,43 +201,44 @@ "Example 0:\n", "Question: What did the president say about Ketanji Brown Jackson\n", "Real Answer: He praised her legal ability and said he nominated her for the supreme court.\n", - "Predicted Answer: The president said that Ketanji Brown Jackson is one of the nation's top legal minds and is a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He said she is a consensus builder and has received a broad range of support since being nominated.\n", + "Predicted Answer: The president said that Ketanji Brown Jackson is one of the nation's top legal minds and that she will continue Justice Breyer's legacy of excellence.\n", "Predicted Grade: CORRECT\n", "\n", "Example 1:\n", "Question: What did the president say about Michael Jackson\n", "Real Answer: Nothing\n", - "Predicted Answer: The president did not mention Michael Jackson.\n", + "Predicted Answer: \n", + "The president did not mention Michael Jackson in this context.\n", "Predicted Grade: CORRECT\n", "\n", "Example 2:\n", - "Question: Who did Russia's Vladimir Putin miscalculate when he attempted to shake the foundations of the free world?\n", - "Real Answer: Putin miscalculated the Ukrainian people.\n", - "Predicted Answer: Putin miscalculated the strength of the Ukrainian people, the support of freedom-loving nations from Europe and the Americas to Asia and Africa, and the resolve of the United States and its allies.\n", + "Question: What did Vladimir Putin miscalculate when he sought to shake the foundations of the free world? \n", + "Real Answer: He miscalculated that the world would roll over and that he could roll into Ukraine without facing resistance.\n", + "Predicted Answer: Putin miscalculated that the West and NATO wouldn't respond to his attack on Ukraine and that he could divide the US and its allies.\n", "Predicted Grade: CORRECT\n", "\n", "Example 3:\n", - "Question: What did President Zelenskyy say in his speech to the European Parliament?\n", - "Real Answer: \"Light will win over darkness.\"\n", - "Predicted Answer: President Zelenskyy said in his speech to the European Parliament “Light will win over darkness.”\n", + "Question: What is the purpose of NATO?\n", + "Real Answer: The purpose of NATO is to secure peace and stability in Europe after World War 2.\n", + "Predicted Answer: The purpose of NATO is to secure peace and stability in Europe after World War 2.\n", "Predicted Grade: CORRECT\n", "\n", "Example 4:\n", - "Question: What countries joined the coalition to confront Putin?\n", - "Real Answer: France, Germany, Italy, the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland.\n", - "Predicted Answer: The coalition included members of the European Union, including France, Germany, Italy, as well as countries like the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland.\n", + "Question: What did the author do to prepare for Putin's attack on Ukraine?\n", + "Real Answer: The author spent months building a coalition of freedom-loving nations from Europe and the Americas to Asia and Africa to confront Putin, shared with the world in advance what they knew Putin was planning, and countered Russia's lies with truth.\n", + "Predicted Answer: The author prepared extensively and carefully. They spent months building a coalition of other freedom-loving nations from Europe and the Americas to Asia and Africa to confront Putin, and they spent countless hours unifying their European allies. They also shared with the world in advance what they knew Putin was planning and precisely how he would try to falsely justify his aggression. They countered Russia’s lies with truth.\n", "Predicted Grade: CORRECT\n", "\n", "Example 5:\n", - "Question: What measures is the US taking to punish Russia and its oligarchs?\n", - "Real Answer: The US is enforcing powerful economic sanctions, cutting off Russia's largest banks from the international financial system, preventing the Russian Ruble from being defended, choking off Russia's access to technology, and joining with European allies to seize yachts, luxury apartments, and private jets.\n", - "Predicted Answer: The US is enforcing economic sanctions, cutting off Russia's access to international financial systems, preventing Russia's central bank from defending the Ruble and making Putin's \"war fund\" worthless, and targeting the crimes of Russian oligarchs by assembling a dedicated task force to seize their yachts, luxury apartments, and private jets. The US is also closing off American airspace to all Russian flights, providing aid to Ukraine, and mobilizing ground forces, air squadrons, and ship deployments to protect NATO countries.\n", + "Question: What are the US and its allies doing to isolate Russia from the world?\n", + "Real Answer: Enforcing powerful economic sanctions, cutting off Russia's largest banks from the international financial system, preventing Russia's central bank from defending the Russian Ruble, choking off Russia's access to technology, and joining with European allies to find and seize assets of Russian oligarchs.\n", + "Predicted Answer: The US and its allies are enforcing economic sanctions on Russia, cutting off its largest banks from the international financial system, preventing its central bank from defending the Russian Ruble, choking off Russia's access to technology, closing American airspace to all Russian flights, and providing support to Ukraine.\n", "Predicted Grade: CORRECT\n", "\n", "Example 6:\n", - "Question: What is the total amount of direct assistance being provided to Ukraine?\n", - "Real Answer: $1 Billion\n", - "Predicted Answer: The total amount of direct assistance being provided to Ukraine is $1 billion.\n", + "Question: How much direct assistance is the U.S. providing to Ukraine?\n", + "Real Answer: The U.S. is providing more than $1 Billion in direct assistance to Ukraine.\n", + "Predicted Answer: The U.S. is providing more than $1 Billion in direct assistance to Ukraine.\n", "Predicted Grade: CORRECT\n", "\n" ] @@ -256,7 +257,7 @@ { "cell_type": "code", "execution_count": null, - "id": "0bb9bc7e", + "id": "bd0b01dc", "metadata": {}, "outputs": [], "source": [] @@ -278,7 +279,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/evaluation/huggingface_datasets.ipynb b/docs/use_cases/evaluation/huggingface_datasets.ipynb similarity index 96% rename from docs/examples/evaluation/huggingface_datasets.ipynb rename to docs/use_cases/evaluation/huggingface_datasets.ipynb index 1f829a9f..6507350d 100644 --- a/docs/examples/evaluation/huggingface_datasets.ipynb +++ b/docs/use_cases/evaluation/huggingface_datasets.ipynb @@ -79,7 +79,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "91c809ea00a549f3b20266ce95b4928d", + "model_id": "92216d733c694ab4bfa812614f2223a4", "version_major": 2, "version_minor": 0 }, @@ -177,7 +177,7 @@ " {'text': ' Fortune cookies are believed to have originated in Japan, where they were known as \"tsujiura senbei.\" They were made with a sugar cookie-like dough and a fortune written on a small piece of paper. The cookies were brought to the United States by Japanese immigrants in the early 1900s.'},\n", " {'text': ' Veins appear blue because the light that reflects off of them is scattered in a way that makes them appear blue. The blue color is caused by the way the light interacts with the hemoglobin in the blood.'},\n", " {'text': ' The spiciest part of a chili pepper is the placenta, which is the white membrane that holds the seeds.'},\n", - " {'text': ' It is recommended to wait 24 hours before filing a missing person report.'}]" + " {'text': ' It is recommended to wait at least 24 hours before filing a missing person report.'}]" ] }, "execution_count": 8, @@ -201,7 +201,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "d6e87e11", "metadata": {}, "outputs": [], @@ -211,7 +211,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "id": "cfc2e624", "metadata": {}, "outputs": [], @@ -223,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "id": "10238f86", "metadata": {}, "outputs": [ @@ -237,7 +237,7 @@ " {'text': ' INCORRECT'}]" ] }, - "execution_count": 12, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -271,7 +271,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/evaluation/question_answering.ipynb b/docs/use_cases/evaluation/question_answering.ipynb similarity index 93% rename from docs/examples/evaluation/question_answering.ipynb rename to docs/use_cases/evaluation/question_answering.ipynb index eaedb9f1..b80844eb 100644 --- a/docs/examples/evaluation/question_answering.ipynb +++ b/docs/use_cases/evaluation/question_answering.ipynb @@ -111,7 +111,7 @@ "data": { "text/plain": [ "[{'text': ' 11 tennis balls'},\n", - " {'text': ' No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not possible for him to catch a screen pass in the NFC championship.'}]" + " {'text': ' No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not likely that he would be catching a screen pass in the NFC championship.'}]" ] }, "execution_count": 6, @@ -157,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "63780020", "metadata": {}, "outputs": [ @@ -174,7 +174,7 @@ "Example 1:\n", "Question: Is the following sentence plausible? \"Joao Moutinho caught the screen pass in the NFC championship.\"\n", "Real Answer: No\n", - "Predicted Answer: No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not possible for him to catch a screen pass in the NFC championship.\n", + "Predicted Answer: No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not likely that he would be catching a screen pass in the NFC championship.\n", "Predicted Grade: CORRECT\n", "\n" ] @@ -201,7 +201,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "d851453b", "metadata": {}, "outputs": [], @@ -224,7 +224,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "c38eb3e9", "metadata": { "scrolled": true @@ -241,10 +241,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "07d68f85", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'exact_match': 0.0, 'f1': 28.125}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "results" ] @@ -274,7 +285,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/examples/prompts/generate_examples.ipynb b/docs/use_cases/generate_examples.ipynb similarity index 86% rename from docs/examples/prompts/generate_examples.ipynb rename to docs/use_cases/generate_examples.ipynb index d33ea65e..540fb472 100644 --- a/docs/examples/prompts/generate_examples.ipynb +++ b/docs/use_cases/generate_examples.ipynb @@ -100,23 +100,14 @@ "text/plain": [ "['',\n", " '',\n", - " 'Question: What is the highest mountain peak in North America?',\n", - " '',\n", - " 'Thought 1: I need to search North America and find the highest mountain peak.',\n", - " '',\n", - " 'Action 1: Search[North America]',\n", - " '',\n", - " 'Observation 1: North America is a continent entirely within the Northern Hemisphere and almost all within the Western Hemisphere.',\n", - " '',\n", - " 'Thought 2: I need to look up \"highest mountain peak\".',\n", - " '',\n", - " 'Action 2: Lookup[highest mountain peak]',\n", - " '',\n", - " 'Observation 2: (Result 1 / 1) Denali, formerly Mount McKinley, is the highest mountain peak in North America, with a summit elevation of 20,310 feet (6,190 m) above sea level.',\n", - " '',\n", - " 'Thought 3: Denali is the highest mountain peak in North America, with a summit elevation of 20,310 feet.',\n", - " '',\n", - " 'Action 3: Finish[20,310 feet]']" + " 'Question: What is the difference between the Illinois and Missouri orogeny?',\n", + " 'Thought 1: I need to search Illinois and Missouri orogeny, and find the difference between them.',\n", + " 'Action 1: Search[Illinois orogeny]',\n", + " 'Observation 1: The Illinois orogeny is a hypothesized orogenic event that occurred in the Late Paleozoic either in the Pennsylvanian or Permian period.',\n", + " 'Thought 2: The Illinois orogeny is a hypothesized orogenic event. I need to search Missouri orogeny next and find its details.',\n", + " 'Action 2: Search[Missouri orogeny]',\n", + " 'Observation 2: The Missouri orogeny was a major tectonic event that occurred in the late Pennsylvanian and early Permian period (about 300 million years ago).',\n", + " 'Thought 3: The Illinois orogeny is hypothesized and occurred in the Late Paleozoic and the Missouri orogeny was a major tectonic event that occurred in the late Pennsylvanian and early Permian period. So the difference between the Illinois and Missouri orogeny is that the Illinois orogeny is hypothesized and occurred in the Late Paleozoic while the Missouri orogeny was a major']" ] }, "execution_count": 4, @@ -153,7 +144,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.10.9" + }, + "vscode": { + "interpreter": { + "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" + } } }, "nbformat": 4, diff --git a/docs/examples/model_laboratory.ipynb b/docs/use_cases/model_laboratory.ipynb similarity index 94% rename from docs/examples/model_laboratory.ipynb rename to docs/use_cases/model_laboratory.ipynb index 56490197..4789d288 100644 --- a/docs/examples/model_laboratory.ipynb +++ b/docs/use_cases/model_laboratory.ipynb @@ -5,9 +5,11 @@ "id": "920a3c1a", "metadata": {}, "source": [ - "# Model Laboratory\n", + "# Model Comparison\n", "\n", - "This example goes over basic functionality of how to use the ModelLaboratory to test out and try different models." + "Constructing your language model application will likely involved choosing between many different options of prompts, models, and even chains to use. When doing so, you will want to compare these different options on different inputs in an easy, flexible, and intuitive way. \n", + "\n", + "LangChain provides the concept of a ModelLaboratory to test out and try different models." ] }, { @@ -246,7 +248,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.7" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/docs/use_cases/question_answering.md b/docs/use_cases/question_answering.md new file mode 100644 index 00000000..f4afd911 --- /dev/null +++ b/docs/use_cases/question_answering.md @@ -0,0 +1,23 @@ +# Question Answering + +Question answering involves fetching multiple documents, and then asking a question of them. +The LLM response will contain the answer to your question, based on the content of the documents. + +The following resources exist: +- [Question Answering Notebook](/modules/chains/combine_docs_examples/question_answering.ipynb): A notebook walking through how to accomplish this task. +- [VectorDB Question Answering Notebook](/modules/chains/combine_docs_examples/vector_db_qa.ipynb): A notebook walking through how to do question answering over a vector database. This can often be useful for when you have a LOT of documents, and you don't want to pass them all to the LLM, but rather first want to do some semantic search over embeddings. + +### Adding in sources + +There is also a variant of this, where in addition to responding with the answer the language model will also cite its sources (eg which of the documents passed in it used). + +The following resources exist: +- [QA With Sources Notebook](/modules/chains/combine_docs_examples/qa_with_sources.ipynb): A notebook walking through how to accomplish this task. +- [VectorDB QA With Sources Notebook](/modules/chains/combine_docs_examples/vector_db_qa_with_sources.ipynb): A notebook walking through how to do question answering with sources over a vector database. This can often be useful for when you have a LOT of documents, and you don't want to pass them all to the LLM, but rather first want to do some semantic search over embeddings. + +### Additional Related Resources + +Additional related resources include: +- [Utilities for working with Documents](/modules/utils/how_to_guides.rst): Guides on how to use several of the utilities which will prove helpful for this task, including Text Splitters (for splitting up long documents) and Embeddings & Vectorstores (useful for the above Vector DB example). +- [CombineDocuments Chains](/modules/chains/combine_docs.md): A conceptual overview of specific types of chains by which you can accomplish this task. +- [Data Augmented Generation](combine_docs.md): An overview of data augmented generation, which is the general concept of combining external data with LLMs (of which this is a subset). diff --git a/docs/use_cases/summarization.md b/docs/use_cases/summarization.md new file mode 100644 index 00000000..e6fd9409 --- /dev/null +++ b/docs/use_cases/summarization.md @@ -0,0 +1,12 @@ +# Summarization + +Summarization involves creating a smaller summary of multiple longer documents. +This can be useful for distilling long documents into the core pieces of information + +The following resources exist: +- [Summarization Notebook](/modules/chains/combine_docs_examples/summarize.ipynb): A notebook walking through how to accomplish this task. + +Additional related resources include: +- [Utilities for working with Documents](/modules/utils/how_to_guides.rst): Guides on how to use several of the utilities which will prove helpful for this task, including Text Splitters (for splitting up long documents). +- [CombineDocuments Chains](/modules/chains/combine_docs.md): A conceptual overview of specific types of chains by which you can accomplish this task. +- [Data Augmented Generation](combine_docs.md): An overview of data augmented generation, which is the general concept of combining external data with LLMs (of which this is a subset). diff --git a/langchain/chains/llm_checker/base.py b/langchain/chains/llm_checker/base.py index c0192d79..e2f606e5 100644 --- a/langchain/chains/llm_checker/base.py +++ b/langchain/chains/llm_checker/base.py @@ -23,6 +23,7 @@ class LLMCheckerChain(Chain, BaseModel): Example: .. code-block:: python + from langchain import OpenAI, LLMCheckerChain llm = OpenAI(temperature=0.7) checker_chain = LLMCheckerChain(llm=llm) diff --git a/langchain/evaluation/qa/generate_prompt.py b/langchain/evaluation/qa/generate_prompt.py index 6eb31374..7b9fedfd 100644 --- a/langchain/evaluation/qa/generate_prompt.py +++ b/langchain/evaluation/qa/generate_prompt.py @@ -18,7 +18,7 @@ These questions should be detailed and be based explicitly on information in the {doc} """ output_parser = RegexParser( - regex=r"QUESTION: (.*?)\nANSWER: (.*)", output_keys=["question", "answer"] + regex=r"QUESTION: (.*?)\nANSWER: (.*)", output_keys=["query", "answer"] ) PROMPT = PromptTemplate( input_variables=["doc"], template=template, output_parser=output_parser diff --git a/langchain/utilities/__init__.py b/langchain/utilities/__init__.py index 34488334..7085c2e2 100644 --- a/langchain/utilities/__init__.py +++ b/langchain/utilities/__init__.py @@ -1,8 +1,14 @@ """General utilities.""" +from langchain.python import PythonREPL +from langchain.requests import RequestsWrapper +from langchain.serpapi import SerpAPIWrapper from langchain.utilities.bash import BashProcess from langchain.utilities.google_search import GoogleSearchAPIWrapper __all__ = [ "BashProcess", + "RequestsWrapper", + "PythonREPL", "GoogleSearchAPIWrapper", + "SerpAPIWrapper", ] diff --git a/tests/unit_tests/prompts/test_loading.py b/tests/unit_tests/prompts/test_loading.py index 11dbec97..16d60bde 100644 --- a/tests/unit_tests/prompts/test_loading.py +++ b/tests/unit_tests/prompts/test_loading.py @@ -15,7 +15,7 @@ def change_directory() -> Iterator: """Change the working directory to the right folder.""" origin = Path().absolute() try: - os.chdir("docs/examples/prompts") + os.chdir("docs/modules/prompts/examples") yield finally: os.chdir(origin)