from itertools import islice
from typing import Iterable, Iterator, TypeVar
import dspy
import requests
from owlapy.agen_kg.few_shot_examples import (
EXAMPLES_FOR_ENTITY_EXTRACTION,
EXAMPLES_FOR_LITERAL_EXTRACTION,
EXAMPLES_FOR_SPL_TRIPLES_EXTRACTION,
EXAMPLES_FOR_TRIPLES_EXTRACTION,
EXAMPLES_FOR_TYPE_ASSERTION,
EXAMPLES_FOR_TYPE_GENERATION,
)
RDFS_COMMENT_IRI = "http://www.w3.org/2000/01/rdf-schema#comment"
RDFS_LABEL_IRI = "http://www.w3.org/2000/01/rdf-schema#label"
# DBpedia often uses British English, so we define a mapping for common American to British English terms.
american_to_british = {
"organization": "organisation",
"color": "colour",
"honor": "honour",
"analyze": "analyse",
"center": "centre",
"meter": "metre",
"theater": "theatre",
"catalog": "catalogue",
"defense": "defence",
"offense": "offence",
"license": "licence", # noun in UK
"practice": "practise", # verb in UK
"traveled": "travelled",
"canceled": "cancelled",
"labeled": "labelled",
"modeling": "modelling",
"program": "programme", # when referring to TV/show
"check": "cheque", # bank sense
"gray": "grey",
"plow": "plough",
}
task_example_mapping = {
"entity_extraction": EXAMPLES_FOR_ENTITY_EXTRACTION,
"triples_extraction": EXAMPLES_FOR_TRIPLES_EXTRACTION,
"type_assertion": EXAMPLES_FOR_TYPE_ASSERTION,
"type_generation": EXAMPLES_FOR_TYPE_GENERATION,
"literal_extraction": EXAMPLES_FOR_LITERAL_EXTRACTION,
"triples_with_numeric_literals_extraction": EXAMPLES_FOR_SPL_TRIPLES_EXTRACTION,
}
[docs]
def run_query(query):
"""Runs a SPARQL query against the DBpedia SPARQL endpoint."""
params = {"query": query, "format": "application/sparql-results+json"}
response = requests.get("http://dbpedia.org/sparql", params=params)
response.raise_for_status()
data = response.json()
return [binding["superclass" if "superclass" in binding else "subclass"]["value"] for binding in data["results"]["bindings"]]
T = TypeVar("T")
[docs]
def chunked_iterator(seq: Iterable[T], size: int = 20) -> Iterator[list[T]]:
"""
Splits an iterable into fixed-size chunks and yields them sequentially.
This function consumes the input iterable lazily and groups its elements
into lists of a maximum given size. It is useful for batching data for
processing, such as API calls or LLM requests.
Args:
seq (Iterable[T]): The input iterable to be chunked.
size (int): The maximum number of elements per chunk. Defaults to 50.
Returns:
Iterator[list[T]]: An iterator over lists, where each list contains
up to `size` elements from the input iterable.
"""
it = iter(seq)
while chunk := list(islice(it, size)):
yield chunk