appl044 commited on
Commit
15b8fb2
·
1 Parent(s): 01c02d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -16
app.py CHANGED
@@ -5,7 +5,8 @@ import ssl
5
  from contextlib import closing
6
  from typing import Optional, Tuple
7
  import datetime
8
-
 
9
  import boto3
10
  import gradio as gr
11
  import requests
@@ -18,7 +19,8 @@ from langchain import ConversationChain, LLMChain
18
 
19
  from langchain.agents import load_tools, initialize_agent
20
  from langchain.chains.conversation.memory import ConversationBufferMemory
21
- from langchain.llms import OpenAI
 
22
  from threading import Lock
23
 
24
  # Console to variable
@@ -49,7 +51,7 @@ tmdb_bearer_token = os.environ["TMDB_BEARER_TOKEN"]
49
 
50
  TOOLS_LIST = ['serpapi', 'wolfram-alpha', 'pal-math', 'pal-colored-objects', 'news-api'] #'google-search','news-api','tmdb-api','open-meteo-api'
51
  TOOLS_DEFAULT_LIST = ['serpapi', 'wolfram-alpha', 'pal-math', 'pal-colored-objects', 'news-api']
52
- BUG_FOUND_MSG = "Congratulations, you've found a bug in this application!"
53
  # AUTH_ERR_MSG = "Please paste your OpenAI key from openai.com to use this application. It is not necessary to hit a button or key after pasting it."
54
  AUTH_ERR_MSG = "Please paste your OpenAI key from openai.com to use this application. "
55
  MAX_TOKENS = 2048
@@ -262,29 +264,53 @@ def load_chain(tools_list, llm):
262
  return chain, express_chain, memory
263
 
264
 
265
- async def set_chain_state_api_key(api_key):
 
266
  # Set the API key for chain_state
267
  chain_state.api_key = api_key
268
 
269
- async def set_express_chain_state_api_key(api_key):
270
- # Set the API key for express_chain_state
271
- express_chain_state.api_key = api_key
272
 
273
- async def set_llm_state_api_key(api_key):
274
  # Set the API key for llm_state
275
- llm_state.api_key = api_key
276
 
277
- async def set_embeddings_state_api_key(api_key):
278
  # Set the API key for embeddings_state
279
- embeddings_state.api_key = api_key
280
 
281
- async def set_qa_chain_state_api_key(api_key):
282
  # Set the API key for qa_chain_state
283
- qa_chain_state.api_key = api_key
284
 
285
- async def set_memory_state_api_key(api_key):
286
  # Set the API key for memory_state
287
- memory_state.api_key = api_key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
  async def set_openai_api_key(api_key):
290
  """
@@ -924,7 +950,7 @@ openai_api_key_textbox.change(set_openai_api_key,
924
  inputs=[openai_api_key_textbox],
925
  outputs=None, _js="(api_key) => localStorage.setItem('open_api_key', api_key)")
926
 
927
- openai_api_key_textbox.change(lambda api_key: asyncio.run(set_openai_api_key(api_key)),
928
  inputs=[openai_api_key_textbox],
929
  outputs=[chain_state, express_chain_state, llm_state, embeddings_state,
930
  qa_chain_state, memory_state])
 
5
  from contextlib import closing
6
  from typing import Optional, Tuple
7
  import datetime
8
+ import promptlayer
9
+ promptlayer.api_key = os.environ.get("PROMPTLAYER_KEY")
10
  import boto3
11
  import gradio as gr
12
  import requests
 
19
 
20
  from langchain.agents import load_tools, initialize_agent
21
  from langchain.chains.conversation.memory import ConversationBufferMemory
22
+ # from langchain.llms import OpenAI
23
+ from promptlayer.langchain.llms import OpenAI
24
  from threading import Lock
25
 
26
  # Console to variable
 
51
 
52
  TOOLS_LIST = ['serpapi', 'wolfram-alpha', 'pal-math', 'pal-colored-objects', 'news-api'] #'google-search','news-api','tmdb-api','open-meteo-api'
53
  TOOLS_DEFAULT_LIST = ['serpapi', 'wolfram-alpha', 'pal-math', 'pal-colored-objects', 'news-api']
54
+ BUG_FOUND_MSG = "Error in the return response. Please try again."
55
  # AUTH_ERR_MSG = "Please paste your OpenAI key from openai.com to use this application. It is not necessary to hit a button or key after pasting it."
56
  AUTH_ERR_MSG = "Please paste your OpenAI key from openai.com to use this application. "
57
  MAX_TOKENS = 2048
 
264
  return chain, express_chain, memory
265
 
266
 
267
+ # async def set_chain_state_api_key(api_key):
268
+ async def set_openai_key(api_key):
269
  # Set the API key for chain_state
270
  chain_state.api_key = api_key
271
 
272
+ # async def set_express_chain_state_api_key(api_key):
273
+ # # Set the API key for express_chain_state
274
+ # express_chain_state.api_key = api_key
275
 
276
+ # async def set_llm_state_api_key(api_key):
277
  # Set the API key for llm_state
278
+ # llm_state.api_key = api_key
279
 
280
+ # async def set_embeddings_state_api_key(api_key):
281
  # Set the API key for embeddings_state
282
+ # embeddings_state.api_key = api_key
283
 
284
+ # async def set_qa_chain_state_api_key(api_key):
285
  # Set the API key for qa_chain_state
286
+ # qa_chain_state.api_key = api_key
287
 
288
+ # async def set_memory_state_api_key(api_key):
289
  # Set the API key for memory_state
290
+ # memory_state.api_key = api_key
291
+
292
+ if api_key and api_key.startswith("sk-") and len(api_key) > 50:
293
+ os.environ["OPENAI_API_KEY"] = api_key
294
+ print("\n\n ++++++++++++++ Setting OpenAI API key ++++++++++++++ \n\n")
295
+ print(str(datetime.datetime.now()) + ": Before OpenAI, OPENAI_API_KEY length: " + str(
296
+ len(os.environ["OPENAI_API_KEY"])))
297
+ llm = OpenAI(temperature=0, max_tokens=MAX_TOKENS)
298
+ print(str(datetime.datetime.now()) + ": After OpenAI, OPENAI_API_KEY length: " + str(
299
+ len(os.environ["OPENAI_API_KEY"])))
300
+ chain, express_chain, memory = load_chain(TOOLS_DEFAULT_LIST, llm)
301
+
302
+ # Pertains to question answering functionality
303
+ embeddings = OpenAIEmbeddings()
304
+ qa_chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
305
+
306
+ print(str(datetime.datetime.now()) + ": After load_chain, OPENAI_API_KEY length: " + str(
307
+ len(os.environ["OPENAI_API_KEY"])))
308
+ os.environ["OPENAI_API_KEY"] = ""
309
+ return chain, express_chain, llm, embeddings, qa_chain, memory
310
+ return None, None, None, None, None, None
311
+
312
+ PROMPTLAYER_API_BASE = "https://api.promptlayer.com"
313
+
314
 
315
  async def set_openai_api_key(api_key):
316
  """
 
950
  inputs=[openai_api_key_textbox],
951
  outputs=None, _js="(api_key) => localStorage.setItem('open_api_key', api_key)")
952
 
953
+ openai_api_key_textbox.change(set_openai_api_key,
954
  inputs=[openai_api_key_textbox],
955
  outputs=[chain_state, express_chain_state, llm_state, embeddings_state,
956
  qa_chain_state, memory_state])