Romain Fayoux commited on
Commit
9707da3
·
1 Parent(s): 51d1178

Added unique thread for langchain agent, messages were persisted between

Browse files
Files changed (1) hide show
  1. langchain_agent.py +4 -1
langchain_agent.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import re
 
3
  from dotenv import load_dotenv
4
  from langchain.agents import create_agent
5
  from langgraph.checkpoint.memory import InMemorySaver
@@ -37,9 +38,11 @@ If you are asked for a comma separated list, apply the above rules depending of
37
 
38
  def __call__(self, question: str) -> str:
39
  print(f"Agent received question (first 50 chars): {question[:50]}...")
 
 
40
  response = self.agent.invoke(
41
  {"messages": [HumanMessage(content=question)]},
42
- {"configurable": {"thread_id": "1"}, "recursion_limit": 50},
43
  )
44
  answer = response["messages"][-1].text
45
  final_answer = self.extract_final_answer(answer)
 
1
  import os
2
  import re
3
+ import uuid
4
  from dotenv import load_dotenv
5
  from langchain.agents import create_agent
6
  from langgraph.checkpoint.memory import InMemorySaver
 
38
 
39
  def __call__(self, question: str) -> str:
40
  print(f"Agent received question (first 50 chars): {question[:50]}...")
41
+ # Generate a unique thread ID for each request
42
+ thread_id = str(uuid.uuid4())
43
  response = self.agent.invoke(
44
  {"messages": [HumanMessage(content=question)]},
45
+ {"configurable": {"thread_id": thread_id}, "recursion_limit": 50},
46
  )
47
  answer = response["messages"][-1].text
48
  final_answer = self.extract_final_answer(answer)