Spaces:
Sleeping
Sleeping
Update app/services/rag_service.py
Browse files- app/services/rag_service.py +46 -5
app/services/rag_service.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import httpx
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from app.services.vector_service import get_document_chunks, search_chunks
|
|
|
|
| 6 |
|
| 7 |
load_dotenv()
|
| 8 |
|
|
@@ -126,9 +128,11 @@ def build_messages(query: str, system_message: str, chat_history: list):
|
|
| 126 |
return messages
|
| 127 |
|
| 128 |
|
| 129 |
-
def call_groq(query: str, system_message: str, chat_history: list) -> str:
|
| 130 |
try:
|
| 131 |
messages = build_messages(query, system_message, chat_history)
|
|
|
|
|
|
|
| 132 |
response = httpx.post(
|
| 133 |
"https://api.groq.com/openai/v1/chat/completions",
|
| 134 |
headers={
|
|
@@ -142,9 +146,23 @@ def call_groq(query: str, system_message: str, chat_history: list) -> str:
|
|
| 142 |
},
|
| 143 |
timeout=30,
|
| 144 |
)
|
|
|
|
| 145 |
|
| 146 |
response.raise_for_status()
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
except httpx.TimeoutException:
|
| 150 |
raise Exception("Groq API timeout. Please try again.")
|
|
@@ -156,9 +174,11 @@ def call_groq(query: str, system_message: str, chat_history: list) -> str:
|
|
| 156 |
raise Exception(f"Groq request failed: {str(e)}")
|
| 157 |
|
| 158 |
|
| 159 |
-
def stream_groq(query: str, system_message: str, chat_history: list):
|
| 160 |
try:
|
| 161 |
messages = build_messages(query, system_message, chat_history)
|
|
|
|
|
|
|
| 162 |
with httpx.stream(
|
| 163 |
"POST",
|
| 164 |
"https://api.groq.com/openai/v1/chat/completions",
|
|
@@ -171,11 +191,13 @@ def stream_groq(query: str, system_message: str, chat_history: list):
|
|
| 171 |
"messages": messages,
|
| 172 |
"temperature": 0.1,
|
| 173 |
"stream": True,
|
|
|
|
| 174 |
},
|
| 175 |
timeout=60,
|
| 176 |
) as response:
|
| 177 |
|
| 178 |
response.raise_for_status()
|
|
|
|
| 179 |
|
| 180 |
for line in response.iter_lines():
|
| 181 |
if not line:
|
|
@@ -188,6 +210,21 @@ def stream_groq(query: str, system_message: str, chat_history: list):
|
|
| 188 |
break
|
| 189 |
|
| 190 |
chunk = json.loads(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
delta = chunk["choices"][0]["delta"].get("content")
|
| 192 |
|
| 193 |
if delta:
|
|
@@ -220,7 +257,9 @@ def ask_question(query: str, user_id: str, document_id: str, chat_history: list
|
|
| 220 |
}
|
| 221 |
|
| 222 |
q, system_message = build_prompt(query, docs)
|
| 223 |
-
|
|
|
|
|
|
|
| 224 |
|
| 225 |
return {
|
| 226 |
"answer": answer,
|
|
@@ -251,8 +290,10 @@ def stream_answer_events(query: str, user_id: str, document_id: str, chat_histor
|
|
| 251 |
return
|
| 252 |
|
| 253 |
q, system_message = build_prompt(query, docs)
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
for token in stream_groq(q, system_message, chat_history):
|
| 256 |
yield {
|
| 257 |
"type": "delta",
|
| 258 |
"content": token,
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import httpx
|
| 4 |
+
import time
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from app.services.vector_service import get_document_chunks, search_chunks
|
| 7 |
+
from app.utils.logging import log_llm_usage
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 128 |
return messages
|
| 129 |
|
| 130 |
|
| 131 |
+
def call_groq(query: str, system_message: str, chat_history: list, user_id: int = None) -> str:
|
| 132 |
try:
|
| 133 |
messages = build_messages(query, system_message, chat_history)
|
| 134 |
+
|
| 135 |
+
start_time = time.time()
|
| 136 |
response = httpx.post(
|
| 137 |
"https://api.groq.com/openai/v1/chat/completions",
|
| 138 |
headers={
|
|
|
|
| 146 |
},
|
| 147 |
timeout=30,
|
| 148 |
)
|
| 149 |
+
response_time = time.time() - start_time
|
| 150 |
|
| 151 |
response.raise_for_status()
|
| 152 |
+
res_json = response.json()
|
| 153 |
+
|
| 154 |
+
# Log usage if user_id is provided
|
| 155 |
+
if user_id and "usage" in res_json:
|
| 156 |
+
usage = res_json["usage"]
|
| 157 |
+
log_llm_usage(
|
| 158 |
+
user_id=user_id,
|
| 159 |
+
model="llama-3.3-70b-versatile",
|
| 160 |
+
prompt_tokens=usage.get("prompt_tokens", 0),
|
| 161 |
+
completion_tokens=usage.get("completion_tokens", 0),
|
| 162 |
+
response_time=response_time
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
return res_json["choices"][0]["message"]["content"]
|
| 166 |
|
| 167 |
except httpx.TimeoutException:
|
| 168 |
raise Exception("Groq API timeout. Please try again.")
|
|
|
|
| 174 |
raise Exception(f"Groq request failed: {str(e)}")
|
| 175 |
|
| 176 |
|
| 177 |
+
def stream_groq(query: str, system_message: str, chat_history: list, user_id: int = None):
|
| 178 |
try:
|
| 179 |
messages = build_messages(query, system_message, chat_history)
|
| 180 |
+
start_time = time.time()
|
| 181 |
+
|
| 182 |
with httpx.stream(
|
| 183 |
"POST",
|
| 184 |
"https://api.groq.com/openai/v1/chat/completions",
|
|
|
|
| 191 |
"messages": messages,
|
| 192 |
"temperature": 0.1,
|
| 193 |
"stream": True,
|
| 194 |
+
"stream_options": {"include_usage": True}
|
| 195 |
},
|
| 196 |
timeout=60,
|
| 197 |
) as response:
|
| 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:
|
|
|
|
| 210 |
break
|
| 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,
|
| 219 |
+
model="llama-3.3-70b-versatile",
|
| 220 |
+
prompt_tokens=usage.get("prompt_tokens", 0),
|
| 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")
|
| 229 |
|
| 230 |
if delta:
|
|
|
|
| 257 |
}
|
| 258 |
|
| 259 |
q, system_message = build_prompt(query, docs)
|
| 260 |
+
# Convert user_id string to int for logging
|
| 261 |
+
uid = int(user_id) if user_id and user_id.isdigit() else None
|
| 262 |
+
answer = call_groq(q, system_message, chat_history, user_id=uid)
|
| 263 |
|
| 264 |
return {
|
| 265 |
"answer": answer,
|
|
|
|
| 290 |
return
|
| 291 |
|
| 292 |
q, system_message = build_prompt(query, docs)
|
| 293 |
+
# Convert user_id string to int for logging
|
| 294 |
+
uid = int(user_id) if user_id and user_id.isdigit() else None
|
| 295 |
|
| 296 |
+
for token in stream_groq(q, system_message, chat_history, user_id=uid):
|
| 297 |
yield {
|
| 298 |
"type": "delta",
|
| 299 |
"content": token,
|