monra commited on
Commit
980e121
2 Parent(s): 096ad32 d774781

Merge branch 'pre-release'

Browse files
Files changed (2) hide show
  1. client/css/global.css +13 -14
  2. server/backend.py +46 -45
client/css/global.css CHANGED
@@ -10,21 +10,20 @@
10
  font-family: var(--font-1);
11
  }
12
 
13
- .theme-light {
14
- --colour-1: #f5f5f5;
15
- --colour-2: #222222;
16
- --colour-3: #333333;
17
- --colour-4: #444444;
18
- --colour-5: #fafafa;
19
- --colour-6: #e0e0e0;
20
-
21
- --accent: #3a3a3a;
22
- --blur-bg: #f9f9f9;
23
- --blur-border: #ebebeb;
24
- --user-input: #333333;
25
- --conversations: #555555;
26
- }
27
 
 
 
 
 
 
 
28
 
29
  .theme-dark {
30
  --colour-1: #181818;
 
10
  font-family: var(--font-1);
11
  }
12
 
13
+ .theme-light {
14
+ --colour-1: #f5f5f5;
15
+ --colour-2: #000000;
16
+ --colour-3: #474747;
17
+ --colour-4: #949494;
18
+ --colour-5: #ebebeb;
19
+ --colour-6: #dadada;
 
 
 
 
 
 
 
20
 
21
+ --accent: #3a3a3a;
22
+ --blur-bg: #ffffff;
23
+ --blur-border: #dbdbdb;
24
+ --user-input: #282828;
25
+ --conversations: #666666;
26
+ }
27
 
28
  .theme-dark {
29
  --colour-1: #181818;
server/backend.py CHANGED
@@ -1,6 +1,5 @@
1
  import re
2
- import time
3
- import g4f
4
  from g4f import ChatCompletion
5
  from flask import request, Response, stream_with_context
6
  from requests import get
@@ -23,51 +22,45 @@ class Backend_Api:
23
  }
24
 
25
  def _conversation(self):
26
- """
27
- Handles the conversation route.
28
 
29
- :return: Response object containing the generated conversation stream
30
  """
31
- max_retries = 3
32
- retries = 0
33
  conversation_id = request.json['conversation_id']
34
 
35
- while retries < max_retries:
36
- try:
37
- jailbreak = request.json['jailbreak']
38
- model = request.json['model']
39
- messages = build_messages(jailbreak)
40
 
41
- # Generate response
42
- response = ChatCompletion.create(
43
- model=model,
44
- stream=True,
45
- chatId=conversation_id,
46
- messages=messages
47
- )
48
 
49
- return Response(stream_with_context(generate_stream(response, jailbreak)), mimetype='text/event-stream')
50
 
51
- except Exception as e:
52
- print(e)
53
- print(e.__traceback__.tb_next)
54
 
55
- retries += 1
56
- if retries >= max_retries:
57
- return {
58
- '_action': '_ask',
59
- 'success': False,
60
- "error": f"an error occurred {str(e)}"
61
- }, 400
62
- time.sleep(3) # Wait 3 second before trying again
63
 
64
 
65
  def build_messages(jailbreak):
66
- """
67
- Build the messages for the conversation.
68
 
69
- :param jailbreak: Jailbreak instruction string
70
- :return: List of messages for the conversation
71
  """
72
  _conversation = request.json['meta']['content']['conversation']
73
  internet_access = request.json['meta']['content']['internet_access']
@@ -77,28 +70,32 @@ def build_messages(jailbreak):
77
  conversation = _conversation
78
 
79
  # Add web results if enabled
80
- conversation += fetch_search_results(
81
- prompt["content"]) if internet_access else []
 
 
 
82
 
83
  # Add jailbreak instructions if enabled
84
  if jailbreak_instructions := getJailbreak(jailbreak):
85
- conversation += jailbreak_instructions
86
 
87
  # Add the prompt
88
- conversation += [prompt]
89
 
90
  # Reduce conversation size to avoid API Token quantity error
91
- conversation = conversation[-4:] if len(conversation) > 3 else conversation
 
92
 
93
  return conversation
94
 
95
 
96
  def fetch_search_results(query):
97
- """
98
- Fetch search results for a given query.
99
 
100
- :param query: Search query string
101
- :return: List of search results
102
  """
103
  search = get('https://ddg-api.herokuapp.com/search',
104
  params={
@@ -110,7 +107,11 @@ def fetch_search_results(query):
110
  for index, result in enumerate(search.json()):
111
  snippet = f'[{index + 1}] "{result["snippet"]}" URL:{result["link"]}.'
112
  snippets += snippet
113
- return [{'role': 'system', 'content': snippets}]
 
 
 
 
114
 
115
 
116
  def generate_stream(response, jailbreak):
 
1
  import re
2
+ from datetime import datetime
 
3
  from g4f import ChatCompletion
4
  from flask import request, Response, stream_with_context
5
  from requests import get
 
22
  }
23
 
24
  def _conversation(self):
25
+ """
26
+ Handles the conversation route.
27
 
28
+ :return: Response object containing the generated conversation stream
29
  """
 
 
30
  conversation_id = request.json['conversation_id']
31
 
32
+ try:
33
+ jailbreak = request.json['jailbreak']
34
+ model = request.json['model']
35
+ messages = build_messages(jailbreak)
 
36
 
37
+ # Generate response
38
+ response = ChatCompletion.create(
39
+ model=model,
40
+ stream=True,
41
+ chatId=conversation_id,
42
+ messages=messages
43
+ )
44
 
45
+ return Response(stream_with_context(generate_stream(response, jailbreak)), mimetype='text/event-stream')
46
 
47
+ except Exception as e:
48
+ print(e)
49
+ print(e.__traceback__.tb_next)
50
 
51
+ return {
52
+ '_action': '_ask',
53
+ 'success': False,
54
+ "error": f"an error occurred {str(e)}"
55
+ }, 400
 
 
 
56
 
57
 
58
  def build_messages(jailbreak):
59
+ """
60
+ Build the messages for the conversation.
61
 
62
+ :param jailbreak: Jailbreak instruction string
63
+ :return: List of messages for the conversation
64
  """
65
  _conversation = request.json['meta']['content']['conversation']
66
  internet_access = request.json['meta']['content']['internet_access']
 
70
  conversation = _conversation
71
 
72
  # Add web results if enabled
73
+ if internet_access:
74
+ current_date = datetime.now().strftime("%Y-%m-%d")
75
+ query = f'Current date: {current_date}. ' + prompt["content"]
76
+ search_results = fetch_search_results(query)
77
+ conversation.extend(search_results)
78
 
79
  # Add jailbreak instructions if enabled
80
  if jailbreak_instructions := getJailbreak(jailbreak):
81
+ conversation.extend(jailbreak_instructions)
82
 
83
  # Add the prompt
84
+ conversation.append(prompt)
85
 
86
  # Reduce conversation size to avoid API Token quantity error
87
+ if len(conversation) > 3:
88
+ conversation = conversation[-4:]
89
 
90
  return conversation
91
 
92
 
93
  def fetch_search_results(query):
94
+ """
95
+ Fetch search results for a given query.
96
 
97
+ :param query: Search query string
98
+ :return: List of search results
99
  """
100
  search = get('https://ddg-api.herokuapp.com/search',
101
  params={
 
107
  for index, result in enumerate(search.json()):
108
  snippet = f'[{index + 1}] "{result["snippet"]}" URL:{result["link"]}.'
109
  snippets += snippet
110
+
111
+ response = "Here are some updated web searches. Use this to improve user response:"
112
+ response += snippets
113
+
114
+ return [{'role': 'system', 'content': response}]
115
 
116
 
117
  def generate_stream(response, jailbreak):