### Install OntoLearner Source: https://github.com/sciknoworg/ontolearner/blob/main/README.md Install the OntoLearner library using pip. This is the standard way to get the latest stable version. ```bash pip install ontolearner ``` -------------------------------- ### Set Up Virtual Environment and Install Dependencies Source: https://github.com/sciknoworg/ontolearner/blob/main/CONTRIBUTING.md Create and activate a Python 3.9 virtual environment, install project dependencies, and set up pre-commit hooks. Ensure you have conda installed. ```bash conda create -n my_env python=3.9 conda activate my_env pip install -r requirements.txt pre-commit install ``` -------------------------------- ### Install OntoLearner from Source Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/installation.rst Install OntoLearner in editable mode from the cloned repository, ideal for development. ```bash pip install -e . ``` -------------------------------- ### Verify OntoLearner Installation Source: https://github.com/sciknoworg/ontolearner/blob/main/README.md Verify the installation by importing the library and printing its version. This confirms that the installation was successful. ```python import ontolearner print(ontolearner.__version__) ``` -------------------------------- ### Install OntoLearner from GitHub Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/installation.rst Install the latest development version of OntoLearner directly from its GitHub repository using pip. ```bash pip install git+https://github.com/sciknoworg/OntoLearner.git ``` -------------------------------- ### Ontolearner Pipeline Setup Source: https://github.com/sciknoworg/ontolearner/blob/main/examples/pipeline.ipynb Sets up environment variables and task definition for Ontolearner pipelines. Ensure HUGGINGFACE_ACCESS_TOKEN and OPENAI_KEY are set if required by the chosen models. ```python HUGGINGFACE_ACCESS_TOKEN=" " OPENAI_KEY=" " TASK = 'taxonomy-discovery' ``` -------------------------------- ### Clone OntoLearner Repository Source: https://github.com/sciknoworg/ontolearner/blob/main/CONTRIBUTING.md Clone the OntoLearner repository to your local machine. This is the first step to start contributing. ```bash git clone git@github.com:sciknoworg/OntoLearner.git cd OntoLearner ``` -------------------------------- ### LLMAugmentedRetrieverLearner Example for Taxonomy Discovery Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/retrieval.rst Demonstrates the end-to-end usage of LLMAugmentedRetrieverLearner for taxonomy discovery, including data loading, augmentation generation, model training, prediction, and evaluation. ```python from ontolearner.learner.retriever import LLMAugmenterGenerator, LLMAugmentedRetriever, LLMAugmenter from ontolearner import LLMAugmentedRetrieverLearner, Wine, train_test_split, evaluation_report ontology = Wine() ontology.load() ontological_data = ontology.extract() train_data, test_data = train_test_split(ontological_data, test_size=0.2, random_state=42) task="taxonomy-discovery" llm_augmenter_generator = LLMAugmenterGenerator(model_id='gpt-4.1-mini', token = 'your_openai_token', top_n_candidate=10) augments = {"config": llm_augmenter_generator.get_config()} augments[task] = llm_augmenter_generator.augment(ontological_data, task=task) base_retriever = LLMAugmentedRetriever() learner = LLMAugmentedRetrieverLearner(base_retriever=base_retriever) learner.set_augmenter(augments) learner.load(model_id="Qwen/Qwen3-Embedding-8B") # Train, Predict, and Evaluate learner.fit(train_data, task=task) predictions = learner.predict(test_data, task=task) truth = learner.tasks_ground_truth_former(test_data, task=task) metrics = evaluation_report(truth, predictions, task=task) print(metrics) ``` -------------------------------- ### Initialize Learner Pipeline with Retriever Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/retrieval.rst Demonstrates using the LearnerPipeline with a retriever-only mode by specifying 'retriever_id'. This example loads the AgrO ontology, extracts data, and splits it into training and test sets. ```python # Import core components from the OntoLearner library from ontolearner import LearnerPipeline, AgrO, train_test_split # Load the AgrO ontology, which includes structured agricultural knowledge ontology = AgrO() ontology.load() # Load ontology data (e.g., entities, relations, metadata) # Extract relation instances from the ontology and split them into training and test sets train_data, test_data = train_test_split( ontology.extract(), # Extract annotated (head, tail, relation) triples test_size=0.2, # 20% for evaluation random_state=42 # Ensures reproducible splits ) # Initialize the learning pipeline using a dense retriever ``` -------------------------------- ### Offline Augmentation Loading and Retriever Setup Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/retrieval.rst Loads previously saved augmentation data and sets up an LLM-augmented retriever. This is recommended for large experiments to avoid repeated LLM calls. ```python # Step 1 — Load augmenter from ontolearner.learner.retriever import LLMAugmenter augmenter = LLMAugmenter("augment.json") # Step 2 — Attach it to the retriever from ontolearner.learner.retriever import LLMAugmentedRetriever from ontolearner.learner import LLMAugmentedRetrieverLearner base_retriever = LLMAugmentedRetriever() learner = LLMAugmentedRetrieverLearner(base_retriever=base_retriever) learner.set_augmenter(augmenter) learner.load(model_id="Qwen/Qwen3-Embedding-8B") # path to desired retriever model. ``` -------------------------------- ### Initialize RAG Learner Components Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/rag.rst Initializes the LLM learner with prompting and label mapping strategies, and the retriever learner to find relevant examples. Requires a Hugging Face API token for LLM access. The top_k parameter for the retriever specifies the number of top candidates to use as context. ```python task = 'non-taxonomic-re' # Provide your huggingface token for LLM access token = '...' # Initialize the LLM learner with prompting and label mapping strategies llm_learner = AutoLLMLearner( prompting=StandardizedPrompting, # Use a pre-defined prompt format label_mapper=LabelMapper(), # Convert between label formats and natural language token=token # API token to access the LLM ) # Initialize the retriever to find top-k relevant facts or examples retriever_learner = AutoRetrieverLearner(top_k=2) # Use top 2 retrieved candidates # Create a RAG (Retrieval-Augmented Generation) pipeline by combining LLM and retriever rag_learner = AutoRAGLearner( llm=llm_learner, retriever=retriever_learner ) # Load the retriever and LLM models by their names or IDs rag_learner.load( ) ``` -------------------------------- ### Metrics Calculation Example Source: https://github.com/sciknoworg/ontolearner/blob/main/examples/pipeline.ipynb This snippet shows an example of calculated metrics including f1_score, precision, recall, total_correct, total_predicted, and total_ground_truth. ```python Metrics: {'f1_score': 0.10596026490066225, 'precision': 0.056338028169014086, 'recall': 0.8888888888888888, 'total_correct': 8, 'total_predicted': 142, 'total_ground_truth': 9} ``` -------------------------------- ### Example RDF Structure for OntoLearner Metadata Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/ontologizer/metadata.rst This snippet shows the RDF/XML structure generated by the OntoLearner Metadata Exporter. It includes a top-level collection resource and examples of individual ontology metadata, demonstrating the use of Dublin Core and custom ontologizer namespaces. ```xml OntoLearner Benchmark Ontologies This Dublin Core metadata collection describes ontologies benchmarked in OntoLearner. It includes information such as title, creator, format, license, and version. OntoLearner Team MIT License 1.4.0 NCIt NCI Thesaurus (NCIt) NCI Thesaurus (NCIt) is a reference terminology that includes broad coverage of the cancer domain... OWL 2023-10-19 Creative Commons 4.0 https://terminology.tib.eu/ts/ontologies/NCIT Medicine Cancer, Oncology 24.04e ``` -------------------------------- ### Configure and Run Learner Pipeline Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/index.rst Demonstrates how to configure a LearnerPipeline with retriever and LLM IDs, set batch size and top_k, and then run the pipeline for training, prediction, and evaluation on a specified task. It also shows how to access and print the computed metrics, elapsed time, and all pipeline outputs. ```python train_data, test_data = train_test_split( ontology.extract(), test_size=0.2, random_state=42 ) # RAG can be configured either by passing both IDs (shown here), # or by passing a prebuilt `rag=` learner object. pipeline = LearnerPipeline( retriever_id='sentence-transformers/all-MiniLM-L6-v2', llm_id='Qwen/Qwen2.5-0.5B-Instruct', hf_token='...', batch_size=32, top_k=5 ) # Run the pipeline: training, prediction, and evaluation in one call outputs = pipeline( train_data=train_data, test_data=test_data, evaluate=True, # Compute metrics like precision, recall, and F1 task='term-typing' # Specifies the task ) # Print final evaluation metrics print("Metrics:", outputs['metrics']) # Print the total time taken for the full pipeline execution print("Elapsed time:", outputs['elapsed_time']) # Print all outputs print(outputs) ``` -------------------------------- ### Initialize SBUNLPFewShotLearner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llms4ol_challenge/sbunlp_learner.rst Configures and initializes the SBUNLPFewShotLearner with a Qwen model for few-shot taxonomy discovery. Sets parameters for model decoding, prompting, and output. ```python from ontolearner import SBUNLPFewShotLearner task = "taxonomy-discovery" llm_learner = SBUNLPFewShotLearner( # Model / decoding model_name="Qwen/Qwen2.5-0.5B-Instruct", try_4bit=True, # use 4-bit if bitsandbytes + CUDA are available max_new_tokens=140, # limit the length of the model's response (JSON output) max_input_tokens=1500, # limit the total prompt length (context window) temperature=0.0, # set to 0.0 for deterministic output top_p=1.0, # Grid settings (N × M prompts) n_train_chunks=7, # N: split training examples into 7 chunks m_test_chunks=7, # M: split test terms into 7 chunks (total 49 prompts) # Run controls limit_prompts=None, # None runs all N × M prompts; set an int for a restricted run output_dir="./outputs/taskC_batches", # dump per-prompt JSON results for debugging ) # Load the underlying LLM and prepare it for few-shot taxonomy discovery llm_learner.load(llm_id=llm_learner.model_name) ``` -------------------------------- ### Elapsed Time Tracking Example Source: https://github.com/sciknoworg/ontolearner/blob/main/examples/pipeline.ipynb This snippet demonstrates how to record the elapsed time for a process, measured in seconds. ```python Elapsed time: 22.675782442092896 ``` -------------------------------- ### Initialize SBUNLPFewShotLearner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llms4ol_challenge/sbunlp_learner.rst Configure the SBUNLP few-shot learner for the Text2Onto task. This learner utilizes an LLM for predictions on synthetic Text2Onto-style samples. Specify the LLM model ID, device (CPU or CUDA), maximum new tokens, and an output directory. ```python from ontolearner.learner.text2onto import SBUNLPFewShotLearner text2onto_learner = SBUNLPFewShotLearner( llm_model_id="Qwen/Qwen2.5-0.5B-Instruct", device="cpu", # set "cuda" if available max_new_tokens=256, output_dir="./results/", ) ``` -------------------------------- ### Load Ontology and Extract Data Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learning_tasks/llms4ol.rst Demonstrates loading an ontology and extracting data for LLMs4OL tasks. Ensure the 'ontolearner' library is installed. ```python from ontolearner import Wine ontology = Wine() ontology.load() data = ontology.extract() print(data) ``` -------------------------------- ### LearnerPipeline with RAG Configuration Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/rag.rst Demonstrates setting up and running a `LearnerPipeline` that utilizes RAG. This involves initializing the pipeline with retriever and LLM IDs, and then calling the pipeline with training and test data for a specific task. It prints evaluation metrics and elapsed time. ```python from ontolearner import ( LearnerPipeline, AutoLLMLearner, AutoRetrieverLearner, AutoRAGLearner, LabelMapper, StandardizedPrompting, AgrO, train_test_split, ) ontology = AgrO() ontology.load() # Load entities, types, and structured term annotations from the ontology ontological_data = ontology.extract() # Extract term-typing instances and split into train and test sets train_data, test_data = train_test_split( ontological_data, test_size=0.2, # Use 20% of the data for evaluation random_state=42 # Ensure reproducibility of the data split ) # Initialize a multi-component learning pipeline (retriever + LLM) # This configuration enables a Retrieval-Augmented Generation (RAG) setup pipeline = LearnerPipeline( retriever_id='sentence-transformers/all-MiniLM-L6-v2', # Dense retriever model for nearest neighbor search llm_id='Qwen2.5-0.5B-Instruct', # Lightweight instruction-tuned LLM for reasoning hf_token='...', # Hugging Face token for accessing gated models batch_size=32, # Batch size for training/prediction if supported top_k=5 # Number of top retrievals to include in RAG prompting ) # Run the pipeline: training, prediction, and evaluation in one call outputs = pipeline( train_data=train_data, test_data=test_data, evaluate=True, # Compute metrics like precision, recall, and F1 task='term-typing' # Specifies the task ) # Print final evaluation metrics print("Metrics:", outputs['metrics']) # Print the total time taken for the full pipeline execution print("Elapsed time:", outputs['elapsed_time']) # Print all outputs (including predictions) print(outputs) ``` -------------------------------- ### Initialize SKHNLPSequentialFTLearner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llms4ol_challenge/skhnlp_learner.rst Configure and initialize the BERT-based learner for supervised fine-tuning. Note that CPU training for BERT-Large is slow. ```python from ontolearner import SKHNLPSequentialFTLearner bert_learner = SKHNLPSequentialFTLearner( model_name="bert-large-uncased", n_prompts=2, random_state=1403, device="cpu", # Note: CPU training for BERT-Large is slow. output_dir="./results/", num_train_epochs=1, per_device_train_batch_size=8, per_device_eval_batch_size=8, warmup_steps=500, weight_decay=0.01, logging_dir="./logs/", logging_steps=50, eval_strategy="epoch", save_strategy="epoch", load_best_model_at_end=True, ) # Load the base BERT model and prepare it for supervised taxonomy discovery bert_learner.load(llm_id=bert_learner.model_name) ``` -------------------------------- ### Load Ontological Data and Split Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llm.rst Imports necessary components, loads an ontology, extracts data, and performs a train-test split. Ensure you have the ontolearner package installed. ```python from ontolearner import AutoLLMLearner, AgrO, train_test_split, LabelMapper, StandardizedPrompting, evaluation_report ontology = AgrO() ontology.load() ontological_data = ontology.extract() train_data, test_data = train_test_split(ontological_data, test_size=0.2, random_state=42) ``` -------------------------------- ### Initialize SBU-NLP Zero-Shot Learner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llms4ol_challenge/sbunlp_learner.rst Initializes the SBUNLPZSLearner with a Qwen model for zero-shot term typing. The learner's fit phase infers the type inventory without fine-tuning the model. ```python from ontolearner import SBUNLPZSLearner llm_learner = SBUNLPZSLearner( # Model / decoding model_id="Qwen/Qwen2.5-0.5B-Instruct", max_new_tokens=64, # sufficient length for JSON list of types temperature=0.0, # deterministic (greedy) output # token=None, # assuming public model access ) # Load the underlying LLM and prepare it for zero-shot term typing llm_learner.load(llm_id=llm_learner.model_id) ``` -------------------------------- ### Format Code with Ruff Source: https://github.com/sciknoworg/ontolearner/blob/main/CONTRIBUTING.md Apply code formatting using the ruff tool to ensure adherence to style guides. This command automatically fixes most formatting issues. ```bash ruff check --fix . ``` -------------------------------- ### Initialize and Load LLM Learner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llm.rst Sets up the AutoLLMLearner with a specified task, Hugging Face token, prompting strategy, label mapper, and loads a desired LLM model. ```python task = 'non-taxonomic-re' token = '...' llm_learner = AutoLLMLearner( prompting=StandardizedPrompting, label_mapper=LabelMapper(), token=token ) llm_learner.load(model_id='Qwen/Qwen2.5-0.5B-Instruct') ``` -------------------------------- ### Benchmark Collection Info Example Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/ontologizer/metadata.rst This snippet shows the RDF/XML representation for the benchmark collection information. The dcterms:hasVersion property here indicates the library version associated with the metadata release. ```xml OntoLearner Benchmark Ontologies ``` -------------------------------- ### Initialize and Run Learner Pipeline in Retriever-Only Mode Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/retrieval.rst Demonstrates initializing a LearnerPipeline with a specific retriever and running it on training and test data for non-taxonomic relation prediction. It also shows how to access evaluation metrics and elapsed time. ```python pipeline = LearnerPipeline( retriever_id='sentence-transformers/all-MiniLM-L6-v2', # Hugging Face model ID for retrieval batch_size=10, # Number of samples to process per batch (if batching is enabled internally) top_k=5 # Retrieve top-5 most relevant support instance per query ) # Run the pipeline on the training and test data # The pipeline performs: fit() → predict() → evaluate() in sequence outputs = pipeline( train_data=train_data, test_data=test_data, evaluate=True, # If True, computes precision, recall, and F1-score task='non-taxonomic-re' # Specifies that we are doing non-taxonomic relation prediction ) # Print the evaluation metrics (precision, recall, F1) print("Metrics:", outputs['metrics']) # Print the total elapsed time for training and evaluation print("Elapsed time:", outputs['elapsed_time']) # Print the full output dictionary (includes predictions) print(outputs) ``` -------------------------------- ### Initialize AlexbekRAGFewShotLearner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llms4ol_challenge/alexbek_learner.rst Configures a retrieval-augmented few-shot learner for the Text2Onto task. Specify LLM and retriever model IDs, device, and parameters like top_k and max_new_tokens. ```python from ontolearner.learner.text2onto import AlexbekRAGFewShotLearner text2onto_learner = AlexbekRAGFewShotLearner( llm_model_id="Qwen/Qwen2.5-0.5B-Instruct", retriever_model_id="sentence-transformers/all-MiniLM-L6-v2", device="cpu", # set "cuda" if available top_k=3, max_new_tokens=256, use_tfidf=True, ) ``` -------------------------------- ### Configure RAG Learner Pipeline Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/quickstart.rst Configures a LearnerPipeline for a RAG-based approach, specifying both a retriever and an LLM. Includes parameters for Hugging Face token, batch size, and top-k retrieved examples. ```python from ontolearner import LearnerPipeline pipeline = LearnerPipeline( retriever_id='sentence-transformers/all-MiniLM-L6-v2', llm_id='Qwen/Qwen2.5-0.5B-Instruct', hf_token='', batch_size=16, top_k=3 ) ``` -------------------------------- ### Import and Load SystemCapabilities Ontology Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/benchmarking/materials_science_and_engineering/systemcapabilities.rst This snippet shows how to import the SystemCapabilities ontology and load an OWL file programmatically using the ontolearner library. It also demonstrates how to extract and access specific data relations from the loaded ontology. ```python from ontolearner.ontology import SystemCapabilities ontology = SystemCapabilities() ontology.load("path/to/SystemCapabilities-ontology.owl") # Extract datasets data = ontology.extract() # Access specific relations term_types = data.term_typings taxonomic_relations = data.type_taxonomies non_taxonomic_relations = data.type_non_taxonomic_relations ``` -------------------------------- ### Custom Mistral-Small LLM Integration Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llm.rst Integrates Mistral-Small using mistral-common tokenizer and chat completion. Implements load for custom model and tokenizer setup, and generate for prompt encoding and output decoding. ```python from ontolearner import AutoLLM from typing import List import torch class MistralLLM(AutoLLM): def load(self, model_id: str) -> None: from mistral_common.tokens.tokenizers.mistral import MistralTokenizer from mistral_common.models.modeling_mistral import Mistral3ForConditionalGeneration self.tokenizer = MistralTokenizer.from_hf_hub(model_id) device_map = "cpu" if self.device == "cpu" else "balanced" self.model = Mistral3ForConditionalGeneration.from_pretrained( model_id, device_map=device_map, torch_dtype=torch.bfloat16, token=self.token ) if not hasattr(self.tokenizer, "pad_token_id") or self.tokenizer.pad_token_id is None: self.tokenizer.pad_token_id = self.model.generation_config.eos_token_id self.label_mapper.fit() @torch.no_grad() def generate(self, inputs: List[str], max_new_tokens: int = 50) -> List[str]: from mistral_common.protocol.instruct.messages import ChatCompletionRequest tokenized_list = [] for prompt in inputs: messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}] tokenized = self.tokenizer.encode_chat_completion(ChatCompletionRequest(messages=messages)) tokenized_list.append(tokenized.tokens) # Pad inputs and create attention masks max_len = max(len(tokens) for tokens in tokenized_list) input_ids, attention_masks = [], [] for tokens in tokenized_list: pad_length = max_len - len(tokens) input_ids.append(tokens + [self.tokenizer.pad_token_id] * pad_length) attention_masks.append([1] * len(tokens) + [0] * pad_length) input_ids = torch.tensor(input_ids).to(self.model.device) attention_masks = torch.tensor(attention_masks).to(self.model.device) outputs = self.model.generate( input_ids=input_ids, attention_mask=attention_masks, eos_token_id=self.model.generation_config.eos_token_id, pad_token_id=self.tokenizer.pad_token_id, max_new_tokens=max_new_tokens, ) decoded_outputs = [] for i, tokens in enumerate(outputs): output_text = self.tokenizer.decode(tokens[len(tokenized_list[i]):]) decoded_outputs.append(output_text) return self.label_mapper.predict(decoded_outputs) ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/installation.rst Set up and activate a Python virtual environment for isolated development. Use 'venv\Scripts\activate' on Windows. ```bash python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate ``` -------------------------------- ### Import and Load AGROVOC Ontology Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/benchmarking/agriculture/agrovoc.rst Use this Python code to import the AGROVOC ontology, load an RDF file, and extract data. Ensure the AGROVOC library is installed and the ontology file path is correct. ```python from ontolearner.ontology import AGROVOC ontology = AGROVOC() ontology.load("path/to/AGROVOC-ontology.rdf") # Extract datasets data = ontology.extract() # Access specific relations term_types = data.term_typings taxonomic_relations = data.type_taxonomies non_taxonomic_relations = data.type_non_taxonomic_relations ``` -------------------------------- ### Split Ontology Data into Train and Test Sets Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learning_tasks/llms4ol.rst Demonstrates splitting extracted ontology data into training and testing sets using a specified ratio and random state. The 'train_test_split' function from 'ontolearner' is used. Ensure 'ontolearner' is installed. ```python from ontolearner import Wine, train_test_split ontology = Wine() ontology.load() data = ontology.extract() train_data, test_data = train_test_split(data, test_size=0.2, random_state=42) ``` -------------------------------- ### Create Branch, Commit, and Push Changes Source: https://github.com/sciknoworg/ontolearner/blob/main/CONTRIBUTING.md Create a new branch for your changes, stage all modified files, commit them with a descriptive message, and push to the remote repository. ```bash git switch -c # do your changes git add . git commit -m "your commit msg" git push ``` -------------------------------- ### Load and Extract VOAF Ontology Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/benchmarking/scholarly_knowledge/voaf.rst This Python snippet demonstrates how to load the VOAF ontology from an RDF file and extract its associated data, including term types and relations. Ensure the 'ontolearner' library is installed and the ontology file path is correct. ```python from ontolearner.ontology import VOAF ontology = VOAF() ontology.load("path/to/VOAF-ontology.rdf") # Extract datasets data = ontology.extract() # Access specific relations term_types = data.term_typings taxonomic_relations = data.type_taxonomies non_taxonomic_relations = data.type_non_taxonomic_relations ``` -------------------------------- ### Qwen3 Instruct LLM Inference with OntoLearner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llm.rst This class facilitates the use of Qwen3's instruct model for inference in OntoLearner. It utilizes chat templates and specific generation parameters for optimal results. ```python class QwenInstructLLM(AutoLLM): def generate(self, inputs: List[str], max_new_tokens: int = 50) -> List[str]: messages = [[{"role": "user", "content": prompt + " Please show your final response with 'answer': 'label'."}] for prompt in inputs] texts = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) encoded_inputs = self.tokenizer(texts, return_tensors="pt", padding="max_length", truncation=True, max_length=256).to(self.model.device) generated_ids = self.model.generate(**encoded_inputs, max_new_tokens=max_new_tokens, use_cache=False, pad_token_id=self.tokenizer.pad_token_id, eos_token_id=self.tokenizer.eos_token_id) decoded_outputs = [] for i in range(len(generated_ids)): prompt_len = encoded_inputs.attention_mask[i].sum().item() ``` -------------------------------- ### Integrate Custom Falcon LLM with AutoLLMLearner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/llm.rst Instantiate AutoLLMLearner with a custom LLM class like FalconLLM. Ensure the custom LLM class implements the necessary load and generate methods. This example shows how to plug in a specific Falcon model. ```python from ontolearner import AutoLLMLearner, LabelMapper, StandardizedPrompting falcon_learner = AutoLLMLearner( prompting=StandardizedPrompting, label_mapper=LabelMapper(), llm=FalconLLM, # 👈 plug in custom Falcon token="...", device="cuda" ) falcon_learner.llm.load(model_id="tiiuae/Falcon-H1-1.5B-Deep-Instruct") # Train and evaluate falcon_learner.fit(train_data, task="term-typing") predictions = falcon_learner.predict(test_data, task="term-typing") print(predictions) ``` -------------------------------- ### Initialize and Load OntoCAPE Ontology Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/benchmarking/materials_science_and_engineering/ontocape.rst This snippet demonstrates how to initialize the OntoCAPE ontology object and load an OWL ontology file. Ensure the ontology file path is correct. ```python from ontolearner.ontology import OntoCAPE # Initialize and load ontology ontology = OntoCAPE() ontology.load("path/to/ontology.owl") # Extract datasets data = ontology.extract() # Access specific relations term_types = data.term_typings taxonomic_relations = data.type_taxonomies non_taxonomic_relations = data.type_non_taxonomic_relations ``` -------------------------------- ### Initialize and Train Retriever Learner Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/retrieval.rst Initializes an AutoRetrieverLearner for a specific task (e.g., 'non-taxonomic-re'), loads a sentence-transformer model, indexes training data, and makes predictions on test data. The 'top_k' parameter controls the number of retrieved examples, and 'batch_size' can be set to manage memory for large contexts. ```python task = 'non-taxonomic-re' ret_learner = AutoRetrieverLearner(top_k=5) ret_learner.load(model_id='sentence-transformers/all-MiniLM-L6-v2') # Index the model on the training data for LLMs4OL task ret_learner.fit(train_data, task=task) predicts = ret_learner.predict(test_data, task=task) truth = ret_learner.tasks_ground_truth_former(data=test_data, task=task) metrics = evaluation_report(y_true=truth, y_pred=predicts, task=task) print(metrics) ``` ```python ret_learner = AutoRetrieverLearner(top_k=5, batch_size=1024) ``` -------------------------------- ### Import and Load PTO Ontology Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/benchmarking/industry/pto.rst This snippet shows how to import the PTO class, instantiate an ontology object, and load an RDF file. It then demonstrates extracting data and accessing specific relations like term types and taxonomies. ```python from ontolearner.ontology import PTO ontology = PTO() ontology.load("path/to/PTO-ontology.rdf") # Extract datasets data = ontology.extract() # Access specific relations term_types = data.term_typings taxonomic_relations = data.type_taxonomies non_taxonomic_relations = data.type_non_taxonomic_relations ``` -------------------------------- ### Import and Load GIST Ontology Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/benchmarking/general_knowledge/gist.rst This snippet demonstrates how to import the GIST ontology class, instantiate it, and load an RDF file. It also shows how to extract data and access specific relations like term types and taxonomies. ```python from ontolearner.ontology import GIST ontology = GIST() ontology.load("path/to/GIST-ontology.rdf") # Extract datasets data = ontology.extract() # Access specific relations term_types = data.term_typings taxonomic_relations = data.type_taxonomies non_taxonomic_relations = data.type_non_taxonomic_relations ``` -------------------------------- ### Load and Extract BBC Wildlife Ontology Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/benchmarking/news_and_media/bbcwildlife.rst Demonstrates how to import the BBCWildlife ontology class, load an ontology file, and extract its data programmatically. Ensure the ontology file path is correct. ```python from ontolearner.ontology import BBCWildlife ontology = BBCWildlife() ontology.load("path/to/BBCWildlife-ontology.ttl") # Extract datasets data = ontology.extract() # Access specific relations term_types = data.term_typings taxonomic_relations = data.type_taxonomies non_taxonomic_relations = data.type_non_taxonomic_relations ``` -------------------------------- ### Running AugmentedRetriever Pipeline Source: https://github.com/sciknoworg/ontolearner/blob/main/examples/pipeline.ipynb Sets up and runs an AugmentedRetriever pipeline. This involves augmenting the retriever with LLM-generated insights. ```python def run_augmented_retriever(): train_data, test_data = load_term_typing_split() data = load_term_typing() from ontolearner.learner.retriever import LLMAugmenterGenerator, LLMAugmentedRetriever llm_augmenter_generator = LLMAugmenterGenerator(model_id='gpt-4.1-mini', token = OPENAI_KEY, top_n_candidate=10) augments = {"config": llm_augmenter_generator.get_config()} augments[TASK] = llm_augmenter_generator.augment(data, task=TASK) retriever = LLMAugmentedRetrieverLearner(base_retriever=LLMAugmentedRetriever(), top_k=5) retriever.set_augmenter(augments) pipeline = LearnerPipeline(retriever=retriever, retriever_id="sentence-transformers/all-MiniLM-L6-v2") return pipeline(train_data=train_data, test_data=test_data, task=TASK, evaluate=True) ``` -------------------------------- ### Initialize and Load GloveRetriever Source: https://github.com/sciknoworg/ontolearner/blob/main/docs/source/learners/retrieval.rst Demonstrates how to initialize a GloveRetriever and load pre-trained GloVe vectors for use with AutoRetrieverLearner. Ensure the path to the GloVe model file is correctly specified. ```python from ontolearner.learner import AutoRetrieverLearner from ontolearner.learner.retriever import GloveRetriever retriever = GloveRetriever() learner = AutoRetrieverLearner(base_retriever=retriever) learner.load(model_id="path/to/glove.txt") # Load pre-trained GloVe vectors ```