Skip to content

armgit / mem0

Public Imported from GitHub
0

The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.

add: API server(#422)

Sahil Kumar Yadava86deb2675b7
diff --git a/docs/examples/api_server.mdx b/docs/examples/api_server.mdx
new file mode 100644
index 000000000..2d077114b
--- /dev/null
+++ b/docs/examples/api_server.mdx
@@ -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://localhost:5000/](http://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! πŸŽ‰
diff --git a/docs/mint.json b/docs/mint.json
index 750afc0b8..81689a71a 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -36,7 +36,7 @@
},
{
"group": "Examples",
- "pages": ["examples/full_stack"]
+ "pages": ["examples/full_stack", "examples/api_server"]
},
{
"group": "Contribution Guidelines",
diff --git a/examples/api_server/.dockerignore b/examples/api_server/.dockerignore
new file mode 100644
index 000000000..1dce42e87
--- /dev/null
+++ b/examples/api_server/.dockerignore
@@ -0,0 +1,8 @@
+__pycache__/
+database
+db
+pyenv
+venv
+.env
+.git
+trash_files/
diff --git a/examples/api_server/.gitignore b/examples/api_server/.gitignore
new file mode 100644
index 000000000..2227fe3e2
--- /dev/null
+++ b/examples/api_server/.gitignore
@@ -0,0 +1,8 @@
+__pycache__
+db
+database
+pyenv
+venv
+.env
+trash_files/
+.ideas.md
\ No newline at end of file
diff --git a/examples/api_server/Dockerfile b/examples/api_server/Dockerfile
new file mode 100644
index 000000000..d353b818c
--- /dev/null
+++ b/examples/api_server/Dockerfile
@@ -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"]
diff --git a/examples/api_server/api_server.py b/examples/api_server/api_server.py
new file mode 100644
index 000000000..224dd7a0e
--- /dev/null
+++ b/examples/api_server/api_server.py
@@ -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()
+
+
[email protected]("/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
+
+
[email protected]("/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)
diff --git a/examples/api_server/docker-compose.yml b/examples/api_server/docker-compose.yml
new file mode 100644
index 000000000..09acb7af7
--- /dev/null
+++ b/examples/api_server/docker-compose.yml
@@ -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"
\ No newline at end of file
diff --git a/examples/api_server/requirements.txt b/examples/api_server/requirements.txt
new file mode 100644
index 000000000..2df2e3b0c
--- /dev/null
+++ b/examples/api_server/requirements.txt
@@ -0,0 +1,2 @@
+flask==2.3.2
+embedchain==0.0.30
\ No newline at end of file
diff --git a/examples/api_server/variables.env b/examples/api_server/variables.env
new file mode 100644
index 000000000..da6725993
--- /dev/null
+++ b/examples/api_server/variables.env
@@ -0,0 +1 @@
+OPENAI_API_KEY=""
\ No newline at end of file