From 6fd2fa971fccc51acf4f312482e32c3b164809bd Mon Sep 17 00:00:00 2001 From: catmeowjiao Date: Thu, 26 Oct 2023 22:18:38 +0800 Subject: [PATCH 1/2] add proxy support --- g4f/gui/server/backend.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 2ba0ca8e..cf6d6358 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -1,4 +1,5 @@ import g4f +import json from flask import request from .internet import search @@ -43,6 +44,15 @@ class Backend_Api: } def _conversation(self): + config = None + proxy = None + try: + config = json.load(open("config.json","r",encoding="utf-8")) + proxy = config["proxy"] + + except Exception: + pass + try: jailbreak = request.json['jailbreak'] internet_access = request.json['meta']['content']['internet_access'] @@ -54,14 +64,25 @@ class Backend_Api: messages = special_instructions[jailbreak] + conversation + search(internet_access, prompt) + [prompt] def stream(): - yield from g4f.ChatCompletion.create( - model=model, - provider=get_provider(provider), - messages=messages, - stream=True, - ) if provider else g4f.ChatCompletion.create( - model=model, messages=messages, stream=True - ) + if proxy != None: + yield from g4f.ChatCompletion.create( + model=model, + provider=get_provider(provider), + messages=messages, + stream=True, + proxy=proxy + ) if provider else g4f.ChatCompletion.create( + model=model, messages=messages, stream=True, proxy=proxy + ) + else: + yield from g4f.ChatCompletion.create( + model=model, + provider=get_provider(provider), + messages=messages, + stream=True, + ) if provider else g4f.ChatCompletion.create( + model=model, messages=messages, stream=True + ) return self.app.response_class(stream(), mimetype='text/event-stream') From 325f8558da81fd7e6dacc0ed8e50859e285f116c Mon Sep 17 00:00:00 2001 From: catmeowjiao Date: Fri, 27 Oct 2023 17:33:47 +0800 Subject: [PATCH 2/2] add api proxy --- g4f/api/__init__.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index b78768bd..fec5606f 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -105,9 +105,25 @@ class Api: logger.info(f'model: {model}, stream: {stream}, request: {messages[-1]["content"]}') - response = self.engine.ChatCompletion.create(model=model, - stream=stream, messages=messages, - ignored=self.list_ignored_providers) + config = None + proxy = None + + try: + config = json.load(open("config.json","r",encoding="utf-8")) + proxy = config["proxy"] + + except Exception: + pass + + if proxy != None: + response = self.engine.ChatCompletion.create(model=model, + stream=stream, messages=messages, + ignored=self.list_ignored_providers, + proxy=proxy) + else: + response = self.engine.ChatCompletion.create(model=model, + stream=stream, messages=messages, + ignored=self.list_ignored_providers) completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28)) completion_timestamp = int(time.time())