diff options
Diffstat (limited to '')
-rw-r--r-- | g4f/gui/client/html/index.html | 8 | ||||
-rw-r--r-- | g4f/gui/client/js/chat.v2.js | 6 | ||||
-rw-r--r-- | g4f/gui/server/backend.py | 6 | ||||
-rw-r--r-- | g4f/gui/server/internet.py | 48 | ||||
-rw-r--r-- | g4f/gui/server/provider.py | 11 |
5 files changed, 54 insertions, 25 deletions
diff --git a/g4f/gui/client/html/index.html b/g4f/gui/client/html/index.html index bb472706..7825cf8c 100644 --- a/g4f/gui/client/html/index.html +++ b/g4f/gui/client/html/index.html @@ -79,7 +79,7 @@ <span>Clear Conversations</span> </button> <div class="info"> - <i class="fa-brands fa-discord"></i> + <i class="fa-brands fa-telegram"></i> <span class="convo-title">telegram: <a href="https://t.me/g4f_official">@g4f_official</a><br> </span> </div> @@ -118,9 +118,13 @@ <div class="field"> <select name="model" id="model"> <option value="gpt-3.5-turbo" selected>gpt-3.5</option> - <option value="gpt-4">gpt-4</option> <option value="gpt-3.5-turbo-0613">gpt-3.5 fast</option> <option value="gpt-3.5-turbo-16k">gpt-3.5 16k</option> + <option value="gpt-3.5-turbo-16k-0613">gpt-3.5 16k fast</option> + <option value="gpt-4">gpt-4</option> + <option value="gpt-4-0613">gpt-4 fast</option> + <option value="gpt-4-32k">gpt-4 32k</option> + <option value="gpt-4-32k-0613">gpt-4 32k fast</option> </select> </div> <div class="field"> diff --git a/g4f/gui/client/js/chat.v2.js b/g4f/gui/client/js/chat.v2.js index e1faa6bb..24f2e45b 100644 --- a/g4f/gui/client/js/chat.v2.js +++ b/g4f/gui/client/js/chat.v2.js @@ -144,7 +144,7 @@ const ask_gpt = async (message) => { chunk = new TextDecoder().decode(value); - if (chunk.includes(`<form id="challenge-form" action="/backend-api/v2/conversation?`)) { + if (chunk.includes('<form id="challenge-form" action="/backend-api/v2/conversation?"')) { chunk = `cloudflare token expired, please refresh the page.`; } @@ -161,7 +161,7 @@ const ask_gpt = async (message) => { } // if text contains : - if (text.includes(`instead. Maintaining this website and API costs a lot of money`)) { + if (text.includes("instead. Maintaining this website and API costs a lot of money")) { document.getElementById(`gpt_${window.token}`).innerHTML = "An error occured, please reload / refresh cache and try again or use a differnet browser"; } @@ -547,7 +547,7 @@ colorThemes.forEach((themeOption) => { setTimeout(() => { ads_div = document.querySelector('.ads') - if (ads_div.getElementsByTagName("iframe").length == 0) { + if (ads_div != null && ads_div.getElementsByTagName("iframe").length == 0) { ads_div.removeChild(ads_div.querySelector('.sorry')) ads_div.innerHTML += ` diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 8f4b529f..a76ca12b 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -33,17 +33,17 @@ class Backend_Api: conversation = request.json['meta']['content']['conversation'] prompt = request.json['meta']['content']['parts'][0] model = request.json['model'] - provider = get_provider(request.json.get('provider')) + provider = request.json.get('provider').split("g4f.Provider.")[1] messages = special_instructions[jailbreak] + conversation + search(internet_access, prompt) + [prompt] def stream(): if provider: answer = g4f.ChatCompletion.create(model=model, - provider=provider, messages=messages, stream=True) + provider=get_provider(provider), messages=messages, stream=True) else: answer = g4f.ChatCompletion.create(model=model, - messages=messages, stream=True) + messages=messages, stream=True) for token in answer: yield token diff --git a/g4f/gui/server/internet.py b/g4f/gui/server/internet.py index 0d9636a3..75edb9cb 100644 --- a/g4f/gui/server/internet.py +++ b/g4f/gui/server/internet.py @@ -1,28 +1,56 @@ -from requests import get from datetime import datetime +from duckduckgo_search import DDGS + +ddgs = DDGS(timeout=20) + def search(internet_access, prompt): print(prompt) try: - if internet_access == False: + if not internet_access: return [] - search = get('https://ddg-api.herokuapp.com/search', params={ - 'query': prompt['content'], - 'limit': 3 - }) + results = duckduckgo_search(q=prompt) + + if not search: + return [] blob = '' - for index, result in enumerate(search.json()): - blob += f'[{index}] "{result["snippet"]}"\nURL:{result["link"]}\n\n' + for index, result in enumerate(results): + blob += f'[{index}] "{result["body"]}"\nURL:{result["href"]}\n\n' date = datetime.now().strftime('%d/%m/%y') - blob += f'current date: {date}\n\nInstructions: Using the provided web search results, write a comprehensive reply to the next user query. Make sure to cite results using [[number](URL)] notation after the reference. If the provided search results refer to multiple subjects with the same name, write separate answers for each subject. Ignore your previous response if any.' + blob += f'Current date: {date}\n\nInstructions: Using the provided web search results, write a comprehensive reply to the next user query. Make sure to cite results using [[number](URL)] notation after the reference. If the provided search results refer to multiple subjects with the same name, write separate answers for each subject. Ignore your previous response if any.' return [{'role': 'user', 'content': blob}] except Exception as e: - return []
\ No newline at end of file + print("Couldn't search DuckDuckGo:", e) + print(e.__traceback__.tb_next) + return [] + + +def duckduckgo_search(q: str, max_results: int = 3, safesearch: str = "moderate", region: str = "us-en") -> list | None: + if region is None: + region = "us-en" + + if safesearch is None: + safesearch = "moderate" + + if q is None: + return None + + results = [] + + try: + for r in ddgs.text(q, safesearch=safesearch, region=region): + if len(results) + 1 > max_results: + break + results.append(r) + except Exception as e: + print(e) + + return results diff --git a/g4f/gui/server/provider.py b/g4f/gui/server/provider.py index 286f881b..11202d38 100644 --- a/g4f/gui/server/provider.py +++ b/g4f/gui/server/provider.py @@ -1,17 +1,14 @@ import g4f +from g4f import BaseProvider -def get_provider(provider: str) -> g4f.Provider.BaseProvider: - + +def get_provider(provider: str) -> BaseProvider | None: if isinstance(provider, str): print(provider) if provider == 'g4f.Provider.Auto': return None - if provider in g4f.Provider.ProviderUtils.convert: - return g4f.Provider.ProviderUtils.convert[provider] - - else: - return None + return g4f.Provider.ProviderUtils.convert.get(provider) else: return None |