Skip to content

Commit 3c74c5b

Browse files
Fix deprecated langchain warning for llama3 (#953)
2 parents 04b6c8c + 319b954 commit 3c74c5b

1 file changed

Lines changed: 52 additions & 52 deletions

File tree

3p-integrations/langchain/langgraph_rag_agent_local.ipynb

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"! pip install -U langchain_community tiktoken langchainhub chromadb langchain langgraph tavily-python sentence-transformers"
18+
"! pip install -U langchain-huggingface langchain_community langchain-ollama tiktoken langchainhub chromadb langchain langgraph tavily-python sentence-transformers beautifulsoup4"
1919
]
2020
},
2121
{
@@ -99,7 +99,8 @@
9999
"os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n",
100100
"os.environ['LANGCHAIN_API_KEY'] = 'LANGCHAIN_API_KEY'\n",
101101
"\n",
102-
"os.environ['TAVILY_API_KEY'] = 'TAVILY_API_KEY'"
102+
"os.environ['TAVILY_API_KEY'] = 'TAVILY_API_KEY'\n",
103+
"os.environ[\"USER_AGENT\"] = \"USER_AGENT_KEY\""
103104
]
104105
},
105106
{
@@ -126,7 +127,7 @@
126127
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
127128
"from langchain_community.document_loaders import WebBaseLoader\n",
128129
"from langchain_community.vectorstores import Chroma\n",
129-
"from langchain_community.embeddings import HuggingFaceEmbeddings\n",
130+
"from langchain_huggingface import HuggingFaceEmbeddings\n",
130131
"\n",
131132
"urls = [\n",
132133
" \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n",
@@ -146,7 +147,7 @@
146147
"vectorstore = Chroma.from_documents(\n",
147148
" documents=doc_splits,\n",
148149
" collection_name=\"rag-chroma\",\n",
149-
" embedding=HuggingFaceEmbeddings(),\n",
150+
" embedding=HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\"),\n",
150151
")\n",
151152
"retriever = vectorstore.as_retriever()"
152153
]
@@ -158,27 +159,27 @@
158159
"metadata": {},
159160
"outputs": [],
160161
"source": [
161-
"### Retrieval Grader \n",
162+
"### Retrieval Grader\n",
162163
"\n",
163164
"from langchain.prompts import PromptTemplate\n",
164-
"from langchain_community.chat_models import ChatOllama\n",
165+
"from langchain_ollama import ChatOllama\n",
165166
"from langchain_core.output_parsers import JsonOutputParser\n",
166167
"\n",
167168
"# LLM\n",
168169
"llm = ChatOllama(model=local_llm, format=\"json\", temperature=0)\n",
169170
"\n",
170171
"prompt = PromptTemplate(\n",
171-
" template=\"\"\"You are a grader assessing relevance \n",
172-
" of a retrieved document to a user question. If the document contains keywords related to the user question, \n",
173-
" grade it as relevant. It does not need to be a stringent test. The goal is to filter out erroneous retrievals. \n",
174-
" \n",
172+
" template=\"\"\"You are a grader assessing relevance\n",
173+
" of a retrieved document to a user question. If the document contains keywords related to the user question,\n",
174+
" grade it as relevant. It does not need to be a stringent test. The goal is to filter out erroneous retrievals.\n",
175+
"\n",
175176
" Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question.\n",
176177
" Provide the binary score as a JSON with a single key 'score' and no premable or explanation.\n",
177-
" \n",
178-
" Here is the retrieved document: \n",
178+
"\n",
179+
" Here is the retrieved document:\n",
179180
" {document}\n",
180-
" \n",
181-
" Here is the user question: \n",
181+
"\n",
182+
" Here is the user question:\n",
182183
" {question}\n",
183184
" \"\"\",\n",
184185
" input_variables=[\"question\", \"document\"],\n",
@@ -206,12 +207,12 @@
206207
"\n",
207208
"# Prompt\n",
208209
"prompt = PromptTemplate(\n",
209-
" template=\"\"\"You are an assistant for question-answering tasks. \n",
210-
" Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. \n",
210+
" template=\"\"\"You are an assistant for question-answering tasks.\n",
211+
" Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know.\n",
211212
" Use three sentences maximum and keep the answer concise:\n",
212-
" Question: {question} \n",
213-
" Context: {context} \n",
214-
" Answer: \n",
213+
" Question: {question}\n",
214+
" Context: {context}\n",
215+
" Answer:\n",
215216
" \"\"\",\n",
216217
" input_variables=[\"question\", \"document\"],\n",
217218
")\n",
@@ -239,25 +240,25 @@
239240
"metadata": {},
240241
"outputs": [],
241242
"source": [
242-
"### Hallucination Grader \n",
243+
"### Hallucination Grader\n",
243244
"\n",
244245
"# LLM\n",
245246
"llm = ChatOllama(model=local_llm, format=\"json\", temperature=0)\n",
246247
"\n",
247248
"# Prompt\n",
248249
"prompt = PromptTemplate(\n",
249-
" template=\"\"\"You are a grader assessing whether \n",
250-
" an answer is grounded in / supported by a set of facts. Give a binary score 'yes' or 'no' score to indicate \n",
251-
" whether the answer is grounded in / supported by a set of facts. Provide the binary score as a JSON with a \n",
250+
" template=\"\"\"You are a grader assessing whether\n",
251+
" an answer is grounded in / supported by a set of facts. Give a binary score 'yes' or 'no' score to indicate\n",
252+
" whether the answer is grounded in / supported by a set of facts. Provide the binary score as a JSON with a\n",
252253
" single key 'score' and no preamble or explanation.\n",
253-
" \n",
254+
"\n",
254255
" Here are the facts:\n",
255-
" {documents} \n",
256+
" {documents}\n",
256257
"\n",
257-
" Here is the answer: \n",
258+
" Here is the answer:\n",
258259
" {generation}\n",
259260
" \"\"\",\n",
260-
" input_variables=[\"generation\", \"documents\"],\n",
261+
" input_variables=[\"generation\", \"documents\"]\n",
261262
")\n",
262263
"\n",
263264
"hallucination_grader = prompt | llm | JsonOutputParser()\n",
@@ -271,19 +272,19 @@
271272
"metadata": {},
272273
"outputs": [],
273274
"source": [
274-
"### Answer Grader \n",
275+
"### Answer Grader\n",
275276
"\n",
276277
"# LLM\n",
277278
"llm = ChatOllama(model=local_llm, format=\"json\", temperature=0)\n",
278279
"\n",
279280
"# Prompt\n",
280281
"prompt = PromptTemplate(\n",
281-
" template=\"\"\"You are a grader assessing whether an \n",
282-
" answer is useful to resolve a question. Give a binary score 'yes' or 'no' to indicate whether the answer is \n",
282+
" template=\"\"\"You are a grader assessing whether an\n",
283+
" answer is useful to resolve a question. Give a binary score 'yes' or 'no' to indicate whether the answer is\n",
283284
" useful to resolve a question. Provide the binary score as a JSON with a single key 'score' and no preamble or explanation.\n",
284-
" \n",
285+
"\n",
285286
" Here is the answer:\n",
286-
" {generation} \n",
287+
" {generation}\n",
287288
"\n",
288289
" Here is the question: {question}\n",
289290
" \"\"\",\n",
@@ -304,28 +305,28 @@
304305
"### Router\n",
305306
"\n",
306307
"from langchain.prompts import PromptTemplate\n",
307-
"from langchain_community.chat_models import ChatOllama\n",
308+
"from langchain_ollama import ChatOllama\n",
308309
"from langchain_core.output_parsers import JsonOutputParser\n",
309310
"\n",
310311
"# LLM\n",
311312
"llm = ChatOllama(model=local_llm, format=\"json\", temperature=0)\n",
312313
"\n",
313314
"prompt = PromptTemplate(\n",
314-
" template=\"\"\"You are an expert at routing a \n",
315-
" user question to a vectorstore or web search. Use the vectorstore for questions on LLM agents, \n",
316-
" prompt engineering, and adversarial attacks. You do not need to be stringent with the keywords \n",
317-
" in the question related to these topics. Otherwise, use web-search. Give a binary choice 'web_search' \n",
318-
" or 'vectorstore' based on the question. Return the a JSON with a single key 'datasource' and \n",
319-
" no premable or explanation. \n",
320-
" \n",
321-
" Question to route: \n",
315+
" template=\"\"\"You are an expert at routing a\n",
316+
" user question to a vectorstore or web search. Use the vectorstore for questions on LLM agents,\n",
317+
" prompt engineering, and adversarial attacks. You do not need to be stringent with the keywords\n",
318+
" in the question related to these topics. Otherwise, use web-search. Give a binary choice 'web_search'\n",
319+
" or 'vectorstore' based on the question. Return the a JSON with a single key 'datasource' and\n",
320+
" no premable or explanation.\n",
321+
"\n",
322+
" Question to route:\n",
322323
" {question}\"\"\",\n",
323324
" input_variables=[\"question\"],\n",
324325
")\n",
325326
"\n",
326327
"question_router = prompt | llm | JsonOutputParser()\n",
327328
"question = \"llm agent memory\"\n",
328-
"docs = retriever.get_relevant_documents(question)\n",
329+
"docs = retriever.invoke(question)\n",
329330
"doc_txt = docs[1].page_content\n",
330331
"print(question_router.invoke({\"question\": question}))"
331332
]
@@ -371,7 +372,7 @@
371372
" question: question\n",
372373
" generation: LLM generation\n",
373374
" web_search: whether to add search\n",
374-
" documents: list of documents \n",
375+
" documents: list of documents\n",
375376
" \"\"\"\n",
376377
" question : str\n",
377378
" generation : str\n",
@@ -412,7 +413,7 @@
412413
" print(\"---GENERATE---\")\n",
413414
" question = state[\"question\"]\n",
414415
" documents = state[\"documents\"]\n",
415-
" \n",
416+
"\n",
416417
" # RAG generation\n",
417418
" generation = rag_chain.invoke({\"context\": documents, \"question\": question})\n",
418419
" return {\"documents\": documents, \"question\": question, \"generation\": generation}\n",
@@ -432,7 +433,7 @@
432433
" print(\"---CHECK DOCUMENT RELEVANCE TO QUESTION---\")\n",
433434
" question = state[\"question\"]\n",
434435
" documents = state[\"documents\"]\n",
435-
" \n",
436+
"\n",
436437
" # Score each doc\n",
437438
" filtered_docs = []\n",
438439
" web_search = \"No\"\n",
@@ -451,7 +452,7 @@
451452
" web_search = \"Yes\"\n",
452453
" continue\n",
453454
" return {\"documents\": filtered_docs, \"question\": question, \"web_search\": web_search}\n",
454-
" \n",
455+
"\n",
455456
"def web_search(state):\n",
456457
" \"\"\"\n",
457458
" Web search based based on the question\n",
@@ -493,7 +494,7 @@
493494
" print(\"---ROUTE QUESTION---\")\n",
494495
" question = state[\"question\"]\n",
495496
" print(question)\n",
496-
" source = question_router.invoke({\"question\": question}) \n",
497+
" source = question_router.invoke({\"question\": question})\n",
497498
" print(source)\n",
498499
" print(source['datasource'])\n",
499500
" if source['datasource'] == 'web_search':\n",
@@ -648,7 +649,7 @@
648649
"source": [
649650
"Trace: \n",
650651
"\n",
651-
"https://smith.langchain.com/public/8d449b67-6bc4-4ecf-9153-759cd21df24f/r"
652+
"https://smith.langchain.com/public/bbdef8eb-0c42-4df6-9eaa-3e7e75c87df9/r"
652653
]
653654
},
654655
{
@@ -660,7 +661,6 @@
660661
"source": [
661662
"# Compile\n",
662663
"app = workflow.compile()\n",
663-
"\n",
664664
"# Test\n",
665665
"from pprint import pprint\n",
666666
"inputs = {\"question\": \"Who are the Bears expected to draft first in the NFL draft?\"}\n",
@@ -677,13 +677,13 @@
677677
"source": [
678678
"Trace: \n",
679679
"\n",
680-
"https://smith.langchain.com/public/c785f9c0-f519-4a38-ad5a-febb59a2139c/r"
680+
"https://smith.langchain.com/public/0e6378e7-b8d4-4979-8357-05f854584cc6/r"
681681
]
682682
},
683683
{
684684
"cell_type": "code",
685685
"execution_count": null,
686-
"id": "a1059b3e-d197-47fc-b55f-82a3406016f3",
686+
"id": "337d5b89-7030-4ae9-85ed-f5f9f6da0c26",
687687
"metadata": {},
688688
"outputs": [],
689689
"source": []
@@ -705,7 +705,7 @@
705705
"name": "python",
706706
"nbconvert_exporter": "python",
707707
"pygments_lexer": "ipython3",
708-
"version": "3.11.9"
708+
"version": "3.12.7"
709709
}
710710
},
711711
"nbformat": 4,

0 commit comments

Comments
 (0)