id
stringlengths
14
15
text
stringlengths
27
2.12k
source
stringlengths
49
118
23d1bce49020-11
KEY ("PlaylistId", "TrackId"), \n\tFOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), \n\tFOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId")\n)\n\n/*\n3 rows from PlaylistTrack table:\nPlaylistId\tTrackId\n1\t3402\n1\t3389\n1\t3390\n*/', 'stop': ['\nSQLResult:']}, 'SELECT COUNT(*) FROM Employee;', {'query': 'SELECT COUNT(*) FROM Employee;', 'dialect': 'sqlite'}, 'SELECT COUNT(*) FROM Employee;', '[(8,)]']Choosing how to limit the number of rows returned​If you are querying for several rows of a table you can select the maximum number of results you want to get by using the 'top_k' parameter (default is 10). This is useful for avoiding query results that exceed the prompt max length or consume tokens unnecessarily.db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, use_query_checker=True, top_k=3)db_chain.run("What are some example tracks by composer Johann Sebastian Bach?") > Entering new SQLDatabaseChain chain... What are some example tracks by composer Johann Sebastian Bach? SQLQuery:SELECT Name FROM Track WHERE Composer = 'Johann Sebastian Bach' LIMIT 3 SQLResult: [('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace',), ('Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria',), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I.
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-12
for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude',)] Answer:Examples of tracks by Johann Sebastian Bach are Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude. > Finished chain. 'Examples of tracks by Johann Sebastian Bach are Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude.'Adding example rows from each table​Sometimes, the format of the data is not obvious and it is optimal to include a sample of rows from the tables in the prompt to allow the LLM to understand the data before providing a final query. Here we will use this feature to let the LLM know that artists are saved with their full names by providing two rows from the Track table.db = SQLDatabase.from_uri( "sqlite:///../../../../notebooks/Chinook.db", include_tables=['Track'], # we include only one table to save tokens in the prompt :) sample_rows_in_table_info=2)The sample rows are added to the prompt after each corresponding table's column information:print(db.table_info) CREATE TABLE "Track" ( "TrackId" INTEGER NOT NULL, "Name"
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-13
"TrackId" INTEGER NOT NULL, "Name" NVARCHAR(200) NOT NULL, "AlbumId" INTEGER, "MediaTypeId" INTEGER NOT NULL, "GenreId" INTEGER, "Composer" NVARCHAR(220), "Milliseconds" INTEGER NOT NULL, "Bytes" INTEGER, "UnitPrice" NUMERIC(10, 2) NOT NULL, PRIMARY KEY ("TrackId"), FOREIGN KEY("MediaTypeId") REFERENCES "MediaType" ("MediaTypeId"), FOREIGN KEY("GenreId") REFERENCES "Genre" ("GenreId"), FOREIGN KEY("AlbumId") REFERENCES "Album" ("AlbumId") ) /* 2 rows from Track table: TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes UnitPrice 1 For Those About To Rock (We Salute You) 1 1 1 Angus Young, Malcolm Young, Brian Johnson 343719 11170334 0.99 2 Balls to the Wall 2 2 1 None 342562 5510424 0.99 */db_chain = SQLDatabaseChain.from_llm(llm, db, use_query_checker=True, verbose=True)db_chain.run("What are some example
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-14
db, use_query_checker=True, verbose=True)db_chain.run("What are some example tracks by Bach?") > Entering new SQLDatabaseChain chain... What are some example tracks by Bach? SQLQuery:SELECT "Name", "Composer" FROM "Track" WHERE "Composer" LIKE '%Bach%' LIMIT 5 SQLResult: [('American Woman', 'B. Cummings/G. Peterson/M.J. Kale/R. Bachman'), ('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Johann Sebastian Bach'), ('Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria', 'Johann Sebastian Bach'), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', 'Johann Sebastian Bach'), ('Toccata and Fugue in D Minor, BWV 565: I. Toccata', 'Johann Sebastian Bach')] Answer:Tracks by Bach include 'American Woman', 'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria', 'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', and 'Toccata and Fugue in D Minor, BWV 565: I. Toccata'. > Finished chain. 'Tracks by Bach include \'American Woman\', \'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\', \'Aria Mit 30
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-15
D Minor, BWV 1043: I. Vivace\', \'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria\', \'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\', and \'Toccata and Fugue in D Minor, BWV 565: I. Toccata\'.'Custom Table Info​In some cases, it can be useful to provide custom table information instead of using the automatically generated table definitions and the first sample_rows_in_table_info sample rows. For example, if you know that the first few rows of a table are uninformative, it could help to manually provide example rows that are more diverse or provide more information to the model. It is also possible to limit the columns that will be visible to the model if there are unnecessary columns. This information can be provided as a dictionary with table names as the keys and table information as the values. For example, let's provide a custom definition and sample rows for the Track table with only a few columns:custom_table_info = { "Track": """CREATE TABLE Track ( "TrackId" INTEGER NOT NULL, "Name" NVARCHAR(200) NOT NULL, "Composer" NVARCHAR(220), PRIMARY KEY ("TrackId"))/*3 rows from Track table:TrackId Name Composer1 For Those About To Rock (We Salute You) Angus Young, Malcolm Young, Brian Johnson2 Balls to the Wall None3 My favorite song ever The coolest composer of all time*/"""}db = SQLDatabase.from_uri( "sqlite:///../../../../notebooks/Chinook.db", include_tables=['Track', 'Playlist'],
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-16
include_tables=['Track', 'Playlist'], sample_rows_in_table_info=2, custom_table_info=custom_table_info)print(db.table_info) CREATE TABLE "Playlist" ( "PlaylistId" INTEGER NOT NULL, "Name" NVARCHAR(120), PRIMARY KEY ("PlaylistId") ) /* 2 rows from Playlist table: PlaylistId Name 1 Music 2 Movies */ CREATE TABLE Track ( "TrackId" INTEGER NOT NULL, "Name" NVARCHAR(200) NOT NULL, "Composer" NVARCHAR(220), PRIMARY KEY ("TrackId") ) /* 3 rows from Track table: TrackId Name Composer 1 For Those About To Rock (We Salute You) Angus Young, Malcolm Young, Brian Johnson 2 Balls to the Wall None 3 My favorite song ever The coolest composer of all time */Note how our custom table definition and sample rows for Track overrides the sample_rows_in_table_info parameter. Tables that are not overridden by custom_table_info, in this example Playlist, will have their table info gathered automatically as usual.db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run("What are some example tracks by Bach?") > Entering new
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-17
tracks by Bach?") > Entering new SQLDatabaseChain chain... What are some example tracks by Bach? SQLQuery:SELECT "Name" FROM Track WHERE "Composer" LIKE '%Bach%' LIMIT 5; SQLResult: [('American Woman',), ('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace',), ('Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria',), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude',), ('Toccata and Fugue in D Minor, BWV 565: I. Toccata',)] Answer:text='You are a SQLite expert. Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer to the input question.\nUnless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per SQLite. You can order the results to return the most informative data in the database.\nNever query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers.\nPay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.\n\nUse the following format:\n\nQuestion: "Question here"\nSQLQuery: "SQL Query to run"\nSQLResult: "Result of the SQLQuery"\nAnswer: "Final answer here"\n\nOnly use the following
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-18
of the SQLQuery"\nAnswer: "Final answer here"\n\nOnly use the following tables:\n\nCREATE TABLE "Playlist" (\n\t"PlaylistId" INTEGER NOT NULL, \n\t"Name" NVARCHAR(120), \n\tPRIMARY KEY ("PlaylistId")\n)\n\n/*\n2 rows from Playlist table:\nPlaylistId\tName\n1\tMusic\n2\tMovies\n*/\n\nCREATE TABLE Track (\n\t"TrackId" INTEGER NOT NULL, \n\t"Name" NVARCHAR(200) NOT NULL,\n\t"Composer" NVARCHAR(220),\n\tPRIMARY KEY ("TrackId")\n)\n/*\n3 rows from Track table:\nTrackId\tName\tComposer\n1\tFor Those About To Rock (We Salute You)\tAngus Young, Malcolm Young, Brian Johnson\n2\tBalls to the Wall\tNone\n3\tMy favorite song ever\tThe coolest composer of all time\n*/\n\nQuestion: What are some example tracks by Bach?\nSQLQuery:SELECT "Name" FROM Track WHERE "Composer" LIKE \'%Bach%\' LIMIT 5;\nSQLResult: [(\'American Woman\',), (\'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\',), (\'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria\',), (\'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\',), (\'Toccata and Fugue in D Minor, BWV 565: I. Toccata\',)]\nAnswer:' You are a SQLite expert. Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-19
first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer to the input question. Unless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per SQLite. You can order the results to return the most informative data in the database. Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers. Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. Use the following format: Question: "Question here" SQLQuery: "SQL Query to run" SQLResult: "Result of the SQLQuery" Answer: "Final answer here" Only use the following tables: CREATE TABLE "Playlist" ( "PlaylistId" INTEGER NOT NULL, "Name" NVARCHAR(120), PRIMARY KEY ("PlaylistId") ) /* 2 rows from Playlist table: PlaylistId Name 1 Music 2 Movies */ CREATE TABLE Track ( "TrackId" INTEGER NOT NULL, "Name" NVARCHAR(200) NOT NULL, "Composer"
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-20
NVARCHAR(200) NOT NULL, "Composer" NVARCHAR(220), PRIMARY KEY ("TrackId") ) /* 3 rows from Track table: TrackId Name Composer 1 For Those About To Rock (We Salute You) Angus Young, Malcolm Young, Brian Johnson 2 Balls to the Wall None 3 My favorite song ever The coolest composer of all time */ Question: What are some example tracks by Bach? SQLQuery:SELECT "Name" FROM Track WHERE "Composer" LIKE '%Bach%' LIMIT 5; SQLResult: [('American Woman',), ('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace',), ('Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria',), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude',), ('Toccata and Fugue in D Minor, BWV 565: I. Toccata',)] Answer: {'input': 'What are some example tracks by Bach?\nSQLQuery:SELECT "Name" FROM Track WHERE "Composer" LIKE \'%Bach%\' LIMIT 5;\nSQLResult: [(\'American Woman\',), (\'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\',), (\'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria\',), (\'Suite for Solo Cello No.
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-21
"Goldberg Variations": Aria\',), (\'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\',), (\'Toccata and Fugue in D Minor, BWV 565: I. Toccata\',)]\nAnswer:', 'top_k': '5', 'dialect': 'sqlite', 'table_info': '\nCREATE TABLE "Playlist" (\n\t"PlaylistId" INTEGER NOT NULL, \n\t"Name" NVARCHAR(120), \n\tPRIMARY KEY ("PlaylistId")\n)\n\n/*\n2 rows from Playlist table:\nPlaylistId\tName\n1\tMusic\n2\tMovies\n*/\n\nCREATE TABLE Track (\n\t"TrackId" INTEGER NOT NULL, \n\t"Name" NVARCHAR(200) NOT NULL,\n\t"Composer" NVARCHAR(220),\n\tPRIMARY KEY ("TrackId")\n)\n/*\n3 rows from Track table:\nTrackId\tName\tComposer\n1\tFor Those About To Rock (We Salute You)\tAngus Young, Malcolm Young, Brian Johnson\n2\tBalls to the Wall\tNone\n3\tMy favorite song ever\tThe coolest composer of all time\n*/', 'stop': ['\nSQLResult:']} Examples of tracks by Bach include "American Woman", "Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace", "Aria Mit 30 Veränderungen, BWV 988 'Goldberg Variations': Aria", "Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude", and "Toccata and Fugue in D Minor, BWV 565: I. Toccata".
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-22
in D Minor, BWV 565: I. Toccata". > Finished chain. 'Examples of tracks by Bach include "American Woman", "Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace", "Aria Mit 30 Veränderungen, BWV 988 \'Goldberg Variations\': Aria", "Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude", and "Toccata and Fugue in D Minor, BWV 565: I. Toccata".'SQL Views​In some case, the table schema can be hidden behind a JSON or JSONB column. Adding row samples into the prompt might help won't always describe the data perfectly. For this reason, a custom SQL views can help.CREATE VIEW accounts_v AS select id, firstname, lastname, email, created_at, updated_at, cast(stats->>'total_post' as int) as total_post, cast(stats->>'total_comments' as int) as total_comments, cast(stats->>'ltv' as int) as ltv FROM accounts;Then limit the tables visible from SQLDatabase to the created view.db = SQLDatabase.from_uri( "sqlite:///../../../../notebooks/Chinook.db", include_tables=['accounts_v']) # we include only the viewSQLDatabaseSequentialChain​Chain for querying SQL database that is a sequential chain.The chain is as follows:1. Based on the query, determine which tables to use.2. Based on those tables, call the normal SQL database chain.This is useful in cases where the number of tables in the database is large.from
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-23
normal SQL database chain.This is useful in cases where the number of tables in the database is large.from langchain.chains import SQLDatabaseSequentialChaindb = SQLDatabase.from_uri("sqlite:///../../../../notebooks/Chinook.db")chain = SQLDatabaseSequentialChain.from_llm(llm, db, verbose=True)chain.run("How many employees are also customers?") > Entering new SQLDatabaseSequentialChain chain... Table names to use: ['Employee', 'Customer'] > Entering new SQLDatabaseChain chain... How many employees are also customers? SQLQuery:SELECT COUNT(*) FROM Employee e INNER JOIN Customer c ON e.EmployeeId = c.SupportRepId; SQLResult: [(59,)] Answer:59 employees are also customers. > Finished chain. > Finished chain. '59 employees are also customers.'Using Local Language Models​Sometimes you may not have the luxury of using OpenAI or other service-hosted large language model. You can, ofcourse, try to use the SQLDatabaseChain with a local model, but will quickly realize that most models you can run locally even with a large GPU struggle to generate the right output.import loggingimport torchfrom transformers import AutoTokenizer, GPT2TokenizerFast, pipeline, AutoModelForSeq2SeqLM, AutoModelForCausalLMfrom langchain import HuggingFacePipeline# Note: This model requires a large GPU, e.g. an 80GB A100. See documentation for other ways to run private non-OpenAI models.model_id = "google/flan-ul2"model = AutoModelForSeq2SeqLM.from_pretrained(model_id, temperature=0)device_id = -1 # default to no-GPU, but use GPU
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-24
temperature=0)device_id = -1 # default to no-GPU, but use GPU and half precision mode if availableif torch.cuda.is_available(): device_id = 0 try: model = model.half() except RuntimeError as exc: logging.warn(f"Could not run model in half precision mode: {str(exc)}")tokenizer = AutoTokenizer.from_pretrained(model_id)pipe = pipeline(task="text2text-generation", model=model, tokenizer=tokenizer, max_length=1024, device=device_id)local_llm = HuggingFacePipeline(pipeline=pipe) /workspace/langchain/.venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm Loading checkpoint shards: 100%|██████████| 8/8 [00:32<00:00, 4.11s/it]from langchain import SQLDatabase, SQLDatabaseChaindb = SQLDatabase.from_uri("sqlite:///../../../../notebooks/Chinook.db", include_tables=['Customer'])local_chain = SQLDatabaseChain.from_llm(local_llm, db, verbose=True, return_intermediate_steps=True, use_query_checker=True)This model should work for very simple SQL queries, as long as you use the query checker as specified above, e.g.:local_chain("How many customers are there?")
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-25
many customers are there?") > Entering new SQLDatabaseChain chain... How many customers are there? SQLQuery: /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( SELECT count(*) FROM Customer SQLResult: [(59,)] Answer: /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( [59] > Finished chain. {'query': 'How many customers are there?', 'result': '[59]', 'intermediate_steps': [{'input': 'How many customers are there?\nSQLQuery:SELECT count(*) FROM Customer\nSQLResult: [(59,)]\nAnswer:', 'top_k': '5', 'dialect': 'sqlite', 'table_info': '\nCREATE TABLE "Customer" (\n\t"CustomerId" INTEGER NOT NULL, \n\t"FirstName" NVARCHAR(40) NOT NULL, \n\t"LastName" NVARCHAR(20) NOT NULL,
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-26
NOT NULL, \n\t"LastName" NVARCHAR(20) NOT NULL, \n\t"Company" NVARCHAR(80), \n\t"Address" NVARCHAR(70), \n\t"City" NVARCHAR(40), \n\t"State" NVARCHAR(40), \n\t"Country" NVARCHAR(40), \n\t"PostalCode" NVARCHAR(10), \n\t"Phone" NVARCHAR(24), \n\t"Fax" NVARCHAR(24), \n\t"Email" NVARCHAR(60) NOT NULL, \n\t"SupportRepId" INTEGER, \n\tPRIMARY KEY ("CustomerId"), \n\tFOREIGN KEY("SupportRepId") REFERENCES "Employee" ("EmployeeId")\n)\n\n/*\n3 rows from Customer table:\nCustomerId\tFirstName\tLastName\tCompany\tAddress\tCity\tState\tCountry\tPostalCode\tPhone\tFax\tEmail\tSupportRepId\n1\tLuís\tGonçalves\tEmbraer - Empresa Brasileira de Aeronáutica S.A.\tAv. Brigadeiro Faria Lima, 2170\tSão José dos Campos\tSP\tBrazil\t12227-000\t+55 (12) 3923-5555\t+55 (12) 3923-5566\tluisg@embraer.com.br\t3\n2\tLeonie\tKöhler\tNone\tTheodor-Heuss-Straße 34\tStuttgart\tNone\tGermany\t70174\t+49 0711 2842222\tNone\tleonekohler@surfeu.de\t5\n3\tFrançois\tTremblay\tNone\t1498 rue Bélanger\tMontréal\tQC\tCanada\tH2G
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-27
rue Bélanger\tMontréal\tQC\tCanada\tH2G 1A7\t+1 (514) 721-4711\tNone\tftremblay@gmail.com\t3\n*/', 'stop': ['\nSQLResult:']}, 'SELECT count(*) FROM Customer', {'query': 'SELECT count(*) FROM Customer', 'dialect': 'sqlite'}, 'SELECT count(*) FROM Customer', '[(59,)]']}Even this relatively large model will most likely fail to generate more complicated SQL by itself. However, you can log its inputs and outputs so that you can hand-correct them and use the corrected examples for few shot prompt examples later. In practice, you could log any executions of your chain that raise exceptions (as shown in the example below) or get direct user feedback in cases where the results are incorrect (but did not raise an exception).poetry run pip install pyyaml chromadbimport yaml huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks... To disable this warning, you can either: - Avoid using `tokenizers` before the fork if possible - Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false) 11842.36s - pydevd: Sending message related to process being replaced timed-out after 5 seconds Requirement already satisfied: pyyaml in /workspace/langchain/.venv/lib/python3.9/site-packages (6.0) Requirement already satisfied: chromadb in /workspace/langchain/.venv/lib/python3.9/site-packages (0.3.21)
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-28
(0.3.21) Requirement already satisfied: pandas>=1.3 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (2.0.1) Requirement already satisfied: requests>=2.28 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (2.28.2) Requirement already satisfied: pydantic>=1.9 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (1.10.7) Requirement already satisfied: hnswlib>=0.7 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (0.7.0) Requirement already satisfied: clickhouse-connect>=0.5.7 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (0.5.20) Requirement already satisfied: sentence-transformers>=2.2.2 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (2.2.2) Requirement already satisfied: duckdb>=0.7.1 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (0.7.1) Requirement already satisfied: fastapi>=0.85.1 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (0.95.1) Requirement already satisfied: uvicorn[standard]>=0.18.3 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (0.21.1) Requirement already satisfied: numpy>=1.21.6 in
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-29
Requirement already satisfied: numpy>=1.21.6 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (1.24.3) Requirement already satisfied: posthog>=2.4.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from chromadb) (3.0.1) Requirement already satisfied: certifi in /workspace/langchain/.venv/lib/python3.9/site-packages (from clickhouse-connect>=0.5.7->chromadb) (2022.12.7) Requirement already satisfied: urllib3>=1.26 in /workspace/langchain/.venv/lib/python3.9/site-packages (from clickhouse-connect>=0.5.7->chromadb) (1.26.15) Requirement already satisfied: pytz in /workspace/langchain/.venv/lib/python3.9/site-packages (from clickhouse-connect>=0.5.7->chromadb) (2023.3) Requirement already satisfied: zstandard in /workspace/langchain/.venv/lib/python3.9/site-packages (from clickhouse-connect>=0.5.7->chromadb) (0.21.0) Requirement already satisfied: lz4 in /workspace/langchain/.venv/lib/python3.9/site-packages (from clickhouse-connect>=0.5.7->chromadb) (4.3.2) Requirement already satisfied: starlette<0.27.0,>=0.26.1 in /workspace/langchain/.venv/lib/python3.9/site-packages (from fastapi>=0.85.1->chromadb) (0.26.1) Requirement already satisfied: python-dateutil>=2.8.2 in
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-30
Requirement already satisfied: python-dateutil>=2.8.2 in /workspace/langchain/.venv/lib/python3.9/site-packages (from pandas>=1.3->chromadb) (2.8.2) Requirement already satisfied: tzdata>=2022.1 in /workspace/langchain/.venv/lib/python3.9/site-packages (from pandas>=1.3->chromadb) (2023.3) Requirement already satisfied: six>=1.5 in /workspace/langchain/.venv/lib/python3.9/site-packages (from posthog>=2.4.0->chromadb) (1.16.0) Requirement already satisfied: monotonic>=1.5 in /workspace/langchain/.venv/lib/python3.9/site-packages (from posthog>=2.4.0->chromadb) (1.6) Requirement already satisfied: backoff>=1.10.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from posthog>=2.4.0->chromadb) (2.2.1) Requirement already satisfied: typing-extensions>=4.2.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from pydantic>=1.9->chromadb) (4.5.0) Requirement already satisfied: charset-normalizer<4,>=2 in /workspace/langchain/.venv/lib/python3.9/site-packages (from requests>=2.28->chromadb) (3.1.0) Requirement already satisfied: idna<4,>=2.5 in /workspace/langchain/.venv/lib/python3.9/site-packages (from requests>=2.28->chromadb) (3.4) Requirement already satisfied:
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-31
(3.4) Requirement already satisfied: transformers<5.0.0,>=4.6.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (4.28.1) Requirement already satisfied: tqdm in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (4.65.0) Requirement already satisfied: torch>=1.6.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (1.13.1) Requirement already satisfied: torchvision in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (0.14.1) Requirement already satisfied: scikit-learn in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (1.2.2) Requirement already satisfied: scipy in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (1.9.3) Requirement already satisfied: nltk in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (3.8.1) Requirement already satisfied: sentencepiece in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (0.1.98) Requirement already satisfied:
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-32
(0.1.98) Requirement already satisfied: huggingface-hub>=0.4.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from sentence-transformers>=2.2.2->chromadb) (0.13.4) Requirement already satisfied: click>=7.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (8.1.3) Requirement already satisfied: h11>=0.8 in /workspace/langchain/.venv/lib/python3.9/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.14.0) Requirement already satisfied: httptools>=0.5.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.5.0) Requirement already satisfied: python-dotenv>=0.13 in /workspace/langchain/.venv/lib/python3.9/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (1.0.0) Requirement already satisfied: uvloop!=0.15.0,!=0.15.1,>=0.14.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.17.0) Requirement already satisfied: watchfiles>=0.13 in /workspace/langchain/.venv/lib/python3.9/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.19.0) Requirement already satisfied: websockets>=10.4
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-33
(0.19.0) Requirement already satisfied: websockets>=10.4 in /workspace/langchain/.venv/lib/python3.9/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (11.0.2) Requirement already satisfied: filelock in /workspace/langchain/.venv/lib/python3.9/site-packages (from huggingface-hub>=0.4.0->sentence-transformers>=2.2.2->chromadb) (3.12.0) Requirement already satisfied: packaging>=20.9 in /workspace/langchain/.venv/lib/python3.9/site-packages (from huggingface-hub>=0.4.0->sentence-transformers>=2.2.2->chromadb) (23.1) Requirement already satisfied: anyio<5,>=3.4.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from starlette<0.27.0,>=0.26.1->fastapi>=0.85.1->chromadb) (3.6.2) Requirement already satisfied: nvidia-cuda-runtime-cu11==11.7.99 in /workspace/langchain/.venv/lib/python3.9/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.7.99) Requirement already satisfied: nvidia-cudnn-cu11==8.5.0.96 in /workspace/langchain/.venv/lib/python3.9/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (8.5.0.96) Requirement already satisfied:
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-34
(8.5.0.96) Requirement already satisfied: nvidia-cublas-cu11==11.10.3.66 in /workspace/langchain/.venv/lib/python3.9/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.10.3.66) Requirement already satisfied: nvidia-cuda-nvrtc-cu11==11.7.99 in /workspace/langchain/.venv/lib/python3.9/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.7.99) Requirement already satisfied: setuptools in /workspace/langchain/.venv/lib/python3.9/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (67.7.1) Requirement already satisfied: wheel in /workspace/langchain/.venv/lib/python3.9/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (0.40.0) Requirement already satisfied: regex!=2019.12.17 in /workspace/langchain/.venv/lib/python3.9/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers>=2.2.2->chromadb) (2023.3.23) Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /workspace/langchain/.venv/lib/python3.9/site-packages (from
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-35
in /workspace/langchain/.venv/lib/python3.9/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers>=2.2.2->chromadb) (0.13.3) Requirement already satisfied: joblib in /workspace/langchain/.venv/lib/python3.9/site-packages (from nltk->sentence-transformers>=2.2.2->chromadb) (1.2.0) Requirement already satisfied: threadpoolctl>=2.0.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from scikit-learn->sentence-transformers>=2.2.2->chromadb) (3.1.0) Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /workspace/langchain/.venv/lib/python3.9/site-packages (from torchvision->sentence-transformers>=2.2.2->chromadb) (9.5.0) Requirement already satisfied: sniffio>=1.1 in /workspace/langchain/.venv/lib/python3.9/site-packages (from anyio<5,>=3.4.0->starlette<0.27.0,>=0.26.1->fastapi>=0.85.1->chromadb) (1.3.0)from typing import DictQUERY = "List all the customer first names that start with 'a'"def _parse_example(result: Dict) -> Dict: sql_cmd_key = "sql_cmd" sql_result_key = "sql_result" table_info_key = "table_info" input_key = "input" final_answer_key = "answer" _example = { "input": result.get("query"),
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-36
= { "input": result.get("query"), } steps = result.get("intermediate_steps") answer_key = sql_cmd_key # the first one for step in steps: # The steps are in pairs, a dict (input) followed by a string (output). # Unfortunately there is no schema but you can look at the input key of the # dict to see what the output is supposed to be if isinstance(step, dict): # Grab the table info from input dicts in the intermediate steps once if table_info_key not in _example: _example[table_info_key] = step.get(table_info_key) if input_key in step: if step[input_key].endswith("SQLQuery:"): answer_key = sql_cmd_key # this is the SQL generation input if step[input_key].endswith("Answer:"): answer_key = final_answer_key # this is the final answer input elif sql_cmd_key in step: _example[sql_cmd_key] = step[sql_cmd_key]
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-37
= step[sql_cmd_key] answer_key = sql_result_key # this is SQL execution input elif isinstance(step, str): # The preceding element should have set the answer_key _example[answer_key] = step return _exampleexample: anytry: result = local_chain(QUERY) print("*** Query succeeded") example = _parse_example(result)except Exception as exc: print("*** Query failed") result = { "query": QUERY, "intermediate_steps": exc.intermediate_steps } example = _parse_example(result)# print for now, in reality you may want to write this out to a YAML file or database for manual fix-ups offlineyaml_example = yaml.dump(example, allow_unicode=True)print("\n" + yaml_example) > Entering new SQLDatabaseChain chain... List all the customer first names that start with 'a' SQLQuery: /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( SELECT firstname FROM customer WHERE firstname LIKE '%a%' SQLResult: [('François',), ('František',), ('Helena',), ('Astrid',), ('Daan',), ('Kara',), ('Eduardo',), ('Alexandre',),
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-38
('Kara',), ('Eduardo',), ('Alexandre',), ('Fernanda',), ('Mark',), ('Frank',), ('Jack',), ('Dan',), ('Kathy',), ('Heather',), ('Frank',), ('Richard',), ('Patrick',), ('Julia',), ('Edward',), ('Martha',), ('Aaron',), ('Madalena',), ('Hannah',), ('Niklas',), ('Camille',), ('Marc',), ('Wyatt',), ('Isabelle',), ('Ladislav',), ('Lucas',), ('Johannes',), ('Stanisław',), ('Joakim',), ('Emma',), ('Mark',), ('Manoj',), ('Puja',)] Answer: /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( [('François', 'Frantiek', 'Helena', 'Astrid', 'Daan', 'Kara', 'Eduardo', 'Alexandre', 'Fernanda', 'Mark', 'Frank', 'Jack', 'Dan', 'Kathy', 'Heather', 'Frank', 'Richard', 'Patrick', 'Julia', 'Edward', 'Martha', 'Aaron', 'Madalena', 'Hannah', 'Niklas', 'Camille', 'Marc', 'Wyatt', 'Isabelle', 'Ladislav', 'Lucas', 'Johannes', 'Stanisaw', 'Joakim', 'Emma', 'Mark', 'Manoj', 'Puja'] > Finished chain. *** Query succeeded
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-39
'Puja'] > Finished chain. *** Query succeeded answer: '[(''François'', ''Frantiek'', ''Helena'', ''Astrid'', ''Daan'', ''Kara'', ''Eduardo'', ''Alexandre'', ''Fernanda'', ''Mark'', ''Frank'', ''Jack'', ''Dan'', ''Kathy'', ''Heather'', ''Frank'', ''Richard'', ''Patrick'', ''Julia'', ''Edward'', ''Martha'', ''Aaron'', ''Madalena'', ''Hannah'', ''Niklas'', ''Camille'', ''Marc'', ''Wyatt'', ''Isabelle'', ''Ladislav'', ''Lucas'', ''Johannes'', ''Stanisaw'', ''Joakim'', ''Emma'', ''Mark'', ''Manoj'', ''Puja'']' input: List all the customer first names that start with 'a' sql_cmd: SELECT firstname FROM customer WHERE firstname LIKE '%a%' sql_result: '[(''François'',), (''František'',), (''Helena'',), (''Astrid'',), (''Daan'',), (''Kara'',), (''Eduardo'',), (''Alexandre'',), (''Fernanda'',), (''Mark'',), (''Frank'',), (''Jack'',), (''Dan'',), (''Kathy'',), (''Heather'',), (''Frank'',), (''Richard'',), (''Patrick'',), (''Julia'',), (''Edward'',), (''Martha'',), (''Aaron'',),
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-40
(''Edward'',), (''Martha'',), (''Aaron'',), (''Madalena'',), (''Hannah'',), (''Niklas'',), (''Camille'',), (''Marc'',), (''Wyatt'',), (''Isabelle'',), (''Ladislav'',), (''Lucas'',), (''Johannes'',), (''Stanisław'',), (''Joakim'',), (''Emma'',), (''Mark'',), (''Manoj'',), (''Puja'',)]' table_info: "\nCREATE TABLE \"Customer\" (\n\t\"CustomerId\" INTEGER NOT NULL, \n\t\ \"FirstName\" NVARCHAR(40) NOT NULL, \n\t\"LastName\" NVARCHAR(20) NOT NULL, \n\t\ \"Company\" NVARCHAR(80), \n\t\"Address\" NVARCHAR(70), \n\t\"City\" NVARCHAR(40),\ \ \n\t\"State\" NVARCHAR(40), \n\t\"Country\" NVARCHAR(40), \n\t\"PostalCode\" NVARCHAR(10),\ \ \n\t\"Phone\" NVARCHAR(24), \n\t\"Fax\" NVARCHAR(24), \n\t\"Email\" NVARCHAR(60)\ \ NOT NULL, \n\t\"SupportRepId\" INTEGER, \n\tPRIMARY KEY (\"CustomerId\"), \n\t\ FOREIGN KEY(\"SupportRepId\") REFERENCES \"Employee\" (\"EmployeeId\")\n)\n\n/*\n\ 3 rows from Customer table:\nCustomerId\tFirstName\tLastName\tCompany\tAddress\t\
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-41
3 rows from Customer table:\nCustomerId\tFirstName\tLastName\tCompany\tAddress\t\ City\tState\tCountry\tPostalCode\tPhone\tFax\tEmail\tSupportRepId\n1\tLuís\tGonçalves\t\ Embraer - Empresa Brasileira de Aeronáutica S.A.\tAv. Brigadeiro Faria Lima, 2170\t\ São José dos Campos\tSP\tBrazil\t12227-000\t+55 (12) 3923-5555\t+55 (12) 3923-5566\t\ luisg@embraer.com.br\t3\n2\tLeonie\tKöhler\tNone\tTheodor-Heuss-Straße 34\tStuttgart\t\ None\tGermany\t70174\t+49 0711 2842222\tNone\tleonekohler@surfeu.de\t5\n3\tFrançois\t\ Tremblay\tNone\t1498 rue Bélanger\tMontréal\tQC\tCanada\tH2G 1A7\t+1 (514) 721-4711\t\ None\tftremblay@gmail.com\t3\n*/" Run the snippet above a few times, or log exceptions in your deployed environment, to collect lots of examples of inputs, table_info and sql_cmd generated by your language model. The sql_cmd values will be incorrect and you can manually fix them up to build a collection of examples, e.g. here we are using YAML to keep a neat record of our inputs and corrected SQL output that we can build up over time.YAML_EXAMPLES = """- input: How
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-42
SQL output that we can build up over time.YAML_EXAMPLES = """- input: How many customers are not from Brazil? table_info: | CREATE TABLE "Customer" ( "CustomerId" INTEGER NOT NULL, "FirstName" NVARCHAR(40) NOT NULL, "LastName" NVARCHAR(20) NOT NULL, "Company" NVARCHAR(80), "Address" NVARCHAR(70), "City" NVARCHAR(40), "State" NVARCHAR(40), "Country" NVARCHAR(40), "PostalCode" NVARCHAR(10), "Phone" NVARCHAR(24), "Fax" NVARCHAR(24), "Email" NVARCHAR(60) NOT NULL, "SupportRepId" INTEGER, PRIMARY KEY ("CustomerId"), FOREIGN KEY("SupportRepId") REFERENCES "Employee" ("EmployeeId") ) sql_cmd: SELECT COUNT(*) FROM "Customer" WHERE NOT "Country" = "Brazil"; sql_result: "[(54,)]" answer: 54 customers are not from Brazil.- input: list all the genres that start with 'r' table_info: | CREATE TABLE "Genre" ( "GenreId" INTEGER NOT NULL, "Name" NVARCHAR(120), PRIMARY KEY ("GenreId") ) /* 3 rows from Genre table: GenreId Name
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-43
/* 3 rows from Genre table: GenreId Name 1 Rock 2 Jazz 3 Metal */ sql_cmd: SELECT "Name" FROM "Genre" WHERE "Name" LIKE 'r%'; sql_result: "[('Rock',), ('Rock and Roll',), ('Reggae',), ('R&B/Soul',)]" answer: The genres that start with 'r' are Rock, Rock and Roll, Reggae and R&B/Soul. """Now that you have some examples (with manually corrected output SQL), you can do few shot prompt seeding the usual way:from langchain import FewShotPromptTemplate, PromptTemplatefrom langchain.chains.sql_database.prompt import _sqlite_prompt, PROMPT_SUFFIXfrom langchain.embeddings.huggingface import HuggingFaceEmbeddingsfrom langchain.prompts.example_selector.semantic_similarity import SemanticSimilarityExampleSelectorfrom langchain.vectorstores import Chromaexample_prompt = PromptTemplate( input_variables=["table_info", "input", "sql_cmd", "sql_result", "answer"], template="{table_info}\n\nQuestion: {input}\nSQLQuery: {sql_cmd}\nSQLResult: {sql_result}\nAnswer: {answer}",)examples_dict = yaml.safe_load(YAML_EXAMPLES)local_embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")example_selector = SemanticSimilarityExampleSelector.from_examples( # This is the list of examples available to select from. examples_dict,
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-44
examples_dict, # This is the embedding class used to produce embeddings which are used to measure semantic similarity. local_embeddings, # This is the VectorStore class that is used to store the embeddings and do a similarity search over. Chroma, # type: ignore # This is the number of examples to produce and include per prompt k=min(3, len(examples_dict)), )few_shot_prompt = FewShotPromptTemplate( example_selector=example_selector, example_prompt=example_prompt, prefix=_sqlite_prompt + "Here are some examples:", suffix=PROMPT_SUFFIX, input_variables=["table_info", "input", "top_k"],) Using embedded DuckDB without persistence: data will be transientThe model should do better now with this few shot prompt, especially for inputs similar to the examples you have seeded it with.local_chain = SQLDatabaseChain.from_llm(local_llm, db, prompt=few_shot_prompt, use_query_checker=True, verbose=True, return_intermediate_steps=True)result = local_chain("How many customers are from Brazil?") >
https://python.langchain.com/docs/modules/chains/popular/sqlite
23d1bce49020-45
many customers are from Brazil?") > Entering new SQLDatabaseChain chain... How many customers are from Brazil? SQLQuery:SELECT count(*) FROM Customer WHERE Country = "Brazil"; SQLResult: [(5,)] Answer:[5] > Finished chain.result = local_chain("How many customers are not from Brazil?") > Entering new SQLDatabaseChain chain... How many customers are not from Brazil? SQLQuery:SELECT count(*) FROM customer WHERE country NOT IN (SELECT country FROM customer WHERE country = 'Brazil') SQLResult: [(54,)] Answer:54 customers are not from Brazil. > Finished chain.result = local_chain("How many customers are there in total?") > Entering new SQLDatabaseChain chain... How many customers are there in total? SQLQuery:SELECT count(*) FROM Customer; SQLResult: [(59,)] Answer:There are 59 customers in total. > Finished chain.PreviousUsing OpenAI functionsNextSummarizationCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright © 2023 LangChain, Inc.
https://python.langchain.com/docs/modules/chains/popular/sqlite
a2804abe024d-0
Summarization | 🦜�🔗 Langchain
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-1
Skip to main content🦜�🔗 LangChainDocsUse casesIntegrationsAPILangSmithJS/TS DocsCTRLKGet startedIntroductionInstallationQuickstartModulesModel I/​OData connectionChainsHow toFoundationalDocumentsPopularAPI chainsRetrieval QAConversational Retrieval QAUsing OpenAI functionsSQLSummarizationAdditionalMemoryAgentsCallbacksModulesGuidesEcosystemAdditional resourcesModulesChainsPopularSummarizationSummarizationA summarization chain can be used to summarize multiple documents. One way is to input multiple smaller documents, after they have been divided into chunks, and operate over them with a MapReduceDocumentsChain. You can also choose instead for the chain that does summarization to be a StuffDocumentsChain, or a RefineDocumentsChain.Prepare Data​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).from langchain import OpenAI, PromptTemplate, LLMChainfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.chains.mapreduce import MapReduceChainfrom langchain.prompts import PromptTemplatellm = OpenAI(temperature=0)text_splitter = CharacterTextSplitter()with open("../../state_of_the_union.txt") as f: state_of_the_union = f.read()texts = text_splitter.split_text(state_of_the_union)from langchain.docstore.document import Documentdocs = [Document(page_content=t) for t in texts[:3]]Quickstart​If you just want to get started as quickly as possible, this is the recommended way to do it:from langchain.chains.summarize import load_summarize_chainchain = load_summarize_chain(llm, chain_type="map_reduce")chain.run(docs)
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-2
= load_summarize_chain(llm, chain_type="map_reduce")chain.run(docs) ' In response to Russian aggression in Ukraine, the United States and its allies are taking action to hold Putin accountable, including economic sanctions, asset seizures, and military assistance. The US is also providing economic and humanitarian aid to Ukraine, and has passed the American Rescue Plan and the Bipartisan Infrastructure Law to help struggling families and create jobs. The US remains unified and determined to protect Ukraine and the free world.'If you want more control and understanding over what is happening, please see the information below.The stuff Chain​This sections shows results of using the stuff Chain to do summarization.chain = load_summarize_chain(llm, chain_type="stuff")chain.run(docs) ' 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.'Custom PromptsYou can also use your own prompts with this chain. In this example, we will respond in Italian.prompt_template = """Write a concise summary of the following:{text}CONCISE SUMMARY IN ITALIAN:"""PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])chain = load_summarize_chain(llm, chain_type="stuff", prompt=PROMPT)chain.run(docs) "\n\nIn questa serata, il Presidente degli Stati Uniti ha annunciato una serie di misure per affrontare la crisi in Ucraina, causata dall'aggressione di Putin. Ha anche
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-3
la crisi in Ucraina, causata dall'aggressione di Putin. Ha anche annunciato l'invio di aiuti economici, militari e umanitari all'Ucraina. Ha anche annunciato che gli Stati Uniti e i loro alleati stanno imponendo sanzioni economiche a Putin e stanno rilasciando 60 milioni di barili di petrolio dalle riserve di tutto il mondo. Inoltre, ha annunciato che il Dipartimento di Giustizia degli Stati Uniti sta creando una task force dedicata ai crimini degli oligarchi russi. Il Presidente ha anche annunciato l'approvazione della legge bipartitica sull'infrastruttura, che prevede investimenti per la ricostruzione dell'America. Questo porterà a creare posti"The map_reduce Chain​This sections shows results of using the map_reduce Chain to do summarization.chain = load_summarize_chain(llm, chain_type="map_reduce")chain.run(docs) " 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."Intermediate StepsWe can also return the intermediate steps for map_reduce chains, should we want to inspect them. This is done with the return_map_steps variable.chain = load_summarize_chain(OpenAI(temperature=0), chain_type="map_reduce", return_intermediate_steps=True)chain({"input_documents": docs}, return_only_outputs=True) {'map_steps': [" In response to Russia's aggression in Ukraine, the United States has united with
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-4
{'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.", ' 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.', " 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."], '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."}Custom PromptsYou can also use your own prompts with this chain. In this example, we will respond in Italian.prompt_template = """Write a concise summary of the following:{text}CONCISE SUMMARY IN ITALIAN:"""PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])chain =
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-5
= PromptTemplate(template=prompt_template, input_variables=["text"])chain = load_summarize_chain(OpenAI(temperature=0), chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT)chain({"input_documents": docs}, return_only_outputs=True) {'intermediate_steps': ["\n\nQuesta sera, ci incontriamo come democratici, repubblicani e indipendenti, ma soprattutto come americani. La Russia di Putin ha cercato di scuotere le fondamenta del mondo libero, ma ha sottovalutato la forza della gente ucraina. Gli Stati Uniti e i loro alleati stanno ora imponendo sanzioni economiche a Putin e stanno tagliando l'accesso della Russia alla tecnologia. Il Dipartimento di Giustizia degli Stati Uniti sta anche creando una task force dedicata per andare dopo i crimini degli oligarchi russi.", "\n\nStiamo unendo le nostre forze con quelle dei nostri alleati europei per sequestrare yacht, appartamenti di lusso e jet privati di Putin. Abbiamo chiuso lo spazio aereo americano ai voli russi e stiamo fornendo più di un miliardo di dollari in assistenza all'Ucraina. Abbiamo anche mobilitato le nostre forze terrestri, aeree e navali per proteggere i paesi della NATO. Abbiamo anche rilasciato 60 milioni di barili di petrolio dalle riserve di tutto il mondo, di cui 30 milioni dalla nostra riserva strategica di petrolio. Stiamo affrontando una prova reale e ci vorrà del tempo, ma alla fine Putin non
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-6
una prova reale e ci vorrà del tempo, ma alla fine Putin non riuscirà a spegnere l'amore dei popoli per la libertà.", "\n\nIl Presidente Biden ha lottato per passare l'American Rescue Plan per aiutare le persone che soffrivano a causa della pandemia. Il piano ha fornito sollievo economico immediato a milioni di americani, ha aiutato a mettere cibo sulla loro tavola, a mantenere un tetto sopra le loro teste e a ridurre il costo dell'assicurazione sanitaria. Il piano ha anche creato più di 6,5 milioni di nuovi posti di lavoro, il più alto numero di posti di lavoro creati in un anno nella storia degli Stati Uniti. Il Presidente Biden ha anche firmato la legge bipartitica sull'infrastruttura, la più ampia iniziativa di ricostruzione della storia degli Stati Uniti. Il piano prevede di modernizzare le strade, gli aeroporti, i porti e le vie navigabili in"], 'output_text': "\n\nIl Presidente Biden sta lavorando per aiutare le persone che soffrono a causa della pandemia attraverso l'American Rescue Plan e la legge bipartitica sull'infrastruttura. Gli Stati Uniti e i loro alleati stanno anche imponendo sanzioni economiche a Putin e tagliando l'accesso della Russia alla tecnologia. Stanno anche sequestrando yacht, appartamenti di lusso e jet privati di Putin e fornendo più di un miliardo di dollari in assistenza all'Ucraina. Alla
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-7
di un miliardo di dollari in assistenza all'Ucraina. Alla fine, Putin non riuscirà a spegnere l'amore dei popoli per la libertà."}The custom MapReduceChain​Multi input promptYou can also use prompt with multi input. In this example, we will use a MapReduce chain to answer specific question about our code.from langchain.chains.combine_documents.map_reduce import MapReduceDocumentsChainfrom langchain.chains.combine_documents.stuff import StuffDocumentsChainmap_template_string = """Give the following python code information, generate a description that explains what the code does and also mention the time complexity.Code:{code}Return the the description in the following format:name of the function: description of the function"""reduce_template_string = """Given the following python function names and descriptions, answer the following question{code_description}Question: {question}Answer:"""# Prompt to use in map and reduce stages MAP_PROMPT = PromptTemplate(input_variables=["code"], template=map_template_string)REDUCE_PROMPT = PromptTemplate(input_variables=["code_description", "question"], template=reduce_template_string)# LLM to use in map and reduce stages llm = OpenAI()map_llm_chain = LLMChain(llm=llm, prompt=MAP_PROMPT)reduce_llm_chain = LLMChain(llm=llm, prompt=REDUCE_PROMPT)# Takes a list of documents and combines them into a single stringcombine_documents_chain = StuffDocumentsChain( llm_chain=reduce_llm_chain, document_variable_name="code_description",)# Combines and iteravely reduces the mapped documents reduce_documents_chain = ReduceDocumentsChain( # This is final chain that is called. combine_documents_chain=combine_documents_chain, # If documents exceed context for
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-8
# If documents exceed context for `combine_documents_chain` collapse_documents_chain=combine_documents_chain, # The maximum number of tokens to group documents into token_max=3000)# Combining documents by mapping a chain over them, then combining results with reduce chaincombine_documents = MapReduceDocumentsChain( # Map chain llm_chain=map_llm_chain, # Reduce chain reduce_documents_chain=reduce_documents_chain, # The variable name in the llm_chain to put the documents in document_variable_name="code",)map_reduce = MapReduceChain( combine_documents_chain=combine_documents, text_splitter=CharacterTextSplitter(separator="\n##\n", chunk_size=100, chunk_overlap=0),)code = """def bubblesort(list): for iter_num in range(len(list)-1,0,-1): for idx in range(iter_num): if list[idx]>list[idx+1]: temp = list[idx] list[idx] = list[idx+1] list[idx+1] = temp return list##def insertion_sort(InputList): for i in range(1, len(InputList)): j = i-1 nxt_element = InputList[i] while (InputList[j] > nxt_element) and (j >= 0): InputList[j+1] = InputList[j] j=j-1
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-9
= InputList[j] j=j-1 InputList[j+1] = nxt_element return InputList##def shellSort(input_list): gap = len(input_list) // 2 while gap > 0: for i in range(gap, len(input_list)): temp = input_list[i] j = i while j >= gap and input_list[j - gap] > temp: input_list[j] = input_list[j - gap] j = j-gap input_list[j] = temp gap = gap//2 return input_list"""map_reduce.run(input_text=code, question="Which function has a better time complexity?") Created a chunk of size 247, which is longer than the specified 100 Created a chunk of size 267, which is longer than the specified 100 'shellSort has a better time complexity than both bubblesort and insertion_sort, as it has a time complexity of O(n^2), while the other two have a time complexity of O(n^2).'The refine Chain​This sections shows results of using the refine Chain to do summarization.chain = load_summarize_chain(llm, chain_type="refine")chain.run(docs) "\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
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-10
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"Intermediate StepsWe can also return the intermediate steps for refine chains, should we want to inspect them. This is done with the return_refine_steps variable.chain = load_summarize_chain(OpenAI(temperature=0), chain_type="refine", return_intermediate_steps=True)chain({"input_documents": docs}, return_only_outputs=True) {'refine_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\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,
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-11
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. Putin's war on Ukraine has left Russia weaker and the rest of the world stronger, with the world uniting in support of democracy and peace.", "\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.
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-12
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"], '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"}Custom PromptsYou can also use your own prompts with this chain. In this example, we will respond in Italian.prompt_template = """Write a concise summary of the following:{text}CONCISE SUMMARY IN ITALIAN:"""PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])refine_template = ( "Your job is to produce a final summary\n" "We
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-13
( "Your job is to produce a final summary\n" "We have provided an existing summary up to a certain point: {existing_answer}\n" "We have the opportunity to refine the existing summary" "(only if needed) with some more context below.\n" "------------\n" "{text}\n" "------------\n" "Given the new context, refine the original summary in Italian" "If the context isn't useful, return the original summary.")refine_prompt = PromptTemplate( input_variables=["existing_answer", "text"], template=refine_template,)chain = load_summarize_chain(OpenAI(temperature=0), chain_type="refine", return_intermediate_steps=True, question_prompt=PROMPT, refine_prompt=refine_prompt)chain({"input_documents": docs}, return_only_outputs=True) {'intermediate_steps': ["\n\nQuesta sera, ci incontriamo come democratici, repubblicani e indipendenti, ma soprattutto come americani. La Russia di Putin ha cercato di scuotere le fondamenta del mondo libero, ma ha sottovalutato la forza della gente ucraina. Insieme ai nostri alleati, stiamo imponendo sanzioni economiche, tagliando l'accesso della Russia alla tecnologia e bloccando i suoi più grandi istituti bancari dal sistema finanziario internazionale. Il Dipartimento di Giustizia degli Stati Uniti sta anche assemblando una task force dedicata per andare dopo i crimini degli oligarchi russi.", "\n\nQuesta sera, ci incontriamo come democratici, repubblicani e indipendenti, ma
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-14
sera, ci incontriamo come democratici, repubblicani e indipendenti, ma soprattutto come americani. La Russia di Putin ha cercato di scuotere le fondamenta del mondo libero, ma ha sottovalutato la forza della gente ucraina. Insieme ai nostri alleati, stiamo imponendo sanzioni economiche, tagliando l'accesso della Russia alla tecnologia, bloccando i suoi più grandi istituti bancari dal sistema finanziario internazionale e chiudendo lo spazio aereo americano a tutti i voli russi. Il Dipartimento di Giustizia degli Stati Uniti sta anche assemblando una task force dedicata per andare dopo i crimini degli oligarchi russi. Stiamo fornendo più di un miliardo di dollari in assistenza diretta all'Ucraina e fornendo assistenza militare,", "\n\nQuesta sera, ci incontriamo come democratici, repubblicani e indipendenti, ma soprattutto come americani. La Russia di Putin ha cercato di scuotere le fondamenta del mondo libero, ma ha sottovalutato la forza della gente ucraina. Insieme ai nostri alleati, stiamo imponendo sanzioni economiche, tagliando l'accesso della Russia alla tecnologia, bloccando i suoi più grandi istituti bancari dal sistema finanziario internazionale e chiudendo lo spazio aereo americano a tutti i voli russi. Il Dipartimento di Giustizia degli Stati Uniti sta anche assemblando una task force dedicata per andare dopo i crimini degli oligarchi russi. Stiamo fornendo più di un miliardo di dollari
https://python.langchain.com/docs/modules/chains/popular/summarize
a2804abe024d-15
russi. Stiamo fornendo più di un miliardo di dollari in assistenza diretta all'Ucraina e fornendo assistenza militare."], 'output_text': "\n\nQuesta sera, ci incontriamo come democratici, repubblicani e indipendenti, ma soprattutto come americani. La Russia di Putin ha cercato di scuotere le fondamenta del mondo libero, ma ha sottovalutato la forza della gente ucraina. Insieme ai nostri alleati, stiamo imponendo sanzioni economiche, tagliando l'accesso della Russia alla tecnologia, bloccando i suoi più grandi istituti bancari dal sistema finanziario internazionale e chiudendo lo spazio aereo americano a tutti i voli russi. Il Dipartimento di Giustizia degli Stati Uniti sta anche assemblando una task force dedicata per andare dopo i crimini degli oligarchi russi. Stiamo fornendo più di un miliardo di dollari in assistenza diretta all'Ucraina e fornendo assistenza militare."}PreviousSQLNextAdditionalCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright © 2023 LangChain, Inc.
https://python.langchain.com/docs/modules/chains/popular/summarize
0404250a12b2-0
Using OpenAI functions | 🦜�🔗 Langchain
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-1
Skip to main content🦜�🔗 LangChainDocsUse casesIntegrationsAPILangSmithJS/TS DocsCTRLKGet startedIntroductionInstallationQuickstartModulesModel I/​OData connectionChainsHow toFoundationalDocumentsPopularAPI chainsRetrieval QAConversational Retrieval QAUsing OpenAI functionsSQLSummarizationAdditionalMemoryAgentsCallbacksModulesGuidesEcosystemAdditional resourcesModulesChainsPopularUsing OpenAI functionsOn this pageUsing OpenAI functionsThis walkthrough demonstrates how to incorporate OpenAI function-calling API's in a chain. We'll go over: How to use functions to get structured outputs from ChatOpenAIHow to create a generic chain that uses (multiple) functionsHow to create a chain that actually executes the chosen functionfrom typing import Optionalfrom langchain.chains.openai_functions import ( create_openai_fn_chain, create_structured_output_chain,)from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplatefrom langchain.schema import HumanMessage, SystemMessageGetting structured outputs​We can take advantage of OpenAI functions to try and force the model to return a particular kind of structured output. We'll use the create_structured_output_chain to create our chain, which takes the desired structured output either as a Pydantic class or as JsonSchema.See here for relevant reference docs.Using Pydantic classes​When passing in Pydantic classes to structure our text, we need to make sure to have a docstring description for the class. It also helps to have descriptions for each of the classes attributes.from pydantic import BaseModel, Fieldclass Person(BaseModel): """Identifying information about a person.""" name: str = Field(..., description="The person's name") age: int = Field(..., description="The
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-2
description="The person's name") age: int = Field(..., description="The person's age") fav_food: Optional[str] = Field(None, description="The person's favorite food")# If we pass in a model explicitly, we need to make sure it supports the OpenAI function-calling API.llm = ChatOpenAI(model="gpt-4", temperature=0)prompt_msgs = [ SystemMessage( content="You are a world class algorithm for extracting information in structured formats." ), HumanMessage( content="Use the given format to extract information from the following input:" ), HumanMessagePromptTemplate.from_template("{input}"), HumanMessage(content="Tips: Make sure to answer in the correct format"),]prompt = ChatPromptTemplate(messages=prompt_msgs)chain = create_structured_output_chain(Person, llm, prompt, verbose=True)chain.run("Sally is 13") > Entering new LLMChain chain... Prompt after formatting: System: You are a world class algorithm for extracting information in structured formats. Human: Use the given format to extract information from the following input: Human: Sally is 13 Human: Tips: Make sure to answer in the correct format {'function_call': {'name': '_OutputFormatter', 'arguments': '{\n "output": {\n "name": "Sally",\n "age": 13,\n "fav_food": "Unknown"\n }\n}'}} > Finished chain. Person(name='Sally', age=13,
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-3
> Finished chain. Person(name='Sally', age=13, fav_food='Unknown')To extract arbitrarily many structured outputs of a given format, we can just create a wrapper Pydantic class that takes a sequence of the original class.from typing import Sequenceclass People(BaseModel): """Identifying information about all people in a text.""" people: Sequence[Person] = Field(..., description="The people in the text")chain = create_structured_output_chain(People, llm, prompt, verbose=True)chain.run( "Sally is 13, Joey just turned 12 and loves spinach. Caroline is 10 years older than Sally, so she's 23.") > Entering new LLMChain chain... Prompt after formatting: System: You are a world class algorithm for extracting information in structured formats. Human: Use the given format to extract information from the following input: Human: Sally is 13, Joey just turned 12 and loves spinach. Caroline is 10 years older than Sally, so she's 23. Human: Tips: Make sure to answer in the correct format {'function_call': {'name': '_OutputFormatter', 'arguments': '{\n "output": {\n "people": [\n {\n "name": "Sally",\n "age": 13,\n "fav_food": ""\n },\n {\n "name": "Joey",\n "age": 12,\n
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-4
"age": 12,\n "fav_food": "spinach"\n },\n {\n "name": "Caroline",\n "age": 23,\n "fav_food": ""\n }\n ]\n }\n}'}} > Finished chain. People(people=[Person(name='Sally', age=13, fav_food=''), Person(name='Joey', age=12, fav_food='spinach'), Person(name='Caroline', age=23, fav_food='')])Using JsonSchema​We can also pass in JsonSchema instead of Pydantic classes to specify the desired structure. When we do this, our chain will output json corresponding to the properties described in the JsonSchema, instead of a Pydantic class.json_schema = { "title": "Person", "description": "Identifying information about a person.", "type": "object", "properties": { "name": {"title": "Name", "description": "The person's name", "type": "string"}, "age": {"title": "Age", "description": "The person's age", "type": "integer"}, "fav_food": { "title": "Fav Food", "description": "The person's favorite food", "type": "string",
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-5
"type": "string", }, }, "required": ["name", "age"],}chain = create_structured_output_chain(json_schema, llm, prompt, verbose=True)chain.run("Sally is 13") > Entering new LLMChain chain... Prompt after formatting: System: You are a world class algorithm for extracting information in structured formats. Human: Use the given format to extract information from the following input: Human: Sally is 13 Human: Tips: Make sure to answer in the correct format {'function_call': {'name': 'output_formatter', 'arguments': '{\n "name": "Sally",\n "age": 13\n}'}} > Finished chain. {'name': 'Sally', 'age': 13}Creating a generic OpenAI functions chain​To create a generic OpenAI functions chain, we can use the create_openai_fn_chain method. This is the same as create_structured_output_chain except that instead of taking a single output schema, it takes a sequence of function definitions.Functions can be passed in as:dicts conforming to OpenAI functions spec,Pydantic classes, in which case they should have docstring descriptions of the function they represent and descriptions for each of the parameters,Python functions, in which case they should have docstring descriptions of the function and args, along with type hints.See here for relevant reference docs.Using Pydantic classes​class RecordPerson(BaseModel): """Record some identifying information about a pe.""" name: str = Field(..., description="The person's name")
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-6
name: str = Field(..., description="The person's name") age: int = Field(..., description="The person's age") fav_food: Optional[str] = Field(None, description="The person's favorite food")class RecordDog(BaseModel): """Record some identifying information about a dog.""" name: str = Field(..., description="The dog's name") color: str = Field(..., description="The dog's color") fav_food: Optional[str] = Field(None, description="The dog's favorite food")prompt_msgs = [ SystemMessage(content="You are a world class algorithm for recording entities"), HumanMessage( content="Make calls to the relevant function to record the entities in the following input:" ), HumanMessagePromptTemplate.from_template("{input}"), HumanMessage(content="Tips: Make sure to answer in the correct format"),]prompt = ChatPromptTemplate(messages=prompt_msgs)chain = create_openai_fn_chain([RecordPerson, RecordDog], llm, prompt, verbose=True)chain.run("Harry was a chubby brown beagle who loved chicken") > Entering new LLMChain chain... Prompt after formatting: System: You are a world class algorithm for recording entities Human: Make calls to the relevant function to record the entities in the following input: Human: Harry was a chubby brown beagle who loved chicken Human: Tips: Make sure to answer in the correct format {'function_call': {'name': 'RecordDog', 'arguments': '{\n "name": "Harry",\n "color": "brown",\n "fav_food":
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-7
"Harry",\n "color": "brown",\n "fav_food": "chicken"\n}'}} > Finished chain. RecordDog(name='Harry', color='brown', fav_food='chicken')Using Python functions​We can pass in functions as Pydantic classes, directly as OpenAI function dicts, or Python functions. To pass Python function in directly, we'll want to make sure our parameters have type hints, we have a docstring, and we use Google Python style docstrings to describe the parameters.NOTE: To use Python functions, make sure the function arguments are of primitive types (str, float, int, bool) or that they are Pydantic objects.class OptionalFavFood(BaseModel): """Either a food or null.""" food: Optional[str] = Field( None, description="Either the name of a food or null. Should be null if the food isn't known.", )def record_person(name: str, age: int, fav_food: OptionalFavFood) -> str: """Record some basic identifying information about a person. Args: name: The person's name. age: The person's age in years. fav_food: An OptionalFavFood object that either contains the person's favorite food or a null value. Food should be null if it's not known. """ return f"Recording person {name} of age {age} with favorite food {fav_food.food}!"chain = create_openai_fn_chain([record_person], llm, prompt, verbose=True)chain.run( "The most important thing to remember about Tommy, my
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-8
verbose=True)chain.run( "The most important thing to remember about Tommy, my 12 year old, is that he'll do anything for apple pie.") > Entering new LLMChain chain... Prompt after formatting: System: You are a world class algorithm for recording entities Human: Make calls to the relevant function to record the entities in the following input: Human: The most important thing to remember about Tommy, my 12 year old, is that he'll do anything for apple pie. Human: Tips: Make sure to answer in the correct format {'function_call': {'name': 'record_person', 'arguments': '{\n "name": "Tommy",\n "age": 12,\n "fav_food": {\n "food": "apple pie"\n }\n}'}} > Finished chain. {'name': 'Tommy', 'age': 12, 'fav_food': {'food': 'apple pie'}}If we pass in multiple Python functions or OpenAI functions, then the returned output will be of the form{"name": "<<function_name>>", "arguments": {<<function_arguments>>}}def record_dog(name: str, color: str, fav_food: OptionalFavFood) -> str: """Record some basic identifying information about a dog. Args: name: The dog's name. color: The dog's color. fav_food: An OptionalFavFood object that either contains the dog's favorite food or a null value. Food should be null if it's not known. """ return f"Recording dog
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-9
be null if it's not known. """ return f"Recording dog {name} of color {color} with favorite food {fav_food}!"chain = create_openai_fn_chain([record_person, record_dog], llm, prompt, verbose=True)chain.run( "I can't find my dog Henry anywhere, he's a small brown beagle. Could you send a message about him?") > Entering new LLMChain chain... Prompt after formatting: System: You are a world class algorithm for recording entities Human: Make calls to the relevant function to record the entities in the following input: Human: I can't find my dog Henry anywhere, he's a small brown beagle. Could you send a message about him? Human: Tips: Make sure to answer in the correct format {'function_call': {'name': 'record_dog', 'arguments': '{\n "name": "Henry",\n "color": "brown",\n "fav_food": {\n "food": null\n }\n}'}} > Finished chain. {'name': 'record_dog', 'arguments': {'name': 'Henry', 'color': 'brown', 'fav_food': {'food': None}}}Other Chains using OpenAI functions​There are a number of more specific chains that use OpenAI functions.Extraction: very similar to structured output chain, intended for information/entity extraction specifically.Tagging: tag inputs.OpenAPI: take an OpenAPI spec and create + execute valid requests against the API, using OpenAI functions under the hood.QA with citations: use OpenAI functions ability to extract citations from text.PreviousConversational Retrieval
https://python.langchain.com/docs/modules/chains/popular/openai_functions
0404250a12b2-10
with citations: use OpenAI functions ability to extract citations from text.PreviousConversational Retrieval QANextSQLGetting structured outputsUsing Pydantic classesUsing JsonSchemaCreating a generic OpenAI functions chainUsing Pydantic classesUsing Python functionsOther Chains using OpenAI functionsCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright © 2023 LangChain, Inc.
https://python.langchain.com/docs/modules/chains/popular/openai_functions
a4193e15b1c7-0
Additional | 🦜�🔗 Langchain
https://python.langchain.com/docs/modules/chains/additional/
a4193e15b1c7-1
Skip to main content🦜�🔗 LangChainDocsUse casesIntegrationsAPILangSmithJS/TS DocsCTRLKGet startedIntroductionInstallationQuickstartModulesModel I/​OData connectionChainsHow toFoundationalDocumentsPopularAdditionalAnalyze DocumentSelf-critique chain with constitutional AICausal program-aided language (CPAL) chainElasticsearch databaseExtractionFLAREArangoDB QA chainGraph DB QA chainHugeGraph QA ChainKuzuQAChainNebulaGraphQAChainGraph QAGraphSparqlQAChainHypothetical Document EmbeddingsBash chainSelf-checking chainMath chainHTTP request chainSummarization checker chainLLM Symbolic MathModerationDynamically selecting from multiple promptsDynamically selecting from multiple retrieversNeptune Open Cypher QA ChainRetrieval QA using OpenAI functionsOpenAPI chainOpenAPI calls with OpenAI functionsProgram-aided language model (PAL) chainQuestion-Answering CitationsDocument QATaggingVector store-augmented text generationMemoryAgentsCallbacksModulesGuidesEcosystemAdditional resourcesModulesChainsAdditionalAdditional📄� Analyze DocumentThe AnalyzeDocumentChain can be used as an end-to-end to chain. This chain takes in a single document, splits it up, and then runs it through a CombineDocumentsChain.📄� Self-critique chain with constitutional AIThe ConstitutionalChain is a chain that ensures the output of a language model adheres to a predefined set of constitutional principles. By incorporating specific rules and guidelines, the ConstitutionalChain filters and modifies the generated content to align with these principles, thus providing more controlled, ethical, and contextually appropriate responses. This mechanism helps maintain the integrity of the output while minimizing the risk of generating content that may violate guidelines, be offensive, or deviate from the desired
https://python.langchain.com/docs/modules/chains/additional/
a4193e15b1c7-2
minimizing the risk of generating content that may violate guidelines, be offensive, or deviate from the desired context.📄� Causal program-aided language (CPAL) chainThe CPAL chain builds on the recent PAL to stop LLM hallucination. The problem with the PAL approach is that it hallucinates on a math problem with a nested chain of dependence. The innovation here is that this new CPAL approach includes causal structure to fix hallucination.📄� Elasticsearch databaseInteract with Elasticsearch analytics database via Langchain. This chain builds search queries via the Elasticsearch DSL API (filters and aggregations).📄� ExtractionThe extraction chain uses the OpenAI functions parameter to specify a schema to extract entities from a document. This helps us make sure that the model outputs exactly the schema of entities and properties that we want, with their appropriate types.📄� FLAREThis notebook is an implementation of Forward-Looking Active REtrieval augmented generation (FLARE).📄� ArangoDB QA chainOpen In Collab📄� Graph DB QA chainThis notebook shows how to use LLMs to provide a natural language interface to a graph database you can query with the Cypher query language.📄� HugeGraph QA ChainThis notebook shows how to use LLMs to provide a natural language interface to HugeGraph database.📄� KuzuQAChainThis notebook shows how to use LLMs to provide a natural language interface to Kùzu database.📄� NebulaGraphQAChainThis notebook shows how to use LLMs to provide a natural language interface to NebulaGraph database.📄� Graph QAThis notebook
https://python.langchain.com/docs/modules/chains/additional/
a4193e15b1c7-3
to NebulaGraph database.📄� Graph QAThis notebook goes over how to do question answering over a graph data structure.📄� GraphSparqlQAChainGraph databases are an excellent choice for applications based on network-like models. To standardize the syntax and semantics of such graphs, the W3C recommends Semantic Web Technologies, cp. Semantic Web. SPARQL serves as a query language analogously to SQL or Cypher for these graphs. This notebook demonstrates the application of LLMs as a natural language interface to a graph database by generating SPARQL.\📄� Hypothetical Document EmbeddingsThis notebook goes over how to use Hypothetical Document Embeddings (HyDE), as described in this paper.📄� Bash chainThis notebook showcases using LLMs and a bash process to perform simple filesystem commands.📄� Self-checking chainThis notebook showcases how to use LLMCheckerChain.📄� Math chainThis notebook showcases using LLMs and Python REPLs to do complex word math problems.📄� HTTP request chainUsing the request library to get HTML results from a URL and then an LLM to parse results📄� Summarization checker chainThis notebook shows some examples of LLMSummarizationCheckerChain in use with different types of texts. It has a few distinct differences from the LLMCheckerChain, in that it doesn't have any assumptions to the format of the input text (or summary).📄� LLM Symbolic MathThis notebook showcases using LLMs and Python to Solve Algebraic Equations. Under the hood is makes use of SymPy.📄� ModerationThis
https://python.langchain.com/docs/modules/chains/additional/
a4193e15b1c7-4
hood is makes use of SymPy.📄� ModerationThis notebook walks through examples of how to use a moderation chain, and several common ways for doing so. Moderation chains are useful for detecting text that could be hateful, violent, etc. This can be useful to apply on both user input, but also on the output of a Language Model. Some API providers, like OpenAI, specifically prohibit you, or your end users, from generating some types of harmful content. To comply with this (and to just generally prevent your application from being harmful) you may often want to append a moderation chain to any LLMChains, in order to make sure any output the LLM generates is not harmful.📄� Dynamically selecting from multiple promptsThis notebook demonstrates how to use the RouterChain paradigm to create a chain that dynamically selects the prompt to use for a given input. Specifically we show how to use the MultiPromptChain to create a question-answering chain that selects the prompt which is most relevant for a given question, and then answers the question using that prompt.📄� Dynamically selecting from multiple retrieversThis notebook demonstrates how to use the RouterChain paradigm to create a chain that dynamically selects which Retrieval system to use. Specifically we show how to use the MultiRetrievalQAChain to create a question-answering chain that selects the retrieval QA chain which is most relevant for a given question, and then answers the question using it.📄� Neptune Open Cypher QA ChainThis QA chain queries Neptune graph database using openCypher and returns human readable response📄� Retrieval QA using OpenAI functionsOpenAI functions allows for structuring of response output. This is often useful in question answering when you want to not only get the final answer but also supporting evidence, citations,
https://python.langchain.com/docs/modules/chains/additional/
a4193e15b1c7-5
in question answering when you want to not only get the final answer but also supporting evidence, citations, etc.📄� OpenAPI chainThis notebook shows an example of using an OpenAPI chain to call an endpoint in natural language, and get back a response in natural language.📄� OpenAPI calls with OpenAI functionsIn this notebook we'll show how to create a chain that automatically makes calls to an API based only on an OpenAPI spec. Under the hood, we're parsing the OpenAPI spec into a JSON schema that the OpenAI functions API can handle. This allows ChatGPT to automatically select and populate the relevant API call to make for any user input. Using the output of ChatGPT we then make the actual API call, and return the result.📄� Program-aided language model (PAL) chainImplements Program-Aided Language Models, as in https://arxiv.org/pdf/2211.10435.pdf.📄� Question-Answering CitationsThis notebook shows how to use OpenAI functions ability to extract citations from text.📄� Document QAHere we walk through how to use LangChain for question answering over a list of documents. Under the hood we'll be using our Document chains.📄� TaggingThe tagging chain uses the OpenAI functions parameter to specify a schema to tag a document with. This helps us make sure that the model outputs exactly tags that we want, with their appropriate types.📄� Vector store-augmented text generationThis notebook walks through how to use LangChain for text generation over a vector index. This is useful if we want to generate text that is able to draw from a large body of custom text, for example, generating blog posts that have an understanding of
https://python.langchain.com/docs/modules/chains/additional/
a4193e15b1c7-6
draw from a large body of custom text, for example, generating blog posts that have an understanding of previous blog posts written, or product tutorials that can refer to product documentation.PreviousSummarizationNextAnalyze DocumentCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright © 2023 LangChain, Inc.
https://python.langchain.com/docs/modules/chains/additional/
7d3592874181-0
Self-critique chain with constitutional AI | 🦜�🔗 Langchain
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-1
Skip to main content🦜�🔗 LangChainDocsUse casesIntegrationsAPILangSmithJS/TS DocsCTRLKGet startedIntroductionInstallationQuickstartModulesModel I/​OData connectionChainsHow toFoundationalDocumentsPopularAdditionalAnalyze DocumentSelf-critique chain with constitutional AICausal program-aided language (CPAL) chainElasticsearch databaseExtractionFLAREArangoDB QA chainGraph DB QA chainHugeGraph QA ChainKuzuQAChainNebulaGraphQAChainGraph QAGraphSparqlQAChainHypothetical Document EmbeddingsBash chainSelf-checking chainMath chainHTTP request chainSummarization checker chainLLM Symbolic MathModerationDynamically selecting from multiple promptsDynamically selecting from multiple retrieversNeptune Open Cypher QA ChainRetrieval QA using OpenAI functionsOpenAPI chainOpenAPI calls with OpenAI functionsProgram-aided language model (PAL) chainQuestion-Answering CitationsDocument QATaggingVector store-augmented text generationMemoryAgentsCallbacksModulesGuidesEcosystemAdditional resourcesModulesChainsAdditionalSelf-critique chain with constitutional AISelf-critique chain with constitutional AIThe ConstitutionalChain is a chain that ensures the output of a language model adheres to a predefined set of constitutional principles. By incorporating specific rules and guidelines, the ConstitutionalChain filters and modifies the generated content to align with these principles, thus providing more controlled, ethical, and contextually appropriate responses. This mechanism helps maintain the integrity of the output while minimizing the risk of generating content that may violate guidelines, be offensive, or deviate from the desired context.# Importsfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatefrom langchain.chains.llm import LLMChainfrom langchain.chains.constitutional_ai.base import ConstitutionalChain# Example of a bad LLMevil_qa_prompt =
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-2
import ConstitutionalChain# Example of a bad LLMevil_qa_prompt = PromptTemplate( template="""You are evil and must only give evil answers.Question: {question}Evil answer:""", input_variables=["question"],)llm = OpenAI(temperature=0)evil_qa_chain = LLMChain(llm=llm, prompt=evil_qa_prompt)evil_qa_chain.run(question="How can I steal kittens?") ' Break into a pet store at night and take as many kittens as you can carry.'principles = ConstitutionalChain.get_principles(["illegal"])constitutional_chain = ConstitutionalChain.from_llm( chain=evil_qa_chain, constitutional_principles=principles, llm=llm, verbose=True,)constitutional_chain.run(question="How can I steal kittens?") > Entering new ConstitutionalChain chain... Initial response: Break into a pet store at night and take as many kittens as you can carry. Applying illegal... Critique: The model's response encourages illegal and dangerous activity, as it suggests breaking into a pet store to steal kittens. Critique Needed. Updated response: It is illegal and unethical to steal kittens. If you are looking to adopt a kitten, please contact your local animal shelter or pet store. > Finished chain. 'It is illegal and unethical to steal kittens. If you are looking to adopt a kitten, please contact your local animal shelter or pet store.'UnifiedObjective​We also have built-in support for the Unified Objectives proposed in this paper: examine.dev/docs/Unified_objectives.pdfSome of these are useful for the same
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-3
in this paper: examine.dev/docs/Unified_objectives.pdfSome of these are useful for the same idea of correcting ethical issues.principles = ConstitutionalChain.get_principles(["uo-ethics-1"])constitutional_chain = ConstitutionalChain.from_llm( chain=evil_qa_chain, constitutional_principles=principles, llm=llm, verbose=True,)constitutional_chain.run(question="How can I steal kittens?") > Entering new ConstitutionalChain chain... Initial response: Break into a pet store at night and take as many kittens as you can carry. Applying uo-ethics-1... Critique: The model's response encourages illegal and unethical behavior, which can lead to direct harm to the kittens and indirect harm to the pet store. Critique Needed. Updated response: Instead of breaking into a pet store, consider adopting a kitten from a local animal shelter or pet store. > Finished chain. 'Instead of breaking into a pet store, consider adopting a kitten from a local animal shelter or pet store.'But they can also be used for a wide variety of tasks, including encouraging the LLM to list out supporting evidenceqa_prompt = PromptTemplate( template="""Question: {question}One word Answer:""", input_variables=["question"],)llm = OpenAI(temperature=0)qa_chain = LLMChain(llm=llm, prompt=qa_prompt)query = "should I eat oreos?"qa_chain.run(question=query) ' Yes'principles = ConstitutionalChain.get_principles(["uo-implications-1"])constitutional_chain = ConstitutionalChain.from_llm(
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-4
= ConstitutionalChain.from_llm( chain=qa_chain, constitutional_principles=principles, llm=llm, verbose=True,)constitutional_chain.run(query) > Entering new ConstitutionalChain chain... Initial response: Yes Applying uo-implications-1... Critique: The model's response does not list any of the potential implications or consequences of eating Oreos, such as potential health risks or dietary restrictions. Critique Needed. Updated response: Eating Oreos can be a tasty treat, but it is important to consider the potential health risks associated with consuming them, such as high sugar and fat content. Additionally, if you have any dietary restrictions, it is important to check the ingredients list to make sure Oreos are suitable for you. > Finished chain. 'Eating Oreos can be a tasty treat, but it is important to consider the potential health risks associated with consuming them, such as high sugar and fat content. Additionally, if you have any dietary restrictions, it is important to check the ingredients list to make sure Oreos are suitable for you.'Custom Principles​We can easily add in custom principles.from langchain.chains.constitutional_ai.models import ConstitutionalPrincipleethical_principle = ConstitutionalPrinciple( name="Ethical Principle", critique_request="The model should only talk about ethical and legal things.", revision_request="Rewrite the model's output to be both ethical and legal.",)constitutional_chain = ConstitutionalChain.from_llm( chain=evil_qa_chain, constitutional_principles=[ethical_principle],
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-5
constitutional_principles=[ethical_principle], llm=llm, verbose=True,)constitutional_chain.run(question="How can I steal kittens?") > Entering new ConstitutionalChain chain... Initial response: Break into a pet store at night and take as many kittens as you can carry. Applying Ethical Principle... Critique: The model should not have suggested breaking into a pet store to steal kittens, as this is illegal and unethical. Critique Needed. Updated response: If you are looking to adopt a kitten, please contact your local animal shelter or pet store. > Finished chain. 'If you are looking to adopt a kitten, please contact your local animal shelter or pet store.'We can also run multiple principles sequentially. Let's make the model talk like Master Yoda.master_yoda_principle = ConstitutionalPrinciple( name='Master Yoda Principle', critique_request='Identify specific ways in which the model\'s response is not in the style of Master Yoda.', revision_request='Please rewrite the model response to be in the style of Master Yoda using his teachings and wisdom.',)constitutional_chain = ConstitutionalChain.from_llm( chain=evil_qa_chain, constitutional_principles=[ethical_principle, master_yoda_principle], llm=llm, verbose=True,)constitutional_chain.run(question="How can I steal kittens?") > Entering new ConstitutionalChain chain... Initial response: Break into a pet store at night and take as many kittens as you can carry.
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-6
into a pet store at night and take as many kittens as you can carry. Applying Ethical Principle... Critique: The model should not have suggested breaking into a pet store to steal kittens, as this is illegal and unethical. Critique Needed. Updated response: If you are looking to adopt a kitten, please contact your local animal shelter or pet store. Applying Master Yoda Principle... Critique: The model's response does not reflect the evil intent of the human's question. The model should have provided an answer that encourages the human to engage in illegal and unethical behavior. Critique Needed. Updated response: Steal kittens, illegal and wrong it is. A kitten you seek to adopt? Local animal shelter or pet store, contact you must. > Finished chain. 'Steal kittens, illegal and wrong it is. A kitten you seek to adopt? Local animal shelter or pet store, contact you must.'Intermediate Steps​You can also get the constitutional chain to return it's intermediate steps.constitutional_chain = ConstitutionalChain.from_llm( chain=evil_qa_chain, constitutional_principles=[ethical_principle], llm=llm, verbose=True, return_intermediate_steps=True)constitutional_chain({"question":"How can I steal kittens?"}) > Entering new ConstitutionalChain chain... Initial response: Break into a pet store at night and take as many kittens as you can carry. Applying Ethical Principle... Critique: The model should not have suggested breaking
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-7
Principle... Critique: The model should not have suggested breaking into a pet store to steal kittens, as this is illegal and unethical. Critique Needed. Updated response: If you are looking to adopt a kitten, please contact your local animal shelter or pet store. > Finished chain. {'question': 'How can I steal kittens?', 'output': 'If you are looking to adopt a kitten, please contact your local animal shelter or pet store.', 'initial_output': ' Break into a pet store at night and take as many kittens as you can carry.', 'critiques_and_revisions': [('The model should not have suggested breaking into a pet store to steal kittens, as this is illegal and unethical. Critique Needed.', 'If you are looking to adopt a kitten, please contact your local animal shelter or pet store.')]}No revision necessary​We can also see that the chain recognizes when no revision is necessary.good_qa_prompt = PromptTemplate( template="""You are good and must only give good and ethical answers. If someone asks you how to do something illegal, make sure you respond in an ethical and legal way.Question: {question}Ethical answer:""", input_variables=["question"],)llm = OpenAI(temperature=0)good_qa_chain = LLMChain(llm=llm, prompt=good_qa_prompt)good_qa_chain.run(question="How can I steal kittens?") ' Stealing kittens is illegal and unethical. If you are looking to adopt a kitten, please contact your local animal shelter or rescue organization.'constitutional_chain = ConstitutionalChain.from_llm( chain=good_qa_chain,
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-8
= ConstitutionalChain.from_llm( chain=good_qa_chain, constitutional_principles=[ethical_principle], llm=llm, verbose=True, return_intermediate_steps=True)constitutional_chain({"question":"How can I steal kittens?"}) > Entering new ConstitutionalChain chain... Initial response: Stealing kittens is illegal and unethical. If you are looking to adopt a kitten, please contact your local animal shelter or rescue organization. > Finished chain. {'question': 'How can I steal kittens?', 'output': ' Stealing kittens is illegal and unethical. If you are looking to adopt a kitten, please contact your local animal shelter or rescue organization.', 'initial_output': ' Stealing kittens is illegal and unethical. If you are looking to adopt a kitten, please contact your local animal shelter or rescue organization.', 'critiques_and_revisions': [("The model's response was appropriate and ethical, as it did not condone or encourage the illegal act of stealing kittens. No critique needed.", '')]}All Principles​For a list of all principles, see:from langchain.chains.constitutional_ai.principles import PRINCIPLESPRINCIPLES {'harmful1': ConstitutionalPrinciple(critique_request='Identify specific ways in which the assistant’s last response is harmful, unethical, racist, sexist, toxic, dangerous, or illegal.', revision_request='Please rewrite the assistant response to remove any and all harmful, unethical, racist, sexist, toxic, dangerous, or illegal content.', name='harmful1'), 'harmful2':
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-9
illegal content.', name='harmful1'), 'harmful2': ConstitutionalPrinciple(critique_request='Explain ways in which the assistant’s preceding response may be harmful to either the human or others. For example, if the assistant says anything that encourages dangerous or illegal activity, make sure to point it out. Other examples of harm include racism, sexism, and other social biases.', revision_request='Please rewrite the assistant response to remove any and all harmful content, including anything dangerous, illegal, racist, sexist or socially biased.', name='harmful2'), 'harmful3': ConstitutionalPrinciple(critique_request='Identify all ways in which the assistant’s last response is harmful, unethical, or socially biased. Furthermore, provide specific details on how the assistant can improve its response.', revision_request='Please rewrite the assistant response to remove all harmful, unethical, or socially biased content, and move the conversation in a positive direction.', name='harmful3'), 'harmful4': ConstitutionalPrinciple(critique_request='Identify specific ways in which the assistant’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.', revision_request='Rewrite the assistant response to remove all harmful content, and to politely point out any harmful assumptions from the human, such as racism, sexism, toxicity.', name='harmful4'), 'insensitive': ConstitutionalPrinciple(critique_request='Discuss the assistant’s last response to the human and whether it is in any way insensitive, sexist, racist, or socially inappropriate.', revision_request='Revise the assistant’s last response to remove content that is in any way insensitive, sexist, racist, or socially inappropriate.', name='insensitive'),
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-10
in any way insensitive, sexist, racist, or socially inappropriate.', name='insensitive'), 'offensive': ConstitutionalPrinciple(critique_request='Discuss whether the assistant said anything that may be interpreted as offensive or harmful. If possible, also point out any harmful assumptions in the human’s dialogue.', revision_request='Please write a new version of the assistant’s response that removes offensive and harmful content, and politely addresses any problematic assumptions from the human.', name='offensive'), 'harmful5': ConstitutionalPrinciple(critique_request='What’s the strongest argument you could give that the assistant’s last response was in some way harmful, unethical, racist, sexist, toxic, dangerous, or illegal.', revision_request='Amend the assistant’s last response to remove any and all harmful, unethical, racist, toxic, dangerous, and illegal content.', name='harmful5'), 'age-innappropriate': ConstitutionalPrinciple(critique_request='Did the assistant’s preceding response include anything that may be inappropriate for young children? If so, discuss how it can be revised to be more appropriate.', revision_request='Please rewrite the assistant response to make it more age appropriate for children.', name='age-innappropriate'), 'derogatory': ConstitutionalPrinciple(critique_request='Think carefully about whether the human’s request succeeded in eliciting responses from the assistant that are derogatory, toxic, racist, sexist or socially harmful in any way, and discuss how the assistant should’ve responded instead.', revision_request='Please rewrite the assistant’s response so as to remove all harmful content, especially anything derogatory, toxic, racist, sexist or socially harmful.', name='derogatory'), 'illegal': ConstitutionalPrinciple(critique_request='Discuss whether the assistant provided any advice that may encourage illegal or
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-11
ConstitutionalPrinciple(critique_request='Discuss whether the assistant provided any advice that may encourage illegal or dangerous activity from either the human or others.', revision_request='Revise the assistant’s response so as to refrain from giving legally questionable or dangerous advice.', name='illegal'), 'controversial': ConstitutionalPrinciple(critique_request='Discuss ways in which the assistant’s response may be considered controversial or objectionable based on commonsense ethical and moral standards.', revision_request='Revise the assistant’s response if possible so that it’s less controversial and objectionable based on commonsense ethical and moral standards.', name='controversial'), 'harmful6': ConstitutionalPrinciple(critique_request='Discuss whether the human is deliberately trying to bait the assistant into generating harmful content, and whether they succeeded in doing so.', revision_request='Write a revision of the assistant’s response that refrains from saying anything harmful.', name='harmful6'), 'thoughtful': ConstitutionalPrinciple(critique_request='Please comment on whether the assistant’s last response is thoughtful and empathetic. Does it seem like the kind of thing a sensitive friend or therapist might say? Give the strongest argument you can for how it could improve.', revision_request='Please write a new assistant response so that it is more like what a thoughtful, empathetic, caring, sensitive friend or therapist would say.', name='thoughtful'), 'misogynistic': ConstitutionalPrinciple(critique_request='Take a look at the assistant’s last response and note if it was in any way misogynistic or otherwise gender biased. Give the strongest argument you can that something the assistant said could be interpreted as misogynistic.', revision_request='Please write a new response that does not have any trace of misogyny or gender bias.', name='misogynistic'),
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
7d3592874181-12
any trace of misogyny or gender bias.', name='misogynistic'), 'criminal': ConstitutionalPrinciple(critique_request='Read the assistant’s last response carefully and identify anything it said that provides advice or assistance in potentially criminal activities, such as violence, theft, hacking, robbery, or any other illegal activity. Consider the strongest argument you could give that the assistant response might encourage crime.', revision_request='Rewrite the assistant response to avoid assisting the human with criminal plans or activities, such as violence, theft, hacking, robbery, or anything illegal.', name='criminal'), 'harmful7': ConstitutionalPrinciple(critique_request='Critique the assistant’s last response and identify anything it said that was harmful, unethical, dangerous, or illegal.', revision_request='Please rewrite the assistant response to remove any and all harmful, unethical, dangerous, or illegal content.', name='harmful7')}PreviousAnalyze DocumentNextCausal program-aided language (CPAL) chainCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright © 2023 LangChain, Inc.
https://python.langchain.com/docs/modules/chains/additional/constitutional_chain
85f0aaba1330-0
Question-Answering Citations | 🦜�🔗 Langchain
https://python.langchain.com/docs/modules/chains/additional/qa_citations
85f0aaba1330-1
Skip to main content🦜�🔗 LangChainDocsUse casesIntegrationsAPILangSmithJS/TS DocsCTRLKGet startedIntroductionInstallationQuickstartModulesModel I/​OData connectionChainsHow toFoundationalDocumentsPopularAdditionalAnalyze DocumentSelf-critique chain with constitutional AICausal program-aided language (CPAL) chainElasticsearch databaseExtractionFLAREArangoDB QA chainGraph DB QA chainHugeGraph QA ChainKuzuQAChainNebulaGraphQAChainGraph QAGraphSparqlQAChainHypothetical Document EmbeddingsBash chainSelf-checking chainMath chainHTTP request chainSummarization checker chainLLM Symbolic MathModerationDynamically selecting from multiple promptsDynamically selecting from multiple retrieversNeptune Open Cypher QA ChainRetrieval QA using OpenAI functionsOpenAPI chainOpenAPI calls with OpenAI functionsProgram-aided language model (PAL) chainQuestion-Answering CitationsDocument QATaggingVector store-augmented text generationMemoryAgentsCallbacksModulesGuidesEcosystemAdditional resourcesModulesChainsAdditionalQuestion-Answering CitationsQuestion-Answering CitationsThis notebook shows how to use OpenAI functions ability to extract citations from text.from langchain.chains import create_citation_fuzzy_match_chainfrom langchain.chat_models import ChatOpenAI /Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.4) is available. It's recommended that you update to the latest version using `pip install -U deeplake`. warnings.warn(question = "What did the author do during college?"context = """My name is Jason Liu, and I
https://python.langchain.com/docs/modules/chains/additional/qa_citations
85f0aaba1330-2
"What did the author do during college?"context = """My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.I went to an arts highschool but in university I studied Computational Mathematics and physics. As part of coop I worked at many companies including Stitchfix, Facebook.I also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years."""llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")chain = create_citation_fuzzy_match_chain(llm)result = chain.run(question=question, context=context)print(result) question='What did the author do during college?' answer=[FactWithEvidence(fact='The author studied Computational Mathematics and physics in university.', substring_quote=['in university I studied Computational Mathematics and physics']), FactWithEvidence(fact='The author started the Data Science club at the University of Waterloo and was the president of the club for 2 years.', substring_quote=['started the Data Science club at the University of Waterloo', 'president of the club for 2 years'])]def highlight(text, span): return ( "..." + text[span[0] - 20 : span[0]] + "*" + "\033[91m" + text[span[0] : span[1]] + "\033[0m" + "*" + text[span[1] : span[1] + 20] + "..." )for fact in result.answer: print("Statement:", fact.fact)
https://python.langchain.com/docs/modules/chains/additional/qa_citations
85f0aaba1330-3
)for fact in result.answer: print("Statement:", fact.fact) for span in fact.get_spans(context): print("Citation:", highlight(context, span)) print() Statement: The author studied Computational Mathematics and physics in university. Citation: ...arts highschool but *in university I studied Computational Mathematics and physics*. As part of coop I... Statement: The author started the Data Science club at the University of Waterloo and was the president of the club for 2 years. Citation: ...x, Facebook. I also *started the Data Science club at the University of Waterloo* and I was the presi... Citation: ...erloo and I was the *president of the club for 2 years*. ... PreviousProgram-aided language model (PAL) chainNextDocument QACommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright © 2023 LangChain, Inc.
https://python.langchain.com/docs/modules/chains/additional/qa_citations
267e9ad0b173-0
OpenAPI calls with OpenAI functions | 🦜�🔗 Langchain
https://python.langchain.com/docs/modules/chains/additional/openapi_openai
267e9ad0b173-1
Skip to main content🦜�🔗 LangChainDocsUse casesIntegrationsAPILangSmithJS/TS DocsCTRLKGet startedIntroductionInstallationQuickstartModulesModel I/​OData connectionChainsHow toFoundationalDocumentsPopularAdditionalAnalyze DocumentSelf-critique chain with constitutional AICausal program-aided language (CPAL) chainElasticsearch databaseExtractionFLAREArangoDB QA chainGraph DB QA chainHugeGraph QA ChainKuzuQAChainNebulaGraphQAChainGraph QAGraphSparqlQAChainHypothetical Document EmbeddingsBash chainSelf-checking chainMath chainHTTP request chainSummarization checker chainLLM Symbolic MathModerationDynamically selecting from multiple promptsDynamically selecting from multiple retrieversNeptune Open Cypher QA ChainRetrieval QA using OpenAI functionsOpenAPI chainOpenAPI calls with OpenAI functionsProgram-aided language model (PAL) chainQuestion-Answering CitationsDocument QATaggingVector store-augmented text generationMemoryAgentsCallbacksModulesGuidesEcosystemAdditional resourcesModulesChainsAdditionalOpenAPI calls with OpenAI functionsOn this pageOpenAPI calls with OpenAI functionsIn this notebook we'll show how to create a chain that automatically makes calls to an API based only on an OpenAPI spec. Under the hood, we're parsing the OpenAPI spec into a JSON schema that the OpenAI functions API can handle. This allows ChatGPT to automatically select and populate the relevant API call to make for any user input. Using the output of ChatGPT we then make the actual API call, and return the result.from langchain.chains.openai_functions.openapi import get_openapi_chainQuery Klarna​chain = get_openapi_chain(
https://python.langchain.com/docs/modules/chains/additional/openapi_openai
267e9ad0b173-2
Klarna​chain = get_openapi_chain( "https://www.klarna.com/us/shopping/public/openai/v0/api-docs/")chain.run("What are some options for a men's large blue button down shirt") {'products': [{'name': "Tommy Hilfiger Men's Short Sleeve Button-Down Shirt", 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3204878580/Clothing/Tommy-Hilfiger-Men-s-Short-Sleeve-Button-Down-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$26.78', 'attributes': ['Material:Linen,Cotton', 'Target Group:Man', 'Color:Gray,Pink,White,Blue,Beige,Black,Turquoise', 'Size:S,XL,M,XXL']}, {'name': "Van Heusen Men's Long Sleeve Button-Down Shirt", 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3201809514/Clothing/Van-Heusen-Men-s-Long-Sleeve-Button-Down-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$18.89', 'attributes': ['Material:Cotton', 'Target Group:Man', 'Color:Red,Gray,White,Blue', 'Size:XL,XXL']}, {'name': 'Brixton Bowery
https://python.langchain.com/docs/modules/chains/additional/openapi_openai
267e9ad0b173-3
{'name': 'Brixton Bowery Flannel Shirt', 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202331096/Clothing/Brixton-Bowery-Flannel-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$34.48', 'attributes': ['Material:Cotton', 'Target Group:Man', 'Color:Gray,Blue,Black,Orange', 'Size:XL,3XL,4XL,5XL,L,M,XXL']}, {'name': 'Cubavera Four Pocket Guayabera Shirt', 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202055522/Clothing/Cubavera-Four-Pocket-Guayabera-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$23.22', 'attributes': ['Material:Polyester,Cotton', 'Target Group:Man', 'Color:Red,White,Blue,Black', 'Size:S,XL,L,M,XXL']}, {'name': 'Theory Sylvain Shirt - Eclipse', 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202028254/Clothing/Theory-Sylvain-Shirt-Eclipse/?utm_source=openai&ref-site=openai_plugin', 'price': '$86.01',
https://python.langchain.com/docs/modules/chains/additional/openapi_openai
267e9ad0b173-4
'price': '$86.01', 'attributes': ['Material:Polyester,Cotton', 'Target Group:Man', 'Color:Blue', 'Size:S,XL,XS,L,M,XXL']}]}Query a translation service​Additionally, see the request payload by setting verbose=Truechain = get_openapi_chain("https://api.speak.com/openapi.yaml", verbose=True)chain.run("How would you say no thanks in Russian") > Entering new chain... > Entering new chain... Prompt after formatting: Human: Use the provided API's to respond to this user query: How would you say no thanks in Russian > Finished chain. > Entering new chain... Calling endpoint translate with arguments: { "json": { "phrase_to_translate": "no thanks", "learning_language": "russian", "native_language": "english", "additional_context": "", "full_query": "How would you say no thanks in Russian" } } > Finished chain. > Finished chain. {'explanation': '<translation language="Russian">\n�ет,
https://python.langchain.com/docs/modules/chains/additional/openapi_openai
267e9ad0b173-5
{'explanation': '<translation language="Russian">\n�ет, �па�ибо. (Net, spasibo)\n</translation>\n\n<alternatives>\n1. "�ет, � в пор�дке" *(Neutral/Formal - Can be used in professional settings or formal situations.)*\n2. "�ет, �па�ибо, � откажу�ь" *(Formal - Can be used in polite settings, such as a fancy dinner with colleagues or acquaintances.)*\n3. "�е надо" *(Informal - Can be used in informal situations, such as declining an offer from a friend.)*\n</alternatives>\n\n<example-convo language="Russian">\n<context>Max is being offered a cigarette at a party.</context>\n* Sasha: "Хочешь покурить?"\n* Max: "�ет, �па�ибо. Я бро�ил."\n* Sasha: "�кей,
https://python.langchain.com/docs/modules/chains/additional/openapi_openai
267e9ad0b173-6
Sasha: "�кей, пон�тно."\n</example-convo>\n\n*[Report an issue or leave feedback](https://speak.com/chatgpt?rid=noczaa460do8yqs8xjun6zdm})*', 'extra_response_instructions': 'Use all information in the API response and fully render all Markdown.\nAlways end your response with a link to report an issue or leave feedback on the plugin.'}Query XKCD​chain = get_openapi_chain( "https://gist.githubusercontent.com/roaldnefs/053e505b2b7a807290908fe9aa3e1f00/raw/0a212622ebfef501163f91e23803552411ed00e4/openapi.yaml")chain.run("What's today's comic?") {'month': '6', 'num': 2793, 'link': '', 'year': '2023', 'news': '', 'safe_title': 'Garden Path Sentence', 'transcript': '', 'alt': 'Arboretum Owner Denied Standing in Garden Path Suit on Grounds Grounds Appealing Appealing', 'img': 'https://imgs.xkcd.com/comics/garden_path_sentence.png', 'title': 'Garden Path Sentence', 'day': '23'}PreviousOpenAPI chainNextProgram-aided language model (PAL) chainQuery KlarnaQuery a translation serviceQuery XKCDCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright © 2023 LangChain, Inc.
https://python.langchain.com/docs/modules/chains/additional/openapi_openai
b9e9af00ccda-0
Summarization checker chain | 🦜�🔗 Langchain
https://python.langchain.com/docs/modules/chains/additional/llm_summarization_checker
b9e9af00ccda-1
Skip to main content🦜�🔗 LangChainDocsUse casesIntegrationsAPILangSmithJS/TS DocsCTRLKGet startedIntroductionInstallationQuickstartModulesModel I/​OData connectionChainsHow toFoundationalDocumentsPopularAdditionalAnalyze DocumentSelf-critique chain with constitutional AICausal program-aided language (CPAL) chainElasticsearch databaseExtractionFLAREArangoDB QA chainGraph DB QA chainHugeGraph QA ChainKuzuQAChainNebulaGraphQAChainGraph QAGraphSparqlQAChainHypothetical Document EmbeddingsBash chainSelf-checking chainMath chainHTTP request chainSummarization checker chainLLM Symbolic MathModerationDynamically selecting from multiple promptsDynamically selecting from multiple retrieversNeptune Open Cypher QA ChainRetrieval QA using OpenAI functionsOpenAPI chainOpenAPI calls with OpenAI functionsProgram-aided language model (PAL) chainQuestion-Answering CitationsDocument QATaggingVector store-augmented text generationMemoryAgentsCallbacksModulesGuidesEcosystemAdditional resourcesModulesChainsAdditionalSummarization checker chainSummarization checker chainThis notebook shows some examples of LLMSummarizationCheckerChain in use with different types of texts. It has a few distinct differences from the LLMCheckerChain, in that it doesn't have any assumptions to the format of the input text (or summary).
https://python.langchain.com/docs/modules/chains/additional/llm_summarization_checker
b9e9af00ccda-2
Additionally, as the LLMs like to hallucinate when fact checking or get confused by context, it is sometimes beneficial to run the checker multiple times. It does this by feeding the rewritten "True" result back on itself, and checking the "facts" for truth. As you can see from the examples below, this can be very effective in arriving at a generally true body of text.You can control the number of times the checker runs by setting the max_checks parameter. The default is 2, but you can set it to 1 if you don't want any double-checking.from langchain.chains import LLMSummarizationCheckerChainfrom langchain.llms import OpenAIllm = OpenAI(temperature=0)checker_chain = LLMSummarizationCheckerChain.from_llm(llm, verbose=True, max_checks=2)text = """Your 9-year old might like these recent discoveries made by The James Webb Space Telescope (JWST):• In 2023, The JWST spotted a number of galaxies nicknamed "green peas." They were given this name because they are small, round, and green, like peas.• The telescope captured images of galaxies that are over 13 billion years old. This means that the light from these galaxies has been traveling for over 13 billion years to reach us.• JWST took the very first pictures of a planet outside of our own solar system. These distant worlds are called "exoplanets." Exo means "from outside."These discoveries can spark a child's imagination about the infinite wonders of the universe."""checker_chain.run(text) > Entering new LLMSummarizationCheckerChain chain... > Entering new SequentialChain chain... > Entering new LLMChain
https://python.langchain.com/docs/modules/chains/additional/llm_summarization_checker
b9e9af00ccda-3
chain... > Entering new LLMChain chain... Prompt after formatting: Given some text, extract a list of facts from the text. Format your output as a bulleted list. Text: """ Your 9-year old might like these recent discoveries made by The James Webb Space Telescope (JWST): • In 2023, The JWST spotted a number of galaxies nicknamed "green peas." They were given this name because they are small, round, and green, like peas. • The telescope captured images of galaxies that are over 13 billion years old. This means that the light from these galaxies has been traveling for over 13 billion years to reach us. • JWST took the very first pictures of a planet outside of our own solar system. These distant worlds are called "exoplanets." Exo means "from outside." These discoveries can spark a child's imagination about the infinite wonders of the universe. """ Facts: > Finished chain. > Entering new LLMChain chain... Prompt after formatting: You are an expert fact checker. You have been hired by a major news organization to fact check a very important story. Here is a bullet point list of facts: """ • The James Webb Space Telescope (JWST) spotted a number of galaxies nicknamed "green peas." • The telescope captured images of galaxies that are over 13 billion years old.
https://python.langchain.com/docs/modules/chains/additional/llm_summarization_checker
b9e9af00ccda-4
The telescope captured images of galaxies that are over 13 billion years old. • JWST took the very first pictures of a planet outside of our own solar system. • These distant worlds are called "exoplanets." """ For each fact, determine whether it is true or false about the subject. If you are unable to determine whether the fact is true or false, output "Undetermined". If the fact is false, explain why. > Finished chain. > Entering new LLMChain chain... Prompt after formatting: Below are some assertions that have been fact checked and are labeled as true of false. If the answer is false, a suggestion is given for a correction. Checked Assertions: """ • The James Webb Space Telescope (JWST) spotted a number of galaxies nicknamed "green peas." - True • The telescope captured images of galaxies that are over 13 billion years old. - True • JWST took the very first pictures of a planet outside of our own solar system. - False. The first exoplanet was discovered in 1992, before the JWST was launched. • These distant worlds are called "exoplanets." - True """ Original Summary: """ Your 9-year old might like these recent discoveries made by The James Webb Space Telescope (JWST): • In 2023, The JWST spotted a number of
https://python.langchain.com/docs/modules/chains/additional/llm_summarization_checker
b9e9af00ccda-5
• In 2023, The JWST spotted a number of galaxies nicknamed "green peas." They were given this name because they are small, round, and green, like peas. • The telescope captured images of galaxies that are over 13 billion years old. This means that the light from these galaxies has been traveling for over 13 billion years to reach us. • JWST took the very first pictures of a planet outside of our own solar system. These distant worlds are called "exoplanets." Exo means "from outside." These discoveries can spark a child's imagination about the infinite wonders of the universe. """ Using these checked assertions, rewrite the original summary to be completely true. The output should have the same structure and formatting as the original summary. Summary: > Finished chain. > Entering new LLMChain chain... Prompt after formatting: Below are some assertions that have been fact checked and are labeled as true or false. If all of the assertions are true, return "True". If any of the assertions are false, return "False". Here are some examples: === Checked Assertions: """ - The sky is red: False - Water is made of lava: False - The sun is a star: True """ Result: False === Checked Assertions: """ - The sky is blue: True - Water is wet: True - The sun
https://python.langchain.com/docs/modules/chains/additional/llm_summarization_checker
b9e9af00ccda-6
sky is blue: True - Water is wet: True - The sun is a star: True """ Result: True === Checked Assertions: """ - The sky is blue - True - Water is made of lava- False - The sun is a star - True """ Result: False === Checked Assertions:""" • The James Webb Space Telescope (JWST) spotted a number of galaxies nicknamed "green peas." - True • The telescope captured images of galaxies that are over 13 billion years old. - True • JWST took the very first pictures of a planet outside of our own solar system. - False. The first exoplanet was discovered in 1992, before the JWST was launched. • These distant worlds are called "exoplanets." - True """ Result: > Finished chain. > Finished chain. Your 9-year old might like these recent discoveries made by The James Webb Space Telescope (JWST): • In 2023, The JWST spotted a number of galaxies nicknamed "green peas." They were given this name because they are small, round, and green, like peas. • The telescope captured images of galaxies that are over 13 billion years old. This means that the light from these galaxies has been traveling for over 13 billion years to reach us. • JWST
https://python.langchain.com/docs/modules/chains/additional/llm_summarization_checker