raghavNCI commited on
Commit
2f3b9d0
·
1 Parent(s): 8573cc3

changes v11

Browse files
Files changed (1) hide show
  1. question.py +53 -41
question.py CHANGED
@@ -7,7 +7,6 @@ from typing import List
7
  from redis_client import redis_client as r
8
  from dotenv import load_dotenv
9
  from urllib.parse import quote_plus
10
- import re
11
  import json
12
 
13
  load_dotenv()
@@ -20,24 +19,54 @@ askMe = APIRouter()
20
  class QuestionInput(BaseModel):
21
  question: str
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  @askMe.post("/ask")
24
  async def ask_question(input: QuestionInput):
25
  question = input.question
26
 
27
- # Basic keyword extraction with stopword filtering
28
- STOPWORDS = {"what", "about", "which", "would", "could", "this", "that"}
29
- keywords = [
30
- kw for kw in re.findall(r"\b\w{4,}\b", question.lower())
31
- if kw not in STOPWORDS
32
- ]
33
- query_string = " OR ".join(f'"{kw}"' for kw in keywords[:7])
34
- encoded_query = quote_plus(query_string)
35
 
36
- print("Query string", encoded_query)
37
 
38
- gnews_url = f"https://gnews.io/api/v4/search?q={encoded_query}&lang=en&max=3&expand=content&token={GNEWS_API_KEY}"
 
39
 
40
- print("GNEWS URL", gnews_url)
 
 
 
 
 
 
41
 
42
  try:
43
  response = requests.get(gnews_url, timeout=10)
@@ -45,11 +74,11 @@ async def ask_question(input: QuestionInput):
45
  articles = response.json().get("articles", [])
46
  except Exception as e:
47
  return {"error": f"GNews API error: {str(e)}"}
48
-
49
- print("the articles are", articles)
50
 
51
  context = "\n\n".join([
52
- article.get("content") or article.get("description") or ""
53
  for article in articles
54
  ])[:1500]
55
 
@@ -60,34 +89,17 @@ async def ask_question(input: QuestionInput):
60
  "sources": []
61
  }
62
 
63
- # Call HF Inference API manually
64
- hf_api_url = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
65
- prompt = f"<s>[INST] Use the context below to answer the question. If not enough information is available, say 'Cannot answer'.\n\nContext:\n{context}\n\nQuestion: {question} [/INST]"
66
- headers = {
67
- "Authorization": f"Bearer {HF_TOKEN}",
68
- "Content-Type": "application/json"
69
- }
70
-
71
- payload = {
72
- "inputs": prompt,
73
- "parameters": {
74
- "max_new_tokens": 256,
75
- "temperature": 0.7
76
- }
77
- }
78
 
 
 
 
79
 
80
- try:
81
- response = requests.post(hf_api_url, headers=headers, data=json.dumps(payload), timeout=30)
82
- response.raise_for_status()
83
- hf_response = response.json()
84
- if isinstance(hf_response, list) and len(hf_response) > 0:
85
- answer = hf_response[0].get("generated_text", "").strip()
86
- else:
87
- answer = "Cannot answer – model did not return a valid response."
88
-
89
- except Exception as e:
90
- return {"error": f"Hugging Face API error: {str(e)}"}
91
 
92
  return {
93
  "question": question,
 
7
  from redis_client import redis_client as r
8
  from dotenv import load_dotenv
9
  from urllib.parse import quote_plus
 
10
  import json
11
 
12
  load_dotenv()
 
19
  class QuestionInput(BaseModel):
20
  question: str
21
 
22
+ HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
23
+ HEADERS = {
24
+ "Authorization": f"Bearer {HF_TOKEN}",
25
+ "Content-Type": "application/json"
26
+ }
27
+
28
+ def mistral_generate(prompt: str, max_new_tokens=128):
29
+ payload = {
30
+ "inputs": prompt,
31
+ "parameters": {
32
+ "max_new_tokens": max_new_tokens,
33
+ "temperature": 0.7
34
+ }
35
+ }
36
+ try:
37
+ response = requests.post(HF_API_URL, headers=HEADERS, data=json.dumps(payload), timeout=30)
38
+ response.raise_for_status()
39
+ result = response.json()
40
+ if isinstance(result, list) and len(result) > 0:
41
+ return result[0].get("generated_text", "").strip()
42
+ else:
43
+ return ""
44
+ except Exception as e:
45
+ return ""
46
+
47
  @askMe.post("/ask")
48
  async def ask_question(input: QuestionInput):
49
  question = input.question
50
 
51
+ # --- 1. Ask Mistral to extract keywords ---
52
+ keyword_prompt = (
53
+ f"<s>[INST] Extract the 3–6 most important keywords or phrases from the question below. "
54
+ f"Return only comma-separated keywords (no explanations).\n\nQuestion: {question} [/INST]"
55
+ )
56
+ raw_keywords = mistral_generate(keyword_prompt, max_new_tokens=32)
 
 
57
 
58
+ print("Raw extracted keywords:", raw_keywords)
59
 
60
+ if not raw_keywords:
61
+ return {"error": "Keyword extraction failed."}
62
 
63
+ # Clean and parse keywords
64
+ keywords = [kw.strip().strip('"') for kw in raw_keywords.split(",") if kw.strip()]
65
+ query_string = " OR ".join(f'"{kw}"' for kw in keywords)
66
+ encoded_query = quote_plus(query_string)
67
+
68
+ gnews_url = f"https://gnews.io/api/v4/search?q={encoded_query}&lang=en&max=3&expand=content&token={GNEWS_API_KEY}"
69
+ print("GNews URL:", gnews_url)
70
 
71
  try:
72
  response = requests.get(gnews_url, timeout=10)
 
74
  articles = response.json().get("articles", [])
75
  except Exception as e:
76
  return {"error": f"GNews API error: {str(e)}"}
77
+
78
+ print("Fetched articles:", articles)
79
 
80
  context = "\n\n".join([
81
+ article.get("description") or ""
82
  for article in articles
83
  ])[:1500]
84
 
 
89
  "sources": []
90
  }
91
 
92
+ # --- 2. Ask Mistral to answer the question using the context ---
93
+ answer_prompt = (
94
+ f"<s>[INST] Use the context below to answer the question. If not enough information is available, say 'Cannot answer'.\n\n"
95
+ f"Context:\n{context}\n\nQuestion: {question} [/INST]"
96
+ )
 
 
 
 
 
 
 
 
 
 
97
 
98
+ answer = mistral_generate(answer_prompt, max_new_tokens=256)
99
+ if not answer:
100
+ answer = "Cannot answer – model did not return a valid response."
101
 
102
+ print("Answer:", answer)
 
 
 
 
 
 
 
 
 
 
103
 
104
  return {
105
  "question": question,