-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
183 lines (142 loc) · 5.84 KB
/
Copy pathcli.py
File metadata and controls
183 lines (142 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
LMD-GPT CLI — Personal AI trained on your data.
Commands:
ingest [--sources obsidian discord gdocs gcal] Embed data into vector store
chat [--source <filter>] RAG chat loop
finetune prepare [--username NAME] Build training dataset
finetune train [--resume CHECKPOINT] QLoRA fine-tune
finetune export Merge + prep for Ollama
status Show system + store info
"""
import argparse
import sys
# -----------------------------------------------------------------------
# Command handlers
# -----------------------------------------------------------------------
def cmd_ingest(args):
from embeddings.store import VectorStore
store = VectorStore()
sources = args.sources or ["obsidian", "discord", "gdocs", "gcal"]
if "obsidian" in sources:
from ingestion.obsidian import load_all
print("Ingesting Obsidian notes…")
docs = load_all()
n = store.add_documents(docs, chunk=True, source="obsidian")
print(f" → {n} chunks from {len(docs)} notes")
if "discord" in sources:
from ingestion.discord import load_all
print("Ingesting Discord messages…")
docs = load_all()
# Messages are already short — don't chunk
n = store.add_documents(docs, chunk=False, source="discord")
print(f" → {n} messages")
if "gdocs" in sources:
from ingestion.gdocs import load_all
print("Ingesting Google Docs…")
docs = load_all()
n = store.add_documents(docs, chunk=True, source="gdocs")
print(f" → {n} chunks from {len(docs)} docs")
if "gcal" in sources:
from ingestion.gcal import load_all
print("Ingesting Google Calendar…")
docs = load_all()
n = store.add_documents(docs, chunk=False, source="gcal")
print(f" → {n} events")
print(f"\nVector store total: {store.count():,} chunks")
def cmd_chat(args):
from rag.chain import stream_chat
source_label = f" [{args.source}]" if args.source else ""
print(f"LMD-GPT (RAG mode{source_label}) — type 'exit' to quit\n")
history: list[dict] = []
while True:
try:
query = input("You: ").strip()
except (EOFError, KeyboardInterrupt):
print("\nBye!")
break
if not query:
continue
if query.lower() in ("exit", "quit", "q"):
break
print("AI: ", end="", flush=True)
response_parts: list[str] = []
try:
for token in stream_chat(query, source_filter=args.source, history=history):
print(token, end="", flush=True)
response_parts.append(token)
except KeyboardInterrupt:
pass
print()
full_response = "".join(response_parts)
history.append({"role": "user", "content": query})
history.append({"role": "assistant", "content": full_response})
history = history[-12:] # keep last 6 turns
def cmd_finetune_prepare(args):
from finetune.prepare_data import prepare
prepare(username=args.username)
def cmd_finetune_train(args):
from finetune.train import train
train(resume_from=args.resume)
def cmd_finetune_export(_args):
from finetune.export import merge_and_save
merge_and_save()
def cmd_status(_args):
from embeddings.store import VectorStore
print("=== Vector Store ===")
store = VectorStore()
print(f" Chunks stored: {store.count():,}")
print("\n=== GPU / Deps ===")
# Re-use the check script logic inline
import scripts.check_gpu as cg
cg.check_nvidia_smi()
cg.check_torch_cuda()
cg.check_bitsandbytes()
print("\n=== Ollama ===")
cg.check_ollama()
cg.check_embedding_model()
cg.check_inference_model()
# -----------------------------------------------------------------------
# CLI wiring
# -----------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
prog="lmd-gpt",
description="Personal AI built from your own data",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
sub = parser.add_subparsers(dest="command", required=True)
# --- ingest ---
p_ingest = sub.add_parser("ingest", help="Embed data into the vector store")
p_ingest.add_argument(
"--sources", nargs="+",
choices=["obsidian", "discord", "gdocs", "gcal"],
help="Which sources to ingest (default: all)",
)
p_ingest.set_defaults(func=cmd_ingest)
# --- chat ---
p_chat = sub.add_parser("chat", help="Start a RAG chat session")
p_chat.add_argument(
"--source",
choices=["obsidian", "discord", "gdocs", "gcal"],
help="Restrict retrieval to a single source",
)
p_chat.set_defaults(func=cmd_chat)
# --- finetune ---
p_ft = sub.add_parser("finetune", help="Fine-tune the model on your data")
ft_sub = p_ft.add_subparsers(dest="ft_command", required=True)
p_prep = ft_sub.add_parser("prepare", help="Build the training JSONL")
p_prep.add_argument("--username", help="Override the name used in training prompts")
p_prep.set_defaults(func=cmd_finetune_prepare)
p_train = ft_sub.add_parser("train", help="Run QLoRA training")
p_train.add_argument("--resume", metavar="CHECKPOINT", help="Resume from a checkpoint dir")
p_train.set_defaults(func=cmd_finetune_train)
p_export = ft_sub.add_parser("export", help="Merge LoRA weights and prep for Ollama")
p_export.set_defaults(func=cmd_finetune_export)
# --- status ---
p_status = sub.add_parser("status", help="Show system + vector store status")
p_status.set_defaults(func=cmd_status)
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()