main.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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
  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. for kw in keywords[:5]: # Limit to first 5 keywords for performance
  74. results = search_foods_by_name(kw, limit=3)
  75. for item in results:
  76. if item['name'] not in found_items:
  77. found_items[item['name']] = item
  78. if len(found_items) >= 5:
  79. break
  80. if not found_items:
  81. return None
  82. # Build a structured context block for the system prompt
  83. lines = [
  84. "[LocalFoodAI Database Context]",
  85. "The user's question relates to foods found in the local verified nutritional database.",
  86. "Use ONLY the following data for specific nutritional values (per 100g serving):",
  87. ""
  88. ]
  89. for item in found_items.values():
  90. line = (
  91. f"- {item['name']}: {item['calories']} kcal | "
  92. f"Protein: {item['protein_g']}g | Fat: {item['fat_g']}g | "
  93. f"Carbs: {item['carbs_g']}g | Fiber: {item['fiber_g']}g | "
  94. f"Sodium: {item['sodium_mg']}mg"
  95. )
  96. lines.append(line)
  97. lines.append("")
  98. lines.append("Always prioritize this local database data over your training memory for these specific foods.")
  99. return "\n".join(lines)
  100. # Mount static files to serve the frontend
  101. app.mount("/static", StaticFiles(directory="static"), name="static")
  102. class ChatMessage(BaseModel):
  103. role: str
  104. content: str
  105. class ChatRequest(BaseModel):
  106. messages: List[ChatMessage]
  107. @app.get("/", response_class=HTMLResponse)
  108. async def read_root():
  109. """Serve the chat interface HTML"""
  110. try:
  111. with open("static/index.html", "r", encoding="utf-8") as f:
  112. return HTMLResponse(content=f.read())
  113. except FileNotFoundError:
  114. return HTMLResponse(content="<h1>Welcome to LocalFoodAI</h1><p>static/index.html not found. Please create the frontend.</p>")
  115. @app.post("/api/register")
  116. async def register_user(user: UserCreate):
  117. if len(user.username.strip()) < 3:
  118. raise HTTPException(status_code=400, detail="Username must be at least 3 characters")
  119. if len(user.password.strip()) < 6:
  120. raise HTTPException(status_code=400, detail="Password must be at least 6 characters")
  121. hashed_password = get_password_hash(user.password)
  122. user_id = create_user(user.username.strip(), hashed_password)
  123. if not user_id:
  124. raise HTTPException(status_code=400, detail="Username already exists")
  125. # Auto-login after registration
  126. token = create_session(user_id)
  127. return {"message": "User registered successfully", "token": token, "username": user.username.strip()}
  128. @app.post("/api/login")
  129. async def login_user(user: UserLogin):
  130. db_user = get_user_by_username(user.username.strip())
  131. if not db_user:
  132. raise HTTPException(status_code=401, detail="Invalid username or password")
  133. if not verify_password(user.password, db_user["password_hash"]):
  134. raise HTTPException(status_code=401, detail="Invalid username or password")
  135. token = create_session(db_user["id"])
  136. return {"status": "success", "username": db_user["username"], "token": token}
  137. @app.post("/api/logout")
  138. async def logout(authorization: Optional[str] = Header(None)):
  139. if authorization and authorization.startswith("Bearer "):
  140. token = authorization.split(" ")[1]
  141. delete_session(token)
  142. return {"message": "Logged out successfully"}
  143. @app.post("/chat")
  144. async def chat_endpoint(request: ChatRequest, current_user: dict = Depends(get_current_user)):
  145. """Proxy chat requests to the local Ollama instance with streaming support.
  146. Automatically enriches prompts with verified local SQLite nutritional data.
  147. """
  148. messages = [msg.model_dump() for msg in request.messages]
  149. # --- TG-35: Local SQL RAG Enrichment ---
  150. db_context = extract_food_context(messages)
  151. if db_context:
  152. logger.info(f"[RAG] Injecting local DB context for user '{current_user['username']}'")
  153. # Prepend as a system message so it acts as grounded knowledge
  154. messages = [{"role": "system", "content": db_context}] + messages
  155. payload = {
  156. "model": MODEL_NAME,
  157. "messages": messages,
  158. "stream": True # Enable streaming for a better UI experience
  159. }
  160. async def generate_response():
  161. try:
  162. async with httpx.AsyncClient() as client:
  163. async with client.stream("POST", OLLAMA_URL, json=payload, timeout=120.0) as response:
  164. if response.status_code != 200:
  165. error_detail = await response.aread()
  166. logger.error(f"Error communicating with Ollama: {error_detail}")
  167. yield f"data: {json.dumps({'error': 'Error communicating with local LLM.'})}\n\n"
  168. return
  169. async for line in response.aiter_lines():
  170. if line:
  171. data = json.loads(line)
  172. if "message" in data and "content" in data["message"]:
  173. content = data["message"]["content"]
  174. yield f"data: {json.dumps({'content': content})}\n\n"
  175. if data.get("done"):
  176. break
  177. except Exception as e:
  178. logger.error(f"Unexpected error during stream: {e}")
  179. yield f"data: {json.dumps({'error': str(e)})}\n\n"
  180. return StreamingResponse(generate_response(), media_type="text/event-stream")
  181. @app.get("/api/food/search")
  182. async def search_food(q: str, current_user: dict = Depends(get_current_user)):
  183. """API endpoint to search for food items securely using token authentication"""
  184. if not q or len(q.strip()) < 1:
  185. return {"results": []}
  186. logger.info(f"User {current_user['username']} searched for [{q}]")
  187. results = search_foods_by_name(q.strip(), limit=15)
  188. return {"results": results}
  189. if __name__ == "__main__":
  190. import uvicorn
  191. uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)