araeyn commited on
Commit
52ce267
1 Parent(s): 6e34cb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -20,6 +20,37 @@ from langchain_core.chat_history import BaseChatMessageHistory
20
  from langchain_community.chat_message_histories import ChatMessageHistory
21
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  if not os.path.isdir('database'):
24
  os.system("unzip database.zip")
25
 
@@ -118,36 +149,7 @@ conversational_rag_chain = RunnableWithMessageHistory(
118
  history_messages_key="chat_history",
119
  output_messages_key="answer",
120
  )
121
- print()
122
- print("-------")
123
- print("started")
124
- print("-------")
125
-
126
- async def echo(websocket):
127
- async for message in websocket:
128
- data = json.loads(message)
129
- if not "message" in message:
130
- return
131
- if not "token" in message:
132
- return
133
- m = data["message"]
134
- token = data["token"]
135
- userData = json.load(open("userData.json", "w"))
136
- docs = retriever.get_relevant_documents(m)
137
- userData[token]["docs"] = str(docs)
138
- response = conversational_rag_chain.invoke(
139
- {"input": m},
140
- config={
141
- "configurable": {"session_id": token}
142
- },
143
- )["answer"]
144
- await websocket.send(json.dumps({"response": response}))
145
 
146
- async def main():
147
- async with serve(echo, "0.0.0.0", 7860):
148
- await asyncio.Future()
149
-
150
- asyncio.run(main())
151
  """
152
  websocket
153
  streamlit app ~> backend
 
20
  from langchain_community.chat_message_histories import ChatMessageHistory
21
  from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
22
 
23
+ print()
24
+ print("-------")
25
+ print("started")
26
+ print("-------")
27
+
28
+ async def echo(websocket):
29
+ async for message in websocket:
30
+ data = json.loads(message)
31
+ if not "message" in message:
32
+ return
33
+ if not "token" in message:
34
+ return
35
+ m = data["message"]
36
+ token = data["token"]
37
+ userData = json.load(open("userData.json", "w"))
38
+ docs = retriever.get_relevant_documents(m)
39
+ userData[token]["docs"] = str(docs)
40
+ response = conversational_rag_chain.invoke(
41
+ {"input": m},
42
+ config={
43
+ "configurable": {"session_id": token}
44
+ },
45
+ )["answer"]
46
+ await websocket.send(json.dumps({"response": response}))
47
+
48
+ async def main():
49
+ async with serve(echo, "0.0.0.0", 7860):
50
+ await asyncio.Future()
51
+
52
+ asyncio.run(main())
53
+
54
  if not os.path.isdir('database'):
55
  os.system("unzip database.zip")
56
 
 
149
  history_messages_key="chat_history",
150
  output_messages_key="answer",
151
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
 
 
 
 
 
153
  """
154
  websocket
155
  streamlit app ~> backend