update Mongodb Atlas
Browse files- app/page_speed/config.py +14 -3
- app/rag/chat_history.py +1 -1
- app/rag/db.py +6 -3
- app/rag/embeddings.py +2 -2
- app/rag/utils.py +1 -1
- app/run_server.py +0 -20
app/page_speed/config.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class Settings(BaseSettings):
|
| 4 |
"""Application settings loaded from environment variables."""
|
|
@@ -25,10 +27,19 @@ class Settings(BaseSettings):
|
|
| 25 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
# MongoDB Configuration (Local)
|
| 27 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
# FastAPI Server Configuration
|
| 34 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1 |
from pydantic_settings import BaseSettings, SettingsConfigDict
|
| 2 |
+
from urllib.parse import quote_plus
|
| 3 |
+
|
| 4 |
|
| 5 |
class Settings(BaseSettings):
|
| 6 |
"""Application settings loaded from environment variables."""
|
|
|
|
| 27 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
# MongoDB Configuration (Local)
|
| 29 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
+
mongo_user: str
|
| 31 |
+
mongo_password: str
|
| 32 |
+
mongo_host: str
|
| 33 |
+
mongo_db: str = "MAAS"
|
| 34 |
+
mongo_collection: str = "chat_histories"
|
| 35 |
|
| 36 |
+
@property
|
| 37 |
+
def mongo_uri(self) -> str:
|
| 38 |
+
pw = quote_plus(self.mongo_password)
|
| 39 |
+
return (
|
| 40 |
+
f"mongodb+srv://{self.mongo_user}:{pw}@{self.mongo_host}/"
|
| 41 |
+
f"{self.mongo_db}?retryWrites=true&w=majority"
|
| 42 |
+
)
|
| 43 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
# FastAPI Server Configuration
|
| 45 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
app/rag/chat_history.py
CHANGED
|
@@ -11,7 +11,7 @@ from langchain.prompts import ChatPromptTemplate
|
|
| 11 |
from .logging_config import logger
|
| 12 |
|
| 13 |
# Get the actual collection object
|
| 14 |
-
db = mongo_client[settings.
|
| 15 |
coll = db[chat_collection_name]
|
| 16 |
|
| 17 |
# LLM & summarization prompt
|
|
|
|
| 11 |
from .logging_config import logger
|
| 12 |
|
| 13 |
# Get the actual collection object
|
| 14 |
+
db = mongo_client[settings.mongo_db]
|
| 15 |
coll = db[chat_collection_name]
|
| 16 |
|
| 17 |
# LLM & summarization prompt
|
app/rag/db.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from pymongo import MongoClient
|
| 2 |
from app.page_speed.config import settings
|
| 3 |
|
|
@@ -5,12 +6,14 @@ from app.page_speed.config import settings
|
|
| 5 |
# MongoDB Initialization
|
| 6 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 7 |
|
| 8 |
-
# Connect to MongoDB using the URI from
|
| 9 |
mongo_client = MongoClient(settings.mongo_uri)
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Collection to store metadata that maps user_id β vectorstore_path
|
| 13 |
vectorstore_meta_coll = mongo_db["vectorstore_metadata"]
|
| 14 |
|
| 15 |
# Name of the collection that MongoDBChatMessageHistory will write to
|
| 16 |
-
chat_collection_name = settings.
|
|
|
|
| 1 |
+
# db.py
|
| 2 |
from pymongo import MongoClient
|
| 3 |
from app.page_speed.config import settings
|
| 4 |
|
|
|
|
| 6 |
# MongoDB Initialization
|
| 7 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
|
| 9 |
+
# Connect to MongoDB using the URI from settings
|
| 10 |
mongo_client = MongoClient(settings.mongo_uri)
|
| 11 |
+
|
| 12 |
+
# Use the renamed settings attributes
|
| 13 |
+
mongo_db = mongo_client[settings.mongo_db]
|
| 14 |
|
| 15 |
# Collection to store metadata that maps user_id β vectorstore_path
|
| 16 |
vectorstore_meta_coll = mongo_db["vectorstore_metadata"]
|
| 17 |
|
| 18 |
# Name of the collection that MongoDBChatMessageHistory will write to
|
| 19 |
+
chat_collection_name = settings.mongo_collection
|
app/rag/embeddings.py
CHANGED
|
@@ -3,7 +3,7 @@ from langchain_huggingface import HuggingFaceEmbeddings
|
|
| 3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
load_dotenv()
|
| 7 |
|
| 8 |
|
| 9 |
def get_llm():
|
|
@@ -17,7 +17,7 @@ def get_llm():
|
|
| 17 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 18 |
temperature=0,
|
| 19 |
max_tokens=1024,
|
| 20 |
-
api_key=os.getenv("GROQ_API_KEY", "")
|
| 21 |
)
|
| 22 |
return llm
|
| 23 |
|
|
|
|
| 3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
load_dotenv()
|
| 7 |
|
| 8 |
|
| 9 |
def get_llm():
|
|
|
|
| 17 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 18 |
temperature=0,
|
| 19 |
max_tokens=1024,
|
| 20 |
+
api_key=os.getenv("GROQ_API_KEY", "")
|
| 21 |
)
|
| 22 |
return llm
|
| 23 |
|
app/rag/utils.py
CHANGED
|
@@ -78,7 +78,7 @@ def build_rag_chain(
|
|
| 78 |
chat_history = MongoDBChatMessageHistory(
|
| 79 |
session_id=chat_id,
|
| 80 |
connection_string=settings.mongo_uri,
|
| 81 |
-
database_name=settings.
|
| 82 |
collection_name=chat_collection_name,
|
| 83 |
)
|
| 84 |
memory = ConversationBufferMemory(
|
|
|
|
| 78 |
chat_history = MongoDBChatMessageHistory(
|
| 79 |
session_id=chat_id,
|
| 80 |
connection_string=settings.mongo_uri,
|
| 81 |
+
database_name=settings.mongo_db,
|
| 82 |
collection_name=chat_collection_name,
|
| 83 |
)
|
| 84 |
memory = ConversationBufferMemory(
|
app/run_server.py
DELETED
|
@@ -1,20 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Server runner script for the PageSpeed Insights API.
|
| 3 |
-
"""
|
| 4 |
-
import uvicorn
|
| 5 |
-
from app.config import settings
|
| 6 |
-
|
| 7 |
-
if __name__ == "__main__":
|
| 8 |
-
print(f"π Starting {settings.app_name}")
|
| 9 |
-
print(f"π Server: {settings.host}:{settings.port}")
|
| 10 |
-
print(f"π§ Debug Mode: {settings.debug}")
|
| 11 |
-
print(f"π API Documentation: http://{settings.host}:{settings.port}/docs")
|
| 12 |
-
print(f"π Alternative Docs: http://{settings.host}:{settings.port}/redoc")
|
| 13 |
-
|
| 14 |
-
uvicorn.run(
|
| 15 |
-
"app.main:app",
|
| 16 |
-
host=settings.host,
|
| 17 |
-
port=settings.port,
|
| 18 |
-
reload=settings.debug,
|
| 19 |
-
log_level="info" if not settings.debug else "debug"
|
| 20 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|