Unable to use ConversationBufferWindowMemory in async mode with FastAPI #27294
Replies: 9 comments 7 replies
-
Hey there, @omarabdelrhmaan! I'm here to help you out with any bugs, questions, or contributions you might have. Let's dive into this together! To resolve the issue with using
Here's an example of how you might adjust your code: from langchain.memory.buffer import ConversationBufferWindowMemory
from langchain_community.chat_message_histories.sql import SQLChatMessageHistory
from sqlalchemy.ext.asyncio import create_async_engine
# Create an async engine
async_engine = create_async_engine("sqlite+aiosqlite:///memory.db")
# Initialize SQLChatMessageHistory in async mode
async_chat_history = SQLChatMessageHistory(
session_id=session_id,
connection=async_engine,
async_mode=True
)
# Initialize ConversationBufferWindowMemory in async mode
memory = ConversationBufferWindowMemory(
memory_key="chat_history",
chat_memory=async_chat_history,
return_messages=True,
k=5
)
# Ensure you use async methods when interacting with memory
async def process_conversation(inputs, outputs):
# Load memory variables asynchronously
memory_variables = await memory.aload_memory_variables(inputs)
# Save context asynchronously
await memory.asave_context(inputs, outputs)
return memory_variables Make sure to replace any synchronous calls with their asynchronous counterparts to avoid the |
Beta Was this translation helpful? Give feedback.
-
@dosu I'm not managing the conversation Memory manually. i just setup it. could you please elaborate your solution and how it could be integrated with ConversationBufferWindowMemory |
Beta Was this translation helpful? Give feedback.
-
how can this be integrated with agent_executor.ainvoke here's my chatservice class
|
Beta Was this translation helpful? Give feedback.
-
below is my chat service class that handles the user query with ainvoke. how can the memory be managed async here
|
Beta Was this translation helpful? Give feedback.
-
Still experiencing the same error File "/lib/python3.8/site-packages/langchain_community/chat_message_histories/sql.py", line 340, in _make_sync_session |
Beta Was this translation helpful? Give feedback.
-
Here's my full setup and still receiving the same error
from langchain_core.messages import AIMessage, HumanMessage from services.agent_service import AgentService
|
Beta Was this translation helpful? Give feedback.
-
I've followed the same instructions you provided but still receiving the same error, any other thoughts to solve this or debug it? |
Beta Was this translation helpful? Give feedback.
-
Where should set_debug(True) be placed? |
Beta Was this translation helpful? Give feedback.
-
Hello, In case someone was looking for a solution, I had to modify the langchain package, especially the SQLChatMessageHistory, ConversationBufferWindowMemory, and memory classes to process the async connections correctly. If you need the full implementation DM me and I'll be happy to help. In the next days, I'll work on creating a pull request with these changes in the main repo of langchain. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm developing a service using langchain and FastAPI and have the chat history managed with SQLChatMessageHistory in async mode. and when I pass the memory using ConversationBufferWindowMemory I receive the following error
ValueError: Attempting to use a sync method in when async mode is turned on. Please use the corresponding async method instead.
Code snippet
Agent Initialization
Beta Was this translation helpful? Give feedback.
All reactions