gpt4free icon indicating copy to clipboard operation
gpt4free copied to clipboard

Help with deploy API

Open 53RG1005 opened this issue 2 years ago • 3 comments

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)
    ```

53RG1005 avatar Oct 14 '23 17:10 53RG1005

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

github-actions[bot] avatar Oct 22 '23 00:10 github-actions[bot]

"messages" should be a list and not only a prompt.

hlohaus avatar Oct 23 '23 17:10 hlohaus

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

github-actions[bot] avatar Jan 06 '24 00:01 github-actions[bot]

Closing due to inactivity.

github-actions[bot] avatar Feb 08 '24 00:02 github-actions[bot]