Replies: 2 comments 1 reply
-
🤖 Hello again, From your description, it seems like the issue might be related to the asynchronous nature of websockets and how LangChain handles tool execution in such an environment. In LangChain, the execution of tools in an asynchronous environment like websockets is handled through the use of Python's async and await keywords. This is evident in the However, in your websocket server implementation, you are not using the async version of the To solve this issue, you could try making the import asyncio
from agent import ChatBot
from websockets.server import serve
async def reply(websocket):
async for message in websocket:
reply = await chatbot.chat(message)
await websocket.send(reply)
async def main():
async with serve(reply, "192.168.0.109", 8765):
await asyncio.Future() # run forever
chatbot = ChatBot()
asyncio.run(main()) And in your class ChatBot():
# ... rest of your code ...
async def chat(self, input):
output = await self.agent_executor.run(input)
if len(self.images) > 0:
return f"{{'output': '{output}', 'images': {self.images}}}"
return output Please note that you might need to adjust other parts of your code to make them compatible with async operations. I hope this helps! Let me know if you have any other questions. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
LangChain WebSocket streaming often lags or breaks under real-time load. No delays. No dropped connections. Just fast, real-time AI chats. |
Beta Was this translation helpful? Give feedback.
-
It's me again. For my next step on my app development I need to communicate with my langchain chatbot instance with a mobile interface via websockets.
I have an implemented it the following way:
The connection works and you can communicate with the agent back and forth successfully. However, there is something odd: the agent is unable to call actions. There is no error, the actions simply don't kick and the agent gives a straight reply. I know there is no flaw in the way I implemented the tools because if I run the agent directly (via Python script), agent is able to run the actions as expected. Agent doesn't call the actions only when I communicate with it via websockets, which makes me suspect it has something to do with the way websockets run the chatbot asynchronously.
For reference, here is my current chatbot implementation:
Beta Was this translation helpful? Give feedback.
All reactions