Merge pull request #1166 from catmeowjiao/add-proxy

add proxy support
This commit is contained in:
Tekky 2023-10-27 18:36:46 +02:00 committed by GitHub
commit 0e700a5a95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 11 deletions

View File

@ -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())

View File

@ -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')