Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,9 +1,17 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# --- 0. Config ---
|
| 9 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
|
@@ -18,10 +26,29 @@ genai.configure(api_key=GEMINI_API_KEY)
|
|
| 18 |
MODEL_NAME = "gemini-2.5-flash-lite"
|
| 19 |
model = genai.GenerativeModel(MODEL_NAME)
|
| 20 |
|
| 21 |
-
# --- 1. FastAPI ---
|
| 22 |
-
app = FastAPI()
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
origins = [
|
| 26 |
"https://jaita-chatbot-react-frontend-v1.hf.space"
|
| 27 |
"https://jaita-chatbot-fastapi-backend.hf.space/chat",
|
|
@@ -35,55 +62,51 @@ app.add_middleware(
|
|
| 35 |
allow_headers=["*"],
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# --- 3.
|
| 39 |
class ChatInput(BaseModel):
|
| 40 |
user_message: str
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
folder_path = os.path.join(os.getcwd(), "documents")
|
| 46 |
-
if collection.count() == 0:
|
| 47 |
-
print("🔍 KB empty. Running ingestion...")
|
| 48 |
-
ingest_documents(folder_path)
|
| 49 |
-
else:
|
| 50 |
-
print(f"✅ KB already populated with {collection.count()} entries. Skipping ingestion.")
|
| 51 |
-
|
| 52 |
-
except Exception as e:
|
| 53 |
-
print(f"KB ingestion failed: {e}")
|
| 54 |
|
| 55 |
-
# ---
|
| 56 |
@app.get("/")
|
| 57 |
async def health_check():
|
| 58 |
return {"status": "ok"}
|
| 59 |
|
| 60 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@app.post("/chat")
|
| 62 |
async def chat_with_ai(input_data: ChatInput):
|
|
|
|
| 63 |
try:
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# Call Gemini directly via SDK
|
| 66 |
-
#resp = model.generate_content(
|
| 67 |
-
# input_data.user_message,
|
| 68 |
-
#)
|
| 69 |
-
#print("resp",resp)
|
| 70 |
-
#bot_response = getattr(resp, "text", None) or "No response text."
|
| 71 |
-
#print("bot_response",bot_response)
|
| 72 |
-
#return {"bot_response": bot_response}
|
| 73 |
-
|
| 74 |
# Retrieve relevant documents from knowledge base
|
| 75 |
-
|
| 76 |
kb_results = search_knowledge_base(input_data.user_message, top_k=10)
|
| 77 |
-
print(f"kb_results are: {kb_results}")
|
| 78 |
|
| 79 |
-
|
| 80 |
context = ""
|
| 81 |
-
relevant_docs
|
| 82 |
if kb_results and kb_results.get('documents'):
|
| 83 |
# Limit context to avoid token limits - take top 2 most relevant
|
| 84 |
relevant_docs = kb_results['documents'][0][:2]
|
| 85 |
context = "\n\n".join(relevant_docs)
|
| 86 |
-
|
| 87 |
# Construct enhanced prompt with context
|
| 88 |
if context:
|
| 89 |
enhanced_prompt = f"""Use the following knowledge base context to answer the user's question accurately.
|
|
@@ -98,11 +121,20 @@ User Question: {input_data.user_message}
|
|
| 98 |
Answer:"""
|
| 99 |
else:
|
| 100 |
enhanced_prompt = f"User Question: {input_data.user_message}\n\nAnswer:"
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# Extract Gemini's response
|
| 105 |
-
bot_response =
|
| 106 |
|
| 107 |
# Include debug info in response
|
| 108 |
debug_info = f"Context found: {'Yes' if context else 'No'}"
|
|
@@ -111,5 +143,23 @@ Answer:"""
|
|
| 111 |
|
| 112 |
return {"bot_response": bot_response, "debug": debug_info}
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["POSTHOG_DISABLED"] = "true" # Disable PostHog telemetry
|
| 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 |
+
from services.kb_creation import collection, ingest_documents, search_knowledge_base
|
| 10 |
+
import logging
|
| 11 |
+
from contextlib import asynccontextmanager
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
logging.basicConfig(level=logging.INFO)
|
| 15 |
|
| 16 |
# --- 0. Config ---
|
| 17 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
|
|
|
| 26 |
MODEL_NAME = "gemini-2.5-flash-lite"
|
| 27 |
model = genai.GenerativeModel(MODEL_NAME)
|
| 28 |
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
|
| 31 |
+
# Load environment variables from the .env file
|
| 32 |
+
load_dotenv()
|
| 33 |
+
|
| 34 |
+
# --- 1. Initialize FastAPI ---
|
| 35 |
+
#app = FastAPI()
|
| 36 |
+
@asynccontextmanager
|
| 37 |
+
async def lifespan(app: FastAPI):
|
| 38 |
+
try:
|
| 39 |
+
folder_path = os.path.join(os.getcwd(), "documents")
|
| 40 |
+
if collection.count() == 0:
|
| 41 |
+
print("🔍 KB empty. Running ingestion...")
|
| 42 |
+
ingest_documents(folder_path)
|
| 43 |
+
else:
|
| 44 |
+
print(f"✅ KB already populated with {collection.count()} entries. Skipping ingestion.")
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"⚠️ KB ingestion failed: {e}")
|
| 47 |
+
yield
|
| 48 |
+
|
| 49 |
+
app = FastAPI(lifespan=lifespan)
|
| 50 |
+
|
| 51 |
+
# --- Configure CORS ---
|
| 52 |
origins = [
|
| 53 |
"https://jaita-chatbot-react-frontend-v1.hf.space"
|
| 54 |
"https://jaita-chatbot-fastapi-backend.hf.space/chat",
|
|
|
|
| 62 |
allow_headers=["*"],
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# --- 3. Define the Request Data Structure ---
|
| 66 |
class ChatInput(BaseModel):
|
| 67 |
user_message: str
|
| 68 |
|
| 69 |
+
# --- 4. Gemini API Setup ---
|
| 70 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 71 |
+
GEMINI_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key={GEMINI_API_KEY}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# --- 5. Endpoints ---
|
| 74 |
@app.get("/")
|
| 75 |
async def health_check():
|
| 76 |
return {"status": "ok"}
|
| 77 |
|
| 78 |
+
#@app.on_event("startup")
|
| 79 |
+
#async def startup():
|
| 80 |
+
# try:
|
| 81 |
+
# folder_path = os.path.join(os.getcwd(), "documents")
|
| 82 |
+
# if collection.count() == 0:
|
| 83 |
+
# print("🔍 KB empty. Running ingestion...")
|
| 84 |
+
# ingest_all_documents(folder_path)
|
| 85 |
+
# else:
|
| 86 |
+
# print(f"✅ KB already populated with {collection.count()} entries. Skipping ingestion.")
|
| 87 |
+
# except Exception as e:
|
| 88 |
+
# print(f"⚠️ KB ingestion failed: {e}")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
@app.post("/chat")
|
| 92 |
async def chat_with_ai(input_data: ChatInput):
|
| 93 |
+
"""Handle chat interactions using Google Generative AI via requests."""
|
| 94 |
try:
|
| 95 |
+
#folder_path = os.path.join(os.getcwd(), "documents")
|
| 96 |
+
#print("folder_path",folder_path)
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# Retrieve relevant documents from knowledge base
|
|
|
|
| 99 |
kb_results = search_knowledge_base(input_data.user_message, top_k=10)
|
| 100 |
+
#print(f"kb_results are: {kb_results}")
|
| 101 |
|
| 102 |
+
# Extract relevant context from search results
|
| 103 |
context = ""
|
| 104 |
+
relevant_docs=[]
|
| 105 |
if kb_results and kb_results.get('documents'):
|
| 106 |
# Limit context to avoid token limits - take top 2 most relevant
|
| 107 |
relevant_docs = kb_results['documents'][0][:2]
|
| 108 |
context = "\n\n".join(relevant_docs)
|
| 109 |
+
|
| 110 |
# Construct enhanced prompt with context
|
| 111 |
if context:
|
| 112 |
enhanced_prompt = f"""Use the following knowledge base context to answer the user's question accurately.
|
|
|
|
| 121 |
Answer:"""
|
| 122 |
else:
|
| 123 |
enhanced_prompt = f"User Question: {input_data.user_message}\n\nAnswer:"
|
| 124 |
+
headers = {"Content-Type": "application/json"}
|
| 125 |
+
payload = {
|
| 126 |
+
"contents": [
|
| 127 |
+
{
|
| 128 |
+
"parts": [{"text": enhanced_prompt}]
|
| 129 |
+
}
|
| 130 |
+
]
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
response = requests.post(GEMINI_URL, headers=headers, json=payload, verify=False) # SSL disabled for testing
|
| 134 |
+
result = response.json()
|
| 135 |
+
#print("result",result)
|
| 136 |
# Extract Gemini's response
|
| 137 |
+
bot_response = result["candidates"][0]["content"]["parts"][0]["text"]
|
| 138 |
|
| 139 |
# Include debug info in response
|
| 140 |
debug_info = f"Context found: {'Yes' if context else 'No'}"
|
|
|
|
| 143 |
|
| 144 |
return {"bot_response": bot_response, "debug": debug_info}
|
| 145 |
|
| 146 |
+
# Make POST request to Gemini API
|
| 147 |
+
#response = requests.post(GEMINI_URL, json=payload,verify=False)
|
| 148 |
+
#if(response.status_code==200):
|
| 149 |
+
# print("response",response.status_code)
|
| 150 |
+
# data = response.json()
|
| 151 |
+
# #print("data",data)
|
| 152 |
+
|
| 153 |
+
# Extract text from response
|
| 154 |
+
#bot_response = ""
|
| 155 |
+
#if "candidates" in data and data["candidates"]:
|
| 156 |
+
# parts = data["candidates"][0].get("content", {}).get("parts", [])
|
| 157 |
+
# for part in parts:
|
| 158 |
+
# if "text" in part:
|
| 159 |
+
# bot_response += part["text"]
|
| 160 |
+
#
|
| 161 |
+
#return {"bot_response": bot_response or "No response text."}
|
| 162 |
+
|
| 163 |
+
|
| 164 |
except Exception as e:
|
| 165 |
raise HTTPException(status_code=500, detail=str(e))
|