Skip to content

Commit

Permalink
feat: create flask api
Browse files Browse the repository at this point in the history
  • Loading branch information
nagarajpandith committed Dec 8, 2023
1 parent eef11e8 commit ca59662
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# GPT Flask API

```
git clone https://github.com/nagarajpandith/gpt-flask.git
cd gpt-flask
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
python3 main.py
```
28 changes: 21 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
from flask import Flask, request, jsonify
import g4f

response = g4f.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
)
app = Flask(__name__)

for message in response:
print(message, flush=True, end='')
@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

content = data['content']

response = g4f.ChatCompletion.create(
model="gpt-3.5-turbo",
provider=g4f.Provider.You,
messages=[{"role": "user", "content": content}],
)

return jsonify({"response": response})

if __name__ == '__main__':
app.run(debug=True)

0 comments on commit ca59662

Please sign in to comment.