Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,87 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import random
|
3 |
-
import time
|
4 |
-
import os
|
5 |
-
from langchain_together import ChatTogether
|
6 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
7 |
-
from langchain_community.document_loaders import TextLoader
|
8 |
-
from langchain_core.prompts import ChatPromptTemplate
|
9 |
-
from langchain_community.vectorstores import FAISS
|
10 |
-
from langchain_core.output_parsers import StrOutputParser
|
11 |
-
from langchain_core.runnables import RunnablePassthrough
|
12 |
-
from langchain_together import TogetherEmbeddings
|
13 |
-
|
14 |
-
os.environ["TOGETHER_API_KEY"] = "6216ce36aadcb06c35436e7d6bbbc18b354d8140f6e805db485d70ecff4481d0"
|
15 |
-
|
16 |
-
#load
|
17 |
-
loader = TextLoader("Resume_data.txt")
|
18 |
-
documents = loader.load()
|
19 |
-
|
20 |
-
# split it into chunks
|
21 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
22 |
-
docs = text_splitter.split_documents(documents)
|
23 |
-
vectorstore = FAISS.from_documents(docs,
|
24 |
-
TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval")
|
25 |
-
)
|
26 |
-
|
27 |
-
retriever = vectorstore.as_retriever()
|
28 |
-
print("assigning model")
|
29 |
-
model = ChatTogether(
|
30 |
-
model="meta-llama/Llama-3-70b-chat-hf",
|
31 |
-
temperature=0.0,
|
32 |
-
max_tokens=500,)
|
33 |
-
|
34 |
-
# template = """<s>[INST] answer from context only as if person is responding (use i instead of you in response). and always answer in short answer.
|
35 |
-
# answer for asked question only, if he greets greet back.
|
36 |
-
template = """
|
37 |
-
{context}
|
38 |
-
Question: {question} [/INST]
|
39 |
-
"""
|
40 |
-
prompt = ChatPromptTemplate.from_template(template)
|
41 |
-
|
42 |
-
chain = (
|
43 |
-
{"context": retriever, "question": RunnablePassthrough()}
|
44 |
-
| prompt
|
45 |
-
| model
|
46 |
-
| StrOutputParser()
|
47 |
-
)
|
48 |
-
|
49 |
-
|
50 |
-
st.title("Simple chat")
|
51 |
-
|
52 |
-
# Initialize chat history
|
53 |
-
if "messages" not in st.session_state:
|
54 |
-
st.session_state.messages = []
|
55 |
-
|
56 |
-
# Display chat messages from history on app rerun
|
57 |
-
for message in st.session_state.messages:
|
58 |
-
with st.chat_message(message["role"]):
|
59 |
-
st.markdown(message["content"])
|
60 |
-
|
61 |
-
# Accept user input
|
62 |
-
if prompt := st.chat_input("What is up?"):
|
63 |
-
# Display user message in chat message container
|
64 |
-
with st.chat_message("user"):
|
65 |
-
st.markdown(prompt)
|
66 |
-
# Add user message to chat history
|
67 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
68 |
-
|
69 |
-
############################################
|
70 |
-
# Streamed response emulator
|
71 |
-
def response_generator():
|
72 |
-
query = f"echo {prompt}"
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
import os
|
5 |
+
from langchain_together import ChatTogether
|
6 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
7 |
+
from langchain_community.document_loaders import TextLoader
|
8 |
+
from langchain_core.prompts import ChatPromptTemplate
|
9 |
+
from langchain_community.vectorstores import FAISS
|
10 |
+
from langchain_core.output_parsers import StrOutputParser
|
11 |
+
from langchain_core.runnables import RunnablePassthrough
|
12 |
+
from langchain_together import TogetherEmbeddings
|
13 |
+
|
14 |
+
os.environ["TOGETHER_API_KEY"] = "6216ce36aadcb06c35436e7d6bbbc18b354d8140f6e805db485d70ecff4481d0"
|
15 |
+
|
16 |
+
#load
|
17 |
+
loader = TextLoader("Resume_data.txt")
|
18 |
+
documents = loader.load()
|
19 |
+
|
20 |
+
# split it into chunks
|
21 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
22 |
+
docs = text_splitter.split_documents(documents)
|
23 |
+
vectorstore = FAISS.from_documents(docs,
|
24 |
+
TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval")
|
25 |
+
)
|
26 |
+
|
27 |
+
retriever = vectorstore.as_retriever()
|
28 |
+
print("assigning model")
|
29 |
+
model = ChatTogether(
|
30 |
+
model="meta-llama/Llama-3-70b-chat-hf",
|
31 |
+
temperature=0.0,
|
32 |
+
max_tokens=500,)
|
33 |
+
|
34 |
+
# template = """<s>[INST] answer from context only as if person is responding (use i instead of you in response). and always answer in short answer.
|
35 |
+
# answer for asked question only, if he greets greet back.
|
36 |
+
template = """
|
37 |
+
{context}
|
38 |
+
Question: {question} [/INST]
|
39 |
+
"""
|
40 |
+
prompt = ChatPromptTemplate.from_template(template)
|
41 |
+
|
42 |
+
chain = (
|
43 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
44 |
+
| prompt
|
45 |
+
| model
|
46 |
+
| StrOutputParser()
|
47 |
+
)
|
48 |
+
|
49 |
+
|
50 |
+
st.title("Simple chat")
|
51 |
+
|
52 |
+
# Initialize chat history
|
53 |
+
if "messages" not in st.session_state:
|
54 |
+
st.session_state.messages = []
|
55 |
+
|
56 |
+
# Display chat messages from history on app rerun
|
57 |
+
for message in st.session_state.messages:
|
58 |
+
with st.chat_message(message["role"]):
|
59 |
+
st.markdown(message["content"])
|
60 |
+
|
61 |
+
# Accept user input
|
62 |
+
if prompt := st.chat_input("What is up?"):
|
63 |
+
# Display user message in chat message container
|
64 |
+
with st.chat_message("user"):
|
65 |
+
st.markdown(prompt)
|
66 |
+
# Add user message to chat history
|
67 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
68 |
+
|
69 |
+
############################################
|
70 |
+
# Streamed response emulator
|
71 |
+
def response_generator():
|
72 |
+
query = f"echo {prompt}"
|
73 |
+
if query != "":
|
74 |
+
for m in chain.stream(query):
|
75 |
+
print(m)
|
76 |
+
yield m + " "
|
77 |
+
else:
|
78 |
+
yield "How can i help you?"
|
79 |
+
|
80 |
+
# time.sleep(0.05)
|
81 |
+
# return chain.invoke(query)
|
82 |
+
###########################################
|
83 |
+
# Display assistant response in chat message container
|
84 |
+
with st.chat_message("assistant"):
|
85 |
+
response = st.write_stream(response_generator())
|
86 |
+
# Add assistant response to chat history
|
87 |
st.session_state.messages.append({"role": "assistant", "content": response})
|