bikin api

mail@pastecode.io avatar
unknown
python
6 months ago
1.2 kB
3
Indexable
Never
import json
import os
from embedchain import App
from flask import Flask, request, jsonify

os.environ["OPENAI_API_KEY"] = "sk-apikeyku"


app = Flask(__name__)
with open('data.json', 'r', encoding='utf-8') as file:
    data = json.load(file)

marbot = App.from_config(config={
    "llm": {
        "provider": "openai",
        "config": {
            "model": "gpt-3.5-turbo",
            "temperature": 0.0,
            "top_p": 1,
            "stream": False,
        },
    },
    "embedder": {
        "provider": "openai",
        "config": {
            "model": "text-embedding-ada-002",
        }
    },
})

marbot.add("data.json")

@app.route('/chat', methods=['POST'])
def chat():
    data = request.json
    query = data.get('query', '')

    if not query:
        return jsonify({"error": "No query provided"}), 400

    try:
        response = marbot.chat(query)
        print(response) 
        return jsonify({"response": response})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(debug=True,
            host="0.0.0.0",
            port=int(os.environ.get("PORT", 8000)))
Leave a Comment