Spaces:
Sleeping
Sleeping
jjz5463
commited on
Commit
·
c61089d
1
Parent(s):
c2053e6
better ratelimit handle using tenacit
Browse files- chatbot_simulator.py +15 -26
chatbot_simulator.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import json_repair
|
| 3 |
from transformers import AutoTokenizer
|
| 4 |
-
from openai import RateLimitError
|
| 5 |
-
import time
|
| 6 |
from prompts import *
|
| 7 |
import re
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
class ChatbotSimulation:
|
|
@@ -50,8 +50,6 @@ class ChatbotSimulation:
|
|
| 50 |
|
| 51 |
def _generate_system_prompt(self):
|
| 52 |
"""Create a dynamic system prompt based on the current state."""
|
| 53 |
-
#current_page = self.user_state['current_page']
|
| 54 |
-
#last_page = self.user_state['last_page']
|
| 55 |
current_page = self.page_history[-1] if len(self.page_history) >= 1 else "Home"
|
| 56 |
last_page = self.page_history[-2] if len(self.page_history) > 1 else "Home"
|
| 57 |
page_info = self._get_page_details(current_page)
|
|
@@ -67,29 +65,20 @@ class ChatbotSimulation:
|
|
| 67 |
page_info=page_info
|
| 68 |
)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def _get_openai_response(self, prompt):
|
| 71 |
-
"""Fetch response from OpenAI API."""
|
| 72 |
-
self.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
temperature=0.7,
|
| 80 |
-
)
|
| 81 |
-
return response.choices[0].message.content
|
| 82 |
-
except RateLimitError as e:
|
| 83 |
-
# Parse the suggested retry time from the error message, default to 5s if not available
|
| 84 |
-
wait_time = 5
|
| 85 |
-
try:
|
| 86 |
-
# Attempt to get the time from the error message
|
| 87 |
-
wait_time = float(e.response['error']['message'].split("in ")[1].split("s")[0])
|
| 88 |
-
except (KeyError, IndexError, ValueError):
|
| 89 |
-
print("Could not parse wait time from error message. Defaulting to 5 seconds.")
|
| 90 |
-
|
| 91 |
-
print(f"Rate limit reached. Retrying in {wait_time} seconds...")
|
| 92 |
-
time.sleep(wait_time)
|
| 93 |
|
| 94 |
def _calculate_token_count(self, conversation):
|
| 95 |
"""Accurately calculate the token count in the conversation using a tokenizer."""
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import json_repair
|
| 3 |
from transformers import AutoTokenizer
|
|
|
|
|
|
|
| 4 |
from prompts import *
|
| 5 |
import re
|
| 6 |
+
from tenacity import retry, wait_fixed, stop_after_attempt, retry_if_exception_type
|
| 7 |
+
from openai import RateLimitError
|
| 8 |
|
| 9 |
|
| 10 |
class ChatbotSimulation:
|
|
|
|
| 50 |
|
| 51 |
def _generate_system_prompt(self):
|
| 52 |
"""Create a dynamic system prompt based on the current state."""
|
|
|
|
|
|
|
| 53 |
current_page = self.page_history[-1] if len(self.page_history) >= 1 else "Home"
|
| 54 |
last_page = self.page_history[-2] if len(self.page_history) > 1 else "Home"
|
| 55 |
page_info = self._get_page_details(current_page)
|
|
|
|
| 65 |
page_info=page_info
|
| 66 |
)
|
| 67 |
|
| 68 |
+
@retry(
|
| 69 |
+
retry=retry_if_exception_type(RateLimitError),
|
| 70 |
+
wait=wait_fixed(5), # Waits for 5 seconds between retries
|
| 71 |
+
stop=stop_after_attempt(5) # Stops after 5 failed attempts
|
| 72 |
+
)
|
| 73 |
def _get_openai_response(self, prompt):
|
| 74 |
+
"""Fetch response from OpenAI API using tenacity for handling retries."""
|
| 75 |
+
response = self.client.chat.completions.create(
|
| 76 |
+
model="gpt-4",
|
| 77 |
+
messages=prompt,
|
| 78 |
+
max_tokens=self.buffer_tokens, # Adjusted max_tokens if needed
|
| 79 |
+
temperature=0.7,
|
| 80 |
+
)
|
| 81 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def _calculate_token_count(self, conversation):
|
| 84 |
"""Accurately calculate the token count in the conversation using a tokenizer."""
|