mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-22 07:32:25 +02:00
Merge pull request #108 from langchain-ai/rlm/update_parser
Minor parser modifications in CRAG example
This commit is contained in:
@@ -70,7 +70,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 13,
|
||||
"id": "abc064ab-7de1-4d03-a987-cd3078438d61",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -84,14 +84,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 12,
|
||||
"id": "9f644869-436e-4bf6-a267-b2465c7b5aef",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Flags for running locally\n",
|
||||
"\n",
|
||||
"run_local = \"Yes\"\n",
|
||||
"run_local = \"No\"\n",
|
||||
"local_llm = \"mistral:instruct\""
|
||||
]
|
||||
},
|
||||
@@ -113,24 +113,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": null,
|
||||
"id": "254ae533-79e0-42f4-b200-1ec9160e1d3d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"bert_load_from_file: gguf version = 2\n",
|
||||
"bert_load_from_file: gguf alignment = 32\n",
|
||||
"bert_load_from_file: gguf data offset = 695552\n",
|
||||
"bert_load_from_file: model name = BERT\n",
|
||||
"bert_load_from_file: model architecture = bert\n",
|
||||
"bert_load_from_file: model file type = 1\n",
|
||||
"bert_load_from_file: bert tokenizer vocab = 30522\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
||||
"from langchain_community.document_loaders import WebBaseLoader\n",
|
||||
@@ -201,7 +187,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 14,
|
||||
"id": "10028794-2fbc-43f9-aa4c-7fe3abd69c1e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -238,7 +224,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 15,
|
||||
"id": "447d1333-082d-479a-a6fa-0ac0df78bb9d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -248,14 +234,13 @@
|
||||
"from typing import Annotated, Sequence, TypedDict\n",
|
||||
"\n",
|
||||
"from langchain import hub\n",
|
||||
"from langchain.output_parsers import PydanticOutputParser\n",
|
||||
"from langchain_core.output_parsers import JsonOutputParser\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain.schema import Document\n",
|
||||
"from langchain_community.chat_models import ChatOllama\n",
|
||||
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
|
||||
"from langchain_community.vectorstores import Chroma\n",
|
||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
|
||||
"from langchain_core.runnables import RunnablePassthrough\n",
|
||||
"from langchain_mistralai.chat_models import ChatMistralAI\n",
|
||||
"\n",
|
||||
@@ -346,19 +331,6 @@
|
||||
" mistral_api_key=mistral_api_key, temperature=0, model=\"mistral-medium\"\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Data model\n",
|
||||
" class grade(BaseModel):\n",
|
||||
" \"\"\"Binary score for relevance check.\"\"\"\n",
|
||||
"\n",
|
||||
" score: str = Field(description=\"Relevance score 'yes' or 'no'\")\n",
|
||||
"\n",
|
||||
" # Set up a parser + inject instructions into the prompt template.\n",
|
||||
" parser = PydanticOutputParser(pydantic_object=grade)\n",
|
||||
"\n",
|
||||
" from langchain_core.output_parsers import JsonOutputParser\n",
|
||||
"\n",
|
||||
" parser = JsonOutputParser(pydantic_object=grade)\n",
|
||||
"\n",
|
||||
" prompt = PromptTemplate(\n",
|
||||
" template=\"\"\"You are a grader assessing relevance of a retrieved document to a user question. \\n \n",
|
||||
" Here is the retrieved document: \\n\\n {context} \\n\\n\n",
|
||||
@@ -366,12 +338,11 @@
|
||||
" If the document contains keywords related to the user question, grade it as relevant. \\n\n",
|
||||
" It does not need to be a stringent test. The goal is to filter out erroneous retrievals. \\n\n",
|
||||
" Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question. \\n\n",
|
||||
" Provide the binary score as a JSON with no premable or explaination and use these instructons to format the output: {format_instructions}\"\"\",\n",
|
||||
" input_variables=[\"query\"],\n",
|
||||
" partial_variables={\"format_instructions\": parser.get_format_instructions()},\n",
|
||||
" Provide the binary score as a JSON with a single key 'score' and no premable or explaination.\"\"\",\n",
|
||||
" input_variables=[\"question\",\"context\"],\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" chain = prompt | llm | parser\n",
|
||||
" chain = prompt | llm | JsonOutputParser()\n",
|
||||
"\n",
|
||||
" # Score\n",
|
||||
" filtered_docs = []\n",
|
||||
@@ -381,7 +352,6 @@
|
||||
" {\n",
|
||||
" \"question\": question,\n",
|
||||
" \"context\": d.page_content,\n",
|
||||
" \"format_instructions\": parser.get_format_instructions(),\n",
|
||||
" }\n",
|
||||
" )\n",
|
||||
" grade = score[\"score\"]\n",
|
||||
@@ -519,7 +489,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 16,
|
||||
"id": "0a63776c-f9cd-46ce-b8cf-95c066dc5b06",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -563,12 +533,14 @@
|
||||
"source": [
|
||||
"## Run\n",
|
||||
"\n",
|
||||
"`Mistral API -` "
|
||||
"`Mistral API -` \n",
|
||||
"\n",
|
||||
"Trace for below run: https://smith.langchain.com/public/0a5cbc97-a2f6-4697-856c-90a6302fd13e/r"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 11,
|
||||
"id": "3ab1d8df-a74e-4b48-a30b-e39bbfd5925a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -599,14 +571,13 @@
|
||||
"'\\n---\\n'\n",
|
||||
"\"Node '__end__':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('Episodic memory stores specific events or experiences, making them unique to '\n",
|
||||
" 'each individual. Semantic memory, on the other hand, involves general '\n",
|
||||
" 'knowledge and facts that are not tied to personal experiences. Procedural '\n",
|
||||
" 'memory is responsible for learning and remembering sequences of actions, '\n",
|
||||
" \"such as riding a bike. These memory types contribute to an agent's learning \"\n",
|
||||
" 'and decision-making processes by allowing it to recall past experiences '\n",
|
||||
" '(episodic), understand and use information (semantic), and perform tasks '\n",
|
||||
" '(procedural).')\n"
|
||||
"('In agent-based systems, episodic memory can be likened to a long-term memory '\n",
|
||||
" \"module that records agents' experiences in natural language, with retrieval \"\n",
|
||||
" 'based on relevance, recency, and importance. Semantic memory is similar to '\n",
|
||||
" 'an external vector store that provides agents with the ability to retain and '\n",
|
||||
" 'recall information over extended periods. Procedural memory can be seen as '\n",
|
||||
" 'the reflection mechanism that synthesizes memories into higher-level '\n",
|
||||
" \"inferences, guiding the agent's future behavior.\")\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -635,12 +606,14 @@
|
||||
"id": "03ee2be9-2368-46ea-9edd-dc064a7c7c96",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"`Locall (Ollama) -` "
|
||||
"`Local (Ollama) -` \n",
|
||||
"\n",
|
||||
"Trace for blow run: https://smith.langchain.com/public/3b23a1d4-720a-4b26-8f34-70d2f20f8832/r"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 11,
|
||||
"id": "16ea2032-59c7-433d-aca4-2828a1239074",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -714,22 +687,6 @@
|
||||
"pprint.pprint(value['keys']['generation'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0931b76a-3ea8-4f2f-9d27-242d48ec3fe3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## LangSmith Traces\n",
|
||||
"\n",
|
||||
"`Mistral API -` \n",
|
||||
"\n",
|
||||
"https://smith.langchain.com/public/1c9ce3f2-76bb-4514-a107-076823e9849e/r\n",
|
||||
"\n",
|
||||
"`Locall (Ollama) -` \n",
|
||||
"\n",
|
||||
"https://smith.langchain.com/public/fd650c43-b0e9-48f4-8cb7-ca77c736d10d/r"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -755,7 +712,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.7"
|
||||
"version": "3.9.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user