Spaces:
Sleeping
Sleeping
Rename main2.py to main.py
Browse files- main2.py → main.py +56 -10
main2.py → main.py
RENAMED
|
@@ -3,9 +3,11 @@ from fastapi import FastAPI, HTTPException
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import google.generativeai as genai
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 6 |
|
| 7 |
# --- 0. Config ---
|
| 8 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
|
|
|
| 9 |
if not GEMINI_API_KEY:
|
| 10 |
raise RuntimeError("GEMINI_API_KEY is not set in environment.")
|
| 11 |
|
|
@@ -37,6 +39,13 @@ app.add_middleware(
|
|
| 37 |
class ChatInput(BaseModel):
|
| 38 |
user_message: str
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# --- 4. Health check ---
|
| 41 |
@app.get("/")
|
| 42 |
async def health_check():
|
|
@@ -45,19 +54,56 @@ async def health_check():
|
|
| 45 |
# --- 5. Chat endpoint using direct Gemini SDK ---
|
| 46 |
@app.post("/chat")
|
| 47 |
async def chat_with_ai(input_data: ChatInput):
|
| 48 |
-
"""
|
| 49 |
-
Handle chat interactions using the official Google Generative AI SDK.
|
| 50 |
-
"""
|
| 51 |
try:
|
| 52 |
|
| 53 |
# Call Gemini directly via SDK
|
| 54 |
-
resp = model.generate_content(
|
| 55 |
-
|
| 56 |
-
)
|
| 57 |
-
print("resp",resp)
|
| 58 |
-
bot_response = getattr(resp, "text", None) or "No response text."
|
| 59 |
-
print("bot_response",bot_response)
|
| 60 |
-
return {"bot_response": bot_response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import google.generativeai as genai
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from embed.py import ingest_documents,search_knowledge_base
|
| 7 |
|
| 8 |
# --- 0. Config ---
|
| 9 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 10 |
+
print("GEMINI API KEY",GEMINI_API_KEY)
|
| 11 |
if not GEMINI_API_KEY:
|
| 12 |
raise RuntimeError("GEMINI_API_KEY is not set in environment.")
|
| 13 |
|
|
|
|
| 39 |
class ChatInput(BaseModel):
|
| 40 |
user_message: str
|
| 41 |
|
| 42 |
+
@app.on_event("startup")
|
| 43 |
+
def startup_ingest():
|
| 44 |
+
try:
|
| 45 |
+
ingest_documents()
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"KB ingestion failed: {e}")
|
| 48 |
+
|
| 49 |
# --- 4. Health check ---
|
| 50 |
@app.get("/")
|
| 51 |
async def health_check():
|
|
|
|
| 54 |
# --- 5. Chat endpoint using direct Gemini SDK ---
|
| 55 |
@app.post("/chat")
|
| 56 |
async def chat_with_ai(input_data: ChatInput):
|
|
|
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
|
| 59 |
# Call Gemini directly via SDK
|
| 60 |
+
#resp = model.generate_content(
|
| 61 |
+
# input_data.user_message,
|
| 62 |
+
#)
|
| 63 |
+
#print("resp",resp)
|
| 64 |
+
#bot_response = getattr(resp, "text", None) or "No response text."
|
| 65 |
+
#print("bot_response",bot_response)
|
| 66 |
+
#return {"bot_response": bot_response}
|
| 67 |
+
|
| 68 |
+
# Retrieve relevant documents from knowledge base
|
| 69 |
+
|
| 70 |
+
kb_results = search_knowledge_base(input_data.user_message, top_k=10)
|
| 71 |
+
print(f"kb_results are: {kb_results}")
|
| 72 |
+
|
| 73 |
+
# Extract relevant context from search results
|
| 74 |
+
context = ""
|
| 75 |
+
relevant_docs = []
|
| 76 |
+
if kb_results and kb_results.get('documents'):
|
| 77 |
+
# Limit context to avoid token limits - take top 2 most relevant
|
| 78 |
+
relevant_docs = kb_results['documents'][0][:2]
|
| 79 |
+
context = "\n\n".join(relevant_docs)
|
| 80 |
+
|
| 81 |
+
# Construct enhanced prompt with context
|
| 82 |
+
if context:
|
| 83 |
+
enhanced_prompt = f"""Use the following knowledge base context to answer the user's question accurately.
|
| 84 |
+
If the context contains relevant information, base your answer on it.
|
| 85 |
+
If the context doesn't help, say you're raising a ticket and provide a ticket number.
|
| 86 |
+
|
| 87 |
+
Knowledge Base Context:
|
| 88 |
+
{context}
|
| 89 |
+
|
| 90 |
+
User Question: {input_data.user_message}
|
| 91 |
+
|
| 92 |
+
Answer:"""
|
| 93 |
+
else:
|
| 94 |
+
enhanced_prompt = f"User Question: {input_data.user_message}\n\nAnswer:"
|
| 95 |
+
|
| 96 |
+
response = model.generate_content(enhanced_prompt)
|
| 97 |
+
print("response",response)
|
| 98 |
+
# Extract Gemini's response
|
| 99 |
+
bot_response = getattr(response, "text", None) or "No response text."
|
| 100 |
+
|
| 101 |
+
# Include debug info in response
|
| 102 |
+
debug_info = f"Context found: {'Yes' if context else 'No'}"
|
| 103 |
+
if context:
|
| 104 |
+
debug_info += f" (Top {len(relevant_docs)} documents used)"
|
| 105 |
+
|
| 106 |
+
return {"bot_response": bot_response, "debug": debug_info}
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
raise HTTPException(status_code=500, detail=str(e))
|