Skip to content

Commit

Permalink
Merge pull request #1166 from catmeowjiao/add-proxy
Browse files Browse the repository at this point in the history
add proxy support
  • Loading branch information
xtekky authored Oct 27, 2023
2 parents 6937ee6 + 325f855 commit 0e700a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
22 changes: 19 additions & 3 deletions g4f/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,25 @@ async def chat_completions(self):

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())
Expand Down
37 changes: 29 additions & 8 deletions g4f/gui/server/backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import g4f
import json

from flask import request
from .internet import search
Expand Down Expand Up @@ -43,6 +44,15 @@ def _gen_title(self):
}

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']
Expand All @@ -54,14 +64,25 @@ def _conversation(self):
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')

Expand Down

0 comments on commit 0e700a5

Please sign in to comment.