{ "cells": [ { "cell_type": "markdown", "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", "metadata": {}, "source": [ "# Persistence with Postgres\n", "\n", ":::note\n", "The langchain-postgres package has not kept up-to-date with the LangGraph package. While we work to make improvements, we will leave the following code for historic context.\n", ":::\n", "\n", "When creating LangGraph agents, you can also set them up so that they persist their state. This allows you to do things like interact with an agent multiple times and have it remember previous interactions.\n", "\n", "This example shows how to use `Postgres` as the backend for persisting checkpoint state.\n", "\n", "The distinguishing code is as follows:\n", "\n", "```python\n", "from psycopg_pool import ConnectionPool\n", "from langchain_postgres import PostgresSaver, PickleCheckpointSerializer\n", "\n", "pool = ConnectionPool(\n", " # Example configuration. Update to your DB\n", " conninfo=\"postgresql://langchain:langchain@localhost:6024/langchain\",\n", " max_size=20,\n", ")\n", "# Remember to close the pool once you're done\n", "\n", "PostgresSaver.create_tables(pool)\n", "\n", "memory = PostgresSaver(\n", " serializer=PickleCheckpointSerializer(),\n", " sync_connection=pool,\n", ")\n", "\n", "# Finally, we compile it!\n", "# This compiles it into a LangChain Runnable,\n", "# meaning you can use it as you would any other runnable\n", "app = workflow.compile(checkpointer=memory)\n", "```" ] }, { "cell_type": "markdown", "id": "2d486fd6", "metadata": {}, "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.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }