add: API server(#422)

Add an example of api server so that devs can quickly get up a bot running along with its api
This commit is contained in:
Sahil Kumar Yadav
2023-08-11 11:30:51 +05:30
committed by GitHub
parent d51c508b40
commit a86deb2675
9 changed files with 132 additions and 1 deletions

View File

@@ -0,0 +1,8 @@
__pycache__/
database
db
pyenv
venv
.env
.git
trash_files/

8
examples/api_server/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
__pycache__
db
database
pyenv
venv
.env
trash_files/
.ideas.md

View File

@@ -0,0 +1,11 @@
FROM python:3.11 AS backend
WORKDIR /usr/src/api
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "api_server.py"]

View File

@@ -0,0 +1,42 @@
from flask import Flask, jsonify, request
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()
data_type = data.get("data_type")
url_or_text = data.get("url_or_text")
if data_type and url_or_text:
try:
chat_bot.add(data_type, url_or_text)
return jsonify({"data": f"Added {data_type}: {url_or_text}"}), 200
except Exception:
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
@app.route("/query", methods=["POST"])
def query():
data = request.get_json()
question = data.get("question")
if question:
try:
response = chat_bot.chat(question)
return jsonify({"data": response}), 200
except Exception:
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)

View File

@@ -0,0 +1,13 @@
version: "3.9"
services:
backend:
container_name: embedchain_api
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
env_file:
- variables.env
ports:
- "5000:5000"

View File

@@ -0,0 +1,2 @@
flask==2.3.2
embedchain==0.0.30

View File

@@ -0,0 +1 @@
OPENAI_API_KEY=""