mirror of
https://github.com/ZTatman/Nadir.git
synced 2026-11-22 12:57:58 +00:00
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:
parent
698eba26c7
commit
47fa83d441
2 changed files with 37 additions and 34 deletions
51
query.py
51
query.py
|
|
@ -2,7 +2,9 @@ from openai import OpenAI
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from db import get_collection
|
from db import get_collection
|
||||||
|
|
||||||
is_env_vars_loaded = load_dotenv()
|
if not load_dotenv():
|
||||||
|
print("⚠️ Warning: .env file not found")
|
||||||
|
|
||||||
model = "llama3.2"
|
model = "llama3.2"
|
||||||
|
|
||||||
# create openai client for local model
|
# create openai client for local model
|
||||||
|
|
@ -22,54 +24,39 @@ try:
|
||||||
if user_question.lower() == "q":
|
if user_question.lower() == "q":
|
||||||
break
|
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)
|
results = collection.query(query_texts=[user_question], n_results=3)
|
||||||
context = ""
|
|
||||||
documents = results["documents"][0]
|
documents = results["documents"][0]
|
||||||
metadatas = results["metadatas"][0]
|
metadatas = results["metadatas"][0]
|
||||||
|
|
||||||
|
# ── 2. Build context string — prompt built AFTER this loop ────────
|
||||||
|
context = ""
|
||||||
for doc, meta in zip(documents, metadatas):
|
for doc, meta in zip(documents, metadatas):
|
||||||
moods = (
|
moods = meta.get("moods", "unknown") or "unknown"
|
||||||
", ".join(
|
tags = meta.get("tags", "none") or "none"
|
||||||
f"{m['name']}({m['intensity']})" for m in meta.get("moods", [])
|
|
||||||
)
|
|
||||||
or "unknown"
|
|
||||||
)
|
|
||||||
tags = ", ".join(meta.get("tags", [])) or "none"
|
|
||||||
context += (
|
context += (
|
||||||
f"Date: {meta.get('date', 'unknown')} | Moods: {moods} | Tags: {tags}\n"
|
f"Date: {meta.get('date', 'unknown')} | Moods: {moods} | Tags: {tags}\n"
|
||||||
)
|
)
|
||||||
context += f"Entry: {doc}\n\n"
|
context += f"Entry: {doc}\n\n"
|
||||||
|
|
||||||
|
# ── 3. Build prompt once, after all entries are in context ─────────
|
||||||
prompt = f"""
|
prompt = f"""
|
||||||
You are a calm, insightful journaling assistant.
|
You are a warm, concise journaling assistant.
|
||||||
Use ONLY the provided journal entries and metadata.
|
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.
|
Journal entries:
|
||||||
Keep it concise (max 120 words).
|
{context}
|
||||||
|
|
||||||
Respond exactly in this format:
|
Question: {user_question}
|
||||||
------------------------------
|
"""
|
||||||
Summary: ...
|
|
||||||
Key pattern:
|
|
||||||
...
|
|
||||||
Evidence:
|
|
||||||
- YYYY-MM-DD: ...
|
|
||||||
- YYYY-MM-DD: ...
|
|
||||||
If unsure:
|
|
||||||
...
|
|
||||||
------------------------------
|
|
||||||
|
|
||||||
Journal Entries:
|
# ── 4. Send to model and print response ───────────────────────────
|
||||||
{context}
|
|
||||||
|
|
||||||
Question: {user_question}
|
|
||||||
"""
|
|
||||||
# send question to local ai model and print response
|
|
||||||
response = client.chat.completions.create(
|
response = client.chat.completions.create(
|
||||||
model=model, messages=[{"role": "user", "content": prompt}]
|
model=model, messages=[{"role": "user", "content": prompt}]
|
||||||
)
|
)
|
||||||
print("\n\n" + response.choices[0].message.content)
|
print("\n\n" + response.choices[0].message.content)
|
||||||
|
|
||||||
user_question = input("\nAsk another question (q to quit): ").strip()
|
user_question = input("\nAsk another question (q to quit): ").strip()
|
||||||
finally:
|
finally:
|
||||||
print("Chat ended...")
|
print("Chat ended...")
|
||||||
|
|
|
||||||
20
store.py
20
store.py
|
|
@ -26,8 +26,24 @@ def add_entry(transcript: str, moods: list[MoodTag], tags: list[str]):
|
||||||
|
|
||||||
metadata = {
|
metadata = {
|
||||||
"date": entry.timestamp.strftime("%Y-%m-%d"),
|
"date": entry.timestamp.strftime("%Y-%m-%d"),
|
||||||
"moods": [{"name": m.name, "intensity": m.intensity} for m in entry.moods],
|
"moods": ",".join(f"{m.name}:{m.intensity}" for m in entry.moods),
|
||||||
"tags": entry.tags,
|
"tags": (",").join(entry.tags),
|
||||||
}
|
}
|
||||||
|
|
||||||
collection.add(ids=[entry.id], documents=[entry.transcript], metadatas=[metadata])
|
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"],
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue