Skip to main content
Output processing code that is specific to the Granite 3 family of models, but not specific to a particular point release.

Functions

FUNC create_dict

create_dict(input_array, **key_attrib_names: str) -> dict
Return a dict indexed by given attributes from an array of dicts. Given an array of dicts and the name of attribute(s) within the array, return a dict containing the contents of the array indexed by the given attribute(s) Args:
  • input_array: Iterable of dicts to index.
  • **key_attrib_names: Keyword arguments whose values are attribute names within each dict. The resulting key is a hyphen-joined concatenation of those attribute values.
Returns:
  • Dict mapping the concatenated attribute value(s) to the original dict entry.

FUNC parse_hallucinations_text

parse_hallucinations_text(hallucinations_text: str) -> list[dict]
Extract hallucinations info as an array from the ”# Hallucinations:” section. Given the hallucinations text output by model under the ”# Hallucinations:” section, extract the hallucinations info as an array of the form: [ { “hallucination_id”: “Hallucination ID output by model”, “risk”: “Hallucination risk flag”, “response_text”: “Substring of response text for which hallucination risk is computed” }, … ] Args:
  • hallucinations_text: Raw text from the model’s ”# Hallucinations:” section.
Returns:
  • List of dicts, each with hallucination_id, risk, and response_text keys.

FUNC add_hallucination_response_spans

add_hallucination_response_spans(hallucination_info: list[dict], response_text_without_citations: str, remove_citations_from_response_text: Callable) -> list[dict]
Add response spans to hallucination info. Given the response text (cleaned from citation tags) and a parsed hallucinations info of the form: [ { “hallucination_id”: “Hallucination ID output by model”, “risk”: “Hallucination risk flag”, “response_text”: “Substring of response text for which hallucination risk is computed” }, … ] add to each hallucination element in the array the following attributes (the “response_text” replaces the attribute of the same name): “response_text”: “The response text corresponding to the hallucination element cleaned from citation tags” “response_begin”: “The begin index of “response_text” within the response text (without citation tags)” “response_end”: “The end index of “response_text” within the response text (without citation tags)” Args:
  • hallucination_info: Parsed hallucination list as returned by parse_hallucinations_text.
  • response_text_without_citations: Full response text with citation tags removed.
  • remove_citations_from_response_text: Callable that strips citation tags from a substring of the response.
Returns:
  • Deep copy of hallucination_info with response_text, response_begin,
  • and response_end populated for each entry.

FUNC add_citation_context_spans

add_citation_context_spans(citation_info: list[dict], docs: list[dict]) -> list[dict]
Add context spans to citation info. Given a set of docs and an array of citations of the form: [ { “citation_id”: “Citation ID output by model”, “doc_id”: “ID of doc where the cited text is drawn from”, “context_text”: “The cited text from the context” }, … ] add to each citation in the array the following two attributes: “context_begin”: “The begin index of “context_text” within document with ID doc_id” “context_end”: “The end index of “context_text” within document with ID doc_id” Args:
  • citation_info: List of citation dicts as produced by the model output parser.
  • docs: List of source document dicts, each with citation_id, doc_id, and text keys.
Returns:
  • Deep copy of citation_info with context_begin and context_end
  • populated for each entry.