Skip to content
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

Fix for backend statistics and dependency lifecycle #152

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.282
rev: v0.7.1
hooks:
- id: ruff
29 changes: 15 additions & 14 deletions app/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from fastapi import FastAPI, Form, Header, HTTPException, UploadFile
from fastapi.responses import (
FileResponse,
RedirectResponse,
StreamingResponse,
)
Expand All @@ -25,7 +24,6 @@
from core.types.countresult import CountResult
from core.types.CountTokenRequest import CountTokenRequest
from core.types.SimplyRequest import SimplyRequest
from core.types.StatisticsResponse import StatisticsResponse
from core.types.SummarizeResult import SummarizeResult
from core.types.SumRequest import SumRequest
from init_app import initApp
Expand Down Expand Up @@ -53,9 +51,9 @@ async def handleAuthError(request, exc: AuthError):
@api_app.post("/sum")
async def sum(
body: str = Form(...),
file: UploadFile = None,
file: UploadFile = None,
id_token: str = Header(None, alias= "X-Ms-Token-Lhmsso-Id-Token"),
access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Access-Token")
access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Access-Token")
) -> SummarizeResult:
cfg = get_config_and_authentificate(access_token=access_token)
department = get_department(id_token=id_token)
Expand Down Expand Up @@ -104,7 +102,7 @@ async def brainstorm(request: BrainstormRequest,
else "Exception in brainstorm: something bad happened"
)
raise HTTPException(status_code=500,detail=msg)

@api_app.post("/simply")
async def simply(request: SimplyRequest,
id_token: str = Header(None, alias= "X-Ms-Token-Lhmsso-Id-Token"),
Expand Down Expand Up @@ -184,7 +182,7 @@ async def chat(request: ChatRequest,
async def getConfig(access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Access-Token")) -> ConfigResponse:
cfg = get_config_and_authentificate(access_token)
response = ConfigResponse(frontend=cfg["configuration_features"].frontend, version=cfg["configuration_features"].version)

models = cast(
List[ModelsConfig], cfg["configuration_features"].backend.models
)
Expand All @@ -200,15 +198,16 @@ async def getConfig(access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Ac


@api_app.get("/statistics")
async def getStatistics(access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Access-Token")) -> StatisticsResponse:
async def getStatistics(access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Access-Token")):
cfg = get_config_and_authentificate(access_token)
repo = cfg["repository"]
if(repo is None):
raise HTTPException(status_code=501, detail="No database for logging statistics configured")
else:
try:
reponse = StatisticsResponse(sum=float(repo.sumByDepartment()), avg=float(repo.avgByDepartment()))
return reponse
try:
sum= repo.sumByDepartment()
avg = repo.avgByDepartment()
return {"sum": sum, "avg": avg}
except Exception as e:
logging.exception(str(e))
raise HTTPException(status_code=500, detail="Get Statistics failed!")
Expand All @@ -226,21 +225,23 @@ async def counttokens(request: CountTokenRequest, access_token: str = Header(Non
logging.exception(str(e))
raise HTTPException(status_code=500, detail="Counttokens failed!")




@api_app.get("/statistics/export")
async def getStatisticsCSV(access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Access-Token")) -> FileResponse:
async def getStatisticsCSV(access_token: str = Header(None, alias="X-Ms-Token-Lhmsso-Access-Token")):
cfg = get_config_and_authentificate(access_token)
repo = cfg["repository"]
if(repo is None):
raise HTTPException(status_code=501, detail="No database for logging statistics configured")
try:
export = repo.export()
return FileResponse(export, filename="statistics.csv", as_attachment=True)
response = StreamingResponse(export, media_type="text/csv")
response.headers["Content-Disposition"] = "attachment; filename=statistics.csv"
return response
except Exception as e:
logging.exception(str(e))
raise HTTPException(status_code=500, detail=e)
raise HTTPException(status_code=500, detail=str(e))


@api_app.get("/health")
Expand Down
20 changes: 8 additions & 12 deletions app/backend/core/datahelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, username: str, host: str, database: str, password: str):
password=password
)
self.engine = create_engine(url)

def setup_schema(self, base):
base.metadata.create_all(self.engine)

Expand All @@ -65,7 +65,7 @@ def getAll(self):
infos_objs = session.query(Requestinfo)
results = infos_objs.all()
return results

def countByDepartment(self):
with Session(self.engine) as session:
queryResult = session.query(Requestinfo.department, func.count(Requestinfo.tokencount)).group_by(Requestinfo.department)
Expand All @@ -77,35 +77,31 @@ def sumByDepartment(self):
with Session(self.engine) as session:
queryResult = session.query(Requestinfo.department, func.sum(Requestinfo.tokencount)).group_by(Requestinfo.department)
results = queryResult.all()
results = [tuple(row) for row in results]
results = [{"department": row[0], "tokencount": float(row[1])} for row in results]
return results

def avgByDepartment(self):
with Session(self.engine) as session:
queryResult = session.query(Requestinfo.department, func.avg(Requestinfo.tokencount)).group_by(Requestinfo.department)
results = queryResult.all()
results = [tuple(row) for row in results]
results = [{"department": row[0], "tokencount": float(row[1])} for row in results]
return results

def clear(self):
with Session(self.engine) as session:
session.query(Requestinfo).delete()
session.commit()

def export(self):
memfile = io.StringIO()
outcsv = csv.writer(memfile, delimiter=',',quotechar='"', quoting = csv.QUOTE_MINIMAL)
outcsv.writerow([column.name for column in Requestinfo.__mapper__.columns])
[outcsv.writerow([getattr(curr, column.name) for column in Requestinfo.__mapper__.columns]) for curr in self.getAll()]

memfile.seek(0)
# Das StringIO-Objekt in ein BytesIO-Objekt umwandeln
memfile_bytesio = io.BytesIO(memfile.getvalue().encode())
return memfile_bytesio

return memfile

def truncate_string(self, s, length):
if len(s) > length:
return s[:length]
return s


24 changes: 12 additions & 12 deletions app/backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ dnspython==2.7.0
# email-validator
email-validator==2.2.0
# via fastapi
fastapi==0.115.2
fastapi==0.115.4
# via mucgpt (pyproject.toml)
fastapi-cli==0.0.5
# via fastapi
Expand Down Expand Up @@ -104,32 +104,32 @@ jsonschema-specifications==2024.10.1
# via
# mucgpt (pyproject.toml)
# jsonschema
langchain==0.3.3
langchain==0.3.5
# via
# mucgpt (pyproject.toml)
# langchain-community
langchain-community==0.3.2
langchain-community==0.3.3
# via mucgpt (pyproject.toml)
langchain-core==0.3.11
langchain-core==0.3.13
# via
# mucgpt (pyproject.toml)
# langchain
# langchain-community
# langchain-openai
# langchain-text-splitters
langchain-openai==0.2.2
langchain-openai==0.2.4
# via mucgpt (pyproject.toml)
langchain-text-splitters==0.3.0
# via langchain
langsmith==0.1.135
langsmith==0.1.137
# via
# mucgpt (pyproject.toml)
# langchain
# langchain-community
# langchain-core
markdown-it-py==3.0.0
# via rich
markupsafe==3.0.0
markupsafe==3.0.2
# via
# mucgpt (pyproject.toml)
# jinja2
Expand All @@ -151,7 +151,7 @@ numpy==1.26.4
# langchain
# langchain-community
# mistral-common
openai==1.51.2
openai==1.52.2
# via
# mucgpt (pyproject.toml)
# langchain-openai
Expand Down Expand Up @@ -185,7 +185,7 @@ pydantic-settings==2.5.2
# via langchain-community
pygments==2.18.0
# via rich
pypdf==5.0.1
pypdf==5.1.0
# via mucgpt (pyproject.toml)
python-dotenv==1.0.1
# via
Expand Down Expand Up @@ -217,7 +217,7 @@ requests==2.32.3
# tiktoken
requests-toolbelt==1.0.0
# via langsmith
rich==13.9.2
rich==13.9.3
# via
# mucgpt (pyproject.toml)
# typer
Expand All @@ -239,7 +239,7 @@ sqlalchemy==2.0.36
# mucgpt (pyproject.toml)
# langchain
# langchain-community
starlette==0.40.0
starlette==0.41.2
# via
# mucgpt (pyproject.toml)
# fastapi
Expand Down Expand Up @@ -281,7 +281,7 @@ watchfiles==0.24.0
# via uvicorn
websockets==13.1
# via uvicorn
werkzeug==3.0.4
werkzeug==3.0.6
# via mucgpt (pyproject.toml)
yarl==1.15.4
# via
Expand Down
40 changes: 20 additions & 20 deletions app/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions app/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"dependencies": {
"@fluentui/react": "^8.120.10",
"@fluentui/react-components": "^9.55.1",
"@fluentui/react-icons": "^2.0.261",
"@fluentui/react-icons": "^2.0.263",
"@react-spring/web": "^9.7.3",
"dompurify": "^3.1.7",
"i18next": "^23.16.0",
"i18next": "^23.16.4",
"markmap-common": "^0.17.1",
"markmap-lib": "^0.17.2",
"markmap-toolbar": "^0.17.2",
Expand All @@ -28,7 +28,7 @@
"ndjson-readablestream": "^1.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-i18next": "^15.0.3",
"react-i18next": "^15.1.0",
"react-markdown": "^9.0.1",
"react-router-dom": "^6.27.0",
"react-syntax-highlighter": "^15.6.1",
Expand All @@ -41,9 +41,9 @@
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"@types/react-syntax-highlighter": "^15.5.13",
"@vitejs/plugin-react": "^4.3.2",
"@vitejs/plugin-react": "^4.3.3",
"prettier": "^3.3.2",
"typescript": "^5.6.3",
"vite": "^5.4.9"
"vite": "^5.4.10"
}
}
Loading
Loading