ci: fix notebook runner & VCR cassette recorder (#1849)

Co-authored-by: vbarda <vadym@langchain.dev>
This commit is contained in:
Isaac Francisco
2024-09-27 17:56:55 -04:00
committed by GitHub
co-authored by vbarda
parent ea0418334b
commit 98f5df4f6f
289 changed files with 4482 additions and 1968 deletions
@@ -407,7 +407,7 @@
}
],
"source": [
"for chunk in simulation.stream({}):\n",
"for chunk in simulation.stream({\"messages\":[]}):\n",
" # Print out all events aside from the final end chunk\n",
" if END not in chunk:\n",
" print(chunk)\n",
@@ -378,10 +378,16 @@
],
"source": [
"import uuid\n",
"\n",
" \n",
"cached_human_responses = ['hi!','rag prompt','1 rag, 2 none, 3 no, 4 no','red','q']\n",
"cached_response_index = 0\n",
"config = {\"configurable\": {\"thread_id\": str(uuid.uuid4())}}\n",
"while True:\n",
" user = input(\"User (q/Q to quit): \")\n",
" try:\n",
" user = input(\"User (q/Q to quit): \")\n",
" except:\n",
" user = cached_human_responses[cached_response_index]\n",
" cached_response_index += 1\n",
" print(f\"User (q/Q to quit): {user}\")\n",
" if user in {\"q\", \"Q\"}:\n",
" print(\"AI: Byebye\")\n",
@@ -693,10 +693,13 @@
],
"source": [
"# Clone the dataset to your tenant to use it\n",
"public_dataset = (\n",
" \"https://smith.langchain.com/public/326674a6-62bd-462d-88ae-eea49d503f9d/d\"\n",
")\n",
"client.clone_public_dataset(public_dataset)"
"try:\n",
" public_dataset = (\n",
" \"https://smith.langchain.com/public/326674a6-62bd-462d-88ae-eea49d503f9d/d\"\n",
" )\n",
" client.clone_public_dataset(public_dataset)\n",
"except:\n",
" print(\"Please setup LangSmith\")"
]
},
{
@@ -790,16 +793,19 @@
"outputs": [],
"source": [
"# Run base case\n",
"experiment_results_ = evaluate(\n",
" predict_base_case,\n",
" data=dataset_name,\n",
" evaluators=code_evalulator,\n",
" experiment_prefix=f\"test-without-langgraph-{expt_llm}\",\n",
" max_concurrency=2,\n",
" metadata={\n",
" \"llm\": expt_llm,\n",
" },\n",
")"
"try:\n",
" experiment_results_ = evaluate(\n",
" predict_base_case,\n",
" data=dataset_name,\n",
" evaluators=code_evalulator,\n",
" experiment_prefix=f\"test-without-langgraph-{expt_llm}\",\n",
" max_concurrency=2,\n",
" metadata={\n",
" \"llm\": expt_llm,\n",
" },\n",
" )\n",
"except:\n",
" print(\"Please setup LangSmith\")"
]
},
{
@@ -810,17 +816,20 @@
"outputs": [],
"source": [
"# Run with langgraph\n",
"experiment_results = evaluate(\n",
" predict_langgraph,\n",
" data=dataset_name,\n",
" evaluators=code_evalulator,\n",
" experiment_prefix=f\"test-with-langgraph-{expt_llm}-{flag}\",\n",
" max_concurrency=2,\n",
" metadata={\n",
" \"llm\": expt_llm,\n",
" \"feedback\": flag,\n",
" },\n",
")"
"try:\n",
" experiment_results = evaluate(\n",
" predict_langgraph,\n",
" data=dataset_name,\n",
" evaluators=code_evalulator,\n",
" experiment_prefix=f\"test-with-langgraph-{expt_llm}-{flag}\",\n",
" max_concurrency=2,\n",
" metadata={\n",
" \"llm\": expt_llm,\n",
" \"feedback\": flag,\n",
" },\n",
" )\n",
"except:\n",
" print(\"Please setup LangSmith\")"
]
},
{
+25 -44
View File
@@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"id": "baf669a0-04ee-492d-80d8-8fcb658ed128",
"metadata": {},
"outputs": [],
@@ -402,7 +402,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"id": "5df33c17-ee1a-409e-b5ec-f24e116da7d1",
"metadata": {},
"outputs": [],
@@ -438,7 +438,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"id": "38231a5b-d018-41ee-a92c-2f2248edf417",
"metadata": {},
"outputs": [],
@@ -510,7 +510,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 5,
"id": "f4f7438b-b6c1-48fd-b70f-185af7a2f64a",
"metadata": {},
"outputs": [],
@@ -616,7 +616,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 6,
"id": "e2d10886-7b1e-485f-91cd-1184a1c99303",
"metadata": {},
"outputs": [],
@@ -707,36 +707,15 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 7,
"id": "f4752239-2aa3-4367-b777-8478c16b9471",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Could not extract a valid value in 3 attempts.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[11], line 15\u001b[0m\n\u001b[1;32m 6\u001b[0m prompt \u001b[38;5;241m=\u001b[39m ChatPromptTemplate\u001b[38;5;241m.\u001b[39mfrom_messages(\n\u001b[1;32m 7\u001b[0m [\n\u001b[1;32m 8\u001b[0m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msystem\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRespond directly using the TranscriptSummary function.\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[1;32m 9\u001b[0m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mplaceholder\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{messages}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m),\n\u001b[1;32m 10\u001b[0m ]\n\u001b[1;32m 11\u001b[0m )\n\u001b[1;32m 13\u001b[0m chain \u001b[38;5;241m=\u001b[39m prompt \u001b[38;5;241m|\u001b[39m bound_llm\n\u001b[0;32m---> 15\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[43mchain\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmessages\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m \u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 20\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mExtract the summary from the following conversation:\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m<convo>\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mformatted\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m</convo>\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n\u001b[1;32m 21\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43mRemember to respond using the TranscriptSummary function.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 22\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 23\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 24\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 25\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 26\u001b[0m results\u001b[38;5;241m.\u001b[39mpretty_print()\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langchain_core/runnables/base.py:3013\u001b[0m, in \u001b[0;36mRunnableSequence.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 3011\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m context\u001b[38;5;241m.\u001b[39mrun(step\u001b[38;5;241m.\u001b[39minvoke, \u001b[38;5;28minput\u001b[39m, config, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 3012\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 3013\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m context\u001b[38;5;241m.\u001b[39mrun(step\u001b[38;5;241m.\u001b[39minvoke, \u001b[38;5;28minput\u001b[39m, config)\n\u001b[1;32m 3014\u001b[0m \u001b[38;5;66;03m# finish the root run\u001b[39;00m\n\u001b[1;32m 3015\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langchain_core/runnables/base.py:5313\u001b[0m, in \u001b[0;36mRunnableBindingBase.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 5307\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21minvoke\u001b[39m(\n\u001b[1;32m 5308\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 5309\u001b[0m \u001b[38;5;28minput\u001b[39m: Input,\n\u001b[1;32m 5310\u001b[0m config: Optional[RunnableConfig] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 5311\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Optional[Any],\n\u001b[1;32m 5312\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Output:\n\u001b[0;32m-> 5313\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbound\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 5314\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 5315\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_merge_configs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 5316\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m{\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 5317\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langchain_core/runnables/base.py:3013\u001b[0m, in \u001b[0;36mRunnableSequence.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 3011\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m context\u001b[38;5;241m.\u001b[39mrun(step\u001b[38;5;241m.\u001b[39minvoke, \u001b[38;5;28minput\u001b[39m, config, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 3012\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 3013\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m context\u001b[38;5;241m.\u001b[39mrun(step\u001b[38;5;241m.\u001b[39minvoke, \u001b[38;5;28minput\u001b[39m, config)\n\u001b[1;32m 3014\u001b[0m \u001b[38;5;66;03m# finish the root run\u001b[39;00m\n\u001b[1;32m 3015\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/pregel/__init__.py:1470\u001b[0m, in \u001b[0;36mPregel.invoke\u001b[0;34m(self, input, config, stream_mode, output_keys, interrupt_before, interrupt_after, debug, **kwargs)\u001b[0m\n\u001b[1;32m 1468\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1469\u001b[0m chunks \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m-> 1470\u001b[0m \u001b[43m\u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1471\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1472\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1473\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_mode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_mode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1474\u001b[0m \u001b[43m \u001b[49m\u001b[43moutput_keys\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moutput_keys\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1475\u001b[0m \u001b[43m \u001b[49m\u001b[43minterrupt_before\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minterrupt_before\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1476\u001b[0m \u001b[43m \u001b[49m\u001b[43minterrupt_after\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minterrupt_after\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1477\u001b[0m \u001b[43m \u001b[49m\u001b[43mdebug\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdebug\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1478\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1479\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 1480\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mstream_mode\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m==\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mvalues\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\n\u001b[1;32m 1481\u001b[0m \u001b[43m \u001b[49m\u001b[43mlatest\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/pregel/__init__.py:1224\u001b[0m, in \u001b[0;36mPregel.stream\u001b[0;34m(self, input, config, stream_mode, output_keys, interrupt_before, interrupt_after, debug, subgraphs)\u001b[0m\n\u001b[1;32m 1213\u001b[0m \u001b[38;5;66;03m# Similarly to Bulk Synchronous Parallel / Pregel model\u001b[39;00m\n\u001b[1;32m 1214\u001b[0m \u001b[38;5;66;03m# computation proceeds in steps, while there are channel updates\u001b[39;00m\n\u001b[1;32m 1215\u001b[0m \u001b[38;5;66;03m# channel updates from step N are only visible in step N+1\u001b[39;00m\n\u001b[1;32m 1216\u001b[0m \u001b[38;5;66;03m# channels are guaranteed to be immutable for the duration of the step,\u001b[39;00m\n\u001b[1;32m 1217\u001b[0m \u001b[38;5;66;03m# with channel updates applied only at the transition between steps\u001b[39;00m\n\u001b[1;32m 1218\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m loop\u001b[38;5;241m.\u001b[39mtick(\n\u001b[1;32m 1219\u001b[0m input_keys\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minput_channels,\n\u001b[1;32m 1220\u001b[0m interrupt_before\u001b[38;5;241m=\u001b[39minterrupt_before,\n\u001b[1;32m 1221\u001b[0m interrupt_after\u001b[38;5;241m=\u001b[39minterrupt_after,\n\u001b[1;32m 1222\u001b[0m manager\u001b[38;5;241m=\u001b[39mrun_manager,\n\u001b[1;32m 1223\u001b[0m ):\n\u001b[0;32m-> 1224\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m_\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrunner\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtick\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1225\u001b[0m \u001b[43m \u001b[49m\u001b[43mloop\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtasks\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1226\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstep_timeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1227\u001b[0m \u001b[43m \u001b[49m\u001b[43mretry_policy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mretry_policy\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1228\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 1229\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# emit output\u001b[39;49;00m\n\u001b[1;32m 1230\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mo\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43moutput\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 1231\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01myield\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mo\u001b[49m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/pregel/runner.py:94\u001b[0m, in \u001b[0;36mPregelRunner.tick\u001b[0;34m(self, tasks, reraise, timeout, retry_policy)\u001b[0m\n\u001b[1;32m 92\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m\n\u001b[1;32m 93\u001b[0m \u001b[38;5;66;03m# panic on failure or timeout\u001b[39;00m\n\u001b[0;32m---> 94\u001b[0m \u001b[43m_panic_or_proceed\u001b[49m\u001b[43m(\u001b[49m\u001b[43mall_futures\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpanic\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mreraise\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/pregel/runner.py:210\u001b[0m, in \u001b[0;36m_panic_or_proceed\u001b[0;34m(futs, timeout_exc_cls, panic)\u001b[0m\n\u001b[1;32m 208\u001b[0m \u001b[38;5;66;03m# raise the exception\u001b[39;00m\n\u001b[1;32m 209\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m panic:\n\u001b[0;32m--> 210\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n\u001b[1;32m 211\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 212\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/pregel/executor.py:61\u001b[0m, in \u001b[0;36mBackgroundExecutor.done\u001b[0;34m(self, task)\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdone\u001b[39m(\u001b[38;5;28mself\u001b[39m, task: concurrent\u001b[38;5;241m.\u001b[39mfutures\u001b[38;5;241m.\u001b[39mFuture) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 60\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 61\u001b[0m \u001b[43mtask\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mresult\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 62\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m GraphInterrupt:\n\u001b[1;32m 63\u001b[0m \u001b[38;5;66;03m# This exception is an interruption signal, not an error\u001b[39;00m\n\u001b[1;32m 64\u001b[0m \u001b[38;5;66;03m# so we don't want to re-raise it on exit\u001b[39;00m\n\u001b[1;32m 65\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtasks\u001b[38;5;241m.\u001b[39mpop(task)\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py:449\u001b[0m, in \u001b[0;36mFuture.result\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 447\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CancelledError()\n\u001b[1;32m 448\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_state \u001b[38;5;241m==\u001b[39m FINISHED:\n\u001b[0;32m--> 449\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__get_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 451\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_condition\u001b[38;5;241m.\u001b[39mwait(timeout)\n\u001b[1;32m 453\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_state \u001b[38;5;129;01min\u001b[39;00m [CANCELLED, CANCELLED_AND_NOTIFIED]:\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py:401\u001b[0m, in \u001b[0;36mFuture.__get_result\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 399\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_exception:\n\u001b[1;32m 400\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 401\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_exception\n\u001b[1;32m 402\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 403\u001b[0m \u001b[38;5;66;03m# Break a reference cycle with the exception in self._exception\u001b[39;00m\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28mself\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/thread.py:58\u001b[0m, in \u001b[0;36m_WorkItem.run\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m 57\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 58\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 59\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 60\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfuture\u001b[38;5;241m.\u001b[39mset_exception(exc)\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/pregel/retry.py:29\u001b[0m, in \u001b[0;36mrun_with_retry\u001b[0;34m(task, retry_policy)\u001b[0m\n\u001b[1;32m 27\u001b[0m task\u001b[38;5;241m.\u001b[39mwrites\u001b[38;5;241m.\u001b[39mclear()\n\u001b[1;32m 28\u001b[0m \u001b[38;5;66;03m# run the task\u001b[39;00m\n\u001b[0;32m---> 29\u001b[0m \u001b[43mtask\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mproc\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtask\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minput\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# if successful, end\u001b[39;00m\n\u001b[1;32m 31\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/utils/runnable.py:345\u001b[0m, in \u001b[0;36mRunnableSeq.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 343\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m context\u001b[38;5;241m.\u001b[39mrun(step\u001b[38;5;241m.\u001b[39minvoke, \u001b[38;5;28minput\u001b[39m, config, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 344\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 345\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m context\u001b[38;5;241m.\u001b[39mrun(step\u001b[38;5;241m.\u001b[39minvoke, \u001b[38;5;28minput\u001b[39m, config)\n\u001b[1;32m 346\u001b[0m \u001b[38;5;66;03m# finish the root run\u001b[39;00m\n\u001b[1;32m 347\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/utils/runnable.py:131\u001b[0m, in \u001b[0;36mRunnableCallable.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 130\u001b[0m context\u001b[38;5;241m.\u001b[39mrun(_set_config_context, config)\n\u001b[0;32m--> 131\u001b[0m ret \u001b[38;5;241m=\u001b[39m \u001b[43mcontext\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 132\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(ret, Runnable) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mrecurse:\n\u001b[1;32m 133\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m ret\u001b[38;5;241m.\u001b[39minvoke(\u001b[38;5;28minput\u001b[39m, config)\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/graph/graph.py:89\u001b[0m, in \u001b[0;36mBranch._route\u001b[0;34m(self, input, config, reader, writer)\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 88\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m\n\u001b[0;32m---> 89\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 90\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_finish(writer, \u001b[38;5;28minput\u001b[39m, result, config)\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/langgraph/utils/runnable.py:123\u001b[0m, in \u001b[0;36mRunnableCallable.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 121\u001b[0m context \u001b[38;5;241m=\u001b[39m copy_context()\n\u001b[1;32m 122\u001b[0m context\u001b[38;5;241m.\u001b[39mrun(_set_config_context, child_config)\n\u001b[0;32m--> 123\u001b[0m ret \u001b[38;5;241m=\u001b[39m \u001b[43mcontext\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 124\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 125\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n",
"Cell \u001b[0;32mIn[5], line 204\u001b[0m, in \u001b[0;36m_bind_validator_with_retries.<locals>.route_validation\u001b[0;34m(state)\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mroute_validation\u001b[39m(state: State) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Literal[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfinalizer\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfallback\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[1;32m 203\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m state[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mattempt_number\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m>\u001b[39m max_attempts:\n\u001b[0;32m--> 204\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 205\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCould not extract a valid value in \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmax_attempts\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m attempts.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 206\u001b[0m )\n\u001b[1;32m 207\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m m \u001b[38;5;129;01min\u001b[39;00m state[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m][::\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]:\n\u001b[1;32m 208\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m m\u001b[38;5;241m.\u001b[39mtype \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mai\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
"\u001b[0;31mValueError\u001b[0m: Could not extract a valid value in 3 attempts."
"name": "stdout",
"output_type": "stream",
"text": [
"ValueError('Could not extract a valid value in 3 attempts.')\n"
]
}
],
@@ -755,18 +734,21 @@
"\n",
"chain = prompt | bound_llm\n",
"\n",
"results = chain.invoke(\n",
" {\n",
" \"messages\": [\n",
" (\n",
" \"user\",\n",
" f\"Extract the summary from the following conversation:\\n\\n<convo>\\n{formatted}\\n</convo>\"\n",
" \"\\n\\nRemember to respond using the TranscriptSummary function.\",\n",
" )\n",
" ]\n",
" },\n",
")\n",
"results.pretty_print()"
"try:\n",
" results = chain.invoke(\n",
" {\n",
" \"messages\": [\n",
" (\n",
" \"user\",\n",
" f\"Extract the summary from the following conversation:\\n\\n<convo>\\n{formatted}\\n</convo>\"\n",
" \"\\n\\nRemember to respond using the TranscriptSummary function.\",\n",
" )\n",
" ]\n",
" },\n",
" )\n",
" results.pretty_print()\n",
"except ValueError as e:\n",
" print(repr(e))"
]
},
{
@@ -844,7 +826,6 @@
" except ImportError:\n",
" raise ImportError(\n",
" \"The 'jsonpatch' library is required for JSONPatch-based retries.\"\n",
" \" Please install it with 'pip install -U jsonpatch'.\"\n",
" )\n",
"\n",
" class JsonPatch(BaseModel):\n",
File diff suppressed because it is too large Load Diff
@@ -48,7 +48,7 @@
},
"outputs": [],
"source": [
"%% capture --no-stderr\n",
"%%capture --no-stderr\n",
"%pip install -U langgraph langchain langchain_openai langchain_experimental"
]
},
@@ -291,7 +291,16 @@
"from langchain_openai import ChatOpenAI\n",
"\n",
"from langgraph.graph import END, StateGraph, START\n",
"from langchain_core.messages import HumanMessage\n",
"from langchain_core.messages import HumanMessage, trim_messages\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"trimmer = trim_messages(\n",
" max_tokens=100000,\n",
" strategy=\"last\",\n",
" token_counter=llm,\n",
" include_system=True,\n",
")\n",
"\n",
"\n",
"def agent_node(state, agent, name):\n",
@@ -331,7 +340,7 @@
" ]\n",
" ).partial(options=str(options), team_members=\", \".join(members))\n",
" return (\n",
" prompt\n",
" prompt | trimmer\n",
" | llm.bind_functions(functions=[function_def], function_call=\"route\")\n",
" | JsonOutputFunctionsParser()\n",
" )"
@@ -130,7 +130,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "72d233ca-1dbf-4b43-b680-b3bf39e3691f",
"metadata": {},
"outputs": [
@@ -144,7 +144,7 @@
"\n",
"=============================\u001b[1m Messages Placeholder \u001b[0m=============================\n",
"\n",
"\u001b[33;1m\u001b[1;3m{{messages}}\u001b[0m\n"
"\u001b[33;1m\u001b[1;3m{messages}\u001b[0m\n"
]
}
],
@@ -155,7 +155,7 @@
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"# Get the prompt to use - you can modify this!\n",
"prompt = hub.pull(\"wfh/react-agent-executor\")\n",
"prompt = hub.pull(\"ih/ih-react-agent-executor\")\n",
"prompt.pretty_print()\n",
"\n",
"# Choose the LLM that will drive the agent\n",
@@ -62,7 +62,7 @@
"metadata": {},
"outputs": [],
"source": [
"%capture --no-stderr\n",
"%%capture --no-stderr\n",
"%pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph nomic[local]"
]
},
+13 -13
View File
@@ -94,7 +94,7 @@
"source": [
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"llm = ChatAnthropic(model=\"claude-3-sonnet-20240229\")\n",
"llm = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\")\n",
"# You could also use OpenAI or another provider\n",
"# from langchain_openai import ChatOpenAI\n",
"\n",
@@ -192,11 +192,11 @@
" response = []\n",
" for attempt in range(3):\n",
" response = self.runnable.invoke(\n",
" {\"messages\": state}, {\"tags\": [f\"attempt:{attempt}\"]}\n",
" {\"messages\": state['messages']}, {\"tags\": [f\"attempt:{attempt}\"]}\n",
" )\n",
" try:\n",
" self.validator.invoke(response)\n",
" return response\n",
" return {\"messages\": response}\n",
" except ValidationError as e:\n",
" state = state + [\n",
" response,\n",
@@ -207,7 +207,7 @@
" tool_call_id=response.tool_calls[0][\"id\"],\n",
" ),\n",
" ]\n",
" return response"
" return {\"messages\": response}"
]
},
{
@@ -259,7 +259,7 @@
"outputs": [],
"source": [
"example_question = \"Why is reflection useful in AI?\"\n",
"initial = first_responder.respond([HumanMessage(content=example_question)])"
"initial = first_responder.respond({\"messages\":[HumanMessage(content=example_question)]})"
]
},
{
@@ -332,20 +332,20 @@
"import json\n",
"\n",
"revised = revisor.respond(\n",
" [\n",
" {\"messages\": [\n",
" HumanMessage(content=example_question),\n",
" initial,\n",
" initial['messages'],\n",
" ToolMessage(\n",
" tool_call_id=initial.tool_calls[0][\"id\"],\n",
" tool_call_id=initial['messages'].tool_calls[0][\"id\"],\n",
" content=json.dumps(\n",
" tavily_tool.invoke(\n",
" {\"query\": initial.tool_calls[0][\"args\"][\"search_queries\"][0]}\n",
" {\"query\": initial['messages'].tool_calls[0][\"args\"][\"search_queries\"][0]}\n",
" )\n",
" ),\n",
" ),\n",
" ]\n",
" ]}\n",
")\n",
"revised"
"revised['messages']"
]
},
{
@@ -439,7 +439,7 @@
"\n",
"def event_loop(state: list) -> Literal[\"execute_tools\", \"__end__\"]:\n",
" # in our case, we'll just stop after N plans\n",
" num_iterations = _get_num_iterations(state)\n",
" num_iterations = _get_num_iterations(state['messages'])\n",
" if num_iterations > MAX_ITERATIONS:\n",
" return END\n",
" return \"execute_tools\"\n",
@@ -598,7 +598,7 @@
")\n",
"for i, step in enumerate(events):\n",
" print(f\"Step {i}\")\n",
" step[-1].pretty_print()"
" step['messages'][-1].pretty_print()"
]
},
{
+26 -20
View File
@@ -56,7 +56,7 @@
"metadata": {},
"outputs": [],
"source": [
"%capture --no-stderr\n",
"%%capture --no-stderr\n",
"%pip install -U langgraph langchain_openai langchain_community"
]
},
@@ -915,14 +915,17 @@
"from langsmith.evaluation import evaluate\n",
"\n",
"dataset_name = \"SQL Agent Response\"\n",
"experiment_results = evaluate(\n",
" predict_sql_agent_answer,\n",
" data=dataset_name,\n",
" evaluators=[answer_evaluator],\n",
" num_repetitions=3,\n",
" experiment_prefix=\"sql-agent-multi-step-response-v-reference\",\n",
" metadata={\"version\": \"Chinook, gpt-4o multi-step-agent\"},\n",
")"
"try:\n",
" experiment_results = evaluate(\n",
" predict_sql_agent_answer,\n",
" data=dataset_name,\n",
" evaluators=[answer_evaluator],\n",
" num_repetitions=3,\n",
" experiment_prefix=\"sql-agent-multi-step-response-v-reference\",\n",
" metadata={\"version\": \"Chinook, gpt-4o multi-step-agent\"},\n",
" )\n",
"except:\n",
" print(\"Please setup LangSmith\")"
]
},
{
@@ -1057,17 +1060,20 @@
"metadata": {},
"outputs": [],
"source": [
"experiment_results = evaluate(\n",
" predict_sql_agent_messages,\n",
" data=dataset_name,\n",
" evaluators=[\n",
" contains_all_tool_calls_in_order,\n",
" contains_all_tool_calls_in_order_exact_match,\n",
" ],\n",
" num_repetitions=3,\n",
" experiment_prefix=\"sql-agent-multi-step-tool-calling-trajecory-in-order\",\n",
" metadata={\"version\": \"Chinook, gpt-4o multi-step-agent\"},\n",
")"
"try:\n",
" experiment_results = evaluate(\n",
" predict_sql_agent_messages,\n",
" data=dataset_name,\n",
" evaluators=[\n",
" contains_all_tool_calls_in_order,\n",
" contains_all_tool_calls_in_order_exact_match,\n",
" ],\n",
" num_repetitions=3,\n",
" experiment_prefix=\"sql-agent-multi-step-tool-calling-trajecory-in-order\",\n",
" metadata={\"version\": \"Chinook, gpt-4o multi-step-agent\"},\n",
" )\n",
"except:\n",
" print(\"Please setup LangSmith\")"
]
},
{
+80 -74
View File
@@ -51,8 +51,7 @@
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"%pip install -U langchain_community langchain_openai langgraph wikipedia scikit-learn langchain_fireworks\n",
"%pip install -U duckduckgo tavily-python"
"%pip install -U langchain_community langchain_openai langchain_fireworks langgraph wikipedia duckduckgo-search tavily-python"
]
},
{
@@ -69,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 86,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -110,16 +109,16 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"\n",
"fast_llm = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
"fast_llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"# Uncomment for a Fireworks model\n",
"# fast_llm = ChatFireworks(model=\"accounts/fireworks/models/firefunction-v1\", max_tokens=32_000)\n",
"long_context_llm = ChatOpenAI(model=\"gpt-4-turbo-preview\")"
"long_context_llm = ChatOpenAI(model=\"gpt-4o\")"
]
},
{
@@ -146,7 +145,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -213,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -224,19 +223,19 @@
"\n",
"## Introduction\n",
"\n",
"Overview of million-plus token context window language models and RAG (Retrieval-Augmented Generation).\n",
"Brief overview of million-plus token context window language models and RAG.\n",
"\n",
"## Million-Plus Token Context Window Language Models\n",
"\n",
"Explanation of million-plus token context window language models, their architecture, training data, and applications.\n",
"Explanation of million-plus token context window language models, their architecture, training process, and capabilities.\n",
"\n",
"## RAG (Retrieval-Augmented Generation)\n",
"## Retrieval-Augmented Generation (RAG)\n",
"\n",
"Overview of RAG, its architecture, how it combines retrieval and generation models, and its use in natural language processing tasks.\n",
"Explanation of RAG model, its components, and how it combines information retrieval with text generation.\n",
"\n",
"## Impact on RAG\n",
"\n",
"Discuss the impact of million-plus token context window language models on RAG, including improvements in performance, efficiency, and challenges faced.\n"
"Discussion on how million-plus token context window language models impact RAG, including improvements in performance, efficiency, and potential challenges.\n"
]
}
],
@@ -261,7 +260,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@@ -288,16 +287,16 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RelatedSubjects(topics=['Language models', 'Retriever-Reader-Generator (RAG) model', 'Natural language processing', 'Machine learning', 'Artificial intelligence', 'Text generation', 'Transformer architecture', 'Context window', 'Impact of language models'])"
"RelatedSubjects(topics=['million-plus token context window language models', 'RAG'])"
]
},
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -319,7 +318,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -370,7 +369,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@@ -409,7 +408,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -418,31 +417,35 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'editors': [{'affiliation': 'Academic Research',\n",
" 'name': 'Dr. Linguist',\n",
" 'role': 'Language Model Expert',\n",
" 'description': 'Dr. Linguist will focus on explaining the technical aspects of million-plus token context window language models and their impact on RAG (Retrieval-Augmented Generation) systems.'},\n",
" {'affiliation': 'Industry',\n",
" 'name': 'TechTrendz',\n",
" 'role': 'AI Solutions Architect',\n",
" 'description': 'TechTrendz will provide insights on the practical applications of million-plus token context window language models in RAG systems and discuss their benefits and challenges in real-world scenarios.'},\n",
" {'affiliation': 'Open Source Community',\n",
" 'name': 'CodeGenius',\n",
" 'role': 'Machine Learning Enthusiast',\n",
" 'description': 'CodeGenius will explore the open-source tools and frameworks available for implementing million-plus token context window language models in RAG systems and share their experiences with the community.'},\n",
" {'affiliation': 'Tech Journalism',\n",
" 'name': 'DataDive',\n",
" 'role': 'AI Technology Journalist',\n",
" 'description': 'DataDive will cover the latest developments and advancements in million-plus token context window language models and their implications for RAG systems, focusing on industry trends and use cases.'}]}"
"{'editors': [{'affiliation': 'Research Institution',\n",
" 'name': 'AliceResearcher',\n",
" 'role': 'Researcher',\n",
" 'description': 'AliceResearcher focuses on the impact of million-plus token context window language models on the Retrieval-Augmented Generation (RAG) framework. They analyze the effectiveness of large language models within the RAG framework and investigate how these models influence information retrieval and generation tasks.'},\n",
" {'affiliation': 'Tech Company',\n",
" 'name': 'BobEngineer',\n",
" 'role': 'Engineer',\n",
" 'description': 'BobEngineer specializes in implementing million-plus token context window language models in practical applications, particularly within the Retrieval-Augmented Generation (RAG) framework. They focus on optimizing the performance and efficiency of these models for real-world usage.'},\n",
" {'affiliation': 'Academic Institution',\n",
" 'name': 'CharlieAcademic',\n",
" 'role': 'Academic',\n",
" 'description': 'CharlieAcademic studies the theoretical implications of integrating million-plus token context window language models with the RAG framework. They explore the broader implications of using such large models for natural language processing and information retrieval.'},\n",
" {'affiliation': 'Industry Expert',\n",
" 'name': 'DianaExpert',\n",
" 'role': 'Industry Expert',\n",
" 'description': 'DianaExpert provides insights from the industry perspective on the impact of million-plus token context window language models on RAG. They focus on practical applications, challenges, and opportunities that arise when utilizing these models in commercial settings.'},\n",
" {'affiliation': 'AI Ethics Organization',\n",
" 'name': 'EveEthicist',\n",
" 'role': 'Ethicist',\n",
" 'description': 'EveEthicist examines the ethical considerations surrounding the use of million-plus token context window language models in the context of RAG. They focus on potential biases, fairness, and transparency issues that may arise from the deployment of such models.'}]}"
]
},
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -467,7 +470,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@@ -518,7 +521,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@@ -576,16 +579,16 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Yes, that's correct. I'm focusing on the technical aspects of million-plus token context window language models and their impact on Retrieval-Augmented Generation (RAG) systems. Can you provide more information on how these large context window language models are trained and how they differ from traditional models in the context of RAG systems?\""
"\"Yes, that's correct. I focus on studying the impact of million-plus token context window language models on the Retrieval-Augmented Generation (RAG) framework. I analyze how these large language models affect information retrieval and generation tasks within the RAG framework. Is there a specific aspect of this topic that you would like to know more about?\""
]
},
"execution_count": 15,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -615,7 +618,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
@@ -641,17 +644,18 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Training process of million-plus token context window language models',\n",
" 'Differences between large context window language models and traditional models in Retrieval-Augmented Generation systems']"
"['impact of million-plus token context window language models on Retrieval-Augmented Generation (RAG) framework',\n",
" 'information retrieval tasks in the RAG framework with large language models',\n",
" 'generation tasks in the RAG framework with million-plus token context window models']"
]
},
"execution_count": 17,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -665,7 +669,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
@@ -705,7 +709,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
@@ -736,7 +740,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@@ -781,16 +785,16 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Large context window language models, such as the Llama2 70B model, can support context windows of more than 100k tokens without continual training through innovations like Dual Chunk Attention (DCA). These models have significantly longer context windows compared to traditional models, with capabilities like processing up to 1 million tokens at once, providing more consistent and relevant outputs. Training these models often involves starting with a smaller window size and gradually increasing it through fine-tuning on larger windows. In contrast, traditional models have much shorter context windows, limiting their ability to process extensive information in a prompt. Retrieval-Augmented Generation (RAG) systems, on the other hand, integrate large language models with external knowledge sources to enhance their performance, offering a pathway to combine the capabilities of models like ChatGPT/GPT-4 with custom data sources for more informed and contextually aware outputs.\\n\\nCitations:\\n\\n[1]: https://arxiv.org/abs/2402.17463\\n[2]: https://blog.google/technology/ai/long-context-window-ai-models/\\n[3]: https://medium.com/@ddxzzx/why-and-how-to-achieve-longer-context-windows-for-llms-5f76f8656ea9\\n[4]: https://blog.google/technology/ai/google-gemini-next-generation-model-february-2024/\\n[5]: https://huggingface.co/papers/2402.13753\\n[6]: https://www.pinecone.io/blog/why-use-retrieval-instead-of-larger-context/\\n[7]: https://medium.com/emalpha/innovations-in-retrieval-augmented-generation-8e6e70f95629\\n[8]: https://inside-machinelearning.com/en/rag/'"
"'Studying the impact of million-plus token context window language models on the Retrieval-Augmented Generation (RAG) framework involves analyzing how these large language models affect information retrieval and generation tasks within the RAG framework. The introduction of large language models with extensive context windows, such as Google Gemini 1.5 Pro with a record 1 million token context window, has sparked discussions in the AI community about the potential implications for RAG. While there are concerns about the negative impact of supermassive context windows on RAG, there is also a recognition of the benefits they bring, enabling more use cases and enhancing performance in knowledge-intensive tasks. Retrieval-Augmented Generation (RAG) combines the generative capabilities of transformer architectures with dynamic information retrieval, allowing large language models to access and integrate relevant external knowledge during text generation, leading to more accurate and credible outputs. RAG has been identified as a valuable solution to address challenges faced by Large Language Models (LLMs), such as hallucination, outdated knowledge, lack of transparency, and untraceable reasoning processes. By incorporating external databases, RAG improves the consistency and coherence of generated content, especially in conversational question answering tasks. RAG has also been recognized as a powerful tool for large language models to efficiently process overly lengthy contexts, with recent LLMs like Gemini-1.5 and GPT-4 showcasing exceptional capabilities in understanding long contexts directly. There is ongoing research and benchmarking to compare the strengths of RAG and long-context LLMs, aiming to leverage the advantages of both approaches for enhanced performance in information retrieval and generation tasks within the RAG framework.\\n\\nCitations:\\n\\n[1]: https://thenewstack.io/do-enormous-llm-context-windows-spell-the-end-of-rag/\\n[2]: https://medium.com/enterprise-rag/why-gemini-1-5-and-other-large-context-models-are-bullish-for-rag-ce3218930bb4\\n[3]: https://medium.com/@amanatulla1606/rag-is-here-to-stay-four-reasons-why-large-context-windows-cant-replace-it-ad112013de25\\n[4]: https://www.freecodecamp.org/news/retrieval-augmented-generation-rag-handbook/\\n[5]: https://www.deepset.ai/blog/long-context-llms-rag\\n[6]: https://arxiv.org/abs/2312.10997\\n[7]: https://arxiv.org/abs/2409.13385\\n[8]: https://irisagent.com/blog/enhancing-large-language-models-a-deep-dive-into-rag-llm-technology/\\n[9]: https://arxiv.org/abs/2407.16833'"
]
},
"execution_count": 21,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -814,11 +818,12 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"max_num_turns = 5\n",
"from langgraph.pregel import RetryPolicy\n",
"\n",
"\n",
"def route_messages(state: InterviewState, name: str = \"Subject_Matter_Expert\"):\n",
@@ -836,18 +841,18 @@
"\n",
"builder = StateGraph(InterviewState)\n",
"\n",
"builder.add_node(\"ask_question\", generate_question)\n",
"builder.add_node(\"answer_question\", gen_answer)\n",
"builder.add_node(\"ask_question\", generate_question, retry=RetryPolicy(max_attempts=5))\n",
"builder.add_node(\"answer_question\", gen_answer, retry=RetryPolicy(max_attempts=5))\n",
"builder.add_conditional_edges(\"answer_question\", route_messages)\n",
"builder.add_edge(\"ask_question\", \"answer_question\")\n",
"\n",
"builder.add_edge(START, \"ask_question\")\n",
"interview_graph = builder.compile().with_config(run_name=\"Conduct Interviews\")"
"interview_graph = builder.compile(checkpointer=False).with_config(run_name=\"Conduct Interviews\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -873,7 +878,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 29,
"metadata": {},
"outputs": [
{
@@ -881,19 +886,21 @@
"output_type": "stream",
"text": [
"ask_question\n",
"-- [AIMessage(content=\"Yes, that's correct. I am focusing on the technical aspects of million-plus token context window language models and their impact on RAG systems. Can you provide more insight into how these large context window models affect the performance and capabilities of RAG systems?\", name\n",
"-- [AIMessage(content=\"Yes, that's correct. My focus is on how million-plus token context window language models impact the Retrieval-Augmented Generation (RAG) framework. I'm interested in understanding how the size and scope of these language models influence the effectiveness of the RAG framework in\n",
"answer_question\n",
"-- [AIMessage(content='The introduction of large context window language models, such as Gemini 1.5 with a 1 million token context window, has raised concerns in the AI community regarding its impact on Retrieval-Augmented Generation (RAG) systems. RAG systems represent a significant advancement over t\n",
"-- [AIMessage(content='The integration of million-plus token context window language models in the Retrieval-Augmented Generation (RAG) framework has emerged as a significant advancement in natural language processing. RAG has been a reliable solution for context-based answer generation, overcoming the\n",
"ask_question\n",
"-- [AIMessage(content='Thank you for the detailed explanation and resources. Could you elaborate on the specific challenges and opportunities that million-plus token context window language models present for RAG systems in terms of improving generation quality, addressing data biases, and the potentia\n",
"-- [AIMessage(content='Can you elaborate on how the retrieval-augmented techniques in the RAG framework enhance the accuracy and relevance of the generated text by accessing external knowledge dynamically? How does this process differ from traditional language modeling approaches, and what specific ben\n",
"answer_question\n",
"-- [AIMessage(content='Million-plus token context window language models present both challenges and opportunities for RAG systems. Challenges include the increased computational cost and complexity associated with processing larger context windows, potential issues with retaining factual accuracy when\n",
"-- [AIMessage(content='The integration of retrieval-augmented techniques in the RAG framework enhances the accuracy and relevance of generated text by dynamically accessing external knowledge sources to enrich the generation process. Unlike traditional language modeling approaches that rely solely on i\n",
"ask_question\n",
"-- [AIMessage(content='Thank you for the detailed information and references provided. It has been insightful to understand both the challenges and opportunities that million-plus token context window language models bring to RAG systems. I appreciate your assistance in shedding light on this complex t\n",
"-- [AIMessage(content='Thank you for the insightful information on how retrieval-augmented techniques enhance the accuracy and relevance of text generation in the RAG framework by incorporating external knowledge sources. How do these external knowledge sources impact the overall performance and adapta\n",
"answer_question\n",
"-- [AIMessage(content=\"You're welcome! If you have any more questions or need further assistance in the future, feel free to reach out. Good luck with your article on RAG systems and million-plus token context window language models!\\n\\nCitations:\\n\\n[1]: https://www.nerdwallet.com/article/finance/exam\n",
"__end__\n",
"-- [AIMessage(content='So you said you were writing an article on Impact of million-plus token context window language models on RAG?', name='Subject Matter Expert'), AIMessage(content=\"Yes, that's correct. I am focusing on the technical aspects of million-plus token context window language models and \n"
"-- [AIMessage(content='External knowledge sources significantly impact the overall performance and adaptability of RAG models in handling various types of information retrieval and generation tasks. By leveraging external knowledge, RAG models access up-to-date information, reduce the incidence of gene\n",
"ask_question\n",
"-- [AIMessage(content='Thank you for sharing insights on how external knowledge sources impact the performance and adaptability of RAG models in handling diverse information retrieval and generation tasks. This information will be valuable for my research on the impact of million-plus token context win\n",
"answer_question\n",
"-- [AIMessage(content=\"The integration of million-plus token context window language models in the Retrieval-Augmented Generation (RAG) framework has sparked discussions in the AI community, with some concerns about the potential impact on RAG's relevance. However, RAG continues to be a valuable soluti\n"
]
}
],
@@ -913,8 +920,7 @@
" name = next(iter(step))\n",
" print(name)\n",
" print(\"-- \", str(step[name][\"messages\"])[:300])\n",
" if END in step:\n",
" final_step = step"
"final_step = step"
]
},
{
@@ -1065,7 +1071,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.vectorstores import SKLearnVectorStore\n",
"from langchain_community.vectorstores import InMemoryVectorStore\n",
"from langchain_core.documents import Document\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
@@ -1077,11 +1083,11 @@
"# This really doesn't need to be a vectorstore for this size of data.\n",
"# It could just be a numpy matrix. Or you could store documents\n",
"# across requests if you want.\n",
"vectorstore = SKLearnVectorStore.from_documents(\n",
"vectorstore = InMemoryVectorStore.from_documents(\n",
" reference_docs,\n",
" embedding=embeddings,\n",
")\n",
"retriever = vectorstore.as_retriever(k=10)"
"retriever = vectorstore.as_retriever(k=3)"
]
},
{
@@ -1520,7 +1526,7 @@
"]\n",
"for i in range(len(nodes)):\n",
" name, node = nodes[i]\n",
" builder_of_storm.add_node(name, node)\n",
" builder_of_storm.add_node(name, node, retry=RetryPolicy(max_attempts=3))\n",
" if i > 0:\n",
" builder_of_storm.add_edge(nodes[i - 1][0], name)\n",
"\n",