-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
73 lines (61 loc) · 2.23 KB
/
cli.py
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
from contextlib import contextmanager
from typing import Any
import click
from art import tprint # type: ignore
from dotenv import load_dotenv
from structlog.testing import capture_logs
from app.helpers.llm import OpenAILlm
from app.helpers.logger import log
from app.helpers.rag import Rag
load_dotenv() # load the environment variables
@contextmanager
@click.command()
@click.option(
"--path",
default="./docs",
help="Path to the directory containing the documents to index.",
)
@click.option(
"--command",
type=click.Choice(["index", "repl"], case_sensitive=False),
help="What do you want to do?\n"
"\nindex: Generate the index for the AI assistant."
"\nrepl: Ask the AI assistant question via a REPL.",
)
def console(path: str, command: str) -> None:
# When running in CLI mode,
# no need to show logs
with capture_logs():
click.echo(tprint("Lucy", font="tarty7"))
if command == "index":
index_files(path, log)
else:
click.echo("Starting the REPL...")
start_repl(OpenAILlm(Rag.get_vector_store(), log))
# Is this what you call self promotion? 😂
click.secho(
"\nCrafted with ❤️ by ©️ clovisphere (https://github.com/clovisphere)",
fg="bright_black",
)
def index_files(path: str, log: Any) -> None:
click.secho("Indexing...", fg="blue")
# TODO: Add progress bar here to show the progress of the indexing
_ = Rag(path, log).etl()
click.secho("Indexing complete! 🎉", fg="green")
def start_repl(llm: OpenAILlm) -> None:
click.secho(
"\nI'm Lucy 🐶, a helpful AI assistant. You can ask me anything.", fg="blue"
)
click.secho("Type 'exit', 'quit', or 'q' to leave the REPL.\n", fg="red")
while True and ((prompt := input("You: ")) not in ["exit", "quit", "q"]):
ai_answer = llm.ask_question(prompt)
click.secho(f"> {ai_answer.strip()}", fg="bright_cyan")
# Leaving us already? 😪
click.secho(
"\nOh noooo! 🐾 You're leaving already? 😪🥺 I'll be here, tail wagging, "
"howling for your return.. woof, woof 🐕",
fg="red",
)
# This is the entry point of the CLI
if __name__ == "__main__":
console()