complete refactor
This commit is contained in:
+88
-51
@@ -1,59 +1,96 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ArgumentSpec:
|
||||
name: str
|
||||
help: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CommandSpec:
|
||||
name: str
|
||||
aliases: tuple[str, ...]
|
||||
help: str
|
||||
arguments: tuple[ArgumentSpec, ...] = ()
|
||||
|
||||
|
||||
COMMAND_SPECS: tuple[CommandSpec, ...] = (
|
||||
CommandSpec(
|
||||
name="list-collections",
|
||||
aliases=("lc",),
|
||||
help="List all collections stored in the local Chroma database.",
|
||||
),
|
||||
CommandSpec(
|
||||
name="create-collection",
|
||||
aliases=("cc",),
|
||||
help="Create a collection in the local Chroma database.",
|
||||
arguments=(
|
||||
ArgumentSpec("collection", "Name of the collection to create."),
|
||||
),
|
||||
),
|
||||
CommandSpec(
|
||||
name="delete-collection",
|
||||
aliases=("dc",),
|
||||
help="Delete a collection from the local Chroma database.",
|
||||
arguments=(
|
||||
ArgumentSpec("collection", "Name of the collection to delete."),
|
||||
),
|
||||
),
|
||||
CommandSpec(
|
||||
name="count",
|
||||
aliases=("co",),
|
||||
help="Count records in a collection from the local Chroma database.",
|
||||
arguments=(
|
||||
ArgumentSpec("collection", "Name of the collection to count."),
|
||||
),
|
||||
),
|
||||
CommandSpec(
|
||||
name="add-data",
|
||||
aliases=("ad",),
|
||||
help="Chunk, embed, and add a file to a collection in the local Chroma database.",
|
||||
arguments=(
|
||||
ArgumentSpec("collection", "Name of the target collection."),
|
||||
ArgumentSpec("file", "Path to the file to chunk and add to the collection."),
|
||||
),
|
||||
),
|
||||
CommandSpec(
|
||||
name="query",
|
||||
aliases=("q",),
|
||||
help="Query a collection with the provided text.",
|
||||
arguments=(
|
||||
ArgumentSpec("collection", "Name of the target collection."),
|
||||
ArgumentSpec("query_text", "The text to query."),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _add_command(
|
||||
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
|
||||
command: CommandSpec,
|
||||
) -> None:
|
||||
subparser = subparsers.add_parser(
|
||||
command.name,
|
||||
aliases=list(command.aliases),
|
||||
help=command.help,
|
||||
description=command.help,
|
||||
)
|
||||
|
||||
for argument in command.arguments:
|
||||
subparser.add_argument(argument.name, help=argument.help)
|
||||
|
||||
subparser.set_defaults(command=command.name)
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="Inspect local Chroma collections.")
|
||||
subparsers = parser.add_subparsers(dest="command")
|
||||
subparsers = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
# List existing collections
|
||||
subparsers.add_parser(
|
||||
"list-collections",
|
||||
aliases=["lc"],
|
||||
help="List all collections stored in the local Chroma database.",
|
||||
)
|
||||
|
||||
# Create a new collection
|
||||
create_parser = subparsers.add_parser(
|
||||
"create-collection",
|
||||
aliases=["cc"],
|
||||
help="Create a collection in the local Chroma database.",
|
||||
)
|
||||
create_parser.add_argument("name", help="Name of the collection to create.")
|
||||
|
||||
# Delete a collection
|
||||
delete_parser = subparsers.add_parser(
|
||||
"delete-collection",
|
||||
aliases=["dc"],
|
||||
help="Delete a collection from the local Chroma database.",
|
||||
)
|
||||
delete_parser.add_argument("name", help="Name of the collection to delete.")
|
||||
|
||||
# Count documents in a collection
|
||||
count_parser = subparsers.add_parser(
|
||||
"count",
|
||||
aliases=["co"],
|
||||
help="Count records in a collection from the local Chroma database.",
|
||||
)
|
||||
count_parser.add_argument("name", help="Name of the collection to count.")
|
||||
|
||||
# Add documents to a collection
|
||||
add_parser = subparsers.add_parser(
|
||||
"add-data",
|
||||
aliases=["ad"],
|
||||
help="Chunk, embed, and add a file to a collection in the local Chroma database.",
|
||||
)
|
||||
add_parser.add_argument("collection", help="Name of the target collection.")
|
||||
add_parser.add_argument(
|
||||
"file", help="Path to the file to chunk and add to the collection."
|
||||
)
|
||||
|
||||
# Query doc
|
||||
query_parser = subparsers.add_parser(
|
||||
"query",
|
||||
aliases=["q"],
|
||||
help="Query a collection with given text/s.",
|
||||
)
|
||||
query_parser.add_argument("collection", help="Name of the target collection.")
|
||||
query_parser.add_argument("texts", help="The text/s to query.")
|
||||
for command in COMMAND_SPECS:
|
||||
_add_command(subparsers, command)
|
||||
|
||||
return parser
|
||||
|
||||
Reference in New Issue
Block a user