Spaces:
Runtime error
Runtime error
Commit ·
2b32e9d
1
Parent(s): b340c14
chatbot: tighten web search prompt (concise, admissions site for MS/PhD)
Browse files- chatbot.py +33 -5
- frontend/src/api/adminHttp.js +8 -5
- frontend/src/api/http.js +5 -3
- frontend/src/config/apiBase.js +28 -0
- frontend/vercel.json +5 -0
chatbot.py
CHANGED
|
@@ -458,12 +458,40 @@ Return only valid JSON."""
|
|
| 458 |
from google import genai
|
| 459 |
from google.genai import types
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
-
Question:
|
|
|
|
| 467 |
|
| 468 |
Prior conversation (may be truncated):
|
| 469 |
{history}
|
|
|
|
| 458 |
from google import genai
|
| 459 |
from google.genai import types
|
| 460 |
|
| 461 |
+
# Mirror query language: Urdu / English explicitly; other codes → same language as question.
|
| 462 |
+
lang_display = {"ur": "Urdu", "en": "English"}.get(detected_language)
|
| 463 |
+
if lang_display:
|
| 464 |
+
lang_rule = (
|
| 465 |
+
f"You MUST answer entirely in {lang_display}. Do not switch languages."
|
| 466 |
+
)
|
| 467 |
+
else:
|
| 468 |
+
lang_rule = (
|
| 469 |
+
"You MUST answer entirely in the same language as the Question above "
|
| 470 |
+
"(if the user wrote in Urdu, use Urdu; if English, use English; "
|
| 471 |
+
"otherwise mirror their language). Do not switch languages."
|
| 472 |
+
)
|
| 473 |
+
|
| 474 |
+
prompt = f"""You are the COMSATS University (Attock campus) assistant. Google Search is available — use it only for COMSATS-related facts.
|
| 475 |
+
|
| 476 |
+
ALLOWED TOPICS — Only COMSATS University (including Attock): admissions, programs, departments, BS/MS/MPhil/PhD and other degrees, fees, eligibility, deadlines, campus, academics, exams, student services, official policies, and similar university matters.
|
| 477 |
+
|
| 478 |
+
POSTGRADUATE (MS / MPhil / PhD) — Whenever the user asks about MS, MPhil, PhD, postgraduate admissions, graduate programs, thesis, or eligibility for masters/doctoral study at COMSATS, you MUST prioritize facts found via search on the official admissions portal: http://admissions.comsats.edu.pk (use search queries that surface this domain). Prefer information consistent with that site; mention key facts briefly (e.g. program names, eligibility, links) without copying long pages verbatim.
|
| 479 |
+
|
| 480 |
+
REFUSE OFF-TOPIC — Do NOT answer: hobbies, entertainment unrelated to COMSATS, drugs or substances, illegal topics, unrelated trivia, creative tasks, or personal advice unrelated to the university. Reply with ONE short polite refusal only — you only help with COMSATS-related questions. No partial off-topic content.
|
| 481 |
+
|
| 482 |
+
CONCISE OUTPUT (strict) —
|
| 483 |
+
• Answer in the fewest sentences needed; default to 2–6 short sentences unless the user explicitly asks for a list.
|
| 484 |
+
• No filler: do not start with "As an AI", "Based on my search", or restate the whole question. Jump straight to the answer.
|
| 485 |
+
• No irrelevant side topics, no long introductions or conclusions, no duplicate points.
|
| 486 |
+
• Prefer one clear fact per sentence; use short bullets only when they improve clarity (e.g. eligibility steps).
|
| 487 |
+
• If uncertain after search, say briefly that verified details were not found; do not invent policies or dates.
|
| 488 |
+
|
| 489 |
+
CORRECTNESS — State facts accurately from search results; do not guess fees, dates, or rules. If multiple campuses exist, clarify when it matters for the user.
|
| 490 |
+
|
| 491 |
+
LANGUAGE — {lang_rule}
|
| 492 |
|
| 493 |
+
Question:
|
| 494 |
+
{question}
|
| 495 |
|
| 496 |
Prior conversation (may be truncated):
|
| 497 |
{history}
|
frontend/src/api/adminHttp.js
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
| 5 |
getStoredAdmin,
|
| 6 |
setAdminSession,
|
| 7 |
} from '../auth/adminStorage'
|
|
|
|
| 8 |
|
| 9 |
async function readDetail(res) {
|
| 10 |
try {
|
|
@@ -22,7 +23,7 @@ async function readDetail(res) {
|
|
| 22 |
async function postAdminRefresh() {
|
| 23 |
const rt = getAdminRefreshToken()
|
| 24 |
if (!rt) return null
|
| 25 |
-
const res = await fetch('/admin/refresh', {
|
| 26 |
method: 'POST',
|
| 27 |
headers: { 'Content-Type': 'application/json' },
|
| 28 |
body: JSON.stringify({ refresh_token: rt }),
|
|
@@ -56,7 +57,8 @@ export async function apiAdminJson(path, options = {}) {
|
|
| 56 |
return h
|
| 57 |
}
|
| 58 |
|
| 59 |
-
|
|
|
|
| 60 |
...fetchOpts,
|
| 61 |
headers: buildHeaders(),
|
| 62 |
})
|
|
@@ -69,7 +71,7 @@ export async function apiAdminJson(path, options = {}) {
|
|
| 69 |
refresh_token: data.refresh_token,
|
| 70 |
admin: getStoredAdmin(),
|
| 71 |
})
|
| 72 |
-
res = await fetch(
|
| 73 |
...fetchOpts,
|
| 74 |
headers: buildHeaders(),
|
| 75 |
})
|
|
@@ -111,7 +113,8 @@ export async function apiAdminUpload(path, formDataFactory, options = {}) {
|
|
| 111 |
return h
|
| 112 |
}
|
| 113 |
|
| 114 |
-
|
|
|
|
| 115 |
method: 'POST',
|
| 116 |
headers: authHeaders(),
|
| 117 |
body: formDataFactory(),
|
|
@@ -125,7 +128,7 @@ export async function apiAdminUpload(path, formDataFactory, options = {}) {
|
|
| 125 |
refresh_token: data.refresh_token,
|
| 126 |
admin: getStoredAdmin(),
|
| 127 |
})
|
| 128 |
-
res = await fetch(
|
| 129 |
method: 'POST',
|
| 130 |
headers: authHeaders(),
|
| 131 |
body: formDataFactory(),
|
|
|
|
| 5 |
getStoredAdmin,
|
| 6 |
setAdminSession,
|
| 7 |
} from '../auth/adminStorage'
|
| 8 |
+
import { apiUrl } from '../config/apiBase'
|
| 9 |
|
| 10 |
async function readDetail(res) {
|
| 11 |
try {
|
|
|
|
| 23 |
async function postAdminRefresh() {
|
| 24 |
const rt = getAdminRefreshToken()
|
| 25 |
if (!rt) return null
|
| 26 |
+
const res = await fetch(apiUrl('/admin/refresh'), {
|
| 27 |
method: 'POST',
|
| 28 |
headers: { 'Content-Type': 'application/json' },
|
| 29 |
body: JSON.stringify({ refresh_token: rt }),
|
|
|
|
| 57 |
return h
|
| 58 |
}
|
| 59 |
|
| 60 |
+
const url = apiUrl(path)
|
| 61 |
+
let res = await fetch(url, {
|
| 62 |
...fetchOpts,
|
| 63 |
headers: buildHeaders(),
|
| 64 |
})
|
|
|
|
| 71 |
refresh_token: data.refresh_token,
|
| 72 |
admin: getStoredAdmin(),
|
| 73 |
})
|
| 74 |
+
res = await fetch(url, {
|
| 75 |
...fetchOpts,
|
| 76 |
headers: buildHeaders(),
|
| 77 |
})
|
|
|
|
| 113 |
return h
|
| 114 |
}
|
| 115 |
|
| 116 |
+
const uploadUrl = apiUrl(path)
|
| 117 |
+
let res = await fetch(uploadUrl, {
|
| 118 |
method: 'POST',
|
| 119 |
headers: authHeaders(),
|
| 120 |
body: formDataFactory(),
|
|
|
|
| 128 |
refresh_token: data.refresh_token,
|
| 129 |
admin: getStoredAdmin(),
|
| 130 |
})
|
| 131 |
+
res = await fetch(uploadUrl, {
|
| 132 |
method: 'POST',
|
| 133 |
headers: authHeaders(),
|
| 134 |
body: formDataFactory(),
|
frontend/src/api/http.js
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
| 5 |
getStoredUser,
|
| 6 |
setSession,
|
| 7 |
} from '../auth/storage'
|
|
|
|
| 8 |
|
| 9 |
async function readDetail(res) {
|
| 10 |
try {
|
|
@@ -22,7 +23,7 @@ async function readDetail(res) {
|
|
| 22 |
async function postRefresh() {
|
| 23 |
const rt = getRefreshToken()
|
| 24 |
if (!rt) return null
|
| 25 |
-
const res = await fetch('/users/refresh', {
|
| 26 |
method: 'POST',
|
| 27 |
headers: { 'Content-Type': 'application/json' },
|
| 28 |
body: JSON.stringify({ refresh_token: rt }),
|
|
@@ -60,7 +61,8 @@ export async function apiJson(path, options = {}) {
|
|
| 60 |
return h
|
| 61 |
}
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
...fetchOpts,
|
| 65 |
headers: buildHeaders(),
|
| 66 |
})
|
|
@@ -73,7 +75,7 @@ export async function apiJson(path, options = {}) {
|
|
| 73 |
refresh_token: data.refresh_token,
|
| 74 |
user: getStoredUser(),
|
| 75 |
})
|
| 76 |
-
res = await fetch(
|
| 77 |
...fetchOpts,
|
| 78 |
headers: buildHeaders(),
|
| 79 |
})
|
|
|
|
| 5 |
getStoredUser,
|
| 6 |
setSession,
|
| 7 |
} from '../auth/storage'
|
| 8 |
+
import { apiUrl } from '../config/apiBase'
|
| 9 |
|
| 10 |
async function readDetail(res) {
|
| 11 |
try {
|
|
|
|
| 23 |
async function postRefresh() {
|
| 24 |
const rt = getRefreshToken()
|
| 25 |
if (!rt) return null
|
| 26 |
+
const res = await fetch(apiUrl('/users/refresh'), {
|
| 27 |
method: 'POST',
|
| 28 |
headers: { 'Content-Type': 'application/json' },
|
| 29 |
body: JSON.stringify({ refresh_token: rt }),
|
|
|
|
| 61 |
return h
|
| 62 |
}
|
| 63 |
|
| 64 |
+
const url = apiUrl(path)
|
| 65 |
+
let res = await fetch(url, {
|
| 66 |
...fetchOpts,
|
| 67 |
headers: buildHeaders(),
|
| 68 |
})
|
|
|
|
| 75 |
refresh_token: data.refresh_token,
|
| 76 |
user: getStoredUser(),
|
| 77 |
})
|
| 78 |
+
res = await fetch(url, {
|
| 79 |
...fetchOpts,
|
| 80 |
headers: buildHeaders(),
|
| 81 |
})
|
frontend/src/config/apiBase.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
function stripTrailingSlash(url) {
|
| 2 |
+
return url.replace(/\/+$/, '')
|
| 3 |
+
}
|
| 4 |
+
|
| 5 |
+
/**
|
| 6 |
+
* Backend URL for production (e.g. Hugging Face Space). Override with `VITE_API_BASE`
|
| 7 |
+
* in Vercel Project → Environment Variables if needed.
|
| 8 |
+
* In development, empty base keeps `/users`, `/response`, … as relative URLs → Vite proxy.
|
| 9 |
+
*/
|
| 10 |
+
export function getApiBase() {
|
| 11 |
+
const fromEnv = import.meta.env.VITE_API_BASE
|
| 12 |
+
if (fromEnv != null && String(fromEnv).trim() !== '') {
|
| 13 |
+
return stripTrailingSlash(String(fromEnv).trim())
|
| 14 |
+
}
|
| 15 |
+
if (import.meta.env.PROD) {
|
| 16 |
+
return 'https://cuiconnect-backend.hf.space'
|
| 17 |
+
}
|
| 18 |
+
return ''
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
/**
|
| 22 |
+
* @param {string} path - absolute path (e.g. `/users/login`)
|
| 23 |
+
*/
|
| 24 |
+
export function apiUrl(path) {
|
| 25 |
+
const base = getApiBase()
|
| 26 |
+
const p = path.startsWith('/') ? path : `/${path}`
|
| 27 |
+
return base ? `${base}${p}` : p
|
| 28 |
+
}
|
frontend/vercel.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"buildCommand": "npm run build",
|
| 3 |
+
"outputDirectory": "dist",
|
| 4 |
+
"rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
|
| 5 |
+
}
|