|
@@ -15,7 +15,7 @@
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"outputs": [],
|
|
"source": [
|
|
"source": [
|
|
- "! pip install -U langchain-ollama langchain_community tiktoken langchainhub chromadb langchain langgraph tavily-python sentence-transformers"
|
|
|
|
|
|
+ "! pip install -U langchain-huggingface langchain_community langchain-ollama tiktoken langchainhub chromadb langchain langgraph tavily-python"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -99,7 +99,8 @@
|
|
"os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n",
|
|
"os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n",
|
|
"os.environ['LANGCHAIN_API_KEY'] = 'LANGCHAIN_API_KEY'\n",
|
|
"os.environ['LANGCHAIN_API_KEY'] = 'LANGCHAIN_API_KEY'\n",
|
|
"\n",
|
|
"\n",
|
|
- "os.environ['TAVILY_API_KEY'] = 'TAVILY_API_KEY'"
|
|
|
|
|
|
+ "os.environ['TAVILY_API_KEY'] = 'TAVILY_API_KEY'\n",
|
|
|
|
+ "os.environ[\"USER_AGENT\"] = \"USER_AGENT_KEY\""
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -126,7 +127,7 @@
|
|
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
|
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
|
"from langchain_community.document_loaders import WebBaseLoader\n",
|
|
"from langchain_community.document_loaders import WebBaseLoader\n",
|
|
"from langchain_community.vectorstores import Chroma\n",
|
|
"from langchain_community.vectorstores import Chroma\n",
|
|
- "from langchain_community.embeddings import HuggingFaceEmbeddings\n",
|
|
|
|
|
|
+ "from langchain_huggingface import HuggingFaceEmbeddings\n",
|
|
"\n",
|
|
"\n",
|
|
"urls = [\n",
|
|
"urls = [\n",
|
|
" \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n",
|
|
" \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n",
|
|
@@ -146,7 +147,7 @@
|
|
"vectorstore = Chroma.from_documents(\n",
|
|
"vectorstore = Chroma.from_documents(\n",
|
|
" documents=doc_splits,\n",
|
|
" documents=doc_splits,\n",
|
|
" collection_name=\"rag-chroma\",\n",
|
|
" collection_name=\"rag-chroma\",\n",
|
|
- " embedding=HuggingFaceEmbeddings(),\n",
|
|
|
|
|
|
+ " embedding=HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\"),\n",
|
|
")\n",
|
|
")\n",
|
|
"retriever = vectorstore.as_retriever()"
|
|
"retriever = vectorstore.as_retriever()"
|
|
]
|
|
]
|
|
@@ -257,7 +258,7 @@
|
|
" Here is the answer:\n",
|
|
" Here is the answer:\n",
|
|
" {generation}\n",
|
|
" {generation}\n",
|
|
" \"\"\",\n",
|
|
" \"\"\",\n",
|
|
- " input_variables=[\"generation\", \"documents\"],\n",
|
|
|
|
|
|
+ " input_variables=[\"generation\", \"documents\"]\n",
|
|
")\n",
|
|
")\n",
|
|
"\n",
|
|
"\n",
|
|
"hallucination_grader = prompt | llm | JsonOutputParser()\n",
|
|
"hallucination_grader = prompt | llm | JsonOutputParser()\n",
|
|
@@ -304,7 +305,7 @@
|
|
"### Router\n",
|
|
"### Router\n",
|
|
"\n",
|
|
"\n",
|
|
"from langchain.prompts import PromptTemplate\n",
|
|
"from langchain.prompts import PromptTemplate\n",
|
|
- "from langchain_community.chat_models import ChatOllama\n",
|
|
|
|
|
|
+ "from langchain_ollama import ChatOllama\n",
|
|
"from langchain_core.output_parsers import JsonOutputParser\n",
|
|
"from langchain_core.output_parsers import JsonOutputParser\n",
|
|
"\n",
|
|
"\n",
|
|
"# LLM\n",
|
|
"# LLM\n",
|
|
@@ -325,7 +326,7 @@
|
|
"\n",
|
|
"\n",
|
|
"question_router = prompt | llm | JsonOutputParser()\n",
|
|
"question_router = prompt | llm | JsonOutputParser()\n",
|
|
"question = \"llm agent memory\"\n",
|
|
"question = \"llm agent memory\"\n",
|
|
- "docs = retriever.get_relevant_documents(question)\n",
|
|
|
|
|
|
+ "docs = retriever.invoke(question)\n",
|
|
"doc_txt = docs[1].page_content\n",
|
|
"doc_txt = docs[1].page_content\n",
|
|
"print(question_router.invoke({\"question\": question}))"
|
|
"print(question_router.invoke({\"question\": question}))"
|
|
]
|
|
]
|
|
@@ -648,7 +649,7 @@
|
|
"source": [
|
|
"source": [
|
|
"Trace: \n",
|
|
"Trace: \n",
|
|
"\n",
|
|
"\n",
|
|
- "https://smith.langchain.com/public/8d449b67-6bc4-4ecf-9153-759cd21df24f/r"
|
|
|
|
|
|
+ "https://smith.langchain.com/public/bbdef8eb-0c42-4df6-9eaa-3e7e75c87df9/r"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -660,7 +661,6 @@
|
|
"source": [
|
|
"source": [
|
|
"# Compile\n",
|
|
"# Compile\n",
|
|
"app = workflow.compile()\n",
|
|
"app = workflow.compile()\n",
|
|
- "\n",
|
|
|
|
"# Test\n",
|
|
"# Test\n",
|
|
"from pprint import pprint\n",
|
|
"from pprint import pprint\n",
|
|
"inputs = {\"question\": \"Who are the Bears expected to draft first in the NFL draft?\"}\n",
|
|
"inputs = {\"question\": \"Who are the Bears expected to draft first in the NFL draft?\"}\n",
|
|
@@ -677,13 +677,36 @@
|
|
"source": [
|
|
"source": [
|
|
"Trace: \n",
|
|
"Trace: \n",
|
|
"\n",
|
|
"\n",
|
|
- "https://smith.langchain.com/public/c785f9c0-f519-4a38-ad5a-febb59a2139c/r"
|
|
|
|
|
|
+ "https://smith.langchain.com/public/0e6378e7-b8d4-4979-8357-05f854584cc6/r"
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ "cell_type": "code",
|
|
|
|
+ "execution_count": 62,
|
|
|
|
+ "id": "bd0254a8-2e85-4462-8626-711fdb2cc913",
|
|
|
|
+ "metadata": {},
|
|
|
|
+ "outputs": [
|
|
|
|
+ {
|
|
|
|
+ "ename": "NameError",
|
|
|
|
+ "evalue": "name 'langchain_community' is not defined",
|
|
|
|
+ "output_type": "error",
|
|
|
|
+ "traceback": [
|
|
|
|
+ "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
|
|
|
+ "\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
|
|
|
|
+ "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[62]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mlangchain_community\u001b[49m.__version__)\n",
|
|
|
|
+ "\u001b[31mNameError\u001b[39m: name 'langchain_community' is not defined"
|
|
|
|
+ ]
|
|
|
|
+ }
|
|
|
|
+ ],
|
|
|
|
+ "source": [
|
|
|
|
+ "import langchain\n",
|
|
|
|
+ "print(langchain_community.__version__)"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"execution_count": null,
|
|
- "id": "a1059b3e-d197-47fc-b55f-82a3406016f3",
|
|
|
|
|
|
+ "id": "337d5b89-7030-4ae9-85ed-f5f9f6da0c26",
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"outputs": [],
|
|
"source": []
|
|
"source": []
|
|
@@ -705,7 +728,7 @@
|
|
"name": "python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"pygments_lexer": "ipython3",
|
|
- "version": "3.11.9"
|
|
|
|
|
|
+ "version": "3.12.7"
|
|
}
|
|
}
|
|
},
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat": 4,
|