rainbowemoji commited on
Commit
af98dc3
·
1 Parent(s): 63c657e

initial commit

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. app.py +63 -0
  3. etf-book.pdf +0 -0
  4. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .streamlit
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pinecone
3
+ import openai
4
+
5
+ from langchain.embeddings.openai import OpenAIEmbeddings
6
+ from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain.vectorstores import Pinecone
8
+ from langchain.document_loaders import PyPDFLoader
9
+ from langchain.chains import ConversationalRetrievalChain
10
+ from langchain.llms import OpenAI
11
+ import streamlit as st
12
+
13
+
14
+ def _initialize_env():
15
+ pinecone.init(
16
+ api_key=st.secrets["pinecone_api_key"],
17
+ environment=st.secrets["pinecone_env"]
18
+ )
19
+ openai.api_key = st.secrets["openai_api_key"]
20
+
21
+
22
+ def _initialize_indexes():
23
+ loader = PyPDFLoader("./etf-book.pdf")
24
+ documents = loader.load()
25
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
26
+ docs = text_splitter.split_documents(documents)
27
+ embeddings = OpenAIEmbeddings()
28
+
29
+ PINECONE_TABLE_NAME = os.getenv('PINECONE_TABLE_NAME')
30
+ db = Pinecone.from_documents(docs, embeddings, index_name=PINECONE_TABLE_NAME)
31
+ return db
32
+
33
+
34
+ def _initialize_retriever(db):
35
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
36
+ qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0, model_name="gpt-3.5-turbo"), retriever)
37
+ return qa
38
+
39
+
40
+ chat_history = []
41
+
42
+ def answer(input):
43
+ result = qa({
44
+ "question": input,
45
+ "chat_history": chat_history
46
+ })
47
+ chat_history = chat_history.append((input, result["answer"]))
48
+ chat_history = chat_history[-10:]
49
+ st.write(
50
+ "Bot: ",
51
+ result["answer"]
52
+ )
53
+
54
+
55
+ _initialize_env()
56
+ db = _initialize_indexes()
57
+ qa = _initialize_retriever(db)
58
+
59
+ question = st.text_input('Question')
60
+ st.button(
61
+ 'Answer',
62
+ on_click=lambda: answer(question)
63
+ )
etf-book.pdf ADDED
Binary file (190 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ langchain==0.0.178
2
+ openai==0.27.7
3
+ pinecone==0.1.0
4
+ streamlit==1.22.0