rwitz commited on
Commit
dfb47fb
1 Parent(s): b74ba04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -6
app.py CHANGED
@@ -77,8 +77,14 @@ def format_alpaca_prompt(state):
77
  j="### Response:\n"
78
  alpaca_prompt2 += j+ message['content']+"\n\n"
79
  return [alpaca_prompt+"### Response:\n",alpaca_prompt2+"### Response:\n"]
80
- def get_bot_response(adapter_id, prompt, state, bot_index):
 
 
 
 
 
81
  alpaca_prompt = format_alpaca_prompt(state)
 
82
  payload = {
83
  "inputs": alpaca_prompt[bot_index],
84
  "parameters": {
@@ -91,11 +97,17 @@ def get_bot_response(adapter_id, prompt, state, bot_index):
91
  "Content-Type": "application/json",
92
  "Authorization": f"Bearer {os.environ.get('PREDIBASE_TOKEN')}"
93
  }
94
- response = requests.post("https://serving.app.predibase.com/79957f/deployments/v2/llms/mistral-7b/generate", json=payload, headers=headers)
95
- try:
96
- response_text = response.json()['generated_text']
97
- except (KeyError, requests.exceptions.JSONDecodeError):
98
- response_text = "Sorry, I couldn't generate a response."
 
 
 
 
 
 
99
  return response_text.split('### Instruction')[0]
100
  def chat_with_bots(user_input, state):
101
  # Use existing bot order from state if available, otherwise shuffle and initialize
 
77
  j="### Response:\n"
78
  alpaca_prompt2 += j+ message['content']+"\n\n"
79
  return [alpaca_prompt+"### Response:\n",alpaca_prompt2+"### Response:\n"]
80
+ import aiohttp
81
+ import asyncio
82
+ from tenacity import retry, stop_after_attempt, wait_exponential
83
+
84
+ @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=5))
85
+ async def get_bot_response(adapter_id, prompt, state, bot_index):
86
  alpaca_prompt = format_alpaca_prompt(state)
87
+ print(alpaca_prompt)
88
  payload = {
89
  "inputs": alpaca_prompt[bot_index],
90
  "parameters": {
 
97
  "Content-Type": "application/json",
98
  "Authorization": f"Bearer {os.environ.get('PREDIBASE_TOKEN')}"
99
  }
100
+ async with aiohttp.ClientSession() as session:
101
+ try:
102
+ async with session.post("https://serving.app.predibase.com/79957f/deployments/v2/llms/mistral-7b/generate",
103
+ json=payload, headers=headers, timeout=10) as response:
104
+ if response.status == 200:
105
+ response_data = await response.json()
106
+ response_text = response_data.get('generated_text', '')
107
+ else:
108
+ response_text = "Sorry, I couldn't generate a response."
109
+ except (aiohttp.ClientError, asyncio.TimeoutError):
110
+ response_text = "Sorry, I couldn't generate a response."
111
  return response_text.split('### Instruction')[0]
112
  def chat_with_bots(user_input, state):
113
  # Use existing bot order from state if available, otherwise shuffle and initialize