Rename embedchain to mem0 and open sourcing code for long term memory (#1474)
Co-authored-by: Deshraj Yadav <deshrajdry@gmail.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import os
|
||||
|
||||
from flask import Blueprint, jsonify, make_response, request
|
||||
from models import APIKey
|
||||
from paths import DB_DIRECTORY_OPEN_AI
|
||||
|
||||
from embedchain import App
|
||||
|
||||
chat_response_bp = Blueprint("chat_response", __name__)
|
||||
|
||||
|
||||
# Chat Response for user query
|
||||
@chat_response_bp.route("/api/get_answer", methods=["POST"])
|
||||
def get_answer():
|
||||
try:
|
||||
data = request.get_json()
|
||||
query = data.get("query")
|
||||
embedding_model = data.get("embedding_model")
|
||||
app_type = data.get("app_type")
|
||||
|
||||
if embedding_model == "open_ai":
|
||||
os.chdir(DB_DIRECTORY_OPEN_AI)
|
||||
api_key = APIKey.query.first().key
|
||||
os.environ["OPENAI_API_KEY"] = api_key
|
||||
if app_type == "app":
|
||||
chat_bot = App()
|
||||
|
||||
response = chat_bot.chat(query)
|
||||
return make_response(jsonify({"response": response}), 200)
|
||||
|
||||
except Exception as e:
|
||||
return make_response(jsonify({"error": str(e)}), 400)
|
||||
72
embedchain/examples/full_stack/backend/routes/dashboard.py
Normal file
72
embedchain/examples/full_stack/backend/routes/dashboard.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from flask import Blueprint, jsonify, make_response, request
|
||||
from models import APIKey, BotList, db
|
||||
|
||||
dashboard_bp = Blueprint("dashboard", __name__)
|
||||
|
||||
|
||||
# Set Open AI Key
|
||||
@dashboard_bp.route("/api/set_key", methods=["POST"])
|
||||
def set_key():
|
||||
data = request.get_json()
|
||||
api_key = data["openAIKey"]
|
||||
existing_key = APIKey.query.first()
|
||||
if existing_key:
|
||||
existing_key.key = api_key
|
||||
else:
|
||||
new_key = APIKey(key=api_key)
|
||||
db.session.add(new_key)
|
||||
db.session.commit()
|
||||
return make_response(jsonify(message="API key saved successfully"), 200)
|
||||
|
||||
|
||||
# Check OpenAI Key
|
||||
@dashboard_bp.route("/api/check_key", methods=["GET"])
|
||||
def check_key():
|
||||
existing_key = APIKey.query.first()
|
||||
if existing_key:
|
||||
return make_response(jsonify(status="ok", message="OpenAI Key exists"), 200)
|
||||
else:
|
||||
return make_response(jsonify(status="fail", message="No OpenAI Key present"), 200)
|
||||
|
||||
|
||||
# Create a bot
|
||||
@dashboard_bp.route("/api/create_bot", methods=["POST"])
|
||||
def create_bot():
|
||||
data = request.get_json()
|
||||
name = data["name"]
|
||||
slug = name.lower().replace(" ", "_")
|
||||
existing_bot = BotList.query.filter_by(slug=slug).first()
|
||||
if existing_bot:
|
||||
return (make_response(jsonify(message="Bot already exists"), 400),)
|
||||
new_bot = BotList(name=name, slug=slug)
|
||||
db.session.add(new_bot)
|
||||
db.session.commit()
|
||||
return make_response(jsonify(message="Bot created successfully"), 200)
|
||||
|
||||
|
||||
# Delete a bot
|
||||
@dashboard_bp.route("/api/delete_bot", methods=["POST"])
|
||||
def delete_bot():
|
||||
data = request.get_json()
|
||||
slug = data.get("slug")
|
||||
bot = BotList.query.filter_by(slug=slug).first()
|
||||
if bot:
|
||||
db.session.delete(bot)
|
||||
db.session.commit()
|
||||
return make_response(jsonify(message="Bot deleted successfully"), 200)
|
||||
return make_response(jsonify(message="Bot not found"), 400)
|
||||
|
||||
|
||||
# Get the list of bots
|
||||
@dashboard_bp.route("/api/get_bots", methods=["GET"])
|
||||
def get_bots():
|
||||
bots = BotList.query.all()
|
||||
bot_list = []
|
||||
for bot in bots:
|
||||
bot_list.append(
|
||||
{
|
||||
"name": bot.name,
|
||||
"slug": bot.slug,
|
||||
}
|
||||
)
|
||||
return jsonify(bot_list)
|
||||
27
embedchain/examples/full_stack/backend/routes/sources.py
Normal file
27
embedchain/examples/full_stack/backend/routes/sources.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import os
|
||||
|
||||
from flask import Blueprint, jsonify, make_response, request
|
||||
from models import APIKey
|
||||
from paths import DB_DIRECTORY_OPEN_AI
|
||||
|
||||
from embedchain import App
|
||||
|
||||
sources_bp = Blueprint("sources", __name__)
|
||||
|
||||
|
||||
# API route to add data sources
|
||||
@sources_bp.route("/api/add_sources", methods=["POST"])
|
||||
def add_sources():
|
||||
try:
|
||||
embedding_model = request.json.get("embedding_model")
|
||||
name = request.json.get("name")
|
||||
value = request.json.get("value")
|
||||
if embedding_model == "open_ai":
|
||||
os.chdir(DB_DIRECTORY_OPEN_AI)
|
||||
api_key = APIKey.query.first().key
|
||||
os.environ["OPENAI_API_KEY"] = api_key
|
||||
chat_bot = App()
|
||||
chat_bot.add(name, value)
|
||||
return make_response(jsonify(message="Sources added successfully"), 200)
|
||||
except Exception as e:
|
||||
return make_response(jsonify(message=f"Error adding sources: {str(e)}"), 400)
|
||||
Reference in New Issue
Block a user