[Python] Implement async singleton connection pool of postgresql for checkpointer in a fast...

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    Can you implement a singleton for this particular code for fastAPI application which will be using the postgresql for connection pool asynchronously and using that pool to have the check pointer saved which is getting used by langgraph Here is the code:

    from psycopg_pool import AsyncConnectionPool

    async with AsyncConnectionPool(
    # Example configuration
    conninfo=DB_URI,
    max_size=20,
    kwargs=connection_kwargs,
    ) as pool:
    checkpointer = AsyncPostgresSaver(pool)

    NOTE: you need to call .setup() the first time you're using your checkpointer
    await checkpointer.setup()

    graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
    config = {"configurable": {"thread_id": "4"}}
    res = await graph.ainvoke(
    {"messages": [("human", "what's the weather in nyc")]}, config
    )

    checkpoint = await checkpointer.aget(config)

    Continue reading...

Compartilhe esta Página