{ "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", "execution_count": 2, "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" ] }, { "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", "execution_count": 4, "id": "aab39528", "metadata": {}, "outputs": [], "source": [ "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.vectorstores.faiss import FAISS\n", "from langchain.text_splitter import CharacterTextSplitter\n", "from langchain import OpenAI, VectorDBQA" ] }, { "cell_type": "code", "execution_count": 8, "id": "16a85d5e", "metadata": {}, "outputs": [], "source": [ "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", "\n", "embeddings = OpenAIEmbeddings()\n", "vectorstore = FAISS.from_texts(texts, embeddings)" ] }, { "cell_type": "code", "execution_count": 12, "id": "6a82e91e", "metadata": {}, "outputs": [], "source": [ "chain = load_chain(\"lc://chains/vector-db-qa/stuff/chain.json\", vectorstore=vectorstore)" ] }, { "cell_type": "code", "execution_count": 10, "id": "efe9b25b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\" The president said that Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers, and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\"" ] }, "execution_count": 10, "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", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }