Fact rendering agent for OntoCast.
This module provides functionality for rendering facts from RDF graphs into
human-readable formats, making the extracted knowledge more accessible and
understandable.
render_facts(state, tools)
Render facts from the current chunk into a human-readable format.
This function takes the facts in the current chunk and renders them into a
more accessible format, making the extracted knowledge easier to understand.
Parameters:
Name |
Type |
Description |
Default |
state
|
AgentState
|
The current agent state containing the chunk to render.
|
required
|
tools
|
ToolBox
|
The toolbox instance providing utility functions.
|
required
|
Returns:
Name | Type |
Description |
AgentState |
AgentState
|
Updated state with rendered facts.
|
Source code in ontocast/agent/render_facts.py
| def render_facts(state: AgentState, tools: ToolBox) -> AgentState:
"""Render facts from the current chunk into a human-readable format.
This function takes the facts in the current chunk and renders them into a
more accessible format, making the extracted knowledge easier to understand.
Args:
state: The current agent state containing the chunk to render.
tools: The toolbox instance providing utility functions.
Returns:
AgentState: Updated state with rendered facts.
"""
logger.info("Starting to render facts")
llm_tool = tools.llm
parser = PydanticOutputParser(pydantic_object=SemanticTriplesFactsReport)
ontology_str = state.current_ontology.graph.serialize(format="turtle")
ontology_instruction_str = ontology_instruction.format(
ontology_iri=state.current_ontology.iri, ontology_str=ontology_str
)
prompt = PromptTemplate(
template=template_prompt_str,
input_variables=[
"ontology_namespace",
"current_doc_namespace",
"text",
"ontology_instruction",
"failure_instruction",
"format_instructions",
],
)
try:
if state.status != Status.SUCCESS and state.failure_reason is not None:
failure_instruction = "The previous attempt to generate triples failed."
if state.failure_stage is not None:
failure_instruction += (
f"\n\nIt failed at the stage: {state.failure_stage}"
)
failure_instruction += f"\n\n{state.failure_reason}"
failure_instruction += (
"\n\nPlease fix the errors "
"and do your best to generate fact triples again."
)
else:
failure_instruction = ""
response = llm_tool(
prompt.format_prompt(
ontology_namespace=state.current_ontology.namespace,
current_doc_namespace=state.current_chunk.namespace,
text=state.current_chunk.text,
ontology_instruction=ontology_instruction_str,
failure_instruction=failure_instruction,
format_instructions=parser.get_format_instructions(),
)
)
proj = parser.parse(response.content)
proj.semantic_graph.sanitize_prefixes_namespaces()
if state.current_chunk.graph is not None:
state.current_chunk.graph += proj.semantic_graph
state.clear_failure()
return state
except Exception as e:
logger.error(f"Failed to generate triples: {str(e)}")
state.set_failure(FailureStages.PARSE_TEXT_TO_FACTS_TRIPLES, str(e))
return state
|