Add QA to client, cli

This commit is contained in:
Yiorgis Gozadinos 2025-06-28 09:21:45 +03:00
parent 85b106c461
commit 4bbc23dbd4
No known key found for this signature in database
3 changed files with 41 additions and 1 deletions

View file

@ -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:

View file

@ -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)"
)

View file

@ -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()