File size: 3,131 Bytes
2db7cb0 6fea6fd 2db7cb0 844c17d 983ded9 2db7cb0 844c17d 2db7cb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import requests
from src.retrieval.query import retrieve
import os
from dotenv import load_dotenv
load_dotenv()
def build_prompt(query, context):
return f"""You are a helpful assistant. Answer using ONLY the context below. Do NOT use outside knowledge.
### Context
{context}
### Question
{query}
### Instructions
- Start directly with the answer. No introduction.
- Use only information from the context.
- Do NOT invent or modify commands beyond fixing formatting.
---
### Structure
- If the question is conceptual or descriptive (e.g. "what is X", "explain X") β
explain fully using all relevant information from the context. Cover features,
use cases, and design principles if present. Do NOT use methods or numbered steps.
- If there is ONE clear workflow β use numbered steps.
- Only treat something as a separate method if the context explicitly presents it as a distinct approach to solving the same task.
- Do NOT create methods from examples, helper classes, or unrelated concepts.
- If the context describes only ONE workflow or approach, DO NOT create multiple methods.
- In that case, use numbered steps instead.
- If there are MULTIPLE valid ways to perform the task:
β group them into separate sections using:
### Method 1 - Name
### Method 2 - Name
- Each method must be self-contained.
- Do NOT mix commands from different methods.
- Do NOT merge all methods into one numbered list.
---
### Code Rules
- All commands MUST be inside fenced code blocks.
- Use:
- ```bash for shell commands
- ```python for Python code
- Commands must be valid and executable.
- Fix formatting issues:
- Merge broken tokens (". env" β ".env")
- Fix paths ("source . env /bin/activate" β "source .env/bin/activate")
- Split combined commands into separate lines
- NEVER:
- put multiple commands on the same line
- leave commands outside code blocks
- use inline code blocks like ```bash command```
---
### Output Requirements
- Output must be clean, valid markdown.
- Code blocks must render correctly.
### Answer
"""
import requests
import os
def generate_answer(prompt, model="openai/gpt-oss-120b"):
response = requests.post(
"https://api.groq.com/openai/v1/chat/completions",
headers={
"Authorization": f"Bearer {os.environ.get('GROQ_API_KEY','').strip()}",
"Content-Type": "application/json"
},
json={
"model": model,
"max_tokens": 2048,
"messages": [
{"role": "user", "content": prompt}
],
"temperature": 0
}
)
data = response.json()
print("GROQ RESPONSE:", data)
return response.json()["choices"][0]["message"]["content"]
def build_context(docs, metas, scores):
context = ""
for i, (doc, meta, score) in enumerate(zip(docs, metas, scores)):
context += f"""
[Source {i+1} | Score: {round(score, 3)} | {meta.get('url', 'N/A')}]
Title: {meta.get("title", "N/A")}
{doc}
{"-"*50}
"""
return context
|