Last active
April 17, 2026 14:12
-
-
Save jasonclark/a6f5f832077da8546726888d16b25692 to your computer and use it in GitHub Desktop.
knowledge-graph-grounded LLM - Code and Sample N-Triple file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _:Na71bca5b03df4749a66d12a30355263c <http://schema.org/name> "Little Big Horn College" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/keywords> "Forestry" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/alternateName> "SMART FireS" . | |
| _:N9d75b00e4cae4509b868672a9020a81d <http://schema.org/name> "Salish Kootenai College" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/description> "A five-year program funded by the National Science Foundation (NSF) and led by Montana universities to better understand prescribed fires and their effects on the community of the state. It focuses on Controlled Burn, Artificial Intelligence, Forestry, and Machine Learning." . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/member> _:Nc10d2f9a80764adeaac7e88f081c2e09 . | |
| _:Nc10d2f9a80764adeaac7e88f081c2e09 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/EducationalOrganization> . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/ResearchProject> . | |
| _:N9d75b00e4cae4509b868672a9020a81d <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/EducationalOrganization> . | |
| _:N391c4942b8d14d2fb9311fd4043c4d42 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/EducationalOrganization> . | |
| _:N14fd8e9dc0134e0fb94d0a7b0493b964 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Organization> . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/member> _:Na71bca5b03df4749a66d12a30355263c . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/member> _:N7b275fb1c95a4bcd85a469ef46e2e127 . | |
| _:Na71bca5b03df4749a66d12a30355263c <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/EducationalOrganization> . | |
| _:N96ac14ce7a0b4f0e8541f519d6869448 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Organization> . | |
| _:N14fd8e9dc0134e0fb94d0a7b0493b964 <http://schema.org/name> "National Science Foundation (NSF)" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/member> _:N30d43667678c4ee4994e806839e110e6 . | |
| _:N391c4942b8d14d2fb9311fd4043c4d42 <http://schema.org/name> "Montana State University" . | |
| _:Nc10d2f9a80764adeaac7e88f081c2e09 <http://schema.org/name> "University of Montana" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/keywords> "Controlled Burn" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/keywords> "Machine Learning" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/name> "Sensors, Machine learning, and Artificial intelligence in Real Time Fire Science (SMART FireS)" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/member> _:N391c4942b8d14d2fb9311fd4043c4d42 . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/keywords> "Artificial Intelligence" . | |
| _:N96ac14ce7a0b4f0e8541f519d6869448 <http://schema.org/name> "National Science Foundation (NSF)" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/sponsor> _:N96ac14ce7a0b4f0e8541f519d6869448 . | |
| _:N7b275fb1c95a4bcd85a469ef46e2e127 <http://schema.org/name> "Flathead Valley Community College" . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/funder> _:N14fd8e9dc0134e0fb94d0a7b0493b964 . | |
| _:N7b275fb1c95a4bcd85a469ef46e2e127 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/EducationalOrganization> . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/member> _:N9d75b00e4cae4509b868672a9020a81d . | |
| _:N30d43667678c4ee4994e806839e110e6 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/EducationalOrganization> . | |
| _:Nd674cdcad5ff4955a11e5d87dc102fbf <http://schema.org/url> <https://www.mtnsfepscor.org/projects/smart-fires> . | |
| _:N30d43667678c4ee4994e806839e110e6 <http://schema.org/name> "Montana Technological University" . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ollama | |
| import os | |
| # --- Configuration --- | |
| MODEL_NAME = "llama3:latest" # Replace with the name of the model you have pulled in Ollama | |
| TRIPLES_DIR = "triples" | |
| # --- 1. Load the triples data --- | |
| all_triples = "" | |
| try: | |
| if not os.path.exists(TRIPLES_DIR): | |
| print(f"Error: The directory '{TRIPLES_DIR}' does not exist.") | |
| else: | |
| for filename in os.listdir(TRIPLES_DIR): | |
| if filename.endswith(".txt"): | |
| file_path = os.path.join(TRIPLES_DIR, filename) | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| all_triples += f.read() + "\n" | |
| print(f"Loaded {len(os.listdir(TRIPLES_DIR))} triple files.") | |
| except Exception as e: | |
| print(f"An error occurred while loading files: {e}") | |
| exit() | |
| # --- 2. Construct the system prompt --- | |
| system_prompt = f""" | |
| You are an expert at analyzing and reasoning over structured knowledge graph data. | |
| I will provide you with a list of facts, each on a new line, in a subject-predicate-object format. | |
| Your task is to answer my questions using ONLY the information I provide in the facts. Do not use any external knowledge. If you cannot answer a question based on the facts, respond with "I cannot answer this question with the provided facts." | |
| Here is the knowledge graph data: | |
| {all_triples} | |
| Please answer my questions based on this data. | |
| """ | |
| # --- 3. Run the chat loop --- | |
| messages = [ | |
| {'role': 'system', 'content': system_prompt} | |
| ] | |
| print("\n--- Ollama Reasoning Chat ---") | |
| print(f"Using model: {MODEL_NAME}") | |
| print("Type your questions below. Type 'exit' to quit.") | |
| while True: | |
| user_query = input("\n> ") | |
| if user_query.lower() == 'exit': | |
| break | |
| # Add the user's query to the messages | |
| messages.append({'role': 'user', 'content': user_query}) | |
| # Provide feedback while processing query | |
| print("Model is working...🧠") | |
| # Send the conversation to Ollama and print the response | |
| try: | |
| response = ollama.chat(model=MODEL_NAME, messages=messages) | |
| llm_response = response['message']['content'] | |
| print(f"\nAI: {llm_response}") | |
| # Add the AI's response to the messages to maintain conversation history | |
| messages.append({'role': 'assistant', 'content': llm_response}) | |
| except ollama.RequestError as e: | |
| print(f"Error communicating with Ollama: {e}. Is Ollama running and the model '{MODEL_NAME}' downloaded?") | |
| messages.pop() # Remove the user message to prevent loop errors | |
| except Exception as e: | |
| print(f"An unexpected error occurred: {e}") | |
| messages.pop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment