prsdm commited on
Commit
d3167a2
1 Parent(s): 6095074

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -142
app.py CHANGED
@@ -1,142 +1,142 @@
1
- import streamlit as st
2
- import os
3
- import weaviate
4
- from dotenv import load_dotenv,find_dotenv
5
- from langchain.vectorstores import Weaviate
6
- from langchain_core.prompts import ChatPromptTemplate
7
- from langchain_community.chat_models import ChatOpenAI
8
- from langchain_core.output_parsers import StrOutputParser
9
- from langchain_core.runnables import RunnablePassthrough
10
-
11
- load_dotenv(find_dotenv())
12
- weaviate_api_key = os.getenv('WEAVIATE_API_KEY')
13
- weaviate_cluster = os.getenv('WEAVIATE_CLUSTER')
14
-
15
- st.set_page_config(page_title="RAG-Roman Empire")
16
- st.image("public/images/banner.png")
17
-
18
- st.write(
19
- """
20
- <div style='text-align: center;'>
21
- <p>Salvete! I'm here to help you learn about the Roman Empire.
22
- Feel free to ask me anything about Roman history and culture!</p>
23
- </div>
24
- """,
25
- unsafe_allow_html=True
26
- )
27
-
28
- with st.expander("Sample questions you can ask"):
29
- st.markdown(
30
- """
31
- <div>
32
- <ul>
33
- <li>What were the major achievements of the Roman Empire?</li>
34
- <li>How did the Roman Empire rise to power?</li>
35
- <li>How did the Roman Empire expand its territory over time? etc.</li>
36
- </ul>
37
- <p><b>Note:</b> you can also ask irrelevant questions to test how the RAG framework is working.</p>
38
- </div>
39
- """,
40
- unsafe_allow_html=True,
41
- )
42
-
43
- st.sidebar.subheader("Your OpenAI Key Please")
44
- openai_api_key = st.sidebar.text_input('Put Your Key Here: ', type="password")
45
-
46
- with st.sidebar:
47
- if openai_api_key.startswith('sk-') and weaviate_api_key and weaviate_cluster:
48
- st.success('API keys and URL already provided!', icon='✅')
49
- else:
50
- st.warning('Please enter your credentials!', icon='⚠️')
51
-
52
-
53
- with st.sidebar.expander("ℹ️ Disclaimer"):
54
- st.caption(
55
- """We appreciate your engagement! Please note, This demo app can be shut down
56
- after the Weaviate cluster expires on 6/22/2024, 2:57 PM.
57
- """
58
- )
59
-
60
- # Adding custom HTML content to the sidebar with your name and heading
61
- st.sidebar.markdown(
62
- """
63
- <p style='text-align: center'> This RAG App is built using: </p>
64
- <div style='text-align: center;'>
65
- <a href='https://weaviate.io/' target='_blank'> Weaviate</a>
66
- <a href='https://www.langchain.com/' target='_blank'> LangChain</a>
67
- <a href='https://openai.com/' target='_blank'><img src="https://img.icons8.com/?size=100&id=ka3InxFU3QZa&format=png&color=000000" height="26"></a>
68
- <a href='https://huggingface.co/' target='_blank'> <img src="https://img.icons8.com/?size=100&id=sop9ROXku5bb&format=png&color=000000" height="25"></a>
69
- </div>
70
- </p>
71
- """,
72
- unsafe_allow_html=True,
73
- )
74
-
75
- st.sidebar.markdown(
76
- """
77
- <div style='margin-top: 20px;'>
78
- <p style='text-align: center; margin-bottom: 0; margin-top: 10;'>App created by</p>
79
- <p style='text-align: center; margin-top: 0;'><b>Prasad Mahamulkar</b></p>
80
- <p style='text-align: center'> GitHub
81
- <a href='https://weaviate.io/' target='_blank'> Repository</a>
82
- <p style='text-align: center'> Follow me for more! </p>
83
- <div style='text-align: center;'>
84
- <a href='https://github.com/prsdm' target='_blank'><img src="https://img.icons8.com/fluency/48/000000/github.png" height="26"></a>
85
- <a href='https://x.com/prsdm17' target='_blank'> <img src="https://img.icons8.com/?size=100&id=phOKFKYpe00C&format=png&color=000000" height="25"></a>
86
- <a href='https://www.linkedin.com/in/prasad-mahamulkar/' target='_blank'><img src="https://img.icons8.com/?size=100&id=8808&format=png&color=000000" height="25"></a>
87
- <a href='https://medium.com/@prasadmahamulkar' target='_blank'><img src="https://img.icons8.com/?size=100&id=XVNvUWCvvlD9&format=png&color=000000" height="25"></a>
88
- </div>
89
- </p>
90
- </div>
91
- """,
92
- unsafe_allow_html=True,
93
- )
94
-
95
- def get_qa_chain():
96
- auth_config = weaviate.auth.AuthApiKey(api_key=weaviate_api_key)
97
- client = weaviate.Client(
98
- url=weaviate_cluster,
99
- additional_headers={"X-OpenAI-Api-key": openai_api_key},
100
- auth_client_secret=auth_config,
101
- startup_period=10
102
- )
103
-
104
- vectorstore = Weaviate(client, "RomanEmpire", "content", attributes=["source"])
105
- retriever = vectorstore.as_retriever()
106
-
107
- template = """You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question.
108
- If the answer is not in the context, just say that you I don't have access to this information. Use five sentences maximum and keep the answer concise.
109
- Question: {question}
110
- Context: {context}
111
- Answer:
112
- """
113
- prompt = ChatPromptTemplate.from_template(template)
114
-
115
- llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, openai_api_key=openai_api_key)
116
-
117
- chain = (
118
- {"context": retriever, "question": RunnablePassthrough()}
119
- | prompt
120
- | llm
121
- | StrOutputParser()
122
- )
123
-
124
- return chain
125
-
126
- def generate_response(input_text):
127
- if openai_api_key and weaviate_api_key and weaviate_cluster:
128
- chain = get_qa_chain()
129
- answer = chain.invoke(input_text)
130
- st.info(answer)
131
- else:
132
- st.warning("Please provide all required API keys and URL!")
133
-
134
- with st.form('my_form'):
135
- text = st.text_input('Enter text:', 'Tell me interesting facts about Roman Empire')
136
- submitted = st.form_submit_button('Submit')
137
- if not openai_api_key.startswith('sk-'):
138
- st.warning('Please enter your OpenAI API key!', icon='⚠')
139
-
140
-
141
- if submitted and openai_api_key.startswith('sk-'):
142
- generate_response(text)
 
1
+ import streamlit as st
2
+ import os
3
+ import weaviate
4
+ from dotenv import load_dotenv,find_dotenv
5
+ from langchain.vectorstores import Weaviate
6
+ from langchain_core.prompts import ChatPromptTemplate
7
+ from langchain_community.chat_models import ChatOpenAI
8
+ from langchain_core.output_parsers import StrOutputParser
9
+ from langchain_core.runnables import RunnablePassthrough
10
+
11
+ load_dotenv(find_dotenv())
12
+ weaviate_api_key = os.getenv('WEAVIATE_API_KEY')
13
+ weaviate_cluster = os.getenv('WEAVIATE_CLUSTER')
14
+
15
+ st.set_page_config(page_title="RAG-Roman Empire")
16
+ st.image("public/images/banner.png")
17
+
18
+ st.write(
19
+ """
20
+ <div style='text-align: center;'>
21
+ <p>Salvete! I'm here to help you learn about the Roman Empire.
22
+ Feel free to ask me anything about Roman history and culture!</p>
23
+ </div>
24
+ """,
25
+ unsafe_allow_html=True
26
+ )
27
+
28
+ with st.expander("Sample questions you can ask"):
29
+ st.markdown(
30
+ """
31
+ <div>
32
+ <ul>
33
+ <li>What were the major achievements of the Roman Empire?</li>
34
+ <li>How did the Roman Empire rise to power?</li>
35
+ <li>How did the Roman Empire expand its territory over time? etc.</li>
36
+ </ul>
37
+ <p><b>Note:</b> you can also ask irrelevant questions to test how the RAG framework is working.</p>
38
+ </div>
39
+ """,
40
+ unsafe_allow_html=True,
41
+ )
42
+
43
+ st.sidebar.subheader("Your OpenAI Key Please")
44
+ openai_api_key = st.sidebar.text_input('Put Your Key Here: ', type="password")
45
+
46
+ with st.sidebar:
47
+ if openai_api_key.startswith('sk-') and weaviate_api_key and weaviate_cluster:
48
+ st.success('API keys and URL already provided!', icon='✅')
49
+ else:
50
+ st.warning('Please enter your credentials!', icon='⚠️')
51
+
52
+
53
+ with st.sidebar.expander("ℹ️ Disclaimer"):
54
+ st.caption(
55
+ """We appreciate your engagement! Please note, This demo app can be shut down
56
+ after the Weaviate cluster expires on 6/22/2024.
57
+ """
58
+ )
59
+
60
+ # Adding custom HTML content to the sidebar with your name and heading
61
+ st.sidebar.markdown(
62
+ """
63
+ <p style='text-align: center'> This RAG App is built using: </p>
64
+ <div style='text-align: center;'>
65
+ <a href='https://weaviate.io/' target='_blank'> Weaviate</a>
66
+ <a href='https://www.langchain.com/' target='_blank'> LangChain</a>
67
+ <a href='https://openai.com/' target='_blank'><img src="https://img.icons8.com/?size=100&id=ka3InxFU3QZa&format=png&color=000000" height="26"></a>
68
+ <a href='https://huggingface.co/' target='_blank'> <img src="https://img.icons8.com/?size=100&id=sop9ROXku5bb&format=png&color=000000" height="25"></a>
69
+ </div>
70
+ </p>
71
+ """,
72
+ unsafe_allow_html=True,
73
+ )
74
+
75
+ st.sidebar.markdown(
76
+ """
77
+ <div style='margin-top: 20px;'>
78
+ <p style='text-align: center; margin-bottom: 0; margin-top: 10;'>App created by</p>
79
+ <p style='text-align: center; margin-top: 0;'><b>Prasad Mahamulkar</b></p>
80
+ <p style='text-align: center'> GitHub
81
+ <a href='https://github.com/prsdm/RAG-Application' target='_blank'> Repository</a>
82
+ <p style='text-align: center'> Follow me for more! </p>
83
+ <div style='text-align: center;'>
84
+ <a href='https://github.com/prsdm' target='_blank'><img src="https://img.icons8.com/fluency/48/000000/github.png" height="26"></a>
85
+ <a href='https://x.com/prsdm17' target='_blank'> <img src="https://img.icons8.com/?size=100&id=phOKFKYpe00C&format=png&color=000000" height="25"></a>
86
+ <a href='https://www.linkedin.com/in/prasad-mahamulkar/' target='_blank'><img src="https://img.icons8.com/?size=100&id=8808&format=png&color=000000" height="25"></a>
87
+ <a href='https://medium.com/@prasadmahamulkar' target='_blank'><img src="https://img.icons8.com/?size=100&id=XVNvUWCvvlD9&format=png&color=000000" height="25"></a>
88
+ </div>
89
+ </p>
90
+ </div>
91
+ """,
92
+ unsafe_allow_html=True,
93
+ )
94
+
95
+ def get_qa_chain():
96
+ auth_config = weaviate.auth.AuthApiKey(api_key=weaviate_api_key)
97
+ client = weaviate.Client(
98
+ url=weaviate_cluster,
99
+ additional_headers={"X-OpenAI-Api-key": openai_api_key},
100
+ auth_client_secret=auth_config,
101
+ startup_period=10
102
+ )
103
+
104
+ vectorstore = Weaviate(client, "RomanEmpire", "content", attributes=["source"])
105
+ retriever = vectorstore.as_retriever()
106
+
107
+ template = """You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question.
108
+ If the answer is not in the context, just say that you I don't have access to this information. Use five sentences maximum and keep the answer concise.
109
+ Question: {question}
110
+ Context: {context}
111
+ Answer:
112
+ """
113
+ prompt = ChatPromptTemplate.from_template(template)
114
+
115
+ llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, openai_api_key=openai_api_key)
116
+
117
+ chain = (
118
+ {"context": retriever, "question": RunnablePassthrough()}
119
+ | prompt
120
+ | llm
121
+ | StrOutputParser()
122
+ )
123
+
124
+ return chain
125
+
126
+ def generate_response(input_text):
127
+ if openai_api_key and weaviate_api_key and weaviate_cluster:
128
+ chain = get_qa_chain()
129
+ answer = chain.invoke(input_text)
130
+ st.info(answer)
131
+ else:
132
+ st.warning("Please provide all required API keys and URL!")
133
+
134
+ with st.form('my_form'):
135
+ text = st.text_input('Enter text:', 'Tell me interesting facts about Roman Empire')
136
+ submitted = st.form_submit_button('Submit')
137
+ if not openai_api_key.startswith('sk-'):
138
+ st.warning('Please enter your OpenAI API key!', icon='⚠')
139
+
140
+
141
+ if submitted and openai_api_key.startswith('sk-'):
142
+ generate_response(text)