A question answering (QA) system is a software application that takes a user's question in natural language and provides a direct, relevant answer by processing the question's intent and retrieving information from a knowledge source or generating a new response.
接下来需要从给定 query 中抽取 entities 和 relations(对应上文的第 1 步)。我构造了一个基础的 system prompt,如下所示:
system_prompt_key_words="""You are a helpful assistant, expert of English language who can extracts keyword from the given question in root form (e.g. ran becomes run) and lowercase. The returned keywords should be critical to answer the question. Categorize the keywords into entity and relation keywords. keywords must be in root form and lowercase. The response should be in following format, no additional text: {"entity": [list of entity keywords], "relation": [list of relation keywords]}"""
context =f""" You are given facts from a knowledge graph:
{triples}
Answer the user query based ONLY on these facts. Answer in full sentence. Query:{query} """ response = ollama.chat(model="gemma3:4b-it-qat", messages=[{"role":"user","content": context}]) print(f'query:{query}\nAnswer:{response["message"]["content"]}')
系统无法回答 “what is the warranty period?”,因为在图里 warranty 是 relation 的 label,但它从问题中被抽取成了 named entity,导致系统找不到任何 edge。因此,我们用于构建 knowledge graph 的核心 system prompt 还需要优化。
还有一些问题需要稍微改写后系统才能回答。但我发现这类问题最终都可以归因于 KG 的构建方式或从 query 中抽取的 keyword。两者都可以通过改进 prompts 来修复。比如,我使用的图中有一条 edge 是:
phone → support_dual_sim → nano sim
这显然并不理想。但这些都可以通过更谨慎地设计用于构建 KG 的 prompt 来修正。正如我在上一篇文章中提到的,我最初是用 chatGPT 生成的 prompt,并在此基础上略作修改。在真实生产场景中,应当花更多时间打磨 prompt。对于企业级应用,还可以尝试更大的 model,因为资源限制不再是问题。