Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,8 @@ 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 |
|
| 10 |
|
| 11 |
|
|
@@ -52,6 +54,16 @@ Answer:""",
|
|
| 52 |
]
|
| 53 |
)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def format_docs(docs):
|
| 56 |
return "\n\n".join(doc.page_content for doc in docs)
|
| 57 |
|
|
@@ -62,36 +74,40 @@ rag_chain = (
|
|
| 62 |
| StrOutputParser()
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
messages.append((user_message, response))
|
| 72 |
-
|
| 73 |
-
# Return the updated
|
| 74 |
return messages, ""
|
| 75 |
|
| 76 |
|
| 77 |
-
# Define the Gradio app
|
| 78 |
with gr.Blocks(theme="rawrsor1/Everforest") as app:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
|
| 82 |
question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
|
| 83 |
submit_btn = gr.Button("Submit")
|
| 84 |
-
|
| 85 |
# Set up interaction for both Enter key and Submit button
|
| 86 |
question_input.submit(
|
| 87 |
-
|
| 88 |
-
inputs=[chatbot, question_input],
|
| 89 |
-
outputs=[chatbot, question_input]
|
| 90 |
)
|
| 91 |
submit_btn.click(
|
| 92 |
-
|
| 93 |
-
inputs=[chatbot, question_input],
|
| 94 |
-
outputs=[chatbot, question_input]
|
| 95 |
)
|
| 96 |
|
| 97 |
# 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 |
+
from langchain.chains import ConversationChain
|
| 10 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 11 |
|
| 12 |
|
| 13 |
|
|
|
|
| 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)
|
| 69 |
|
|
|
|
| 74 |
| StrOutputParser()
|
| 75 |
)
|
| 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
|