Pijush2023 commited on
Commit
d5add66
·
verified ·
1 Parent(s): 393e5d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -30
app.py CHANGED
@@ -6,8 +6,7 @@ from langchain_core.prompts import ChatPromptTemplate
6
  from langchain_core.runnables import RunnablePassthrough
7
  from langchain_groq import ChatGroq
8
  from langchain.embeddings.openai import OpenAIEmbeddings
9
- from langchain.chains import ConversationChain
10
- from langchain.memory import ConversationBufferWindowMemory
11
 
12
 
13
 
@@ -54,15 +53,7 @@ Answer:""",
54
  ]
55
  )
56
 
57
- # Conversation history memory
58
- memory = ConversationBufferWindowMemory(k=3)
59
 
60
- # Define the conversation chain
61
- conversation = ConversationChain(
62
- llm=llm,
63
- memory=memory,
64
- verbose=True
65
- )
66
 
67
  def format_docs(docs):
68
  return "\n\n".join(doc.page_content for doc in docs)
@@ -76,38 +67,37 @@ rag_chain = (
76
 
77
 
78
 
79
- # Define the Gradio app
80
- def chatbot_response(messages, user_message):
81
- # Update memory with the user's message
82
- memory.chat_memory.add_user_message(user_message)
83
-
84
- # Get the response from the conversation chain
85
- response = conversation.run(input=user_message)
86
-
87
- # Append the user's message and the response to the chat history
88
  messages.append((user_message, response))
89
-
90
- # Return the updated messages and clear the input box
91
  return messages, ""
92
 
93
 
94
- with gr.Blocks(theme="rawrsor1/Everforest") as app:
95
-
96
 
 
 
 
 
97
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
98
  question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
99
  submit_btn = gr.Button("Submit")
100
-
101
  # Set up interaction for both Enter key and Submit button
102
  question_input.submit(
103
- chatbot_response,
104
- inputs=[chatbot, question_input],
105
- outputs=[chatbot, question_input]
106
  )
107
  submit_btn.click(
108
- chatbot_response,
109
- inputs=[chatbot, question_input],
110
- outputs=[chatbot, question_input]
111
  )
112
 
113
  # Launch the Gradio app
 
6
  from langchain_core.runnables import RunnablePassthrough
7
  from langchain_groq import ChatGroq
8
  from langchain.embeddings.openai import OpenAIEmbeddings
9
+
 
10
 
11
 
12
 
 
53
  ]
54
  )
55
 
 
 
56
 
 
 
 
 
 
 
57
 
58
  def format_docs(docs):
59
  return "\n\n".join(doc.page_content for doc in docs)
 
67
 
68
 
69
 
70
+ # Function to handle chatbot interaction
71
+ def rag_chain_response(messages, user_message):
72
+ # Generate a response using the RAG chain
73
+ response = rag_chain.invoke(user_message)
74
+
75
+ # Append the user's message and the response to the chat
 
 
 
76
  messages.append((user_message, response))
77
+
78
+ # Return the updated chat and clear the input box
79
  return messages, ""
80
 
81
 
 
 
82
 
83
+ # Define the Gradio app
84
+ with gr.Blocks(theme="rawrsor1/Everforest") as app:
85
+
86
+
87
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
88
  question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
89
  submit_btn = gr.Button("Submit")
90
+
91
  # Set up interaction for both Enter key and Submit button
92
  question_input.submit(
93
+ rag_chain_response, # Function to handle input and generate response
94
+ inputs=[chatbot, question_input], # Pass current conversation state and user input
95
+ outputs=[chatbot, question_input] # Update conversation state and clear the input
96
  )
97
  submit_btn.click(
98
+ rag_chain_response, # Function to handle input and generate response
99
+ inputs=[chatbot, question_input], # Pass current conversation state and user input
100
+ outputs=[chatbot, question_input] # Update conversation state and clear the input
101
  )
102
 
103
  # Launch the Gradio app