raghavNCI
commited on
Commit
·
ed08d36
1
Parent(s):
8114df9
changes v15
Browse files- question.py +24 -22
question.py
CHANGED
@@ -33,7 +33,6 @@ def extract_last_keywords(raw: str, max_keywords=6):
|
|
33 |
return parts
|
34 |
return []
|
35 |
|
36 |
-
|
37 |
def mistral_generate(prompt: str, max_new_tokens=128):
|
38 |
payload = {
|
39 |
"inputs": prompt,
|
@@ -50,22 +49,32 @@ def mistral_generate(prompt: str, max_new_tokens=128):
|
|
50 |
return result[0].get("generated_text", "").strip()
|
51 |
else:
|
52 |
return ""
|
53 |
-
except Exception
|
54 |
return ""
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
@askMe.post("/ask")
|
57 |
async def ask_question(input: QuestionInput):
|
58 |
question = input.question
|
59 |
|
60 |
-
#
|
61 |
keyword_prompt = (
|
62 |
f"Extract the 3–6 most important keywords from the following question. "
|
63 |
f"Return only the keywords, comma-separated (no explanations):\n\n"
|
64 |
f"{question}"
|
65 |
)
|
66 |
-
|
67 |
raw_keywords = mistral_generate(keyword_prompt, max_new_tokens=32)
|
68 |
-
|
69 |
keywords = extract_last_keywords(raw_keywords)
|
70 |
|
71 |
print("Raw extracted keywords:", keywords)
|
@@ -73,25 +82,18 @@ async def ask_question(input: QuestionInput):
|
|
73 |
if not keywords:
|
74 |
return {"error": "Keyword extraction failed."}
|
75 |
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
gnews_url = f"https://gnews.io/api/v4/search?q={encoded_query}&lang=en&max=3&expand=content&token={GNEWS_API_KEY}"
|
81 |
-
|
82 |
-
print("GNews URL:", gnews_url)
|
83 |
-
|
84 |
-
try:
|
85 |
-
response = requests.get(gnews_url, timeout=10)
|
86 |
-
response.raise_for_status()
|
87 |
-
articles = response.json().get("articles", [])
|
88 |
-
except Exception as e:
|
89 |
-
return {"error": f"GNews API error: {str(e)}"}
|
90 |
|
91 |
print("Fetched articles:", articles)
|
92 |
|
93 |
context = "\n\n".join([
|
94 |
-
article.get("content") or ""
|
95 |
for article in articles
|
96 |
])[:1500]
|
97 |
|
@@ -102,12 +104,12 @@ async def ask_question(input: QuestionInput):
|
|
102 |
"sources": []
|
103 |
}
|
104 |
|
105 |
-
#
|
106 |
answer_prompt = (
|
107 |
-
f"<s>[INST] Use the context below to answer the question.
|
|
|
108 |
f"Context:\n{context}\n\nQuestion: {question} [/INST]"
|
109 |
)
|
110 |
-
|
111 |
answer = mistral_generate(answer_prompt, max_new_tokens=256)
|
112 |
if not answer:
|
113 |
answer = "Cannot answer – model did not return a valid response."
|
|
|
33 |
return parts
|
34 |
return []
|
35 |
|
|
|
36 |
def mistral_generate(prompt: str, max_new_tokens=128):
|
37 |
payload = {
|
38 |
"inputs": prompt,
|
|
|
49 |
return result[0].get("generated_text", "").strip()
|
50 |
else:
|
51 |
return ""
|
52 |
+
except Exception:
|
53 |
return ""
|
54 |
|
55 |
+
def fetch_gnews_articles(query: str) -> List[dict]:
|
56 |
+
encoded_query = quote(query)
|
57 |
+
gnews_url = f"https://gnews.io/api/v4/search?q={encoded_query}&lang=en&max=5&expand=content&token={GNEWS_API_KEY}"
|
58 |
+
print("GNews URL:", gnews_url)
|
59 |
+
try:
|
60 |
+
response = requests.get(gnews_url, timeout=10)
|
61 |
+
response.raise_for_status()
|
62 |
+
return response.json().get("articles", [])
|
63 |
+
except Exception as e:
|
64 |
+
print("GNews API error:", str(e))
|
65 |
+
return []
|
66 |
+
|
67 |
@askMe.post("/ask")
|
68 |
async def ask_question(input: QuestionInput):
|
69 |
question = input.question
|
70 |
|
71 |
+
# Step 1: Ask Mistral to extract keywords
|
72 |
keyword_prompt = (
|
73 |
f"Extract the 3–6 most important keywords from the following question. "
|
74 |
f"Return only the keywords, comma-separated (no explanations):\n\n"
|
75 |
f"{question}"
|
76 |
)
|
|
|
77 |
raw_keywords = mistral_generate(keyword_prompt, max_new_tokens=32)
|
|
|
78 |
keywords = extract_last_keywords(raw_keywords)
|
79 |
|
80 |
print("Raw extracted keywords:", keywords)
|
|
|
82 |
if not keywords:
|
83 |
return {"error": "Keyword extraction failed."}
|
84 |
|
85 |
+
# Step 2: Fetch articles using AND, then fallback to OR
|
86 |
+
query_and = " AND ".join(f'"{kw}"' for kw in keywords)
|
87 |
+
articles = fetch_gnews_articles(query_and)
|
88 |
|
89 |
+
if not articles:
|
90 |
+
query_or = " OR ".join(f'"{kw}"' for kw in keywords)
|
91 |
+
articles = fetch_gnews_articles(query_or)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
print("Fetched articles:", articles)
|
94 |
|
95 |
context = "\n\n".join([
|
96 |
+
article.get("content") or article.get("description") or ""
|
97 |
for article in articles
|
98 |
])[:1500]
|
99 |
|
|
|
104 |
"sources": []
|
105 |
}
|
106 |
|
107 |
+
# Step 3: Ask Mistral to answer using the context
|
108 |
answer_prompt = (
|
109 |
+
f"<s>[INST] Use the context below to answer the question. "
|
110 |
+
f"If not enough information is available, say 'Cannot answer'.\n\n"
|
111 |
f"Context:\n{context}\n\nQuestion: {question} [/INST]"
|
112 |
)
|
|
|
113 |
answer = mistral_generate(answer_prompt, max_new_tokens=256)
|
114 |
if not answer:
|
115 |
answer = "Cannot answer – model did not return a valid response."
|