Spaces:
Sleeping
Sleeping
Download rag/generator.py from Qurrah/replyflow-rag: direct link, hf CLI and curl.
- Browser
- Download file 2.06 kB
-
https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/rag/generator.py
- Command line
-
hf download hf://spaces/Qurrah/replyflow-rag/rag/generator.py
-
curl -L -o generator.py https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/rag/generator.py
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 |