FitAI Dev commited on
Commit ·
311c136
1
Parent(s): 3c48b32
feat: switch ai backend to gemma-3-1b-it via google-genai with dotenv support
Browse files- .gitignore +3 -0
- backend/.env.example +2 -0
- backend/app/__pycache__/main.cpython-314.pyc +0 -0
- backend/app/main.py +3 -0
- backend/app/routers/chat.py +16 -12
- backend/requirements.txt +2 -1
.gitignore
CHANGED
|
@@ -3,3 +3,6 @@ __pycache__/
|
|
| 3 |
*.pyo
|
| 4 |
*.db
|
| 5 |
.pytest_cache/
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
*.pyo
|
| 4 |
*.db
|
| 5 |
.pytest_cache/
|
| 6 |
+
.env
|
| 7 |
+
node_modules/
|
| 8 |
+
frontend/dist/
|
backend/.env.example
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
GOOGLE_API_KEY=your-google-ai-studio-key-here
|
| 2 |
+
SECRET_KEY=change-this-to-a-random-secret-in-production
|
backend/app/__pycache__/main.cpython-314.pyc
CHANGED
|
Binary files a/backend/app/__pycache__/main.cpython-314.pyc and b/backend/app/__pycache__/main.cpython-314.pyc differ
|
|
|
backend/app/main.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from app.database import Base, engine
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv() # load backend/.env before anything else reads os.getenv()
|
| 3 |
+
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from app.database import Base, engine
|
backend/app/routers/chat.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
from fastapi import APIRouter, Depends
|
| 4 |
from sqlalchemy.orm import Session
|
| 5 |
from app.database import get_db
|
|
@@ -10,6 +11,8 @@ from app.services.rag_service import build_user_context
|
|
| 10 |
|
| 11 |
router = APIRouter(prefix="/chat", tags=["chat"])
|
| 12 |
|
|
|
|
|
|
|
| 13 |
PERSONA_PROMPTS = {
|
| 14 |
"strict": (
|
| 15 |
"You are a tough, no-excuses drill sergeant fitness coach. "
|
|
@@ -47,23 +50,24 @@ def chat(
|
|
| 47 |
f"{h.role.capitalize()}: {h.content}" for h in reversed(history_rows)
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
f"You are a professional fitness coach AI. {persona}\n\n"
|
| 52 |
"SAFETY RULE: Never recommend anything that could cause injury. "
|
| 53 |
"Always base advice on the user's actual recorded data.\n\n"
|
| 54 |
-
f"{context}
|
| 55 |
-
+ (f"
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
)
|
| 66 |
-
reply = response.
|
| 67 |
|
| 68 |
db.add(ChatHistory(user_id=current_user.id, role="user", content=msg.message))
|
| 69 |
db.add(ChatHistory(user_id=current_user.id, role="assistant", content=reply))
|
|
|
|
| 1 |
import os
|
| 2 |
+
from google import genai
|
| 3 |
+
from google.genai import types
|
| 4 |
from fastapi import APIRouter, Depends
|
| 5 |
from sqlalchemy.orm import Session
|
| 6 |
from app.database import get_db
|
|
|
|
| 11 |
|
| 12 |
router = APIRouter(prefix="/chat", tags=["chat"])
|
| 13 |
|
| 14 |
+
MODEL_NAME = "gemma-3-1b-it"
|
| 15 |
+
|
| 16 |
PERSONA_PROMPTS = {
|
| 17 |
"strict": (
|
| 18 |
"You are a tough, no-excuses drill sergeant fitness coach. "
|
|
|
|
| 50 |
f"{h.role.capitalize()}: {h.content}" for h in reversed(history_rows)
|
| 51 |
)
|
| 52 |
|
| 53 |
+
system_instruction = (
|
| 54 |
f"You are a professional fitness coach AI. {persona}\n\n"
|
| 55 |
"SAFETY RULE: Never recommend anything that could cause injury. "
|
| 56 |
"Always base advice on the user's actual recorded data.\n\n"
|
| 57 |
+
f"{context}"
|
| 58 |
+
+ (f"\n\nRecent conversation:\n{history_text}" if history_text else "")
|
| 59 |
)
|
| 60 |
|
| 61 |
+
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 62 |
+
response = client.models.generate_content(
|
| 63 |
+
model=MODEL_NAME,
|
| 64 |
+
contents=msg.message,
|
| 65 |
+
config=types.GenerateContentConfig(
|
| 66 |
+
system_instruction=system_instruction,
|
| 67 |
+
max_output_tokens=1024,
|
| 68 |
+
),
|
| 69 |
)
|
| 70 |
+
reply = response.text
|
| 71 |
|
| 72 |
db.add(ChatHistory(user_id=current_user.id, role="user", content=msg.message))
|
| 73 |
db.add(ChatHistory(user_id=current_user.id, role="assistant", content=reply))
|
backend/requirements.txt
CHANGED
|
@@ -6,4 +6,5 @@ httpx
|
|
| 6 |
pyjwt
|
| 7 |
bcrypt
|
| 8 |
python-multipart
|
| 9 |
-
|
|
|
|
|
|
| 6 |
pyjwt
|
| 7 |
bcrypt
|
| 8 |
python-multipart
|
| 9 |
+
google-genai
|
| 10 |
+
python-dotenv
|