Skip to content

Commit

Permalink
chore: Add db_logger.py and db.py files for database logging and inte…
Browse files Browse the repository at this point in the history
…rface
  • Loading branch information
amirrr committed Jun 1, 2024
1 parent 3787176 commit 85035f0
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 1,443 deletions.
1,443 changes: 0 additions & 1,443 deletions lang_chain.ipynb

This file was deleted.

Empty file added server/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ def post(self):
api.add_resource(HelloWorld, "/api/workflow")



# Create a new prompt
class Prompt(Resource):
def post(self):
prompt_data = request.json
prompt_id = prompt_data["prompt_id"]

document = {
"prompt_id": prompt_id,
"current_version": 1,
"versions": [{"version": 1, "data": prompt_data["data"]}],
}

collection.insert_one(document)

return jsonify({"message": "Prompt created", "prompt_id": prompt_id}), 201
# @app.route("/prompt", methods=["POST"])
# def create_prompt():
# prompt_data = request.json
# prompt_id = prompt_data["prompt_id"]

# document = {
# "prompt_id": prompt_id,
# "current_version": 1,
# "versions": [{"version": 1, "data": prompt_data["data"]}],
# }

# collection.insert_one(document)

# return jsonify({"message": "Prompt created", "prompt_id": prompt_id}), 201


if __name__ == "__main__":
# getting list of command line arguments
parser = argparse.ArgumentParser(description="Flask RESTful api end point.")
Expand Down
Empty file added server/db/__init__.py
Empty file.
59 changes: 59 additions & 0 deletions server/db/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import certifi
import logging
from dotenv import load_dotenv
from db_logger import bcolors
import datetime

# Database interface
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from bson.objectid import ObjectId

load_dotenv()


class DatabaseInterface:
def __init__(self):
uri = os.getenv("DB_URI")
self.client = MongoClient(
uri, server_api=ServerApi("1"), tlsCAFile=certifi.where()
)
self.db = self.client.get_database("atlas_main")
self.logger = logging.getLogger(__name__)
print(f"{bcolors.OKGREEN}Database interface initialized{bcolors.ENDC}")

def get_collection_names(self):
return self.db.list_collection_names()

def create_collection(self, collection_name):
return self.db.create_collection(collection_name)

def drop_collection(self, collection_name):
return self.db.drop_collection(collection_name)

def insert_one(self, collection_name, data):
collection = self.db[collection_name]
return collection.insert_one(data)

def insert_many(self, collection_name, data):
collection = self.db[collection_name]
return collection.insert_many(data)


if __name__ == "__main__":
db_interface = DatabaseInterface()

document_parent = {
"prompt_id": ObjectId(),
"feature_path": "experiments.conditions.name",
"github_url": "server/features/experiments/conditions/name.py",
"versions": [
{
"performace": 0.2,
"average_cost": 0.3,
"commit_SHA": "1234567890",
"created_at": datetime.datetime.now(),
}
],
}
10 changes: 10 additions & 0 deletions server/db/db_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added server/utils/__init__.py
Empty file.

0 comments on commit 85035f0

Please sign in to comment.