-
Notifications
You must be signed in to change notification settings - Fork 0
/
langchain_streamlit.py
52 lines (40 loc) · 1.22 KB
/
langchain_streamlit.py
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
import streamlit as st
from langchain.text_splitter import CharacterTextSplitter
from dotenv import load_dotenv
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.callbacks import get_openai_callback
st.header("Content Question")
st.write("Enter text")
text_upload = st.file_uploader("Drop a text file",type="txt")
if text_upload is None:
print("text is None")
exit(0)
text = text_upload.getvalue().decode("utf-8")
st.write(text)
splitter = CharacterTextSplitter(
separator="\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = splitter.split_text(text)
st.header("Chunks")
st.write(chunks)
load_dotenv()
embeddings = OpenAIEmbeddings()
knowledge_base = FAISS.from_texts(chunks, embeddings)
question = st.text_input("Question")
if not question:
exit(0)
references = knowledge_base.similarity_search(question)
st.header("Result")
st.write(references)
llm = OpenAI()
chain = load_qa_chain(llm, chain_type="stuff")
with get_openai_callback() as cb:
response = chain.run(input_documents=references, question=question)
print(cb)
st.write(response)