Spaces:
Sleeping
Sleeping
add error handling to openai api
Browse files- 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 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
return
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|