mirror of
https://github.com/ZTatman/Nadir.git
synced 2026-11-22 04:47:58 +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",
|
"name": "FastAPI backend",
|
||||||
"runtimeExecutable": "uv",
|
"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
|
"port": 8000
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,17 @@
|
||||||
import chromadb
|
from functools import lru_cache
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import chromadb
|
||||||
|
|
||||||
_DB_PATH = str(Path(__file__).parent / "chroma_db")
|
_DB_PATH = str(Path(__file__).parent / "chroma_db")
|
||||||
|
|
||||||
|
|
||||||
def get_collection():
|
@lru_cache(maxsize=1)
|
||||||
"""
|
def get_diary_collection():
|
||||||
Retrieves chromadb collection by either retrieving one that was already created or by creating a new one
|
"""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)
|
client = chromadb.PersistentClient(_DB_PATH)
|
||||||
return client.get_or_create_collection(
|
return client.get_or_create_collection(
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,3 @@
|
||||||
import openai
|
|
||||||
import streamlit
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Hello from nadir!")
|
print("Hello from nadir!")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,22 @@
|
||||||
from openai import OpenAI
|
from functools import lru_cache
|
||||||
from dotenv import load_dotenv
|
|
||||||
from db import get_collection
|
|
||||||
|
|
||||||
if not load_dotenv():
|
from db import get_diary_collection
|
||||||
print("⚠️ Warning: .env file not found")
|
from dotenv import load_dotenv
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
model = "llama3.2"
|
model = "llama3.2"
|
||||||
|
|
||||||
# create openai client for local model
|
|
||||||
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
|
|
||||||
|
|
||||||
# connect to chromadb
|
@lru_cache(maxsize=1)
|
||||||
collection = get_collection()
|
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:
|
def query_entries(user_question: str) -> str:
|
||||||
|
|
@ -23,6 +28,10 @@ def query_entries(user_question: str) -> str:
|
||||||
Returns:
|
Returns:
|
||||||
A string response from the AI based on relevant diary entries
|
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())
|
n = min(3, collection.count())
|
||||||
if n == 0:
|
if n == 0:
|
||||||
return "No diary entries found. Record some entries first."
|
return "No diary entries found. Record some entries first."
|
||||||
|
|
@ -34,7 +43,7 @@ def query_entries(user_question: str) -> str:
|
||||||
|
|
||||||
# ── 2. Build context string ────────────────────────────────────────
|
# ── 2. Build context string ────────────────────────────────────────
|
||||||
context = ""
|
context = ""
|
||||||
for doc, meta in zip(documents, metadatas):
|
for doc, meta in zip(documents, metadatas, strict=True):
|
||||||
moods = meta.get("moods", "unknown") or "unknown"
|
moods = meta.get("moods", "unknown") or "unknown"
|
||||||
tags = meta.get("tags", "none") or "none"
|
tags = meta.get("tags", "none") or "none"
|
||||||
context += (
|
context += (
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from db import get_collection
|
from db import get_diary_collection
|
||||||
from models import JournalEntry, MoodTag
|
from models import JournalEntry, MoodTag
|
||||||
|
|
||||||
# from pydantic import BaseModel
|
# from pydantic import BaseModel
|
||||||
|
|
||||||
collection = get_collection()
|
collection = get_diary_collection()
|
||||||
|
|
||||||
|
|
||||||
def add_entry(transcript: str, moods: list[MoodTag], tags: list[str]):
|
def add_entry(transcript: str, moods: list[MoodTag], tags: list[str]):
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue