Spaces:
Runtime error
Runtime error
theekshana
commited on
Commit
•
1584231
1
Parent(s):
99f2e6a
add openai api key from front end
Browse files- .env +0 -1
- __pycache__/qaPipeline_chain_only.cpython-311.pyc +0 -0
- app.py +44 -1
- qaPipeline_chain_only.py +13 -6
.env
CHANGED
@@ -11,7 +11,6 @@ TARGET_SOURCE_CHUNKS=4
|
|
11 |
|
12 |
#API token keys
|
13 |
HUGGINGFACEHUB_API_TOKEN=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
|
14 |
-
OPENAI_API_KEY=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
|
15 |
ANYSCALE_ENDPOINT_TOKEN=esecret_n1svfld85uklyx5ebaasyiw2m9
|
16 |
|
17 |
#api app
|
|
|
11 |
|
12 |
#API token keys
|
13 |
HUGGINGFACEHUB_API_TOKEN=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
|
|
|
14 |
ANYSCALE_ENDPOINT_TOKEN=esecret_n1svfld85uklyx5ebaasyiw2m9
|
15 |
|
16 |
#api app
|
__pycache__/qaPipeline_chain_only.cpython-311.pyc
CHANGED
Binary files a/__pycache__/qaPipeline_chain_only.cpython-311.pyc and b/__pycache__/qaPipeline_chain_only.cpython-311.pyc differ
|
|
app.py
CHANGED
@@ -42,6 +42,7 @@ def initialize_session_state():
|
|
42 |
"is_parameters_changed":False,
|
43 |
"show_source_files": False,
|
44 |
"user_question":'',
|
|
|
45 |
}
|
46 |
|
47 |
for k, v in SESSION_DEFAULTS.items():
|
@@ -96,6 +97,29 @@ def side_bar():
|
|
96 |
|
97 |
st.markdown("\n")
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
# if st.button("Create FAISS db"):
|
100 |
# try:
|
101 |
# with st.spinner('creating faiss vector store'):
|
@@ -170,10 +194,29 @@ def parameters_change_button(chat_model, show_source):
|
|
170 |
time.sleep(1) # Wait for 3 seconds
|
171 |
alert.empty() # Clear the alert
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
@st.cache_data
|
174 |
def get_answer_from_backend(query, model, dataset):
|
175 |
# response = qaPipeline.run(query=query, model=model, dataset=dataset)
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
return response
|
178 |
|
179 |
|
|
|
42 |
"is_parameters_changed":False,
|
43 |
"show_source_files": False,
|
44 |
"user_question":'',
|
45 |
+
'openai_api_key':None,
|
46 |
}
|
47 |
|
48 |
for k, v in SESSION_DEFAULTS.items():
|
|
|
97 |
|
98 |
st.markdown("\n")
|
99 |
|
100 |
+
with st.form('openai api key'):
|
101 |
+
if st.session_state.model == 'openai/gpt-3.5':
|
102 |
+
api_key = st.text_input(
|
103 |
+
"Enter openai api key",
|
104 |
+
type="password",
|
105 |
+
value=st.session_state.openai_api_key,
|
106 |
+
help="enter an openai api key created from 'https://platform.openai.com/account/api-keys'",
|
107 |
+
)
|
108 |
+
|
109 |
+
submit_key = st.form_submit_button(
|
110 |
+
"Save key",
|
111 |
+
# on_click=update_parameters_change
|
112 |
+
)
|
113 |
+
|
114 |
+
if submit_key:
|
115 |
+
st.session_state.openai_api_key = api_key
|
116 |
+
st.text(st.session_state.openai_api_key)
|
117 |
+
alert = st.success("openai api key updated")
|
118 |
+
time.sleep(1) # Wait for 3 seconds
|
119 |
+
alert.empty() # Clear the alert
|
120 |
+
|
121 |
+
|
122 |
+
st.markdown("\n")
|
123 |
# if st.button("Create FAISS db"):
|
124 |
# try:
|
125 |
# with st.spinner('creating faiss vector store'):
|
|
|
194 |
time.sleep(1) # Wait for 3 seconds
|
195 |
alert.empty() # Clear the alert
|
196 |
|
197 |
+
import re
|
198 |
+
def is_valid_open_ai_api_key(secretKey):
|
199 |
+
if re.search("^sk-[a-zA-Z0-9]{32,}$", secretKey ):
|
200 |
+
return True
|
201 |
+
else: return False
|
202 |
+
|
203 |
@st.cache_data
|
204 |
def get_answer_from_backend(query, model, dataset):
|
205 |
# response = qaPipeline.run(query=query, model=model, dataset=dataset)
|
206 |
+
if model == MODELS['openai/gpt-3.5']:
|
207 |
+
openai_api_key = st.session_state.openai_api_key
|
208 |
+
print(f"> front end validating openai api key")
|
209 |
+
print(is_valid_open_ai_api_key(openai_api_key))
|
210 |
+
if is_valid_open_ai_api_key(openai_api_key):
|
211 |
+
print(f"> front end openai api key validated")
|
212 |
+
response = qaPipeline.run_agent(query=query, model=model, dataset=dataset, openai_api_key=openai_api_key)
|
213 |
+
else:
|
214 |
+
print(f"Invalid openai api key")
|
215 |
+
st.error(f"Invalid openai api key")
|
216 |
+
st.stop()
|
217 |
+
else:
|
218 |
+
response = qaPipeline.run_agent(query=query, model=model, dataset=dataset)
|
219 |
+
|
220 |
return response
|
221 |
|
222 |
|
qaPipeline_chain_only.py
CHANGED
@@ -45,6 +45,12 @@ verbose = os.environ.get('VERBOSE')
|
|
45 |
# activate/deactivate the streaming StdOut callback for LLMs
|
46 |
callbacks = [StreamingStdOutCallbackHandler()]
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def get_local_LLAMA2():
|
49 |
import torch
|
50 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
@@ -93,11 +99,11 @@ class QAPipeline:
|
|
93 |
|
94 |
self.qa_chain = None
|
95 |
|
96 |
-
def run_agent(self,query, model, dataset):
|
97 |
|
98 |
try:
|
99 |
if (self.llm_name != model) or (self.dataset_name != dataset) or (self.qa_chain == None):
|
100 |
-
self.set_model(model)
|
101 |
self.set_vectorstore(dataset)
|
102 |
self.set_qa_chain()
|
103 |
|
@@ -121,7 +127,7 @@ class QAPipeline:
|
|
121 |
return
|
122 |
|
123 |
|
124 |
-
def set_model(self,model_type):
|
125 |
if model_type != self.llm_name:
|
126 |
match model_type:
|
127 |
case "gpt4all":
|
@@ -133,9 +139,10 @@ class QAPipeline:
|
|
133 |
case "tiiuae/falcon-7b-instruct":
|
134 |
self.llm = HuggingFaceHub(repo_id=model_type, model_kwargs={"temperature":0.001, "max_length":1024})
|
135 |
case "openai":
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
139 |
case "Deci/DeciLM-6b":
|
140 |
self.llm = ChatOpenAI(model_name="Deci/DeciLM-6b", temperature=0)
|
141 |
case "local/LLAMA2":
|
|
|
45 |
# activate/deactivate the streaming StdOut callback for LLMs
|
46 |
callbacks = [StreamingStdOutCallbackHandler()]
|
47 |
|
48 |
+
import re
|
49 |
+
def is_valid_open_ai_api_key(secretKey):
|
50 |
+
if re.search("^sk-[a-zA-Z0-9]{32,}$", secretKey ):
|
51 |
+
return True
|
52 |
+
else: return False
|
53 |
+
|
54 |
def get_local_LLAMA2():
|
55 |
import torch
|
56 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
99 |
|
100 |
self.qa_chain = None
|
101 |
|
102 |
+
def run_agent(self,query, model, dataset, openai_api_key=None):
|
103 |
|
104 |
try:
|
105 |
if (self.llm_name != model) or (self.dataset_name != dataset) or (self.qa_chain == None):
|
106 |
+
self.set_model(model, openai_api_key)
|
107 |
self.set_vectorstore(dataset)
|
108 |
self.set_qa_chain()
|
109 |
|
|
|
127 |
return
|
128 |
|
129 |
|
130 |
+
def set_model(self, model_type, openai_api_key):
|
131 |
if model_type != self.llm_name:
|
132 |
match model_type:
|
133 |
case "gpt4all":
|
|
|
139 |
case "tiiuae/falcon-7b-instruct":
|
140 |
self.llm = HuggingFaceHub(repo_id=model_type, model_kwargs={"temperature":0.001, "max_length":1024})
|
141 |
case "openai":
|
142 |
+
print(f"> openai_api_key: {openai_api_key}")
|
143 |
+
if is_valid_open_ai_api_key(openai_api_key):
|
144 |
+
self.llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key=openai_api_key )
|
145 |
+
else: return KeyError("openai_api_key is not valid")
|
146 |
case "Deci/DeciLM-6b":
|
147 |
self.llm = ChatOpenAI(model_name="Deci/DeciLM-6b", temperature=0)
|
148 |
case "local/LLAMA2":
|