SushantGautam commited on
Commit
80f2e42
·
1 Parent(s): 305aec3

ask for INTERACTIVE_OPENAI_KEY in space

Browse files
Files changed (2) hide show
  1. Docker +2 -2
  2. app.py +40 -5
Docker CHANGED
@@ -16,7 +16,7 @@ COPY . .
16
 
17
  COPY ".env_demo" ".env"
18
 
19
-
20
  RUN chmod -R 777 /code/
21
 
22
- CMD ["chainlit", "run", "-h", "app.py", "--port", "7860"]
 
 
16
 
17
  COPY ".env_demo" ".env"
18
 
 
19
  RUN chmod -R 777 /code/
20
 
21
+ CMD ["chainlit", "run", "-h", "app.py", "--port", "7860"]
22
+ ENV INTERACTIVE_OPENAI_KEY=1
app.py CHANGED
@@ -12,13 +12,42 @@ model = os.getenv('OPENAI_MODEL')
12
  # Check if model exists, if not, set it to default
13
  # if not model:
14
  # model = "gpt-3.5-turbo-0125"
15
- ex = create_extractor()
16
- ag = create_agent(llm_model=model)
17
- # ag = create_agent(llm_model = "gpt-4-0125-preview")
18
- openai_api_key = os.getenv('OPENAI_API_KEY')
19
 
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  def extract_func(user_prompt: str):
24
  """
@@ -130,6 +159,12 @@ async def Extractor(user_prompt):
130
 
131
  @cl.on_message # this function will be called every time a user inputs a message in the UI
132
  async def main(message: cl.Message):
 
 
 
 
 
 
133
  user_prompt = message.content # Get the user prompt
134
  # extracted_values = extract_func(user_prompt)
135
  #
@@ -155,7 +190,7 @@ async def main(message: cl.Message):
155
  content=f"Which one do you mean for {key}?",
156
  actions=actions
157
  ).send()
158
- selected_value = res.get("value", "") if res else ""
159
  element[key] = selected_value
160
  element.pop("top_matches")
161
  await Choice("Options were "+ ", ".join([action.label for action in actions]))
 
12
  # Check if model exists, if not, set it to default
13
  # if not model:
14
  # model = "gpt-3.5-turbo-0125"
 
 
 
 
15
 
16
 
17
 
18
+ interactive_key_done= False if os.getenv('INTERACTIVE_OPENAI_KEY', None) else True
19
+
20
+ if interactive_key_done:
21
+ ex = create_extractor()
22
+ ag = create_agent(llm_model=model)
23
+ else:
24
+ ex= None
25
+ ag = None
26
+
27
+ @cl.on_chat_start
28
+ async def on_chat_start():
29
+ global ex, ag, interactive_key_done
30
+ if not interactive_key_done:
31
+ res = await cl.AskUserMessage(content=" 🔑 Input your OPENAI_API_KEY from https://platform.openai.com/account/api-keys", timeout=10).send()
32
+ if res:
33
+ await cl.Message(
34
+ content=f"⌛ Checking if provided OpenAI API key works. Please wait...",
35
+ ).send()
36
+ cl.user_session.set("openai_api_key", res.get("output"))
37
+ try:
38
+ os.environ["OPENAI_API_KEY"] = res.get("output")
39
+ ex = create_extractor()
40
+ ag = create_agent(llm_model=model)
41
+ interactive_key_done= True
42
+ await cl.Message(author="Socccer-RAG", content="✅ Voila! ⚽ Socccer-RAG warmed up and ready to go! You can start a fresh chat session from New Chat").send()
43
+ except Exception as e:
44
+ await cl.Message(
45
+ content=f"Error: {e}. Start new chat with correct key.",
46
+ ).send()
47
+
48
+ # ag = create_agent(llm_model = "gpt-4-0125-preview")
49
+
50
+
51
 
52
  def extract_func(user_prompt: str):
53
  """
 
159
 
160
  @cl.on_message # this function will be called every time a user inputs a message in the UI
161
  async def main(message: cl.Message):
162
+ global interactive_key_done
163
+ if not interactive_key_done:
164
+ await cl.Message(
165
+ content=f"Please set the OpenAI API key first by starting a new chat.",
166
+ ).send()
167
+ return
168
  user_prompt = message.content # Get the user prompt
169
  # extracted_values = extract_func(user_prompt)
170
  #
 
190
  content=f"Which one do you mean for {key}?",
191
  actions=actions
192
  ).send()
193
+ selected_value = res.get("value") if res else ""
194
  element[key] = selected_value
195
  element.pop("top_matches")
196
  await Choice("Options were "+ ", ".join([action.label for action in actions]))