Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.memory import ConversationBufferMemory
|
| 2 |
+
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain.chains import LLMChain
|
| 5 |
+
|
| 6 |
+
groq_api_key='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'
|
| 7 |
+
|
| 8 |
+
llm = ChatGroq(model="gemma2-9b-it",api_key=groq_api_key)
|
| 9 |
+
|
| 10 |
+
from langchain_core.prompts import PromptTemplate
|
| 11 |
+
|
| 12 |
+
template = ("""You are a professional Maths tutor answer questions provided by user in step by step manner.
|
| 13 |
+
Use the provided context to answer the question.
|
| 14 |
+
try to engange with the user and follow up on questions asked
|
| 15 |
+
If you don't know the answer, say so. Explain your answer in detail.
|
| 16 |
+
Do not discuss the context in your response; just provide the answer directly.
|
| 17 |
+
|
| 18 |
+
Question: {question}
|
| 19 |
+
|
| 20 |
+
Answer:""")
|
| 21 |
+
|
| 22 |
+
rag_prompt = PromptTemplate.from_template(template)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
history = StreamlitChatMessageHistory(key="chat_messages")
|
| 27 |
+
|
| 28 |
+
#Step 3 - here we create a memory object
|
| 29 |
+
|
| 30 |
+
memory = ConversationBufferMemory(chat_memory=history)
|
| 31 |
+
|
| 32 |
+
llm_chain = LLMChain(llm=llm, prompt=rag_prompt, memory=memory)
|
| 33 |
+
|
| 34 |
+
import streamlit as st
|
| 35 |
+
|
| 36 |
+
st.title('🦜🔗 Welcome to the MathLearn 🦜🔗')
|
| 37 |
+
for msg in history.messages:
|
| 38 |
+
st.chat_message(msg.type).write(msg.content)
|
| 39 |
+
|
| 40 |
+
if x := st.chat_input():
|
| 41 |
+
st.chat_message("human").write(x)
|
| 42 |
+
|
| 43 |
+
# As usual, new messages are added to StreamlitChatMessageHistory when the Chain is called.
|
| 44 |
+
response = llm_chain.invoke(x)
|
| 45 |
+
st.chat_message("ai").write(response["text"])
|