|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from langchain_core.callbacks.base import Callbacks |
| 6 | +from langchain_core.embeddings import Embeddings |
| 7 | +from langchain_core.language_models.chat_models import BaseChatModel |
| 8 | +from langchain_core.runnables import RunnableConfig |
| 9 | +from langgraph.checkpoint.base import BaseCheckpointSaver |
| 10 | +from langgraph.checkpoint.memory import MemorySaver |
| 11 | +from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver |
| 12 | +from langgraph.graph.state import CompiledStateGraph, StateGraph |
| 13 | +from psycopg import AsyncConnection |
| 14 | +from psycopg_pool import AsyncConnectionPool |
| 15 | + |
| 16 | +from agent.models import get_embedding, get_llm |
| 17 | +from agent.profiles import ProfileName, create_profile_graphs |
| 18 | +from util.logging import logging |
| 19 | + |
| 20 | +LANGGRAPH_DB_URI = f"postgresql://{os.getenv('POSTGRES_USER')}:{os.getenv('POSTGRES_PASSWORD')}@postgres:5432/{os.getenv('POSTGRES_LANGGRAPH_DB')}?sslmode=disable" |
| 21 | + |
| 22 | +if not os.getenv("POSTGRES_LANGGRAPH_DB"): |
| 23 | + logging.warning("POSTGRES_LANGGRAPH_DB undefined; falling back to MemorySaver.") |
| 24 | + |
| 25 | + |
| 26 | +class AgentGraph: |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + profiles: list[ProfileName], |
| 30 | + ) -> None: |
| 31 | + # Get base models |
| 32 | + llm: BaseChatModel = get_llm("openai", "gpt-4o-mini") |
| 33 | + embedding: Embeddings = get_embedding("openai", "text-embedding-3-large") |
| 34 | + |
| 35 | + self.uncompiled_graph: dict[str, StateGraph] = create_profile_graphs( |
| 36 | + profiles, llm, embedding |
| 37 | + ) |
| 38 | + |
| 39 | + # The following are set asynchronously by calling initialize() |
| 40 | + self.graph: dict[str, CompiledStateGraph] | None = None |
| 41 | + self.pool: AsyncConnectionPool[AsyncConnection[dict[str, Any]]] | None = None |
| 42 | + |
| 43 | + def __del__(self) -> None: |
| 44 | + if self.pool: |
| 45 | + asyncio.run(self.close_pool()) |
| 46 | + |
| 47 | + async def initialize(self) -> dict[str, CompiledStateGraph]: |
| 48 | + checkpointer: BaseCheckpointSaver[str] = await self.create_checkpointer() |
| 49 | + return { |
| 50 | + profile: graph.compile(checkpointer=checkpointer) |
| 51 | + for profile, graph in self.uncompiled_graph.items() |
| 52 | + } |
| 53 | + |
| 54 | + async def create_checkpointer(self) -> BaseCheckpointSaver[str]: |
| 55 | + if not os.getenv("POSTGRES_LANGGRAPH_DB"): |
| 56 | + return MemorySaver() |
| 57 | + self.pool = AsyncConnectionPool( |
| 58 | + conninfo=LANGGRAPH_DB_URI, |
| 59 | + max_size=20, |
| 60 | + open=False, |
| 61 | + timeout=30, |
| 62 | + kwargs={ |
| 63 | + "autocommit": True, |
| 64 | + "prepare_threshold": 0, |
| 65 | + }, |
| 66 | + ) |
| 67 | + await self.pool.open() |
| 68 | + checkpointer = AsyncPostgresSaver(self.pool) |
| 69 | + await checkpointer.setup() |
| 70 | + return checkpointer |
| 71 | + |
| 72 | + async def close_pool(self) -> None: |
| 73 | + if self.pool: |
| 74 | + await self.pool.close() |
| 75 | + |
| 76 | + async def ainvoke( |
| 77 | + self, |
| 78 | + user_input: str, |
| 79 | + profile: str, |
| 80 | + *, |
| 81 | + callbacks: Callbacks, |
| 82 | + thread_id: str, |
| 83 | + enable_postprocess: bool = True, |
| 84 | + ) -> dict[str, Any]: |
| 85 | + if self.graph is None: |
| 86 | + self.graph = await self.initialize() |
| 87 | + if profile not in self.graph: |
| 88 | + return {} |
| 89 | + result: dict[str, Any] = await self.graph[profile].ainvoke( |
| 90 | + {"user_input": user_input}, |
| 91 | + config=RunnableConfig( |
| 92 | + callbacks=callbacks, |
| 93 | + configurable={ |
| 94 | + "thread_id": thread_id, |
| 95 | + "enable_postprocess": enable_postprocess, |
| 96 | + }, |
| 97 | + ), |
| 98 | + ) |
| 99 | + return result |
0 commit comments