Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,101 +1,104 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
os.environ["POSTHOG_DISABLED"] = "true"
|
| 3 |
import requests
|
| 4 |
from fastapi import FastAPI, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
-
from kb_embed import search_knowledge_base
|
| 9 |
import logging
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Load environment variables from the .env file
|
| 18 |
load_dotenv()
|
| 19 |
|
| 20 |
-
# --- 1. Initialize FastAPI ---
|
| 21 |
app = FastAPI()
|
| 22 |
|
| 23 |
-
# --- 2. Configure CORS ---
|
| 24 |
-
#origins = [
|
| 25 |
-
# "http://localhost:5173",
|
| 26 |
-
# "http://localhost:3000",
|
| 27 |
-
#]
|
| 28 |
-
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
| 31 |
-
allow_origins=[
|
|
|
|
|
|
|
|
|
|
| 32 |
allow_credentials=True,
|
| 33 |
allow_methods=["*"],
|
| 34 |
allow_headers=["*"],
|
| 35 |
)
|
| 36 |
|
| 37 |
-
# --- 3. Define the Request Data Structure ---
|
| 38 |
class ChatInput(BaseModel):
|
| 39 |
user_message: str
|
| 40 |
|
| 41 |
-
# --- 4. Gemini API Setup ---
|
| 42 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 43 |
-
GEMINI_URL =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
# --- 5. Endpoints ---
|
| 46 |
@app.get("/")
|
| 47 |
async def health_check():
|
| 48 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
@app.post("/chat")
|
| 51 |
async def chat_with_gemini(input_data: ChatInput):
|
| 52 |
-
|
| 53 |
# 1. Search Knowledge Base
|
| 54 |
kb_results = search_knowledge_base(input_data.user_message, top_k=10)
|
| 55 |
-
logging.info(kb_results)
|
| 56 |
-
|
| 57 |
context = ""
|
| 58 |
relevant_docs = []
|
| 59 |
-
|
| 60 |
-
# 2. Extract relevant KB docs
|
| 61 |
-
if kb_results and kb_results.get("documents"):
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
context = "\n\n".join(relevant_docs)
|
| 64 |
-
|
| 65 |
# 3. If KB contains direct answer → RETURN it (No LLM call)
|
| 66 |
-
if context:
|
| 67 |
kb_answer = f"From knowledge base:\n\n{context}"
|
| 68 |
return {
|
| 69 |
"bot_response": kb_answer,
|
| 70 |
-
"debug_info": f"Context found: YES, docs used: {len(relevant_docs)}"
|
| 71 |
}
|
| 72 |
-
|
| 73 |
# 4. If KB empty → fallback to Gemini
|
| 74 |
enhanced_prompt = (
|
| 75 |
f"User question: {input_data.user_message}\n\n"
|
| 76 |
"No relevant KB found. You must raise a ticket.\n"
|
| 77 |
"Say: 'I'm raising a ticket. Ticket# 12345'."
|
| 78 |
)
|
| 79 |
-
|
| 80 |
-
# 5. Call Gemini
|
| 81 |
headers = {"Content-Type": "application/json"}
|
| 82 |
-
payload = {
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
]
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
bot_response = result["candidates"][0]["content"]["parts"][0]["text"]
|
| 94 |
-
|
| 95 |
return {
|
| 96 |
"bot_response": bot_response,
|
| 97 |
-
"debug_info": "Context found: NO"
|
| 98 |
}
|
| 99 |
-
|
| 100 |
-
#except Exception as e:
|
| 101 |
-
# raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
+
|
| 2 |
+
# main.py
|
| 3 |
import os
|
| 4 |
+
os.environ["POSTHOG_DISABLED"] = "true"
|
| 5 |
import requests
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
from kb_embed import search_knowledge_base, ingest_documents, collection, DOCS_DIR
|
| 11 |
import logging
|
| 12 |
|
|
|
|
|
|
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
load_dotenv()
|
| 15 |
|
|
|
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
app.add_middleware(
|
| 19 |
CORSMiddleware,
|
| 20 |
+
allow_origins=[
|
| 21 |
+
"https://jaita-chatbot-react-frontend-v1.hf.space", # frontend space origin
|
| 22 |
+
"https://jaita-chatbot-fastapi-backend.hf.space", # backend space origin
|
| 23 |
+
],
|
| 24 |
allow_credentials=True,
|
| 25 |
allow_methods=["*"],
|
| 26 |
allow_headers=["*"],
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
class ChatInput(BaseModel):
|
| 30 |
user_message: str
|
| 31 |
|
|
|
|
| 32 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 33 |
+
GEMINI_URL = (
|
| 34 |
+
f"https://generativelanguage.googleapis.com/v1beta/models/"
|
| 35 |
+
f"gemini-2.5-flash-lite:generateContent?key={GEMINI_API_KEY}"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
@app.on_event("startup")
|
| 39 |
+
def startup_ingest():
|
| 40 |
+
try:
|
| 41 |
+
# Make sure DOCS_DIR exists and has .docx files, then ingest
|
| 42 |
+
if DOCS_DIR.exists():
|
| 43 |
+
logging.info(f"Starting KB ingestion from: {DOCS_DIR}")
|
| 44 |
+
ingest_documents(str(DOCS_DIR))
|
| 45 |
+
else:
|
| 46 |
+
logging.warning(f"Docs directory not found: {DOCS_DIR}")
|
| 47 |
+
logging.info(f"Chroma collection count after startup: {collection.count()}")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logging.exception(f"KB ingestion failed: {e}")
|
| 50 |
|
|
|
|
| 51 |
@app.get("/")
|
| 52 |
async def health_check():
|
| 53 |
+
return {
|
| 54 |
+
"status": "ok",
|
| 55 |
+
"kb_count": collection.count(),
|
| 56 |
+
"docs_dir_exists": DOCS_DIR.exists()
|
| 57 |
+
}
|
| 58 |
|
| 59 |
@app.post("/chat")
|
| 60 |
async def chat_with_gemini(input_data: ChatInput):
|
|
|
|
| 61 |
# 1. Search Knowledge Base
|
| 62 |
kb_results = search_knowledge_base(input_data.user_message, top_k=10)
|
| 63 |
+
logging.info(f"KB query results keys: {list(kb_results.keys()) if kb_results else 'None'}")
|
| 64 |
+
|
| 65 |
context = ""
|
| 66 |
relevant_docs = []
|
| 67 |
+
|
| 68 |
+
# 2. Extract relevant KB docs if present
|
| 69 |
+
if kb_results and kb_results.get("documents") and len(kb_results["documents"]) > 0:
|
| 70 |
+
# kb_results["documents"] is a list of lists (one per query)
|
| 71 |
+
first_query_docs = kb_results["documents"][0]
|
| 72 |
+
relevant_docs = first_query_docs[:2]
|
| 73 |
context = "\n\n".join(relevant_docs)
|
| 74 |
+
|
| 75 |
# 3. If KB contains direct answer → RETURN it (No LLM call)
|
| 76 |
+
if context.strip():
|
| 77 |
kb_answer = f"From knowledge base:\n\n{context}"
|
| 78 |
return {
|
| 79 |
"bot_response": kb_answer,
|
| 80 |
+
"debug_info": f"Context found: YES, docs used: {len(relevant_docs)}, kb_count: {collection.count()}"
|
| 81 |
}
|
| 82 |
+
|
| 83 |
# 4. If KB empty → fallback to Gemini
|
| 84 |
enhanced_prompt = (
|
| 85 |
f"User question: {input_data.user_message}\n\n"
|
| 86 |
"No relevant KB found. You must raise a ticket.\n"
|
| 87 |
"Say: 'I'm raising a ticket. Ticket# 12345'."
|
| 88 |
)
|
| 89 |
+
|
|
|
|
| 90 |
headers = {"Content-Type": "application/json"}
|
| 91 |
+
payload = {"contents": [{"parts": [{"text": enhanced_prompt}]}]}
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
response = requests.post(GEMINI_URL, headers=headers, json=payload)
|
| 95 |
+
result = response.json()
|
| 96 |
+
bot_response = result["candidates"][0]["content"]["parts"][0]["text"]
|
| 97 |
+
except Exception as e:
|
| 98 |
+
logging.exception(f"Gemini call failed: {e}")
|
| 99 |
+
raise HTTPException(status_code=500, detail="LLM call failed")
|
| 100 |
+
|
|
|
|
|
|
|
|
|
|
| 101 |
return {
|
| 102 |
"bot_response": bot_response,
|
| 103 |
+
"debug_info": f"Context found: NO, kb_count: {collection.count()}"
|
| 104 |
}
|
|
|
|
|
|
|
|
|