Return an LLM answer with local wiki citations.
(
user_id: str,
question: str,
*,
limit: int = 8,
pinned_nodes: Optional[List[Dict[str, Any]]] = None,
allowed_node_ids: Optional[Iterable[str]] = None,
response_language: str = "zh",
)
| 218 | |
| 219 | |
| 220 | def answer_question( |
| 221 | user_id: str, |
| 222 | question: str, |
| 223 | *, |
| 224 | limit: int = 8, |
| 225 | pinned_nodes: Optional[List[Dict[str, Any]]] = None, |
| 226 | allowed_node_ids: Optional[Iterable[str]] = None, |
| 227 | response_language: str = "zh", |
| 228 | ) -> Dict[str, Any]: |
| 229 | """Return an LLM answer with local wiki citations.""" |
| 230 | language = _normalize_response_language(response_language) |
| 231 | started = time.time() |
| 232 | hits, embedding_error = _prepare_hits(user_id, question, limit, pinned_nodes, allowed_node_ids=allowed_node_ids) |
| 233 | if not hits: |
| 234 | return _empty_answer(started, response_language=language) |
| 235 | |
| 236 | prompt = _build_prompt(question, hits) |
| 237 | llm = build_llm_provider() |
| 238 | llm_error = None |
| 239 | response = None |
| 240 | try: |
| 241 | response = llm.generate(prompt, system=_system_prompt(language), temperature=0.0, max_tokens=_answer_max_tokens()) |
| 242 | answer_text = response.text |
| 243 | except Exception as exc: |
| 244 | llm_error = str(exc) |
| 245 | answer_text = _extractive_fallback(hits, llm_error, response_language=language) |
| 246 | citations = _build_citations(user_id, hits) |
| 247 | return { |
| 248 | "text": answer_text, |
| 249 | "citations": citations, |
| 250 | "elapsed_ms": int((time.time() - started) * 1000), |
| 251 | "response_language": language, |
| 252 | "token_usage": _token_usage(llm, response, embedding_error, llm_error), |
| 253 | } |
| 254 | |
| 255 | |
| 256 | def answer_question_stream( |
nothing calls this directly
no test coverage detected