-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Feat/session management #86
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8cd0c34
feat: modular logs
brunoalho99 b16a4a2
feat: routing
brunoalho99 930963b
feat: sessions
brunoalho99 ce6953d
feat: message_id logic implemented
brunoalho99 1a01208
name update
brunoalho99 bd084c3
feat: add dev github actions
brunoalho99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,57 @@ | ||
from typing import List | ||
|
||
from fastapi import APIRouter, Depends | ||
from sqlalchemy.orm import Session | ||
|
||
from llmstudio.tracking.database import engine, get_db | ||
from llmstudio.tracking.logs import crud, models, schemas | ||
|
||
models.Base.metadata.create_all(bind=engine) | ||
|
||
|
||
class LogsRoutes: | ||
def __init__(self, router: APIRouter): | ||
self.router = router | ||
|
||
# Define routes | ||
self.define_routes() | ||
|
||
def define_routes(self): | ||
# Add log | ||
self.router.post( | ||
"/logs", | ||
response_model=schemas.LogDefault, | ||
)(self.add_log) | ||
|
||
# Read logs | ||
self.router.get("/logs", response_model=List[schemas.LogDefault])( | ||
self.read_logs | ||
) | ||
|
||
# Read logs by session | ||
self.router.get("/logs_by_session", response_model=List[schemas.LogDefault])( | ||
self.read_logs_by_session | ||
) | ||
|
||
async def add_log( | ||
self, log: schemas.LogDefaultCreate, db: Session = Depends(get_db) | ||
): | ||
return crud.add_log(db=db, log=log) | ||
|
||
async def read_logs( | ||
self, skip: int = 0, limit: int = 1000, db: Session = Depends(get_db) | ||
): | ||
logs = crud.get_logs(db, skip=skip, limit=limit) | ||
return logs | ||
|
||
async def read_logs_by_session( | ||
self, | ||
session_id: str, | ||
skip: int = 0, | ||
limit: int = 1000, | ||
db: Session = Depends(get_db), | ||
): | ||
logs = crud.get_logs_by_session( | ||
db, session_id=session_id, skip=skip, limit=limit | ||
) | ||
return logs |
3 changes: 1 addition & 2 deletions
3
llmstudio/tracking/models.py → llmstudio/tracking/logs/models.py
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
File renamed without changes.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case, we might need more methods: