gpt4free
gpt4free copied to clipboard
Help with deploy API
I want to wrap api for my personal prompts and secure it with token
But when i try to POST request I take turns getting a normal response and the next request gets a 404 and so they alternate. What's the problem?
Here is the code
from g4f.api import run_api
import openai
from flask import Flask, request, jsonify
import multiprocessing
import time
openai.api_key = ""
openai.api_base = "http://0.0.0.0:1337"
app = Flask(__name__)
valid_token = "SOMETOKEN"
@app.route("/", methods=["GET"])
def index():
return "Hello :)"
@app.route("/chat", methods=["POST"])
def chat():
data = request.get_json()
if 'token' not in data or 'prompt' not in data:
return jsonify({"error": "Missing required fields ('token', 'prompt')"}), 400
token = data['token']
if token != valid_token:
return jsonify({"error": "Invalid token"}), 403 # 403 Forbidden
prompt = data["prompt"]
chat_completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=prompt,
max_tokens=50
)
response = chat_completion.choices[0].message.content
return jsonify({"response": response})
if __name__ == '__main__':
api_process = multiprocessing.Process(target=run_api)
api_process.start()
app.run(host='0.0.0.0', port=5000, debug=True)
```
Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.
"messages" should be a list and not only a prompt.
Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.
Closing due to inactivity.