diff --git a/.claude/launch.json b/.claude/launch.json index 39769cb..dbd8fde 100644 --- a/.claude/launch.json +++ b/.claude/launch.json @@ -4,7 +4,16 @@ { "name": "FastAPI backend", "runtimeExecutable": "uv", - "runtimeArgs": ["run", "uvicorn", "main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"], + "runtimeArgs": [ + "run", + "uvicorn", + "backend.main:app", + "--reload", + "--host", + "0.0.0.0", + "--port", + "8000" + ], "port": 8000 } ] diff --git a/backend/db.py b/backend/db.py index 1a802aa..d07e341 100644 --- a/backend/db.py +++ b/backend/db.py @@ -1,12 +1,17 @@ -import chromadb +from functools import lru_cache from pathlib import Path +import chromadb + _DB_PATH = str(Path(__file__).parent / "chroma_db") -def get_collection(): - """ - Retrieves chromadb collection by either retrieving one that was already created or by creating a new one +@lru_cache(maxsize=1) +def get_diary_collection(): + """Retrieves diary chromadb collection by either retrieving one that was already created or by creating a new one + + Returns: + A chromadb collection with the name "diary" """ client = chromadb.PersistentClient(_DB_PATH) return client.get_or_create_collection( diff --git a/backend/main.py b/backend/main.py index b11b522..dd9799a 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,8 +1,3 @@ -import openai -import streamlit - - - def main(): print("Hello from nadir!") diff --git a/backend/query.py b/backend/query.py index 79e3dff..815dea4 100644 --- a/backend/query.py +++ b/backend/query.py @@ -1,17 +1,22 @@ -from openai import OpenAI -from dotenv import load_dotenv -from db import get_collection +from functools import lru_cache -if not load_dotenv(): - print("⚠️ Warning: .env file not found") +from db import get_diary_collection +from dotenv import load_dotenv +from openai import OpenAI model = "llama3.2" -# create openai client for local model -client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama") -# connect to chromadb -collection = get_collection() +@lru_cache(maxsize=1) +def get_openai_client(): + """Loads environement variables and creates a cached OpenAI client. + + Returns: + An OpenAI client + """ + load_dotenv() + client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama") + return client def query_entries(user_question: str) -> str: @@ -23,6 +28,10 @@ def query_entries(user_question: str) -> str: Returns: A string response from the AI based on relevant diary entries """ + # create openai client and connect to chromadb + client = get_openai_client() + collection = get_diary_collection() + n = min(3, collection.count()) if n == 0: return "No diary entries found. Record some entries first." @@ -34,7 +43,7 @@ def query_entries(user_question: str) -> str: # ── 2. Build context string ──────────────────────────────────────── context = "" - for doc, meta in zip(documents, metadatas): + for doc, meta in zip(documents, metadatas, strict=True): moods = meta.get("moods", "unknown") or "unknown" tags = meta.get("tags", "none") or "none" context += ( diff --git a/backend/store.py b/backend/store.py index 5abc49e..235335a 100644 --- a/backend/store.py +++ b/backend/store.py @@ -1,11 +1,12 @@ import uuid from datetime import datetime -from db import get_collection +from db import get_diary_collection from models import JournalEntry, MoodTag + # from pydantic import BaseModel -collection = get_collection() +collection = get_diary_collection() def add_entry(transcript: str, moods: list[MoodTag], tags: list[str]):