-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b16a4a2
commit 930963b
Showing
13 changed files
with
161 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from datetime import datetime | ||
from typing import Any, Dict, List, Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from sqlalchemy.orm import Session | ||
|
||
from llmstudio.tracking.session import models, schemas | ||
|
||
|
||
def get_project_by_name(db: Session, name: str): | ||
return db.query(models.Project).filter(models.Project.name == name).first() | ||
|
||
|
||
def get_session_by_id(db: Session, session_id: str): | ||
return ( | ||
db.query(models.SessionDefault) | ||
.filter(models.SessionDefault.session_id == session_id) | ||
.first() | ||
) | ||
|
||
|
||
def add_session(db: Session, session: schemas.SessionDefaultCreate): | ||
db_session = models.SessionDefault(**session.dict()) | ||
|
||
db.add(db_session) | ||
db.commit() | ||
db.refresh(db_session) | ||
return db_session | ||
|
||
|
||
def update_session(db: Session, session: schemas.SessionDefaultCreate): | ||
existing_session = get_session_by_id(db, session.session_id) | ||
for key, value in session.dict().items(): | ||
setattr(existing_session, key, value) | ||
|
||
db.commit() | ||
db.refresh(existing_session) | ||
return existing_session | ||
|
||
|
||
def upsert_session(db: Session, session: schemas.SessionDefaultCreate): | ||
try: | ||
return update_session(db, session) | ||
except: | ||
return add_session(db, session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from fastapi import APIRouter, Depends | ||
from sqlalchemy.orm import Session | ||
|
||
from llmstudio.tracking.database import engine, get_db | ||
from llmstudio.tracking.session import crud, models, schemas | ||
|
||
models.Base.metadata.create_all(bind=engine) | ||
|
||
|
||
class SessionsRoutes: | ||
def __init__(self, router: APIRouter): | ||
self.router = router | ||
self.define_routes() | ||
|
||
def define_routes(self): | ||
# Add session | ||
self.router.post( | ||
"/session", | ||
response_model=schemas.SessionDefault, | ||
)(self.add_session) | ||
|
||
# Read session | ||
self.router.get("/session/{session_id}", response_model=schemas.SessionDefault)( | ||
self.get_session | ||
) | ||
|
||
async def add_session( | ||
self, session: schemas.SessionDefaultCreate, db: Session = Depends(get_db) | ||
): | ||
return crud.upsert_session(db=db, session=session) | ||
|
||
async def get_session(self, session_id: str, db: Session = Depends(get_db)): | ||
logs = crud.get_session_by_id(db, session_id=session_id) | ||
return logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from sqlalchemy import JSON, Column, DateTime, String | ||
from sqlalchemy.sql import func | ||
|
||
from llmstudio.tracking.database import Base | ||
|
||
|
||
class SessionDefault(Base): | ||
__tablename__ = "sessions" | ||
session_id = Column(String, primary_key=True) | ||
chat_history = Column(JSON) | ||
updated_at = Column( | ||
DateTime(timezone=True), onupdate=func.now(), server_default=func.now() | ||
) | ||
created_at = Column(DateTime(timezone=True), server_default=func.now()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from datetime import datetime | ||
from typing import Any, Dict, List | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class SessionDefaultBase(BaseModel): | ||
session_id: str | ||
chat_history: List[Dict[str, Any]] = None | ||
|
||
|
||
class SessionDefault(SessionDefaultBase): | ||
created_at: datetime | ||
updated_at: datetime | ||
|
||
|
||
class SessionDefaultCreate(SessionDefaultBase): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters