Explorar o código

[Bug fix] Fix sqlite related issue with api server example (#857)

Deshraj Yadav hai 1 ano
pai
achega
78ec91a3a9

+ 6 - 1
examples/api_server/Dockerfile

@@ -8,4 +8,9 @@ COPY . .
 
 EXPOSE 5000
 
-CMD ["python", "api_server.py"]
+ENV FLASK_APP=api_server.py
+
+ENV FLASK_RUN_EXTRA_FILES=/usr/src/api/*
+ENV FLASK_ENV=development
+
+CMD ["flask", "run", "--host=0.0.0.0", "--reload"]

+ 8 - 9
examples/api_server/api_server.py

@@ -1,3 +1,5 @@
+import logging
+
 from flask import Flask, jsonify, request
 
 from embedchain import App
@@ -5,11 +7,6 @@ from embedchain import App
 app = Flask(__name__)
 
 
-def initialize_chat_bot():
-    global chat_bot
-    chat_bot = App()
-
-
 @app.route("/add", methods=["POST"])
 def add():
     data = request.get_json()
@@ -17,9 +14,10 @@ def add():
     url_or_text = data.get("url_or_text")
     if data_type and url_or_text:
         try:
-            chat_bot.add(data_type, url_or_text)
+            App().add(url_or_text, data_type=data_type)
             return jsonify({"data": f"Added {data_type}: {url_or_text}"}), 200
         except Exception:
+            logging.exception(f"Failed to add {data_type=}: {url_or_text=}")
             return jsonify({"error": f"Failed to add {data_type}: {url_or_text}"}), 500
     return jsonify({"error": "Invalid request. Please provide 'data_type' and 'url_or_text' in JSON format."}), 400
 
@@ -30,9 +28,10 @@ def query():
     question = data.get("question")
     if question:
         try:
-            response = chat_bot.query(question)
+            response = App().query(question)
             return jsonify({"data": response}), 200
         except Exception:
+            logging.exception(f"Failed to query {question=}")
             return jsonify({"error": "An error occurred. Please try again!"}), 500
     return jsonify({"error": "Invalid request. Please provide 'question' in JSON format."}), 400
 
@@ -43,13 +42,13 @@ def chat():
     question = data.get("question")
     if question:
         try:
-            response = chat_bot.chat(question)
+            response = App().chat(question)
             return jsonify({"data": response}), 200
         except Exception:
+            logging.exception(f"Failed to chat {question=}")
             return jsonify({"error": "An error occurred. Please try again!"}), 500
     return jsonify({"error": "Invalid request. Please provide 'question' in JSON format."}), 400
 
 
 if __name__ == "__main__":
-    initialize_chat_bot()
     app.run(host="0.0.0.0", port=5000, debug=False)

+ 3 - 1
examples/api_server/docker-compose.yml

@@ -10,4 +10,6 @@ services:
     env_file:
       - variables.env
     ports:
-      - "5000:5000"
+      - "5000:5000"
+    volumes:
+      - .:/usr/src/api

+ 1 - 1
examples/api_server/requirements.txt

@@ -1,2 +1,2 @@
 flask==2.3.2
-embedchain==0.0.58
+embedchain[dataloaders]==0.0.78

+ 6 - 5
pyproject.toml

@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "embedchain"
-version = "0.0.77"
+version = "0.0.78"
 description = "Data platform for LLMs - Load, index, retrieve and sync any unstructured data"
 authors = ["Taranjeet Singh, Deshraj Yadav"]
 license = "Apache License"
@@ -160,13 +160,14 @@ milvus = ["pymilvus"]
 dataloaders=[
     "youtube-transcripts-api",
     "beautifulsoup4",
+    "docx2txt",
+    "duckduckgo-search",
+    "jq",
+    "langchain",
     "pypdf",
     "pytube",
-    "duckduckgo-search",
-    "docx2txt",
-    "unstructured",
     "sentence-transformers",
-    "jq",
+    "unstructured",
 ]
 vertexai = ["google-cloud-aiplatform"]
 llama2 = ["replicate"]