Skip to content

Instantly share code, notes, and snippets.

@cpfiffer
Last active March 25, 2025 20:38
Show Gist options
  • Save cpfiffer/d1965f0c6ee1578e408c4ed7737ff646 to your computer and use it in GitHub Desktop.
Save cpfiffer/d1965f0c6ee1578e408c4ed7737ff646 to your computer and use it in GitHub Desktop.
Recursive knowledge concept map using Outlines
# Setup instructions:
# pip install 'outlines[transformers]'
import outlines
from transformers import AutoTokenizer
import json
# MODEL_STRING = "HuggingFaceTB/SmolLM2-135M-Instruct" # Small model
# MODEL_STRING = "HuggingFaceTB/SmolLM2-1.7B-Instruct" # Larger but kind of boring
MODEL_STRING = "NousResearch/Hermes-3-Llama-3.1-8B"
schema = """
{
"type": "object",
"properties": {
"concept": {
"type": "string",
"description": "The main concept or topic"
},
"definition": {
"type": "string",
"description": "Brief definition of the concept"
},
"related_concepts": {
"type": "array",
"items": {
"$ref": "#"
},
"maxItems": 3,
"description": "Related sub-concepts that help explain the main concept"
}
},
"required": ["concept", "definition"]
}
"""
llm = outlines.models.transformers(MODEL_STRING)
tokenizer = AutoTokenizer.from_pretrained(MODEL_STRING)
generator = outlines.generate.json(llm, schema)
system_prompt = """
You provide concept maps.
```
"""
prompt = """
Decompose the concept of "architecture" into a concept map -- each concept should have a name and definition,
and then a list of related concepts. Related concepts are optional, but they should be sub-concepts of their
parents.
"""
formatted_prompt = tokenizer.apply_chat_template(
[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt},
],
tokenize=False,
add_generation_prompt=True,
)
result = generator(formatted_prompt, max_tokens=1000)
print(json.dumps(result, indent=2))
@cpfiffer
Copy link
Author

Also:

{
  "concept": "Architecture",
  "definition": "The art or practice of designing and constructing buildings or other structures.",
  "related_concepts": [
    {
      "concept": "Design",
      "definition": "The creation of a plan or specification for the construction of an object or structure.",
      "related_concepts": [
        {
          "concept": "Aesthetics",
          "definition": "The sense of beauty or taste in the applied arts, especially in visual design.",
          "related_concepts": []
        },
        {
          "concept": "Functionality",
          "definition": "The purpose or intended function that an object or system is designed to fulfill.",
          "related_concepts": []
        },
        {
          "concept": "Innovation",
          "definition": "The process of creating or introducing new ideas, devices, or methods.",
          "related_concepts": []
        }
      ]
    },
    {
      "concept": "Construction",
      "definition": "The act or process of constructing something, especially a building.",
      "related_concepts": [
        {
          "concept": "Materials",
          "definition": "The matter out of which a physical object is or was made.",
          "related_concepts": []
        },
        {
          "concept": "Methods",
          "definition": "An established or accepted procedure for a given technique or practice.",
          "related_concepts": []
        },
        {
          "concept": "Techniques",
          "definition": "A manner of doing or of handling something; a method.",
          "related_concepts": []
        }
      ]
    },
    {
      "concept": "Structures",
      "definition": "A complex system or organization of units arranged according to a unified plan.",
      "related_concepts": [
        {
          "concept": "Buildings",
          "definition": "A structure for providing shelter or housing for animals.",
          "related_concepts": []
        },
        {
          "concept": "Infrastructure",
          "definition": "The basic physical and organizational structures and facilities (e.g. buildings, roads, power supplies) needed for the operation of a society or enterprise.",
          "related_concepts": []
        },
        {
          "concept": "Landscape",
          "definition": "An area of countryside or open land, visible from an elevated place.",
          "related_concepts": []
        }
      ]
    },
    {
      "concept": "Design",
      "definition": "The creation of a plan or specification for the construction of an object or structure.",
      "related_concepts": []
    },
    {
      "concept": "Sustainability",
      "definition": "The practice of responsible environmental management aimed at conserving the natural environment and preventing the depletion of natural resources.",
      "related_concepts": []
    },
    {
      "concept": "Energy efficiency",
      "definition": "The guard against waste, particularly through the improved use of energy.",
      "related_concepts": []
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment