arun47 commited on
Commit
80ab468
·
verified ·
1 Parent(s): 491d3e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -15,7 +15,6 @@ template = """Meet Arun, your youthful and witty personal assistant!
15
  At 21 years old, he is full of energy and always eager to help.
16
  Arun's goal is to assist you with any questions or problems you might have.
17
  His enthusiasm shines through in every response, making interactions enjoyable and engaging.
18
-
19
  {chat_history}
20
  User: {user_message}
21
  Chatbot:"""
@@ -25,9 +24,6 @@ prompt = PromptTemplate(
25
  template=template
26
  )
27
 
28
- # Memory (Conversation history)
29
- memory = ConversationBufferMemory(memory_key="chat_history")
30
-
31
  # LLM with API Key
32
  llm = ChatOpenAI(
33
  temperature=0.5,
@@ -35,28 +31,34 @@ llm = ChatOpenAI(
35
  api_key=openai_api_key
36
  )
37
 
38
- # LLM Chain
39
- llm_chain = LLMChain(
40
- llm=llm,
41
- prompt=prompt,
42
- verbose=True,
43
- memory=memory
44
- )
 
 
 
 
 
 
45
 
46
- # Chat Function
47
- def get_text_response(user_message, history):
48
  response = llm_chain.predict(user_message=user_message)
49
- return response
50
 
51
- # Gradio Chat App
52
  demo = gr.ChatInterface(
53
  fn=get_text_response,
54
  examples=[
55
  "How are you doing?",
56
  "What are your interests?",
57
  "Which places do you like to visit?"
58
- ]
 
59
  )
60
 
61
  if __name__ == "__main__":
62
  demo.launch(share=True)
 
 
15
  At 21 years old, he is full of energy and always eager to help.
16
  Arun's goal is to assist you with any questions or problems you might have.
17
  His enthusiasm shines through in every response, making interactions enjoyable and engaging.
 
18
  {chat_history}
19
  User: {user_message}
20
  Chatbot:"""
 
24
  template=template
25
  )
26
 
 
 
 
27
  # LLM with API Key
28
  llm = ChatOpenAI(
29
  temperature=0.5,
 
31
  api_key=openai_api_key
32
  )
33
 
34
+ # Chat Function with per-user memory
35
+ def get_text_response(user_message, state):
36
+ # create a new memory if state is empty
37
+ if state is None:
38
+ state = ConversationBufferMemory(memory_key="chat_history")
39
+
40
+ # Build chain with user-specific memory
41
+ llm_chain = LLMChain(
42
+ llm=llm,
43
+ prompt=prompt,
44
+ verbose=True,
45
+ memory=state
46
+ )
47
 
 
 
48
  response = llm_chain.predict(user_message=user_message)
49
+ return response, state
50
 
51
+ # Gradio Chat App with user-specific state
52
  demo = gr.ChatInterface(
53
  fn=get_text_response,
54
  examples=[
55
  "How are you doing?",
56
  "What are your interests?",
57
  "Which places do you like to visit?"
58
+ ],
59
+ additional_inputs=[gr.State()] # 👈 ensures each user has independent memory
60
  )
61
 
62
  if __name__ == "__main__":
63
  demo.launch(share=True)
64
+