Skip to content

Commit

Permalink
add: API server(mem0ai#422)
Browse files Browse the repository at this point in the history
Add an example of api server so that devs can quickly get up a bot running along with its api
  • Loading branch information
sahilyadav902 committed Aug 11, 2023
1 parent d51c508 commit a86deb2
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 1 deletion.
46 changes: 46 additions & 0 deletions docs/examples/api_server.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: '🌍 API Server'
---

### 🐳 Docker Setup

- Open variables.env, and edit it to add your 🔑 `OPENAI_API_KEY`.
- To setup your api server using docker, run the following command inside this folder using your terminal.

```bash
docker-compose up --build
```

📝 Note: The build command might take a while to install all the packages depending on your system resources.

### 🚀 Usage Instructions

- Your api server is running on [http:https://localhost:5000/](http:https://localhost:5000/)
- To use the api server, make an api call to the endpoints `/add` and `/query` using the json formats discussed below.
- To add data sources to the bot:
```json
// Request
{
"data_type": "your_data_type_here",
"url_or_text": "your_url_or_text_here"
}

// Response
{
"data": "Added data_type: url_or_text"
}
```
- To ask questions from the bot:
```json
// Request
{
"question": "your_question_here"
}

// Response
{
"data": "your_answer_here"
}
```

🎉 Happy Chatting! 🎉
2 changes: 1 addition & 1 deletion docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
{
"group": "Examples",
"pages": ["examples/full_stack"]
"pages": ["examples/full_stack", "examples/api_server"]
},
{
"group": "Contribution Guidelines",
Expand Down
8 changes: 8 additions & 0 deletions examples/api_server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__/
database
db
pyenv
venv
.env
.git
trash_files/
8 changes: 8 additions & 0 deletions examples/api_server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__
db
database
pyenv
venv
.env
trash_files/
.ideas.md
11 changes: 11 additions & 0 deletions examples/api_server/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
42 changes: 42 additions & 0 deletions examples/api_server/api_server.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions examples/api_server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions examples/api_server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask==2.3.2
embedchain==0.0.30
1 change: 1 addition & 0 deletions examples/api_server/variables.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=""

0 comments on commit a86deb2

Please sign in to comment.