sohojoe commited on
Commit
156e5b3
1 Parent(s): b49fa2f

add error handling to openai api

Browse files
Files changed (1) hide show
  1. chat_service.py +53 -25
chat_service.py CHANGED
@@ -105,29 +105,57 @@ I fell off the pink step, and I had an accident.
105
  self._messages.append({"role": "user", "content": prompt})
106
  agent_response = ""
107
  current_sentence = ""
108
-
109
- response = await openai.ChatCompletion.acreate(
110
- model=self._model_id,
111
- messages=self._messages,
112
- temperature=1.0, # use 1.0 for debugging/deterministic results
113
- stream=True
114
- )
115
-
116
- async for chunk in response:
117
- if cancel_event.is_set():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  return
119
- chunk_message = chunk['choices'][0]['delta']
120
- if 'content' in chunk_message:
121
- chunk_text = chunk_message['content']
122
- current_sentence += chunk_text
123
- agent_response += chunk_text
124
- text_to_speak = self._should_we_send_to_voice(current_sentence)
125
- if text_to_speak:
126
- yield text_to_speak
127
- current_sentence = current_sentence[len(text_to_speak):]
128
-
129
- if cancel_event.is_set():
130
- return
131
- if len(current_sentence) > 0:
132
- yield current_sentence
133
- self._messages.append({"role": "assistant", "content": agent_response})
 
 
 
 
 
 
 
 
 
 
105
  self._messages.append({"role": "user", "content": prompt})
106
  agent_response = ""
107
  current_sentence = ""
108
+ delay = 0.1
109
+
110
+ while True:
111
+ try:
112
+ response = await openai.ChatCompletion.acreate(
113
+ model=self._model_id,
114
+ messages=self._messages,
115
+ temperature=1.0, # use 1.0 for debugging/deterministic results
116
+ stream=True
117
+ )
118
+
119
+ async for chunk in response:
120
+ if cancel_event.is_set():
121
+ return
122
+ chunk_message = chunk['choices'][0]['delta']
123
+ if 'content' in chunk_message:
124
+ chunk_text = chunk_message['content']
125
+ current_sentence += chunk_text
126
+ agent_response += chunk_text
127
+ text_to_speak = self._should_we_send_to_voice(current_sentence)
128
+ if text_to_speak:
129
+ yield text_to_speak
130
+ current_sentence = current_sentence[len(text_to_speak):]
131
+
132
+ if cancel_event.is_set():
133
+ return
134
+ if len(current_sentence) > 0:
135
+ yield current_sentence
136
+ self._messages.append({"role": "assistant", "content": agent_response})
137
  return
138
+
139
+ except openai.error.APIError as e:
140
+ print(f"OpenAI API returned an API Error: {e}")
141
+ print(f"Retrying in {delay} seconds...")
142
+ await asyncio.sleep(delay)
143
+ delay *= 2
144
+
145
+ except openai.error.APIConnectionError as e:
146
+ print(f"Failed to connect to OpenAI API: {e}")
147
+ print(f"Retrying in {delay} seconds...")
148
+ await asyncio.sleep(delay)
149
+ delay *= 2
150
+
151
+ except openai.error.RateLimitError as e:
152
+ print(f"OpenAI API request exceeded rate limit: {e}")
153
+ print(f"Retrying in {delay} seconds...")
154
+ await asyncio.sleep(delay)
155
+ delay *= 2
156
+
157
+ except Exception as e:
158
+ print(f"OpenAI API unknown error: {e}")
159
+ print(f"Retrying in {delay} seconds...")
160
+ await asyncio.sleep(delay)
161
+ delay *= 2