Skip to content

Commit

Permalink
Merge pull request #48 from vgulerianb/vikrant/add-templates-on-start
Browse files Browse the repository at this point in the history
chore(Templates): Add templates on boot up
  • Loading branch information
rohanpooniwala authored Apr 13, 2023
2 parents 9b876ed + 8272958 commit 7768322
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from commons import config as c
from commons.utils import add_default_user
from commons.utils import add_default_user, add_default_templates

# Routers
from api.auth import auth_router
Expand Down Expand Up @@ -37,6 +37,7 @@
)

add_default_user()
add_default_templates()

####################################################
################ INITIALIZE ########################
Expand Down
26 changes: 25 additions & 1 deletion server/commons/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import jwt
from passlib.hash import sha256_crypt
from sqlalchemy.exc import IntegrityError
from database import db_session, User, Prompt, IntermediateStep
from database import db_session, User, Prompt, IntermediateStep, Template
from commons import config as c
import database_constants as constants
from fastapi import HTTPException
from sqlalchemy import func
import json

logger = c.get_logger(__name__)

Expand All @@ -20,6 +21,29 @@ def add_default_user():
except IntegrityError as e:
logger.info("Not adding default user")

def add_default_templates():
db = db_session()
try:
with open("./examples/index.json") as f:
data = json.load(f)
for template_data in data:
template = db.query(Template).filter_by(id=template_data["id"]).first()
if template:
template.name = template_data["name"]
template.description = template_data["description"]
with open("./examples/"+template_data['dag']) as f:
dag = json.load(f)
template.dag = dag
else:
with open("./examples/"+template_data["dag"]) as f:
dag = json.load(f)
template = Template(name=template_data["name"], description=template_data["description"], dag=dag)
db.add(template)
db.commit()
except IntegrityError as e:
logger.info("Not adding default templates")



def get_user_from_jwt(token):
try:
Expand Down
8 changes: 8 additions & 0 deletions server/examples/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"id": 1,
"name": "Base Template",
"description": "This is a basic prompt to start from. It contains just a simple conversational chain.",
"dag": "basic_template.json"
}
]

0 comments on commit 7768322

Please sign in to comment.