Spaces:
Sleeping

thabsheeeeeer commited on
Commit
73f057f
·
verified ·
1 Parent(s): f091d9b

Update app/services/rag_service.py

Browse files
Files changed (1) hide show
  1. app/services/rag_service.py +5 -3
app/services/rag_service.py CHANGED
@@ -198,6 +198,7 @@ def stream_groq(query: str, system_message: str, chat_history: list, user_id: in
198
 
199
  response.raise_for_status()
200
  response_time = time.time() - start_time
 
201
 
202
  for line in response.iter_lines():
203
  if not line:
@@ -211,8 +212,8 @@ def stream_groq(query: str, system_message: str, chat_history: list, user_id: in
211
 
212
  chunk = json.loads(data)
213
 
214
- # Handle usage data in stream
215
- if user_id and "usage" in chunk and chunk["usage"]:
216
  usage = chunk["usage"]
217
  log_llm_usage(
218
  user_id=user_id,
@@ -221,8 +222,9 @@ def stream_groq(query: str, system_message: str, chat_history: list, user_id: in
221
  completion_tokens=usage.get("completion_tokens", 0),
222
  response_time=response_time
223
  )
 
224
 
225
- if not chunk["choices"]:
226
  continue
227
 
228
  delta = chunk["choices"][0]["delta"].get("content")
 
198
 
199
  response.raise_for_status()
200
  response_time = time.time() - start_time
201
+ logged_usage = False
202
 
203
  for line in response.iter_lines():
204
  if not line:
 
212
 
213
  chunk = json.loads(data)
214
 
215
+ # Log usage only ONCE per stream
216
+ if user_id and not logged_usage and "usage" in chunk and chunk["usage"]:
217
  usage = chunk["usage"]
218
  log_llm_usage(
219
  user_id=user_id,
 
222
  completion_tokens=usage.get("completion_tokens", 0),
223
  response_time=response_time
224
  )
225
+ logged_usage = True
226
 
227
+ if not chunk.get("choices"):
228
  continue
229
 
230
  delta = chunk["choices"][0]["delta"].get("content")