Run a Q&A query against the knowledge base. Args: question: The user's question. kb_dir: Root of the knowledge base. model: LLM model name. stream: If True, print response tokens to stdout as they arrive. raw: If True, write raw markdown source instead of
(
question: str,
kb_dir: Path,
model: str,
stream: bool = False,
*,
raw: bool = False,
)
| 244 | |
| 245 | |
| 246 | async def run_query( |
| 247 | question: str, |
| 248 | kb_dir: Path, |
| 249 | model: str, |
| 250 | stream: bool = False, |
| 251 | *, |
| 252 | raw: bool = False, |
| 253 | ) -> str: |
| 254 | """Run a Q&A query against the knowledge base. |
| 255 | |
| 256 | Args: |
| 257 | question: The user's question. |
| 258 | kb_dir: Root of the knowledge base. |
| 259 | model: LLM model name. |
| 260 | stream: If True, print response tokens to stdout as they arrive. |
| 261 | raw: If True, write raw markdown source instead of rendering it |
| 262 | (still keeps tool-call line styling). |
| 263 | |
| 264 | Returns: |
| 265 | The agent's final answer as a string. |
| 266 | """ |
| 267 | import sys |
| 268 | |
| 269 | from agents import RawResponsesStreamEvent, RunItemStreamEvent |
| 270 | from openai.types.responses import ResponseTextDeltaEvent |
| 271 | |
| 272 | from openkb.config import load_config |
| 273 | |
| 274 | openkb_dir = kb_dir / ".openkb" |
| 275 | config = load_config(openkb_dir / "config.yaml") |
| 276 | language: str = config.get("language", "en") |
| 277 | |
| 278 | wiki_root = str(kb_dir / "wiki") |
| 279 | |
| 280 | agent = build_query_agent(wiki_root, model, language=language) |
| 281 | |
| 282 | if not stream: |
| 283 | result = await Runner.run(agent, question, max_turns=MAX_TURNS) |
| 284 | return result.final_output or "" |
| 285 | |
| 286 | import os |
| 287 | |
| 288 | use_color = sys.stdout.isatty() and not os.environ.get("NO_COLOR", "") |
| 289 | |
| 290 | from openkb.agent.chat import ( |
| 291 | _build_style, |
| 292 | _fmt, |
| 293 | _format_tool_line, |
| 294 | _make_markdown, |
| 295 | _make_rich_console, |
| 296 | ) |
| 297 | |
| 298 | style = _build_style(use_color) |
| 299 | |
| 300 | from rich.live import Live |
| 301 | |
| 302 | if use_color and not raw: |
| 303 | console = _make_rich_console() |