app.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import logging
  2. import os
  3. from flask import Flask, jsonify, request
  4. from embedchain import Pipeline as App
  5. app = Flask(__name__)
  6. os.environ["OPENAI_API_KEY"] = "sk-xxx"
  7. @app.route("/add", methods=["POST"])
  8. def add():
  9. data = request.get_json()
  10. data_type = data.get("data_type")
  11. url_or_text = data.get("url_or_text")
  12. if data_type and url_or_text:
  13. try:
  14. App().add(url_or_text, data_type=data_type)
  15. return jsonify({"data": f"Added {data_type}: {url_or_text}"}), 200
  16. except Exception:
  17. logging.exception(f"Failed to add {data_type=}: {url_or_text=}")
  18. return jsonify({"error": f"Failed to add {data_type}: {url_or_text}"}), 500
  19. return jsonify({"error": "Invalid request. Please provide 'data_type' and 'url_or_text' in JSON format."}), 400
  20. @app.route("/query", methods=["POST"])
  21. def query():
  22. data = request.get_json()
  23. question = data.get("question")
  24. if question:
  25. try:
  26. response = App().query(question)
  27. return jsonify({"data": response}), 200
  28. except Exception:
  29. logging.exception(f"Failed to query {question=}")
  30. return jsonify({"error": "An error occurred. Please try again!"}), 500
  31. return jsonify({"error": "Invalid request. Please provide 'question' in JSON format."}), 400
  32. @app.route("/chat", methods=["POST"])
  33. def chat():
  34. data = request.get_json()
  35. question = data.get("question")
  36. if question:
  37. try:
  38. response = App().chat(question)
  39. return jsonify({"data": response}), 200
  40. except Exception:
  41. logging.exception(f"Failed to chat {question=}")
  42. return jsonify({"error": "An error occurred. Please try again!"}), 500
  43. return jsonify({"error": "Invalid request. Please provide 'question' in JSON format."}), 400
  44. @app.route("/api/python")
  45. def hello_world():
  46. return "<p>Hello, World!</p>"