Enhance query and store functionality: Added .env file loading check with warning, improved context building in query.py, and updated metadata formatting in store.py. Included a test voice entry for better demonstration.

This commit is contained in:
ZTatman 2026-03-13 13:10:15 -04:00
parent 698eba26c7
commit 47fa83d441
2 changed files with 37 additions and 34 deletions

View file

@ -2,7 +2,9 @@ from openai import OpenAI
from dotenv import load_dotenv
from db import get_collection
is_env_vars_loaded = load_dotenv()
if not load_dotenv():
print("⚠️ Warning: .env file not found")
model = "llama3.2"
# create openai client for local model
@ -22,54 +24,39 @@ try:
if user_question.lower() == "q":
break
# create context for prompt to local ai model
# ── 1. Retrieve relevant entries from ChromaDB ────────────────────
results = collection.query(query_texts=[user_question], n_results=3)
context = ""
documents = results["documents"][0]
metadatas = results["metadatas"][0]
# ── 2. Build context string — prompt built AFTER this loop ────────
context = ""
for doc, meta in zip(documents, metadatas):
moods = (
", ".join(
f"{m['name']}({m['intensity']})" for m in meta.get("moods", [])
)
or "unknown"
)
tags = ", ".join(meta.get("tags", [])) or "none"
moods = meta.get("moods", "unknown") or "unknown"
tags = meta.get("tags", "none") or "none"
context += (
f"Date: {meta.get('date', 'unknown')} | Moods: {moods} | Tags: {tags}\n"
)
context += f"Entry: {doc}\n\n"
# ── 3. Build prompt once, after all entries are in context ─────────
prompt = f"""
You are a calm, insightful journaling assistant.
Use ONLY the provided journal entries and metadata.
You are a warm, concise journaling assistant.
Answer in 1-2 sentences maximum.
Always end by asking if the user wants to explore further.
Use only the journal entries below — do not invent details.
If the entries don’t support a clear answer, say so.
Keep it concise (max 120 words).
Journal entries:
{context}
Respond exactly in this format:
------------------------------
Summary: ...
Key pattern:
...
Evidence:
- YYYY-MM-DD: ...
- YYYY-MM-DD: ...
If unsure:
...
------------------------------
Question: {user_question}
"""
Journal Entries:
{context}
Question: {user_question}
"""
# send question to local ai model and print response
# ── 4. Send to model and print response ───────────────────────────
response = client.chat.completions.create(
model=model, messages=[{"role": "user", "content": prompt}]
)
print("\n\n" + response.choices[0].message.content)
user_question = input("\nAsk another question (q to quit): ").strip()
finally:
print("Chat ended...")

View file

@ -26,8 +26,24 @@ def add_entry(transcript: str, moods: list[MoodTag], tags: list[str]):
metadata = {
"date": entry.timestamp.strftime("%Y-%m-%d"),
"moods": [{"name": m.name, "intensity": m.intensity} for m in entry.moods],
"tags": entry.tags,
"moods": ",".join(f"{m.name}:{m.intensity}" for m in entry.moods),
"tags": (",").join(entry.tags),
}
collection.add(ids=[entry.id], documents=[entry.transcript], metadatas=[metadata])
# Test voice entry
add_entry(
transcript=(
"Feeling pretty scattered today. Work was overwhelming and I "
"snapped at a colleague over something small. Didn't sleep well "
"last night and skipped lunch which always makes things worse."
),
moods=[
MoodTag(name="irritable", intensity=7),
MoodTag(name="anxious", intensity=6),
MoodTag(name="ashamed", intensity=4),
],
tags=["work", "poor sleep", "skipped meal"],
)