Added extra logging
This commit is contained in:
parent
6d85fd4341
commit
95fc6dccf8
2 changed files with 15 additions and 5 deletions
|
|
@ -36,7 +36,6 @@ def _extract_text_from_training_file(file_obj: TrainingFile) -> str:
|
||||||
return _decode_text_bytes(raw_bytes).strip()
|
return _decode_text_bytes(raw_bytes).strip()
|
||||||
|
|
||||||
def _get_text_chunks(text: str, size: int = 10000):
|
def _get_text_chunks(text: str, size: int = 10000):
|
||||||
"""Slices text into rough blocks to prevent HTTP timeouts."""
|
|
||||||
for i in range(0, len(text), size):
|
for i in range(0, len(text), size):
|
||||||
yield text[i:i + size]
|
yield text[i:i + size]
|
||||||
|
|
||||||
|
|
@ -50,8 +49,6 @@ def ingest_training_file_task(self, file_uuid):
|
||||||
file_obj.status = 'ingesting'
|
file_obj.status = 'ingesting'
|
||||||
file_obj.save()
|
file_obj.save()
|
||||||
|
|
||||||
target_dimensions = RoleRagDocument._meta.get_field('embedding').dimensions
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
raw_text = _extract_text_from_training_file(file_obj)
|
raw_text = _extract_text_from_training_file(file_obj)
|
||||||
if not raw_text:
|
if not raw_text:
|
||||||
|
|
@ -70,7 +67,7 @@ def ingest_training_file_task(self, file_uuid):
|
||||||
json={
|
json={
|
||||||
"text": text_segment,
|
"text": text_segment,
|
||||||
"threshold": 95,
|
"threshold": 95,
|
||||||
"target_dimensions": target_dimensions,
|
"target_dimensions": settings.EMBEDDING_DIMENSIONS,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
|
||||||
|
|
@ -77,14 +77,16 @@ async def health():
|
||||||
def _resolve_target_dimensions(payload: Dict[str, Any]) -> int:
|
def _resolve_target_dimensions(payload: Dict[str, Any]) -> int:
|
||||||
raw_target = payload.get("target_dimensions")
|
raw_target = payload.get("target_dimensions")
|
||||||
if raw_target in (None, ""):
|
if raw_target in (None, ""):
|
||||||
raise HTTPException(status_code=400, detail="'target_dimensions' is required")
|
raise HTTPException(status_code=400, detail="'target_dimensions' is required and must be a positive integer")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
target = int(raw_target)
|
target = int(raw_target)
|
||||||
except (TypeError, ValueError) as exc:
|
except (TypeError, ValueError) as exc:
|
||||||
|
logger.warning("Invalid target_dimensions value: %s", raw_target)
|
||||||
raise HTTPException(status_code=400, detail="'target_dimensions' must be an integer") from exc
|
raise HTTPException(status_code=400, detail="'target_dimensions' must be an integer") from exc
|
||||||
|
|
||||||
if target <= 0:
|
if target <= 0:
|
||||||
|
logger.warning("Non-positive target_dimensions value: %s", target)
|
||||||
raise HTTPException(status_code=400, detail="'target_dimensions' must be > 0")
|
raise HTTPException(status_code=400, detail="'target_dimensions' must be > 0")
|
||||||
|
|
||||||
return target
|
return target
|
||||||
|
|
@ -103,13 +105,18 @@ def pad_and_normalize(embeddings: Tensor, target_dimensions: int) -> Tensor:
|
||||||
async def embeddings(request: Request):
|
async def embeddings(request: Request):
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
input_data = data.get("input", "")
|
input_data = data.get("input", "")
|
||||||
|
input_kind = type(input_data).__name__
|
||||||
|
input_count = len(input_data) if isinstance(input_data, list) else (1 if isinstance(input_data, str) else 0)
|
||||||
|
logger.info("/v1/embeddings request received: input_kind=%s input_count=%s", input_kind, input_count)
|
||||||
target_dimensions = _resolve_target_dimensions(data)
|
target_dimensions = _resolve_target_dimensions(data)
|
||||||
|
logger.info("/v1/embeddings resolved target_dimensions=%s", target_dimensions)
|
||||||
|
|
||||||
if isinstance(input_data, str):
|
if isinstance(input_data, str):
|
||||||
inputs = [input_data]
|
inputs = [input_data]
|
||||||
elif isinstance(input_data, list):
|
elif isinstance(input_data, list):
|
||||||
inputs = [str(item) for item in input_data if str(item).strip()]
|
inputs = [str(item) for item in input_data if str(item).strip()]
|
||||||
else:
|
else:
|
||||||
|
logger.warning("/v1/embeddings bad input type: %s", input_kind)
|
||||||
raise HTTPException(status_code=400, detail="'input' must be a string or list of strings")
|
raise HTTPException(status_code=400, detail="'input' must be a string or list of strings")
|
||||||
|
|
||||||
if not inputs:
|
if not inputs:
|
||||||
|
|
@ -157,16 +164,22 @@ async def semantic_chunk(request: Request):
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
raw_text = data.get("text", "")
|
raw_text = data.get("text", "")
|
||||||
threshold_percentile = data.get("threshold", 95)
|
threshold_percentile = data.get("threshold", 95)
|
||||||
|
raw_text_len = len(raw_text) if isinstance(raw_text, str) else -1
|
||||||
|
logger.info("/v1/semantic-chunk request received: text_len=%s threshold=%s", raw_text_len, threshold_percentile,)
|
||||||
target_dimensions = _resolve_target_dimensions(data)
|
target_dimensions = _resolve_target_dimensions(data)
|
||||||
|
logger.info("/v1/semantic-chunk resolved target_dimensions=%s", target_dimensions)
|
||||||
|
|
||||||
if not raw_text:
|
if not raw_text:
|
||||||
|
logger.info("/v1/semantic-chunk empty text payload")
|
||||||
return {"chunks": [], "embeddings": []}
|
return {"chunks": [], "embeddings": []}
|
||||||
|
|
||||||
if len(raw_text) > 50000:
|
if len(raw_text) > 50000:
|
||||||
|
logger.warning("/v1/semantic-chunk payload too large: text_len=%s", len(raw_text))
|
||||||
raise HTTPException(status_code=413, detail="Text block too large. Please batch on the client.")
|
raise HTTPException(status_code=413, detail="Text block too large. Please batch on the client.")
|
||||||
|
|
||||||
model = state.get("embed_model")
|
model = state.get("embed_model")
|
||||||
if model is None:
|
if model is None:
|
||||||
|
logger.error("/v1/semantic-chunk embedding model not initialized")
|
||||||
raise HTTPException(status_code=503, detail="Embedding model not initialized")
|
raise HTTPException(status_code=503, detail="Embedding model not initialized")
|
||||||
|
|
||||||
sentences = [s.strip() for s in raw_text.replace('\n', ' ').split('. ') if s.strip()]
|
sentences = [s.strip() for s in raw_text.replace('\n', ' ').split('. ') if s.strip()]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue