zolicsaki commited on
Commit
f91a345
·
verified ·
1 Parent(s): a6aefc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -9,6 +9,7 @@ import json
9
  from consts import AUTO_SEARCH_KEYWORD, SEARCH_TOOL_INSTRUCTION, RELATED_QUESTIONS_TEMPLATE_SEARCH, SEARCH_TOOL_INSTRUCTION, RAG_TEMPLATE, GOOGLE_SEARCH_ENDPOINT, DEFAULT_SEARCH_ENGINE_TIMEOUT, RELATED_QUESTIONS_TEMPLATE_NO_SEARCH
10
  import re
11
  import asyncio
 
12
 
13
  import streamlit as st
14
  import yaml
@@ -26,6 +27,7 @@ from visual_env_utils import are_credentials_set, env_input_fields, initialize_e
26
  logging.basicConfig(level=logging.INFO)
27
  GOOGLE_API_KEY = st.secrets["google_api_key"]
28
  GOOGLE_CX = st.secrets["google_cx"]
 
29
 
30
  CONFIG_PATH = os.path.join(current_dir, "config.yaml")
31
 
@@ -63,7 +65,7 @@ def st_capture(output_func: Callable[[str], None]) -> Generator:
63
  stdout.write = new_write # type: ignore
64
  yield
65
 
66
- async def run_samba_api_inference(query, system_prompt = None, ignore_context=False, max_tokens_to_generate=None, num_seconds_to_sleep=5):
67
  # First construct messages
68
  messages = []
69
  if system_prompt is not None:
@@ -85,8 +87,13 @@ async def run_samba_api_inference(query, system_prompt = None, ignore_context=Fa
85
  }
86
  if max_tokens_to_generate is not None:
87
  payload["max_tokens"] = max_tokens_to_generate
 
 
 
 
 
88
  headers = {
89
- "Authorization": f"Basic {st.session_state.SAMBANOVA_API_KEY}",
90
  "Content-Type": "application/json"
91
  }
92
 
@@ -98,9 +105,8 @@ async def run_samba_api_inference(query, system_prompt = None, ignore_context=Fa
98
  st.info(f"Invalid Key! Please make sure you have a valid SambaCloud key from https://cloud.sambanova.ai/.")
99
  return "Invalid Key! Please make sure you have a valid SambaCloud key from https://cloud.sambanova.ai/."
100
  if post_response.status_code in {429, 504}:
101
- st.info("Rate limit hit because of all the pipelined queries, wait one second...")
102
  await asyncio.sleep(num_seconds_to_sleep)
103
- return await run_samba_api_inference(query) # Retry the request
104
  else:
105
  print(f"Request failed with status code: {post_response.status_code}. Error: {e}")
106
  return "Invalid Key! Please make sure you have a valid SambaCloud key from https://cloud.sambanova.ai/."
 
9
  from consts import AUTO_SEARCH_KEYWORD, SEARCH_TOOL_INSTRUCTION, RELATED_QUESTIONS_TEMPLATE_SEARCH, SEARCH_TOOL_INSTRUCTION, RAG_TEMPLATE, GOOGLE_SEARCH_ENDPOINT, DEFAULT_SEARCH_ENGINE_TIMEOUT, RELATED_QUESTIONS_TEMPLATE_NO_SEARCH
10
  import re
11
  import asyncio
12
+ import random
13
 
14
  import streamlit as st
15
  import yaml
 
27
  logging.basicConfig(level=logging.INFO)
28
  GOOGLE_API_KEY = st.secrets["google_api_key"]
29
  GOOGLE_CX = st.secrets["google_cx"]
30
+ BACKUP_KEYS = [st.secrets["backup_key_1"], st.secrets["backup_key_2"], st.secrets["backup_key_3"]]
31
 
32
  CONFIG_PATH = os.path.join(current_dir, "config.yaml")
33
 
 
65
  stdout.write = new_write # type: ignore
66
  yield
67
 
68
+ async def run_samba_api_inference(query, system_prompt = None, ignore_context=False, max_tokens_to_generate=None, num_seconds_to_sleep=1, over_ride_key=None):
69
  # First construct messages
70
  messages = []
71
  if system_prompt is not None:
 
87
  }
88
  if max_tokens_to_generate is not None:
89
  payload["max_tokens"] = max_tokens_to_generate
90
+
91
+ if over_ride_key is None:
92
+ api_key = st.session_state.SAMBANOVA_API_KEY
93
+ else:
94
+ api_key = over_ride_key
95
  headers = {
96
+ "Authorization": f"Basic {api_key}",
97
  "Content-Type": "application/json"
98
  }
99
 
 
105
  st.info(f"Invalid Key! Please make sure you have a valid SambaCloud key from https://cloud.sambanova.ai/.")
106
  return "Invalid Key! Please make sure you have a valid SambaCloud key from https://cloud.sambanova.ai/."
107
  if post_response.status_code in {429, 504}:
 
108
  await asyncio.sleep(num_seconds_to_sleep)
109
+ return await run_samba_api_inference(query, random.choice(BACKUP_KEYS)) # Retry the request
110
  else:
111
  print(f"Request failed with status code: {post_response.status_code}. Error: {e}")
112
  return "Invalid Key! Please make sure you have a valid SambaCloud key from https://cloud.sambanova.ai/."