main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import json
  2. import logging
  3. import httpx
  4. import bcrypt
  5. from contextlib import asynccontextmanager
  6. from fastapi import FastAPI, HTTPException, Depends, Header
  7. from database import create_tables, create_user, get_user_by_username, create_session, get_user_from_token, delete_session, search_foods_by_name, save_chat_message, get_user_chat_history
  8. from fastapi.responses import HTMLResponse, StreamingResponse
  9. from fastapi.staticfiles import StaticFiles
  10. from pydantic import BaseModel
  11. from typing import List, Generator, Optional
  12. logging.basicConfig(level=logging.INFO)
  13. logger = logging.getLogger(__name__)
  14. @asynccontextmanager
  15. async def lifespan(app: FastAPI):
  16. create_tables()
  17. yield
  18. app = FastAPI(title="LocalFoodAI Chat", lifespan=lifespan)
  19. # Use direct bcrypt for better environment compatibility
  20. def get_password_hash(password: str):
  21. # Hash requires bytes
  22. pwd_bytes = password.encode('utf-8')
  23. salt = bcrypt.gensalt()
  24. hashed = bcrypt.hashpw(pwd_bytes, salt)
  25. return hashed.decode('utf-8')
  26. def verify_password(plain_password: str, hashed_password: str):
  27. # bcrypt.checkpw handles verification
  28. return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
  29. class UserCreate(BaseModel):
  30. username: str
  31. password: str
  32. class UserLogin(BaseModel):
  33. username: str
  34. password: str
  35. async def get_current_user(authorization: Optional[str] = Header(None)):
  36. if not authorization or not authorization.startswith("Bearer "):
  37. raise HTTPException(status_code=401, detail="Authentication required")
  38. token = authorization.split(" ")[1]
  39. user = get_user_from_token(token)
  40. if not user:
  41. raise HTTPException(status_code=401, detail="Invalid or expired session")
  42. return user
  43. OLLAMA_URL = "http://localhost:11434/api/chat"
  44. MODEL_NAME = "llama3.1:8b"
  45. # Common stopwords to strip before searching the food database
  46. _STOPWORDS = {
  47. 'how', 'many', 'much', 'calories', 'does', 'have', 'has', 'is', 'are',
  48. 'in', 'the', 'a', 'an', 'of', 'for', 'with', 'what', 'tell', 'me',
  49. 'about', 'nutritional', 'value', 'nutrition', 'macro', 'macros',
  50. 'protein', 'fat', 'carbs', 'fiber', 'can', 'you', 'i', 'want', 'need',
  51. 'eat', 'eating', 'food', 'meal', 'diet', 'healthy', 'make', 'cook',
  52. 'recipe', 'per', '100g', 'gram', 'grams', 'serving'
  53. }
  54. def extract_food_context(messages: list) -> str | None:
  55. """Scan the last user message for food keywords and enrich with local DB data."""
  56. # Find the last user message
  57. last_user_msg = None
  58. for msg in reversed(messages):
  59. role = msg.get('role', '') if isinstance(msg, dict) else msg.role
  60. content = msg.get('content', '') if isinstance(msg, dict) else msg.content
  61. if role == 'user':
  62. last_user_msg = content
  63. break
  64. if not last_user_msg:
  65. return None
  66. # Extract meaningful keywords by removing stopwords
  67. words = last_user_msg.lower().replace('?', '').replace(',', '').split()
  68. keywords = [w for w in words if w not in _STOPWORDS and len(w) > 2]
  69. if not keywords:
  70. return None
  71. # Try each keyword against the local food database, collect unique results
  72. found_items = {}
  73. # Optimization: Only use the first 2 most relevant keywords to keep context small on CPU
  74. for kw in keywords[:2]:
  75. results = search_foods_by_name(kw, limit=2)
  76. for item in results:
  77. # Truncate extremely long USDA names for performance
  78. short_name = item['name'][:100] + ("..." if len(item['name']) > 100 else "")
  79. if short_name not in found_items:
  80. found_items[short_name] = item
  81. if len(found_items) >= 3:
  82. break
  83. if not found_items:
  84. return None
  85. # Build a structured context block for the system prompt
  86. lines = [
  87. "[LocalFoodAI Database Context]",
  88. "The user's question relates to foods found in the local verified nutritional database.",
  89. "Use ONLY the following data for specific nutritional values (per 100g serving):",
  90. ""
  91. ]
  92. for name, item in found_items.items():
  93. line = (
  94. f"- {name}: {item['calories']} kcal | "
  95. f"P: {item['protein_g']}g | F: {item['fat_g']}g | "
  96. f"C: {item['carbs_g']}g"
  97. )
  98. lines.append(line)
  99. lines.append("")
  100. lines.append("Always prioritize this local database data over your training memory for these specific foods.")
  101. return "\n".join(lines)
  102. # Mount static files to serve the frontend
  103. app.mount("/static", StaticFiles(directory="static"), name="static")
  104. class ChatMessage(BaseModel):
  105. role: str
  106. content: str
  107. class ChatRequest(BaseModel):
  108. messages: List[ChatMessage]
  109. @app.get("/", response_class=HTMLResponse)
  110. async def read_root():
  111. """Serve the chat interface HTML"""
  112. try:
  113. with open("static/index.html", "r", encoding="utf-8") as f:
  114. return HTMLResponse(content=f.read())
  115. except FileNotFoundError:
  116. return HTMLResponse(content="<h1>Welcome to LocalFoodAI</h1><p>static/index.html not found. Please create the frontend.</p>")
  117. @app.post("/api/register")
  118. async def register_user(user: UserCreate):
  119. if len(user.username.strip()) < 3:
  120. raise HTTPException(status_code=400, detail="Username must be at least 3 characters")
  121. if len(user.password.strip()) < 6:
  122. raise HTTPException(status_code=400, detail="Password must be at least 6 characters")
  123. hashed_password = get_password_hash(user.password)
  124. user_id = create_user(user.username.strip(), hashed_password)
  125. if not user_id:
  126. raise HTTPException(status_code=400, detail="Username already exists")
  127. # Auto-login after registration
  128. token = create_session(user_id)
  129. return {"message": "User registered successfully", "token": token, "username": user.username.strip()}
  130. @app.post("/api/login")
  131. async def login_user(user: UserLogin):
  132. db_user = get_user_by_username(user.username.strip())
  133. if not db_user:
  134. raise HTTPException(status_code=401, detail="Invalid username or password")
  135. if not verify_password(user.password, db_user["password_hash"]):
  136. raise HTTPException(status_code=401, detail="Invalid username or password")
  137. token = create_session(db_user["id"])
  138. return {"status": "success", "username": db_user["username"], "token": token}
  139. @app.post("/api/logout")
  140. async def logout(authorization: Optional[str] = Header(None)):
  141. if authorization and authorization.startswith("Bearer "):
  142. token = authorization.split(" ")[1]
  143. delete_session(token)
  144. return {"message": "Logged out successfully"}
  145. @app.post("/chat")
  146. async def chat_endpoint(request: ChatRequest, current_user: dict = Depends(get_current_user)):
  147. """Proxy chat requests to the local Ollama instance with streaming support.
  148. Automatically enriches prompts with verified local SQLite nutritional data.
  149. """
  150. # Keep only the last 6 messages for context window performance on CPU
  151. all_messages = [msg.model_dump() for msg in request.messages]
  152. messages = all_messages[-6:]
  153. # Save the latest user message to DB
  154. if messages and messages[-1]['role'] == 'user':
  155. save_chat_message(current_user['id'], 'user', messages[-1]['content'])
  156. # --- TG-35: Local SQL RAG Enrichment ---
  157. db_context = extract_food_context(messages)
  158. if db_context:
  159. # Prepend as a system message so it acts as grounded knowledge
  160. # We ensure it's a short, concise instruction to prevent context bloat
  161. messages = [{"role": "system", "content": db_context}] + messages
  162. logger.info(f"[Chat] User '{current_user['username']}' is chatting. Context items: {'Yes' if db_context else 'No'}. Message count: {len(messages)}")
  163. payload = {
  164. "model": MODEL_NAME,
  165. "messages": messages,
  166. "stream": True
  167. }
  168. async def generate_response():
  169. try:
  170. bot_full_response = ""
  171. async with httpx.AsyncClient(timeout=300.0) as client:
  172. # Use a combined timeout for the entire request
  173. async with client.stream("POST", OLLAMA_URL, json=payload, timeout=300.0) as response:
  174. if response.status_code != 200:
  175. error_detail = await response.aread()
  176. logger.error(f"Ollama returned error {response.status_code}: {error_detail}")
  177. yield f"data: {json.dumps({'error': f'LLM Error ({response.status_code})'})}\n\n"
  178. return
  179. async for line in response.aiter_lines():
  180. if line:
  181. try:
  182. data = json.loads(line)
  183. if "message" in data and "content" in data["message"]:
  184. content = data["message"]["content"]
  185. bot_full_response += content
  186. yield f"data: {json.dumps({'content': content})}\n\n"
  187. if data.get("done"):
  188. break
  189. except json.JSONDecodeError:
  190. continue
  191. # Save final bot response to DB
  192. if bot_full_response.strip():
  193. save_chat_message(current_user['id'], 'assistant', bot_full_response)
  194. except Exception as e:
  195. logger.exception(f"Unexpected error during chat stream: {e}")
  196. yield f"data: {json.dumps({'error': 'A technical error occurred while generating the response.'})}\n\n"
  197. return StreamingResponse(generate_response(), media_type="text/event-stream")
  198. @app.get("/api/chat/history")
  199. async def get_history(current_user: dict = Depends(get_current_user)):
  200. """Fetch the chat history for the authenticated user"""
  201. history = get_user_chat_history(current_user['id'])
  202. return {"history": history}
  203. @app.get("/api/food/search")
  204. async def search_food(q: str, current_user: dict = Depends(get_current_user)):
  205. """API endpoint to search for food items securely using token authentication"""
  206. if not q or len(q.strip()) < 1:
  207. return {"results": []}
  208. logger.info(f"User {current_user['username']} searched for [{q}]")
  209. results = search_foods_by_name(q.strip(), limit=15)
  210. return {"results": results}
  211. if __name__ == "__main__":
  212. import uvicorn
  213. uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)