Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions context_use/cli/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
from context_use.cli.commands.pipeline import PipelineCommand
from context_use.cli.commands.proxy import ProxyCommand
from context_use.cli.commands.reset import ResetCommand
from context_use.cli.commands.search import SearchCommand

TOP_LEVEL_COMMANDS: list[type[BaseCommand]] = [
ProxyCommand,
PipelineCommand,
IngestCommand,
DescribeCommand,
EmbedCommand,
SearchCommand,
ResetCommand,
]

Expand Down
65 changes: 65 additions & 0 deletions context_use/cli/commands/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from __future__ import annotations

import argparse
from typing import TYPE_CHECKING

from context_use.cli import output as out
from context_use.cli.base import ApiCommand
from context_use.cli.config import Config

if TYPE_CHECKING:
from context_use import ContextUse


class SearchCommand(ApiCommand):
name = "search"
help = "Semantic search over embedded threads"
description = (
"Search thread embeddings by semantic similarity. "
"Requires that 'embed' has been run first."
)
llm_mode = "sync"

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument("query", help="Semantic search query")
parser.add_argument(
"--top-k", type=int, default=10, help="Number of results (default: 10)"
)
parser.add_argument(
"--interaction-types",
type=str,
default=None,
help="Comma-separated list of interaction types to filter by",
)

async def run(
self,
cfg: Config,
ctx: ContextUse,
args: argparse.Namespace,
) -> None:
interaction_types: list[str] | None = None
if args.interaction_types:
interaction_types = [t.strip() for t in args.interaction_types.split(",")]

results = await ctx.search_threads(
query=args.query,
top_k=args.top_k,
interaction_types=interaction_types,
)

if not results:
out.warn("No matching threads found.")
return

out.header(f"Search results ({len(results)})")
print()
for i, r in enumerate(results, 1):
sim = out.dim(f"similarity={r.similarity:.4f}")
ts = r.asat.strftime("%Y-%m-%d %H:%M")
print(f" {i}. [{r.interaction_type}] {ts} {sim}")
preview = r.content[:200].replace("\n", " ")
if len(r.content) > 200:
preview += "…"
print(f" {preview}")
print()
Loading