File size: 7,305 Bytes
3fda415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""

rag_chain.py

------------

Core RAG (Retrieval-Augmented Generation) pipeline.

Loaded once at startup by main.py β€” all objects are module-level singletons.



Flow:

    User question

        β†’ embed with sentence-transformers

        β†’ query ChromaDB for top-5 similar Q&As

        β†’ build prompt with those Q&As as context

        β†’ call Groq LLaMA-3.3-70b

        β†’ return grounded answer

"""

import os
import chromadb
from chromadb.config import Settings
from sentence_transformers import SentenceTransformer
from groq import Groq
from dotenv import load_dotenv

load_dotenv()

# ─── Config ──────────────────────────────────────────────────────────────────
CHROMA_PATH   = os.getenv("CHROMA_PATH", "chroma_db")
COLLECTION    = "python_qa"
MODEL_NAME    = "all-MiniLM-L6-v2"
PRIMARY_GROQ_MODEL = os.getenv("GROQ_MODEL", "llama-3.3-70b-versatile")
GROQ_API_KEY  = os.getenv("GROQ_API_KEY")
TOP_K         = 5    # How many SO Q&As to retrieve as context

# Candidate fallback models if configured model is unavailable or deprecated
CANDIDATE_MODELS = [
    PRIMARY_GROQ_MODEL,
    "llama-3.3-70b-versatile",
    "llama-3.1-8b-instant",
    "llama-3.2-11b-vision-preview",
    "llama-3.2-90b-vision-preview",
    "llama3-70b-8192",
    "llama3-8b-8192",
    "mixtral-8x7b-32768",
    "gemma2-9b-it",
]
FALLBACK_MODELS = list(dict.fromkeys([m for m in CANDIDATE_MODELS if m]))

if not GROQ_API_KEY:
    raise EnvironmentError("❌ GROQ_API_KEY not set. Add it to your .env file.")

# ─── Load once at module import (not per-request!) ───────────────────────────
print(f"πŸ€– Loading embedding model '{MODEL_NAME}'...")
_embedder = SentenceTransformer(MODEL_NAME)

print(f"Connecting to ChromaDB at '{CHROMA_PATH}'...")
_chroma_client = chromadb.PersistentClient(
    path=CHROMA_PATH,
    settings=Settings(anonymized_telemetry=False)
)
_collection    = _chroma_client.get_collection(COLLECTION)
print(f"   βœ… {_collection.count():,} documents in index")

print(f"⚑ Initializing Groq client...")
_groq = Groq(api_key=GROQ_API_KEY)

# Dynamically discover active models if API allows
_active_models = []
try:
    models_resp = _groq.models.list()
    _active_models = [m.id for m in models_resp.data if hasattr(m, 'id')]
    print(f"   Found {len(_active_models)} available Groq models: {_active_models}")
except Exception as e:
    print(f"   ⚠️ Could not fetch Groq model list dynamically: {e}")

# Determine starting model
if PRIMARY_GROQ_MODEL in _active_models:
    GROQ_MODEL = PRIMARY_GROQ_MODEL
elif _active_models:
    preferred = [m for m in _active_models if any(k in m.lower() for k in ["llama", "mixtral", "gemma"])]
    GROQ_MODEL = preferred[0] if preferred else _active_models[0]
    print(f"   ⚠️ Model '{PRIMARY_GROQ_MODEL}' not in active list. Using '{GROQ_MODEL}'.")
else:
    GROQ_MODEL = PRIMARY_GROQ_MODEL

print(f"   βœ… Primary model set to: {GROQ_MODEL}")

# ─── System prompt ───────────────────────────────────────────────────────────
_SYSTEM_PROMPT = """You are an expert Python programming assistant powered by real Stack Overflow data.



Rules:

1. Answer ONLY based on the Stack Overflow context provided below.

2. If the context does not fully cover the question, say so briefly, then answer from your knowledge.

3. Always include working code examples in Python using markdown code blocks (```python ... ```).

4. Be concise but complete β€” avoid unnecessary padding.

5. If multiple approaches exist, list them with trade-offs.

"""

def _call_groq_with_fallback(messages: list) -> tuple[str, str]:
    """

    Calls Groq chat completion API with automatic model fallback

    if the chosen model is not found or encounters a 404 error.



    Returns: (answer_text, model_used)

    """
    global GROQ_MODEL

    models_to_try = list(dict.fromkeys([GROQ_MODEL] + _active_models + FALLBACK_MODELS))

    last_exception = None
    for model_name in models_to_try:
        try:
            response = _groq.chat.completions.create(
                model=model_name,
                messages=messages,
                temperature=0.1,
                max_tokens=1024,
            )
            answer = response.choices[0].message.content
            GROQ_MODEL = model_name
            return answer, model_name
        except Exception as e:
            err_str = str(e)
            if "404" in err_str or "model_not_found" in err_str or "does not exist" in err_str:
                print(f"⚠️ Model '{model_name}' not available on Groq (404/model_not_found). Trying fallback...")
                last_exception = e
                continue
            else:
                raise e

    raise RuntimeError(
        f"All attempted Groq models ({models_to_try}) failed. Last error: {last_exception}"
    )

# ─── Main function called by FastAPI ─────────────────────────────────────────
def get_answer(question: str) -> dict:
    """

    Parameters

    ----------

    question : str  β€” The user's Python question



    Returns

    -------

    dict with keys: question, answer, sources, model, docs_retrieved

    """
    # 1️⃣ Embed the question
    query_vec = _embedder.encode(question).tolist()

    # 2️⃣ Retrieve top-K similar Q&As from vector DB
    results = _collection.query(
        query_embeddings=[query_vec],
        n_results=TOP_K,
        include=["documents", "metadatas", "distances"],
    )

    docs      = results["documents"][0]
    metas     = results["metadatas"][0]
    distances = results["distances"][0]

    # 3️⃣ Build context block from retrieved docs
    context_parts = []
    source_titles = []
    for i, (doc, meta, dist) in enumerate(zip(docs, metas, distances), 1):
        title = meta.get("title", "Unknown")
        score = meta.get("question_score", 0)
        context_parts.append(
            f"--- Stack Overflow Result {i} ---\n"
            f"Title: {title} (votes: {score})\n"
            f"Relevance: {(1 - dist) * 100:.1f}%\n\n"
            f"{doc}"
        )
        source_titles.append(title)

    context_str = "\n\n".join(context_parts)

    # 4️⃣ Build prompt and call Groq with fallback
    user_message = (
        f"Python Question: {question}\n\n"
        f"Stack Overflow Context:\n{context_str}\n\n"
        f"Please answer the question based on the context above."
    )

    messages = [
        {"role": "system", "content": _SYSTEM_PROMPT},
        {"role": "user",   "content": user_message},
    ]

    answer, model_used = _call_groq_with_fallback(messages)

    return {
        "question":       question,
        "answer":         answer,
        "sources":        source_titles,
        "model":          model_used,
        "docs_retrieved": TOP_K,
    }