monra commited on
Commit
720c1cf
1 Parent(s): 40cdb75

Add retry mechanism when response returns error

Browse files
Files changed (1) hide show
  1. server/backend.py +34 -25
server/backend.py CHANGED
@@ -1,5 +1,5 @@
1
  import re
2
- import g4f
3
  from g4f import ChatCompletion
4
  from googletrans import Translator
5
  from flask import request
@@ -30,32 +30,41 @@ class Backend_Api:
30
  # update_proxies = threading.Thread(
31
  # target=update_working_proxies, daemon=True)
32
  # update_proxies.start()
33
-
34
- def _conversation(self):
 
 
 
 
35
  """
36
- Handles the conversation route.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- :return: Response object containing the generated conversation stream
39
- """
40
- try:
41
- jailbreak = request.json['jailbreak']
42
- model = request.json['model']
43
- messages = build_messages(jailbreak)
44
-
45
- # Generate response
46
- response = ChatCompletion.create(model=model, stream=True,
47
- messages=messages)
48
-
49
- return self.app.response_class(generate_stream(response, jailbreak), mimetype='text/event-stream')
50
-
51
- except Exception as e:
52
- print(e)
53
- print(e.__traceback__.tb_next)
54
- return {
55
- '_action': '_ask',
56
- 'success': False,
57
- "error": f"an error occurred {str(e)}"
58
- }, 400
59
 
60
 
61
  def build_messages(jailbreak):
 
1
  import re
2
+ import time
3
  from g4f import ChatCompletion
4
  from googletrans import Translator
5
  from flask import request
 
30
  # update_proxies = threading.Thread(
31
  # target=update_working_proxies, daemon=True)
32
  # update_proxies.start()
33
+
34
+ def _conversation(self):
35
+ """
36
+ Handles the conversation route.
37
+
38
+ :return: Response object containing the generated conversation stream
39
  """
40
+ max_retries = 3
41
+ retries = 0
42
+
43
+ while retries < max_retries:
44
+ try:
45
+ jailbreak = request.json['jailbreak']
46
+ model = request.json['model']
47
+ messages = build_messages(jailbreak)
48
+
49
+ # Generate response
50
+ response = ChatCompletion.create(model=model, stream=True,
51
+ messages=messages)
52
+
53
+ return self.app.response_class(generate_stream(response, jailbreak), mimetype='text/event-stream')
54
+
55
+ except Exception as e:
56
+ print(e)
57
+ print(e.__traceback__.tb_next)
58
+
59
+ retries += 1
60
+ if retries >= max_retries:
61
+ return {
62
+ '_action': '_ask',
63
+ 'success': False,
64
+ "error": f"an error occurred {str(e)}"
65
+ }, 400
66
+ time.sleep(3) # Wait 3 second before trying again
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
 
70
  def build_messages(jailbreak):