-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca59662
commit b1fdea2
Showing
1 changed file
with
26 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,46 @@ | ||
from flask import Flask, request, jsonify | ||
import g4f | ||
import sys | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/chat_completion', methods=['POST']) | ||
def chat_completion(): | ||
data = request.get_json() | ||
|
||
if 'content' not in data: | ||
return jsonify({"error": "Missing 'content' in the request"}), 400 | ||
if 'content' not in data or 'provider' not in data: | ||
return jsonify({"error": "Missing 'content' or 'provider' in the request"}), 400 | ||
|
||
content = data['content'] | ||
pname = data['provider'] | ||
|
||
llm = getattr(getattr(getattr(sys.modules[__name__], "g4f"), "Provider"), pname) | ||
|
||
model = "gpt-3.5-turbo" | ||
|
||
if not llm.supports_gpt_35_turbo: | ||
model = "gpt-4" | ||
|
||
response = g4f.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
provider=g4f.Provider.You, | ||
model=model, | ||
provider=llm, | ||
messages=[{"role": "user", "content": content}], | ||
) | ||
|
||
if response.strip() == '': | ||
raise Exception('Empty result') | ||
|
||
return jsonify({"response": response}) | ||
|
||
@app.route('/working_providers', methods=['GET']) | ||
def working_providers(): | ||
working_providers_list = [ | ||
provider.__name__ | ||
for provider in g4f.Provider.__providers__ | ||
if provider.working | ||
] | ||
|
||
return jsonify({"working_providers": working_providers_list}) | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |