File size: 1,892 Bytes
f06ec84
 
 
 
 
 
 
 
 
e0ef00a
cb57aa5
fb78408
 
 
 
 
 
7c25492
 
 
 
 
 
 
 
 
 
fb78408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73b0971
fb78408
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import subprocess

# List of packages to install
packages = ['langchain', 'langchain-community', 'langchainhub','langchain-chroma','langchain-groq','langchain-huggingface','gradio']  

# Install packages
for package in packages:
    subprocess.check_call(['pip', 'install', package])


from langchain.memory import ConversationBufferMemory
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
from langchain_groq import ChatGroq
from langchain.chains import LLMChain

groq_api_key='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'

llm = None

def load_model():
    global llm
    if llm is None:
        llm=ChatGroq(model="gemma2-9b-it",api_key=groq_api_key)
    return llm


llm = load_model()

from langchain_core.prompts import PromptTemplate

template = ("""You are a professional Maths tutor answer questions provided by user in step by step manner.
    Use the provided context to answer the question.
    try to engange with the user and follow up on questions asked
    If you don't know the answer, say so. Explain your answer in detail.
    Do not discuss the context in your response; just provide the answer directly.
    
    Question: {question}

    Answer:""")

rag_prompt = PromptTemplate.from_template(template)



history = StreamlitChatMessageHistory(key="chat_messages")

#Step 3 - here we create a memory object

memory = ConversationBufferMemory(chat_memory=history)

llm_chain = LLMChain(llm=llm, prompt=rag_prompt, memory=memory)

import streamlit as st

st.title('πŸ¦œπŸ”— Welcome to the MathLearn ')
for msg in history.messages:
    st.chat_message(msg.type).write(msg.content)

if x := st.chat_input():
    st.chat_message("human").write(x)

    # As usual, new messages are added to StreamlitChatMessageHistory when the Chain is called.
    response = llm_chain.invoke(x)
    st.chat_message("ai").write(response["text"])