PadmasaliGovardhan commited on
Commit
bba9407
·
1 Parent(s): f1e8605

api update commit final

Browse files
Files changed (1) hide show
  1. app/rag_app.py +48 -47
app/rag_app.py CHANGED
@@ -1,96 +1,97 @@
1
-
2
  # app/rag_app.py
3
  import os
4
- from groq import Groq
5
  import httpx
 
6
  from .embeddings import EmbeddingManager
7
  from .store import VectorStore
8
 
 
9
  class RAGApp:
10
  def __init__(self):
11
  self.embedder = None
12
  self.vectorstore = None
13
  self.client = None
14
-
15
  try:
 
16
  self.embedder = EmbeddingManager()
17
  self.vectorstore = VectorStore()
18
- api_key = os.getenv("GROQ_API_KEY")
19
- custom_http_client = httpx.Client()
20
- self.client = Groq(api_key=api_key, http_client=custom_http_client)
21
- except Exception as e:
22
- print("RAGApp init error:", e)
23
- # Optionally, re-raise to crash on startup.
24
- # raise RuntimeError(f"RAGApp init failed: {e}")
25
 
26
- # ...rest unchanged...
 
 
 
 
27
 
 
 
 
 
 
 
 
 
28
 
 
 
 
29
 
 
30
  def add_notes(self, text):
31
- chunks = [text[i:i+1000] for i in range(0, len(text), 800)]
32
  embeddings = self.embedder.generate_embeddings(chunks)
33
  self.vectorstore.add_documents(chunks, embeddings)
34
  return len(chunks)
35
 
 
36
  def ask(self, query):
37
  try:
38
- # 1️⃣ Generate embedding for query
 
 
 
39
  q_embed = self.embedder.generate_embeddings([query])[0]
40
 
41
- # 2️⃣ Retrieve most relevant chunks from vector store
42
  docs = self.vectorstore.retrieve_similar_docs(q_embed, top_k=3)
43
- context = "\n\n".join(docs)
44
 
45
- # 3️⃣ Prepare the system and user prompts
46
  messages = [
47
  {
48
  "role": "system",
49
  "content": (
50
- "You are a world-class engineering tutor specializing in Electronics, Embedded Systems, and Programming. "
51
- "Your teaching style dynamically adapts based on the student's question type.\n\n"
52
- "### 🧩 Behavior Rules:\n"
53
- "1️⃣ If the question is conceptual, explain step-by-step with analogies and real-world relevance.\n"
54
- "2️⃣ If the question involves code, analyze, fix, and explain why the fix works.\n"
55
- "3️⃣ If hardware-related, combine theory with hardware behavior and signals.\n"
56
- "4️⃣ If theory from uploaded notes, summarize and add context from real-world applications.\n\n"
57
- "### 🧠 Response Structure:\n"
58
- "1. Motivation / Why It Matters\n"
59
- "2. Concept Breakdown / Explanation\n"
60
- "3. Analogy\n"
61
- "4. Code or Example\n"
62
- "5. Practical Insight\n"
63
- "6. Common Mistakes + Tips\n\n"
64
- "### ✨ Style Guidelines:\n"
65
- "- Use bold keywords and emojis, and Markdown for structure.\n"
66
- "- Be friendly yet technically precise.\n"
67
- "- Never say 'as an AI model'.\n"
68
- "- If context from notes is relevant, integrate it smoothly.\n\n"
69
- "Your goal: help the student truly understand the concept."
70
  ),
71
  },
72
  {
73
  "role": "user",
74
- "content": f"Context:\n{context}\n\nQuestion: {query}\nAnswer:",
75
  },
76
  ]
77
 
78
- # 4️⃣ Call Groq API
79
  completion = self.client.chat.completions.create(
80
- model="openai/gpt-oss-20b",
81
  messages=messages,
82
- temperature=0.3,
83
  max_tokens=800,
84
- top_p=1
85
  )
86
 
87
- # 5️⃣ Extract response
88
- for chunk in completion:
89
- key, value = chunk
90
- if key == 'choices':
91
- return value[0].message.content.strip()
92
 
93
- return "No valid response from model."
94
 
95
  except Exception as e:
96
  print("❌ Error in ask():", e)
 
 
1
  # app/rag_app.py
2
  import os
 
3
  import httpx
4
+ from openai import OpenAI
5
  from .embeddings import EmbeddingManager
6
  from .store import VectorStore
7
 
8
+
9
  class RAGApp:
10
  def __init__(self):
11
  self.embedder = None
12
  self.vectorstore = None
13
  self.client = None
14
+
15
  try:
16
+ # Initialize embedder and FAISS vectorstore
17
  self.embedder = EmbeddingManager()
18
  self.vectorstore = VectorStore()
 
 
 
 
 
 
 
19
 
20
+ # Load Hugging Face token
21
+
22
+ api_key = os.getenv("HF_TOKEN")
23
+ if not api_key:
24
+ raise ValueError("HF_TOKEN not found in environment variables")
25
 
26
+ # Use Hugging Face OpenAI-compatible router
27
+ self.client = OpenAI(
28
+ base_url="https://router.huggingface.co/v1",
29
+ api_key=api_key,
30
+ http_client=httpx.Client(timeout=60.0),
31
+ )
32
+
33
+ print("✅ RAGApp initialized successfully with Hugging Face router.")
34
 
35
+ except Exception as e:
36
+ print("❌ RAGApp init error:", e)
37
+ self.client = None
38
 
39
+ # 🧠 Add notes and store embeddings
40
  def add_notes(self, text):
41
+ chunks = [text[i:i + 1000] for i in range(0, len(text), 800)]
42
  embeddings = self.embedder.generate_embeddings(chunks)
43
  self.vectorstore.add_documents(chunks, embeddings)
44
  return len(chunks)
45
 
46
+ # 💬 Query the system
47
  def ask(self, query):
48
  try:
49
+ if not self.client:
50
+ return "Error: API client not initialized."
51
+
52
+ # 1️⃣ Create embedding for query
53
  q_embed = self.embedder.generate_embeddings([query])[0]
54
 
55
+ # 2️⃣ Retrieve similar chunks
56
  docs = self.vectorstore.retrieve_similar_docs(q_embed, top_k=3)
57
+ context = "\n\n".join(docs) if docs else "No context found in notes."
58
 
59
+ # 3️⃣ Prepare the conversation
60
  messages = [
61
  {
62
  "role": "system",
63
  "content": (
64
+ "You are a world-class engineering tutor specializing in Electronics, Embedded Systems, and Programming.\n"
65
+ "Your responses must be clear, technically accurate, and engaging.\n\n"
66
+ "### Behavior:\n"
67
+ "1. If the question is conceptual explain with clarity and real-world relevance.\n"
68
+ "2. If it involves code analyze, correct, and explain fixes.\n"
69
+ "3. If hardware-related explain theory + circuit/signal behavior.\n"
70
+ "4. If theory-based from uploaded notes connect with practical examples.\n\n"
71
+ "### Output Style:\n"
72
+ "- Use Markdown.\n"
73
+ "- Highlight key terms with bold text.\n"
74
+ "- Use emojis and structured headings for readability.\n"
75
+ "- Avoid phrases like 'as an AI model'."
 
 
 
 
 
 
 
 
76
  ),
77
  },
78
  {
79
  "role": "user",
80
+ "content": f"Context:\n{context}\n\nQuestion: {query}\nAnswer clearly and in detail below:",
81
  },
82
  ]
83
 
84
+ # 4️⃣ Call Hugging Face model via router
85
  completion = self.client.chat.completions.create(
86
+ model="openai/gpt-oss-20b", # ✅ correct model route
87
  messages=messages,
88
+ temperature=0.4,
89
  max_tokens=800,
 
90
  )
91
 
92
+ # 5️⃣ Return the model's response
93
+ return completion.choices[0].message.content
 
 
 
94
 
 
95
 
96
  except Exception as e:
97
  print("❌ Error in ask():", e)