ShawnAI commited on
Commit
9ea3ea2
·
1 Parent(s): 2197c6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -22
app.py CHANGED
@@ -17,11 +17,11 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"
17
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
18
  OPENAI_TEMP = 0
19
 
20
- PINECONE_KEY = os.environ["PINECONE_KEY"]
21
- PINECONE_ENV = "asia-northeast1-gcp"
22
- PINECONE_INDEX = "3gpp"
23
 
24
- EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2"
25
 
26
  # return top-k text chunks from vector store
27
  TOP_K_DEFAULT = 10
@@ -47,8 +47,8 @@ MODEL_WARNING = "Please paste your OpenAI API Key from openai.com and press 'Ent
47
 
48
 
49
  webui_title = """
50
- # 3GPP OpenAI Chatbot for Hackathon Demo
51
-
52
  """
53
 
54
  KEY_INIT = "Initialize Model"
@@ -56,21 +56,22 @@ KEY_SUBMIT = "Submit"
56
  KEY_CLEAR = "Clear"
57
 
58
  init_message = f"""Welcome to use 3GPP Chatbot, this demo toolkit is based on OpenAI with LangChain and Pinecone
59
- 1. Insert your OpenAI API key and click `{KEY_INIT}`
60
- 2. Insert your Question and click `{KEY_SUBMIT}`
61
  """
62
 
63
  #----------------------------------------------------------------------------------------------------------
64
  #----------------------------------------------------------------------------------------------------------
65
 
66
- def init_model(api_key):
67
  try:
68
- if api_key and api_key.startswith("sk-") and len(api_key) > 50:
 
69
 
70
- embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
71
 
72
- pinecone.init(api_key = PINECONE_KEY,
73
- environment = PINECONE_ENV)
74
 
75
  #llm = OpenAI(temperature=OPENAI_TEMP, model_name="gpt-3.5-turbo-0301")
76
 
@@ -79,7 +80,7 @@ def init_model(api_key):
79
 
80
  chain = load_qa_chain(llm, chain_type="stuff")
81
 
82
- db = Pinecone.from_existing_index(index_name = PINECONE_INDEX,
83
  embedding = embeddings)
84
 
85
  return api_key, MODEL_DONE, chain, db, None
@@ -173,7 +174,7 @@ with gr.Blocks(
173
 
174
  with gr.Row():
175
  with gr.Column(scale=10):
176
- api_textbox = gr.Textbox(
177
  label = "OpenAI API Key",
178
  show_label = False,
179
  value = OPENAI_API_KEY,
@@ -218,14 +219,49 @@ with gr.Blocks(
218
  label="Vector similarity top_k",
219
  interactive=True)
220
  detail_panel = gr.Chatbot(label="Related Docs")
221
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
- api_textbox.submit(init_model,
224
- api_textbox,
225
- [api_textbox, model_statusbox, llm_chain, vector_db, chatbot])
226
- init.click(init_model,
227
- api_textbox,
228
- [api_textbox, model_statusbox, llm_chain, vector_db, chatbot])
229
 
230
  submit.click(user,
231
  [query, chatbot],
 
17
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
18
  OPENAI_TEMP = 0
19
 
20
+ PINECONE_KEY = os.environ.get("PINECONE_KEY", "")
21
+ PINECONE_ENV = os.environ.get("PINECONE_ENV", "asia-northeast1-gcp")
22
+ PINECONE_INDEX = os.environ.get("PINECONE_INDEX", "3gpp")
23
 
24
+ EMBEDDING_MODEL = os.environ.get("PINECONE_INDEX", "sentence-transformers/all-mpnet-base-v2")
25
 
26
  # return top-k text chunks from vector store
27
  TOP_K_DEFAULT = 10
 
47
 
48
 
49
  webui_title = """
50
+ # OpenAI Chatbot Based on Vector Database
51
+ ## Example of 3GPP
52
  """
53
 
54
  KEY_INIT = "Initialize Model"
 
56
  KEY_CLEAR = "Clear"
57
 
58
  init_message = f"""Welcome to use 3GPP Chatbot, this demo toolkit is based on OpenAI with LangChain and Pinecone
59
+ 1. Insert your OpenAI API key and click `{KEY_INIT}`
60
+ 2. Insert your Question and click `{KEY_SUBMIT}`
61
  """
62
 
63
  #----------------------------------------------------------------------------------------------------------
64
  #----------------------------------------------------------------------------------------------------------
65
 
66
+ def init_model(api_key, emb_name, db_api_key, db_env, db_index):
67
  try:
68
+ if (api_key and api_key.startswith("sk-") and len(api_key) > 50) and \
69
+ (emb_name and db_api_key and db_env and db_index):
70
 
71
+ embeddings = HuggingFaceEmbeddings(model_name=emb_name)
72
 
73
+ pinecone.init(api_key = db_api_key,
74
+ environment = db_env)
75
 
76
  #llm = OpenAI(temperature=OPENAI_TEMP, model_name="gpt-3.5-turbo-0301")
77
 
 
80
 
81
  chain = load_qa_chain(llm, chain_type="stuff")
82
 
83
+ db = Pinecone.from_existing_index(index_name = db_index,
84
  embedding = embeddings)
85
 
86
  return api_key, MODEL_DONE, chain, db, None
 
174
 
175
  with gr.Row():
176
  with gr.Column(scale=10):
177
+ llm_api_textbox = gr.Textbox(
178
  label = "OpenAI API Key",
179
  show_label = False,
180
  value = OPENAI_API_KEY,
 
219
  label="Vector similarity top_k",
220
  interactive=True)
221
  detail_panel = gr.Chatbot(label="Related Docs")
222
+
223
+ with gr.Tab("Database"):
224
+ with gr.Row():
225
+ emb_textbox = gr.Textbox(
226
+ label = "Embedding Model",
227
+ # show_label = False,
228
+ value = EMBEDDING_MODEL,
229
+ placeholder = "Paste Your Embedding Model Repo on HuggingFace",
230
+ lines=1,
231
+ interactive=True,
232
+ type='email')
233
+ with gr.Row():
234
+ db_api_textbox = gr.Textbox(
235
+ label = "Pinecone API Key",
236
+ # show_label = False,
237
+ value = PINECONE_KEY,
238
+ placeholder = "Paste Your Pinecone API Key (xx-xx-xx-xx-xx) and Hit ENTER",
239
+ lines=1,
240
+ interactive=True,
241
+ type='password')
242
+ with gr.Row():
243
+ db_env_textbox = gr.Textbox(
244
+ label = "Pinecone Environment",
245
+ # show_label = False,
246
+ value = PINECONE_ENV,
247
+ placeholder = "Paste Your Pinecone Environment (xx-xx-xx) and Hit ENTER",
248
+ lines=1,
249
+ interactive=True,
250
+ type='email')
251
+ db_index_textbox = gr.Textbox(
252
+ label = "Pinecone Index",
253
+ # show_label = False,
254
+ value = PINECONE_INDEX,
255
+ placeholder = "Paste Your Pinecone Index (xxxx) and Hit ENTER",
256
+ lines=1,
257
+ interactive=True,
258
+ type='email')
259
+
260
+ init_input = [llm_api_textbox, emb_textbox, db_api_textbox, db_env_textbox, db_index_textbox]
261
+ init_output = [llm_api_textbox, model_statusbox, llm_chain, vector_db, chatbot]
262
 
263
+ llm_api_textbox.submit(init_model, init_input, init_output)
264
+ init.click(init_model, init_input, init_output)
 
 
 
 
265
 
266
  submit.click(user,
267
  [query, chatbot],