carolanderson commited on
Commit
1927487
·
1 Parent(s): 732d634

change api key handling

Browse files
Files changed (1) hide show
  1. app.py +19 -17
app.py CHANGED
@@ -11,14 +11,13 @@ HumanMessagePromptTemplate,
11
  from langchain.memory import ConversationBufferWindowMemory
12
  from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
13
  from langchain.schema import AIMessage, HumanMessage
 
14
  import streamlit as st
15
 
16
 
17
-
18
-
19
- @st.cache_resource
20
- def set_api_key(api_key):
21
- os.environ["OPENAI_API_KEY"] = api_key
22
 
23
 
24
  @st.cache_resource
@@ -58,8 +57,10 @@ if __name__ == "__main__":
58
  st.write("To keep input lengths down and costs reasonable,"
59
  " this bot only 'remembers' the past three turns of conversation.")
60
  st.write("To clear all memory and start fresh, click 'Clear history'" )
61
-
62
- API_KEY = st.sidebar.text_input(
 
 
63
  'API Key',
64
  type='password',
65
  help="Enter your OpenAI API key to use this app",
@@ -79,29 +80,30 @@ if __name__ == "__main__":
79
  value=0.9,
80
  help="Set the decoding temperature. Lower temperatures give more predictable outputs."
81
  )
82
-
83
- if API_KEY is not None:
84
- set_api_key(API_KEY)
 
85
  memory = setup_memory()
86
  chain = get_chain(model_name, memory, temperature)
87
  if st.button("Clear history"):
88
  chain.memory.clear()
89
  st.cache_resource.clear()
90
- for message in chain.memory.buffer:
91
  st.chat_message(message.type).write(message.content)
92
  text = st.chat_input()
93
  if text:
94
  with st.chat_message("user"):
95
  st.write(text)
96
- result = chain.predict(input=text)
97
- with st.chat_message("assistant"):
98
- st.write(result)
 
 
 
99
 
100
 
101
 
102
-
103
-
104
-
105
 
106
 
107
 
 
11
  from langchain.memory import ConversationBufferWindowMemory
12
  from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
13
  from langchain.schema import AIMessage, HumanMessage
14
+ from openai.error import AuthenticationError
15
  import streamlit as st
16
 
17
 
18
+ def set_api_key(USER_API_KEY):
19
+ os.environ["OPENAI_API_KEY"] = str(USER_API_KEY)
20
+ st.cache_resource.clear() # clear existing chains with old api key
 
 
21
 
22
 
23
  @st.cache_resource
 
57
  st.write("To keep input lengths down and costs reasonable,"
58
  " this bot only 'remembers' the past three turns of conversation.")
59
  st.write("To clear all memory and start fresh, click 'Clear history'" )
60
+
61
+ st.sidebar.title("Enter API key and model")
62
+
63
+ USER_API_KEY = st.sidebar.text_input(
64
  'API Key',
65
  type='password',
66
  help="Enter your OpenAI API key to use this app",
 
80
  value=0.9,
81
  help="Set the decoding temperature. Lower temperatures give more predictable outputs."
82
  )
83
+
84
+
85
+ if USER_API_KEY is not None:
86
+ set_api_key(USER_API_KEY)
87
  memory = setup_memory()
88
  chain = get_chain(model_name, memory, temperature)
89
  if st.button("Clear history"):
90
  chain.memory.clear()
91
  st.cache_resource.clear()
92
+ for message in chain.memory.buffer: # display chat history
93
  st.chat_message(message.type).write(message.content)
94
  text = st.chat_input()
95
  if text:
96
  with st.chat_message("user"):
97
  st.write(text)
98
+ try:
99
+ result = chain.predict(input=text)
100
+ with st.chat_message("assistant"):
101
+ st.write(result)
102
+ except AuthenticationError:
103
+ st.warning("Enter a valid OpenAI API key", icon="⚠️")
104
 
105
 
106
 
 
 
 
107
 
108
 
109