Spaces:
Sleeping
Sleeping
File size: 3,838 Bytes
34cb636 159023c c5d2823 34cb636 3bf390a 34cb636 159023c 34cb636 159023c 34cb636 159023c 34cb636 159023c 34cb636 159023c 34cb636 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.prompts import PromptTemplate
from langchain_together import Together
import os
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationalRetrievalChain
import streamlit as st
import time
st.set_page_config(page_title="zhagaramGPT")
col1, col2, col3 = st.columns([2,6,2])
with col2:
st.image("logo.jpeg")
st.markdown(
"""
<style>
div[data-baseweb="input"] input {
border-color: #000000;
}
margin-top: 0 !important;
div.stButton > button:first-child {
background-color: #808080;
color:white;
}
div.stButton > button:active {
background-color: #808080;
color : white;
}
div[data-testid="stStatusWidget"] div button {
display: none;
}
.reportview-container {
margin-top: -2em;
}
#MainMenu {visibility: hidden;}
.stDeployButton {display:none;}
footer {visibility: hidden;}
#stDecoration {display:none;}
button[title="View fullscreen"]{
visibility: hidden;}
</style>
""",
unsafe_allow_html=True,
)
def reset_conversation():
st.session_state.messages = []
st.session_state.memory.clear()
if "messages" not in st.session_state:
st.session_state.messages = []
if "memory" not in st.session_state:
st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history",return_messages=True)
embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True,"revision":"289f532e14dbbbd5a04753fa58739e9ba766f3c7"})
db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 4})
prompt_template = """<s>[INST]This is a chat template and As a legal chat ai specializing in Sericultural related Queries!!.
CONTEXT: {context}
CHAT HISTORY: {chat_history}
QUESTION: {question}
ANSWER:
</s>[INST]
"""
prompt = PromptTemplate(template=prompt_template,
input_variables=['context', 'question', 'chat_history'])
# You can also use other LLMs options from https://python.langchain.com/docs/integrations/llms. Here I have used TogetherAI API
TOGETHER_AI_API= os.environ['TOGETHER_AI']="2a7c5dcdbb1049a39117ac0865c4d04008d49db31aa85a3258603817af16dbd0"
llm = Together(
model="mistralai/Mistral-7B-Instruct-v0.2",
temperature=0.5,
max_tokens=1024,
together_api_key=f"{TOGETHER_AI_API}"
)
qa = ConversationalRetrievalChain.from_llm(
llm=llm,
memory=st.session_state.memory,
retriever=db_retriever,
combine_docs_chain_kwargs={'prompt': prompt}
)
for message in st.session_state.messages:
role = message.get("role")
content = message.get("content")
with st.chat_message(role, avatar="user.svg" if role == "human" else "ai"):
st.write(content)
input_prompt = st.chat_input("message LAWGpt.....")
if input_prompt:
with st.chat_message("human",avatar="user.svg"):
st.write(input_prompt)
st.session_state.messages.append({"role":"human","content":input_prompt})
full_response = " "
with st.chat_message("ai"):
with st.spinner("Thinking..."):
result = qa.invoke(input=input_prompt)
message_placeholder = st.empty()
full_response = " \n"
for chunk in result["answer"]:
full_response+=chunk
time.sleep(0.02)
message_placeholder.markdown(full_response+" ▌")
st.button('Reset All Chat 🗑️', on_click=reset_conversation)
st.session_state.messages.append({"role": "ai", "content": result["answer"], "avatar": "ai"})
|