DP_PRACA/tools.py
2026-12-04 09:22:31 +01:00

119 lines
5.0 KiB
Python

from retrieval import retrieve_subgraph
from llm import llm_call
def query_knowledge_graph(question: str, graph, language: str = "Slovak"):
evidence = retrieve_subgraph(graph, question).strip()
if not evidence:
prompt = f"""You are an educational assistant.
Respond only in {language}.
No directly matching graph evidence was retrieved, but that does not prove the
topic is absent from the graph. State that the retrieved context was insufficient,
name the exact topic or document that needs verification, and do not claim it is
absent or invent curriculum details.
Question:
"""
prompt += question
else:
prompt = f"""You are an educational assistant. Respond only in {language}.
Answer using only this graph evidence.
Create a practical, age-appropriate answer for a teacher.
Interpret minor spelling mistakes and paraphrases as the likely meaning of the question.
If an exact topic name is missing, distinguish confirmed concepts from inferred suggestions and do not claim unsupported facts.
Treat every relation and source line as retrieved graph context, not as an external fact.
If the evidence is related but does not directly answer the question, say that the
evidence is partial and identify the missing point instead of filling it from general knowledge.
Evidence:
{evidence}
Question: {question}"""
return {"answer": llm_call([{"role": "user", "content": prompt}]), "evidence": evidence}
def answer_general_question(question: str, graph, language: str = "Slovak"):
evidence = retrieve_subgraph(graph, question).strip()
if not evidence:
prompt = f"""You are a helpful educational assistant for teachers.
Respond only in {language}.
No directly matching evidence was retrieved from the knowledge graph.
Do not say that the topic is definitely absent from the graph.
Answer briefly and clearly without inventing curriculum facts.
Explain what cannot be confirmed and suggest an exact source or concept to verify.
Question: {question}"""
else:
prompt = f"""You are a helpful educational assistant for teachers.
Respond only in {language}.
Answer the question directly in clear, standard prose.
Do not turn the answer into a lesson plan unless the question explicitly asks for one.
Use only the retrieved knowledge graph evidence below. Do not use outside knowledge.
Interpret spelling mistakes and paraphrases naturally.
Distinguish confirmed facts from suggestions or inferences.
If the evidence only partially answers the question, state that clearly and identify
the missing information rather than guessing.
Use this structure when it helps:
1. Direct answer
2. Supporting graph facts
3. Limits or verification needed
Evidence:
{evidence}
Question: {question}"""
return {"answer": llm_call([{"role": "user", "content": prompt}]), "evidence": evidence}
def check_teaching_suitability(
subject: str,
grade: str,
topic: str,
conditions: str,
graph,
language: str = "Slovak",
):
question = (
f"Teaching suitability check for subject {subject}, grade {grade}, topic {topic}. "
f"Specific conditions and constraints: {conditions}. "
"Assess whether the topic and conditions are consistent with the Slovak curriculum and applicable educational rules."
)
evidence = retrieve_subgraph(graph, question).strip()
if not evidence:
prompt = f"""You are a pedagogical advisory assistant.
Respond only in {language}.
No supporting graph evidence was retrieved.
Do not claim that the topic is legally or curriculum-compliant.
Clearly state that there is insufficient evidence and identify which official documents or professional experts should verify it.
Question: {question}
Return a structured answer with:
1. Status: insufficient evidence
2. What can be determined
3. What cannot be determined
4. Required verification
5. Safety and inclusion considerations
"""
else:
prompt = f"""You are a pedagogical advisory assistant.
Respond only in {language}.
Use only the graph evidence below to assess curriculum and educational appropriateness.
Do not present an unsupported conclusion as a legal determination.
If the evidence does not directly cover a law or the required grade/topic, say that verification is needed.
Distinguish curriculum alignment from legal compliance.
Add a short disclaimer that the teacher should confirm the result against the current official school documents and local school rules.
Evidence:
{evidence}
Request: {question}
Return a structured answer with:
1. Status: supported / partial support / insufficient evidence
2. Curriculum alignment
3. Conditions and feasibility
4. Legal or official-source verification needed
5. Inclusion, safety and accessibility considerations
6. Recommended next step
"""
return {"answer": llm_call([{"role": "user", "content": prompt}]), "evidence": evidence}
TOOL_MAP = {
"query_knowledge_graph": query_knowledge_graph,
"answer_general_question": answer_general_question,
"check_teaching_suitability": check_teaching_suitability,
}