Jaita commited on
Commit
858a55a
·
verified ·
1 Parent(s): eef1674

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +103 -0
main.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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=["https://jaita-chatbot-react-frontend-v1.hf.space"],
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 = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key={GEMINI_API_KEY}"
44
+
45
+ # --- 5. Endpoints ---
46
+ @app.get("/")
47
+ async def health_check():
48
+ return {"status": "ok"}
49
+
50
+ @app.post("/chat")
51
+ async def chat_with_gemini(input_data: ChatInput):
52
+ try:
53
+ # Retrieve relevant documents from knowledge base
54
+
55
+ kb_results = search_knowledge_base(input_data.user_message, top_k=10)
56
+ logging.info(f"kb_results are: {kb_results}")
57
+
58
+ # Extract relevant context from search results
59
+ context = ""
60
+ if kb_results and kb_results.get('documents'):
61
+ # Limit context to avoid token limits - take top 2 most relevant
62
+ relevant_docs = kb_results['documents'][0][:2]
63
+ context = "\n\n".join(relevant_docs)
64
+
65
+ # Construct enhanced prompt with context
66
+ if context:
67
+ enhanced_prompt = f"""Use the following knowledge base context to answer the user's question accurately.
68
+ If the context contains relevant information, base your answer on it.
69
+ If the context doesn't help, say you're raising a ticket and provide a ticket number.
70
+
71
+ Knowledge Base Context:
72
+ {context}
73
+
74
+ User Question: {input_data.user_message}
75
+
76
+ Answer:"""
77
+ else:
78
+ enhanced_prompt = f"User Question: {input_data.user_message}\n\nAnswer:"
79
+
80
+ headers = {"Content-Type": "application/json"}
81
+ payload = {
82
+ "contents": [
83
+ {
84
+ "parts": [{"text": enhanced_prompt}]
85
+ }
86
+ ]
87
+ }
88
+
89
+ response = requests.post(GEMINI_URL, headers=headers, json=payload, verify=False) # SSL disabled for testing
90
+ result = response.json()
91
+
92
+ # Extract Gemini's response
93
+ bot_response = result["candidates"][0]["content"]["parts"][0]["text"]
94
+
95
+ # Include debug info in response
96
+ debug_info = f"Context found: {'Yes' if context else 'No'}"
97
+ if context:
98
+ debug_info += f" (Top {len(relevant_docs)} documents used)"
99
+
100
+ return {"bot_response": bot_response, "debug": debug_info}
101
+
102
+ except Exception as e:
103
+ raise HTTPException(status_code=500, detail=str(e))