From 4bbc23dbd40925bcf29b8c094546e43ea4a75c50 Mon Sep 17 00:00:00 2001 From: Yiorgis Gozadinos Date: Sat, 28 Jun 2025 09:21:45 +0300 Subject: [PATCH] Add QA to client, cli --- src/haiku/rag/app.py | 11 +++++++++++ src/haiku/rag/cli.py | 15 +++++++++++++++ src/haiku/rag/client.py | 16 +++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/haiku/rag/app.py b/src/haiku/rag/app.py index 6db14c83..7e33ba5b 100644 --- a/src/haiku/rag/app.py +++ b/src/haiku/rag/app.py @@ -61,6 +61,17 @@ class HaikuRAGApp: for chunk, score in results: self._rich_print_search_result(chunk, score) + async def ask(self, question: str): + async with HaikuRAG(db_path=self.db_path) as self.client: + try: + answer = await self.client.ask(question) + self.console.print(f"[bold blue]Question:[/bold blue] {question}") + self.console.print() + self.console.print("[bold green]Answer:[/bold green]") + self.console.print(Markdown(answer)) + except Exception as e: + self.console.print(f"[red]Error: {e}[/red]") + def _rich_print_document(self, doc: Document, truncate: bool = False): """Format a document for display.""" if truncate: diff --git a/src/haiku/rag/cli.py b/src/haiku/rag/cli.py index 71e2c8b9..2e012cf1 100644 --- a/src/haiku/rag/cli.py +++ b/src/haiku/rag/cli.py @@ -113,6 +113,21 @@ def search( event_loop.run_until_complete(app.search(query=query, limit=limit, k=k)) +@cli.command("ask", help="Ask a question using the QA agent") +def ask( + question: str = typer.Argument( + help="The question to ask", + ), + db: Path = typer.Option( + get_default_data_dir() / "haiku.rag.sqlite", + "--db", + help="Path to the SQLite database file", + ), +): + app = HaikuRAGApp(db_path=db) + event_loop.run_until_complete(app.ask(question=question)) + + @cli.command( "serve", help="Start the haiku.rag MCP server (by default in streamable HTTP mode)" ) diff --git a/src/haiku/rag/client.py b/src/haiku/rag/client.py index 920f262a..0f24b3b9 100644 --- a/src/haiku/rag/client.py +++ b/src/haiku/rag/client.py @@ -36,7 +36,7 @@ class HaikuRAG: """Async context manager entry.""" return self - async def __aexit__(self, exc_type, exc_val, exc_tb): + async def __aexit__(self, exc_type, exc_val, exc_tb): # noqa: ARG002 """Async context manager exit.""" self.close() return False @@ -256,6 +256,20 @@ class HaikuRAG: """ return await self.chunk_repository.search_chunks_hybrid(query, limit, k) + async def ask(self, question: str) -> str: + """Ask a question using the configured QA agent. + + Args: + question: The question to ask + + Returns: + The generated answer as a string + """ + from haiku.rag.qa import get_qa_agent + + qa_agent = get_qa_agent(self) + return await qa_agent.answer(question) + def close(self): """Close the underlying store connection.""" self.store.close()