Initial commit
This commit is contained in:
0
app/utils/__init__.py
Normal file
0
app/utils/__init__.py
Normal file
36
app/utils/chunking.py
Normal file
36
app/utils/chunking.py
Normal file
@@ -0,0 +1,36 @@
|
||||
def chunk_text(
|
||||
text: str,
|
||||
chunk_size: int = 512,
|
||||
overlap: int = 50,
|
||||
) -> list[dict]:
|
||||
"""Text in ueberlappende Chunks aufteilen"""
|
||||
chunks = []
|
||||
start = 0
|
||||
index = 0
|
||||
|
||||
while start < len(text):
|
||||
end = start + chunk_size
|
||||
chunk = text[start:end]
|
||||
|
||||
if end < len(text):
|
||||
last_period = max(
|
||||
chunk.rfind(". "),
|
||||
chunk.rfind(".\n"),
|
||||
chunk.rfind("! "),
|
||||
chunk.rfind("? "),
|
||||
)
|
||||
if last_period > chunk_size // 2:
|
||||
end = start + last_period + 1
|
||||
chunk = text[start:end]
|
||||
|
||||
if chunk.strip():
|
||||
chunks.append({
|
||||
"text": chunk.strip(),
|
||||
"index": index,
|
||||
"start": start,
|
||||
})
|
||||
|
||||
start = end - overlap
|
||||
index += 1
|
||||
|
||||
return chunks
|
||||
24
app/utils/stats.py
Normal file
24
app/utils/stats.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import time
|
||||
import logging
|
||||
from typing import Optional
|
||||
from app.database import pool
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
async def track_usage(
|
||||
user_id: str,
|
||||
action: str,
|
||||
store_id: Optional[str] = None,
|
||||
tokens: int = 0,
|
||||
duration: float = 0
|
||||
):
|
||||
try:
|
||||
async with pool.acquire() as conn:
|
||||
await conn.execute(
|
||||
"""INSERT INTO usage_stats
|
||||
(user_id, store_id, action, tokens, duration)
|
||||
VALUES ($1, $2, $3, $4, $5)""",
|
||||
user_id, store_id, action, tokens, round(duration, 3)
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Tracking Fehler: {e}")
|
||||
Reference in New Issue
Block a user