langchain/docs/modules/chains/generic/from_hub.ipynb

168 lines
4.1 KiB
Plaintext
Raw Normal View History

2023-01-27 15:10:26 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "25c90e9e",
"metadata": {},
"source": [
"# Loading from LangChainHub\n",
"\n",
"This notebook covers how to load chains from [LangChainHub](https://github.com/hwchase17/langchain-hub)."
]
},
{
"cell_type": "code",
2023-02-13 07:02:01 +00:00
"execution_count": 5,
2023-01-27 15:10:26 +00:00
"id": "8b54479e",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import load_chain\n",
"\n",
"chain = load_chain(\"lc://chains/llm-math/chain.json\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4828f31f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
"whats 2 raised to .12\u001b[32;1m\u001b[1;3m\n",
"Answer: 1.0791812460476249\u001b[0m\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
2023-01-27 15:10:26 +00:00
]
},
{
"data": {
"text/plain": [
"'Answer: 1.0791812460476249'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.run(\"whats 2 raised to .12\")"
]
},
{
"cell_type": "markdown",
"id": "8db72cda",
"metadata": {},
"source": [
"Sometimes chains will require extra arguments that were not serialized with the chain. For example, a chain that does question answering over a vector database will require a vector database."
]
},
{
"cell_type": "code",
2023-02-13 07:02:01 +00:00
"execution_count": 1,
2023-01-27 15:10:26 +00:00
"id": "aab39528",
"metadata": {},
"outputs": [],
"source": [
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
2023-02-13 07:02:01 +00:00
"from langchain.vectorstores import Chroma\n",
2023-01-27 15:10:26 +00:00
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain import OpenAI, VectorDBQA"
]
},
{
"cell_type": "code",
2023-02-13 07:02:01 +00:00
"execution_count": 3,
2023-01-27 15:10:26 +00:00
"id": "16a85d5e",
"metadata": {},
2023-02-13 07:02:01 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running Chroma using direct local API.\n",
"Using DuckDB in-memory for database. Data will be transient.\n"
]
}
],
2023-01-27 15:10:26 +00:00
"source": [
2023-02-13 07:02:01 +00:00
"from langchain.document_loaders import TextLoader\n",
"loader = TextLoader('../../state_of_the_union.txt')\n",
"documents = loader.load()\n",
2023-01-27 15:10:26 +00:00
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
2023-02-13 07:02:01 +00:00
"texts = text_splitter.split_documents(documents)\n",
2023-01-27 15:10:26 +00:00
"\n",
"embeddings = OpenAIEmbeddings()\n",
2023-02-13 07:02:01 +00:00
"vectorstore = Chroma.from_documents(texts, embeddings)"
2023-01-27 15:10:26 +00:00
]
},
{
"cell_type": "code",
2023-02-13 07:02:01 +00:00
"execution_count": 6,
2023-01-27 15:10:26 +00:00
"id": "6a82e91e",
"metadata": {},
"outputs": [],
"source": [
"chain = load_chain(\"lc://chains/vector-db-qa/stuff/chain.json\", vectorstore=vectorstore)"
]
},
{
"cell_type": "code",
2023-02-13 07:02:01 +00:00
"execution_count": 7,
2023-01-27 15:10:26 +00:00
"id": "efe9b25b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2023-02-13 07:02:01 +00:00
"\" The president said that Ketanji Brown Jackson is a Circuit Court of Appeals Judge, one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans, and will continue Justice Breyer's legacy of excellence.\""
2023-01-27 15:10:26 +00:00
]
},
2023-02-13 07:02:01 +00:00
"execution_count": 7,
2023-01-27 15:10:26 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"chain.run(query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f910a32f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-02-13 07:02:01 +00:00
"version": "3.9.1"
2023-01-27 15:10:26 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}