monra commited on
Commit
9a577ee
1 Parent(s): 8c1f2d9

Improves web access

Browse files
Files changed (1) hide show
  1. server/backend.py +23 -14
server/backend.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -63,11 +64,11 @@ class Backend_Api:
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 +78,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 +115,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
  import time
3
  import g4f
4
+ from datetime import datetime
5
  from g4f import ChatCompletion
6
  from flask import request, Response, stream_with_context
7
  from requests import get
 
64
 
65
 
66
  def build_messages(jailbreak):
67
+ """
68
+ Build the messages for the conversation.
69
 
70
+ :param jailbreak: Jailbreak instruction string
71
+ :return: List of messages for the conversation
72
  """
73
  _conversation = request.json['meta']['content']['conversation']
74
  internet_access = request.json['meta']['content']['internet_access']
 
78
  conversation = _conversation
79
 
80
  # Add web results if enabled
81
+ if internet_access:
82
+ current_date = datetime.now().strftime("%Y-%m-%d")
83
+ query = f'Current date: {current_date}. ' + prompt["content"]
84
+ search_results = fetch_search_results(query)
85
+ conversation.extend(search_results)
86
 
87
  # Add jailbreak instructions if enabled
88
  if jailbreak_instructions := getJailbreak(jailbreak):
89
+ conversation.extend(jailbreak_instructions)
90
 
91
  # Add the prompt
92
+ conversation.append(prompt)
93
 
94
  # Reduce conversation size to avoid API Token quantity error
95
+ if len(conversation) > 3:
96
+ conversation = conversation[-4:]
97
 
98
  return conversation
99
 
100
 
101
  def fetch_search_results(query):
102
+ """
103
+ Fetch search results for a given query.
104
 
105
+ :param query: Search query string
106
+ :return: List of search results
107
  """
108
  search = get('https://ddg-api.herokuapp.com/search',
109
  params={
 
115
  for index, result in enumerate(search.json()):
116
  snippet = f'[{index + 1}] "{result["snippet"]}" URL:{result["link"]}.'
117
  snippets += snippet
118
+
119
+ response = "Here are some updated web searches. Use this to improve user response:"
120
+ response += snippets
121
+
122
+ return [{'role': 'system', 'content': response}]
123
 
124
 
125
  def generate_stream(response, jailbreak):