mirror of
https://github.com/ZTatman/Nadir.git
synced 2026-11-20 12:03:30 +00:00
refactor: fix import time sideffects in query.py, db.py, organize imports
This commit is contained in:
parent
0c0ba74c09
commit
9d4f96e405
5 changed files with 41 additions and 22 deletions
|
|
@ -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
|
||||
}
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -1,8 +1,3 @@
|
|||
import openai
|
||||
import streamlit
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
print("Hello from nadir!")
|
||||
|
||||
|
|
|
|||
|
|
@ -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 += (
|
||||
|
|
|
|||
|
|
@ -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]):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue