replyflow-rag / rag /generator.py
Qurrah's picture
REFINE: replace print statements with logger.debug for usage tracking in RAG answer generation
f868509
Raw History Blame Contribute Delete
2.06 kB
import logging
from clients.groq import client
from core.config import settings
from rag.prompt_loader import load_prompt
logger = logging.getLogger(__name__)
GROQ_MODEL = settings.groq_model
def generate_rag_answer(query: str, chunks: list[dict], brand_name: str) -> str:
if not chunks:
return "I'm sorry, I don't have enough information to answer that. Please contact our support team for assistance."
context = "\n\n".join(
f"[{c['source_type'].upper()}]: {c['content']}"
for c in chunks
)
system_prompt = load_prompt("rag").format(brand_name=brand_name, context=context)
response = client.chat.completions.create(
model=GROQ_MODEL,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": query},
],
temperature=0.1,
max_tokens=256,
)
logger.debug("RAG answer usage | %s", response.usage)
return response.choices[0].message.content.strip()
def generate_reply_from_contexts(query: str, rag_context: str, inventory_context: str, brand_name: str) -> str:
if not rag_context and not inventory_context:
return "I'm sorry, I don't have enough information to answer that. Please contact our support team for assistance."
system_prompt = load_prompt("reply").format(
brand_name=brand_name,
rag_context=rag_context if rag_context else "No policy or FAQ information available.",
inventory_context=inventory_context if inventory_context else "No inventory information available.",
)
response = client.chat.completions.create(
model=GROQ_MODEL,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": query},
],
temperature=0.1,
max_tokens=300,
)
logger.debug("Combined reply usage | %s", response.usage)
return response.choices[0].message.content.strip()
generate_answer = generate_rag_answer
generate_combined_answer = generate_reply_from_contexts