What is Skeleton of Thoughts and its Python Implementation?

Shikha Sen 17 Jul, 2024
10 min read

Introduction 

Imagine having the full potential of your favorite AI assistant, transforming it from a helpful tool into a creative powerhouse. Picture a world where your interactions with Large Language Models(LLMs) are efficient, profoundly intuitive, and impactful. Enter the Skeleton of Thoughts (SoT) – a groundbreaking framework poised to revolutionize artificial intelligence and natural language processing. Let’s examine this intriguing idea in more detail and see how it might improve your AI-powered creative and problem-solving processes.

Skeleton of Thoughts

Overview

  1. The Skeleton of Thoughts (SoT) framework transforms AI into a creative powerhouse with structured yet adaptable outlines for complex tasks.
  2. SoT enables AI to address challenges with clarity and depth by providing a flexible conceptual framework.
  3. Core elements of SoT include structured outlines, flexible expansion, hierarchical thinking, iterative development, and cross-linking.
  4. Implementing SoT involves creating a skeletal outline, expanding points, and connecting parts using OpenAI’s GPT model.
  5. SoT offers clarity, scalability, flexibility, holistic understanding, and efficient exploration of complex topics.
  6. Real-world applications of SoT include academic research, product development, and strategic planning for structured exploration of intricate subjects.

Revealing the Thought Skeleton

What if you could construct an AI conceptual framework that allows flexibility and creativity while guiding the model’s thought process? That is precisely the goal of the Skeleton of Thoughts. SoT enables AI models to tackle challenges with unprecedented clarity, coherence, and depth by providing a structured yet adaptable outline for complex tasks.

The Core Concept of SoT

  1. Structured Outline: A high-level structure outlining the main elements of the assignment.
  2. Flexible Expansion: All of the skeletal components can be improved and expanded upon as required.
  3. Hierarchical Thinking: Ideas are organized in a logical, nested structure.
  4. Iterative Development: The skeleton changes as the AI investigates and refines concepts.
  5. Cross-Linking: Dynamically created connections between various skeleton components are possible.

Implementing the Skeleton of Thoughts

Let’s bring this concept to life with a Python implementation that leverages OpenAI’s GPT model:

Pre-Requisite and Setup

!pip install openai --upgrade

Importing Libraries

from openai importOpenAI
import openai 
import time 
import re
from IPython.display import Markdown, display

Setting API Key Configuration 

os.environ["OPENAI_API_KEY"]= “Your openAPIKey”
from openai import OpenAI
from openai import OpenAI
import time
import re

class SkeletonOfThoughts:
    """
    A class to create and manipulate a skeletal outline using OpenAI's API.
    """


    def __init__(self, api_key, model="gpt-3.5-turbo"):
        """
        Initialize the SkeletonOfThoughts object.


        Args:
            api_key (str): OpenAI API key
            model (str): OpenAI model to use (default is "gpt-3.5-turbo")
        """
        self.client = OpenAI(api_key=api_key)
        self.model = model
        self.skeleton = {}


    def create_skeleton(self, topic):
        """
        Create a skeletal outline for the given topic.


        topic (str): The topic to create a skeleton for


        Returns:
            dict: The created skeleton outline
        """
        prompt = f"""Create a detailed skeletal outline for the topic: '{topic}'.
        Use the following format:
        1. Main point
        1.1. Subpoint
        1.2. Subpoint
        2. Main point
        2.1. Subpoint
        2.2. Subpoint
        Provide at least 3 main points with 2-3 subpoints each."""
        try:
            response = self.execute_prompt(prompt, max_tokens=500)
            print(f"Raw API Response:\n{response}")  # Debug print
            self.skeleton = self.parse_skeleton(response)
            print(f"Parsed Skeleton:\n{self.skeleton}")  # Debug print
            return self.skeleton
        except Exception as e:
            print(f"Error creating skeleton: {str(e)}")
            return {}


    def expand_point(self, point_number):
        """
        Expand a specific point in the skeleton.


   
            point_number (int): The number of the point to expand


        Returns:
            str: The expanded point content
        """
        point_key = self.find_point_key(point_number)
        if point_key is None:
            return f"Point {point_number} not found in the skeleton."
        current_content = self.skeleton[point_key]
        prompt = f"Expand on the following point: {current_content}"
        expansion = self.execute_prompt(prompt)
        self.skeleton[point_key] = f"{current_content}\n{expansion}"
        return self.skeleton[point_key]


    def add_cross_link(self, point1, point2):
        """
        Add a cross-link between two points in the skeleton.


  
         point1 (int): The number of the first point
         point2 (int): The number of the second point


        Returns:
            str: The created cross-link explanation
        """
        key1 = self.find_point_key(point1)
        key2 = self.find_point_key(point2)
        if key1 is None or key2 is None:
            return "One or both points not found in the skeleton."
        prompt = f"Explain how these two points are related: 1) {self.skeleton[key1]} 2) {self.skeleton[key2]}"
        link = self.execute_prompt(prompt)
        link_key = f"Link: {key1} - {key2}"
        self.skeleton[link_key] = link
        return link


    def execute_prompt(self, prompt, max_tokens=500):
        """
        Execute a prompt using the OpenAI API.


        
            prompt (str): The prompt to send to the API
            max_tokens (int): Maximum number of tokens in the response


        Returns:
            str: The API response content
        """
        try:
            response = self.client.chat.completions.create(
                model=self.model,
                messages=[
                    {"role": "system", "content": "You are an AI assistant creating a structured outline."},
                    {"role": "user", "content": prompt}
                ],
                max_tokens=max_tokens
            )
            content = response.choices[0].message.content.strip()
            print(f"API Response Content:\n{content}")  # Debug print
            return content
        except Exception as e:
            print(f"Error executing prompt: {str(e)}")
            return ""


    def parse_skeleton(self, text):
        """
        Parse the skeleton text into a structured dictionary.


       
            text (str): The raw skeleton text to parse


        Returns:
            dict: The parsed skeleton structure
        """
        print("Parsing text:", text)  # Debug print
        lines = text.split('\n')
        skeleton = {}
        current_main_point = ""
        for line in lines:
            line = line.strip()
            if not line:
                continue


            # Match main points (1., 2., etc.)
            main_point_match = re.match(r'^(\d+\.)\s*(.*)', line)
            if main_point_match:
                current_main_point = main_point_match.group(1)
                skeleton[current_main_point] = main_point_match.group(2)


            # Match subpoints (1.1., 1.2., 2.1., etc.)
            subpoint_match = re.match(r'^(\d+\.\d+\.)\s*(.*)', line)
            if subpoint_match:
                subpoint_key = subpoint_match.group(1)
                skeleton[subpoint_key] = subpoint_match.group(2)


            # If it's not a numbered point, add it to the current main point
            elif current_main_point and line:
                skeleton[current_main_point] += " " + line


        return skeleton


    def find_point_key(self, point_number):
        """
        Find the key in the skeleton dictionary for a given point number.


         point_number (int): The point number to find


        Returns:
            str: The key in the skeleton dictionary, or None if not found
        """
        for key in self.skeleton.keys():
            if key.startswith(str(point_number) + '.'):
                return key
        return None


    def display_skeleton(self):
        """
        Display the current skeleton structure.
        """
        if not self.skeleton:
            print("The skeleton is empty.")
        else:
            for key, value in self.skeleton.items():
                print(f"{key} {value}\n")


def main():
    """
    Main function to demonstrate the usage of SkeletonOfThoughts class.
    """
    api_key = key  # Replace with your actual OpenAI API key
    if api_key == "your-api-key-here":
        print("Please replace 'your-api-key-here' with your actual OpenAI API key.")
        return


    try:
        sot = SkeletonOfThoughts(api_key)


        topic = "The Impact of Artificial Intelligence on Future Job Markets"
        print(f"Creating skeleton for topic: {topic}")
        initial_skeleton = sot.create_skeleton(topic)


        if not initial_skeleton:
            print("Failed to create initial skeleton. Please check the error messages above.")
            return


        print("\nInitial Skeleton:")
        sot.display_skeleton()


        # Expand a point
        print("\nExpanding Point 1:")
        expanded_point = sot.expand_point(1)
        print(expanded_point)


        # Add a cross-link
        print("\nAdding Cross-link between points 1 and 2:")
        link = sot.add_cross_link(1, 2)
        print(link)


        print("\nFinal Skeleton:")
        sot.display_skeleton()


    except Exception as e:
        print(f"An error occurred: {str(e)}")


if __name__ == "__main__":
    main()

Output With Explanation

OUTPUT
OUTPUT

Creating skeleton: The system created a detailed outline for the topic “The Impact of Artificial Intelligence on Future Job Markets.” This outline includes main points and subpoints covering various aspects of AI’s impact on job markets.

Initial Skeleton: The parsed skeleton is displayed, showing the outline’s structure with main points and subpoints. However, there seem to be some parsing issues, as some subpoints are incorrectly associated with main points.

OUTPUT

Expanding Point 1: The system expanded on the first point, providing a detailed explanation of the definition of Artificial Intelligence. This expansion includes what AI is, its components, potential impacts, and ethical considerations.

OUTPUT

Adding Cross-link between points 1 and 2: The system connected the first point (Definition of AI) and the second point (Discussion on upskilling and retraining). It explains how these points are related through their shared focus on AI’s impact on jobs and the workforce.

OUTPUT
OUTPUT

Final Skeleton: The final skeleton combines all the previous steps. It includes the initial outline, the expanded first point, and the cross-link between points 1 and 2. This provides a comprehensive structure for discussing AI’s impact on future job markets.

This implementation brings the Skeleton of Thoughts to life:

  1. We have created a Skeleton Of Thoughts class that encapsulates our approach, initialized with an OpenAI API key and a model choice (defaulting to “gpt3.5-turbo”).
  2. The create_skeleton method generates an initial outline for a given topic:
    • It uses a specific prompt format to ensure a structured response from the AI.
    • The method handles API interactions and parses the response into a dictionary structure.
  3. expand_point allows for deeper exploration of specific points in the skeleton:
    • It identifies the point to expand based on its number.
    • The method then generates additional content for that point using the AI.
  4. add_cross_link establishes connections between different parts of the skeleton:
    • It takes two-point numbers as input and explains their relationship.
    • The link is then added to the skeleton as a new entry.
  5. The skeleton is stored as a dictionary, with keys representing point numbers (e.g., “1.”, “1.1.”) and values containing the content.
  6. Additional helper methods enhance functionality:
    • execute_prompt handles API calls with error management and debugging output.
    • parse_skeleton converts the AI’s text response into a structured dictionary.
    • find_point_key locates specific points in the skeleton.
    • display_skeleton prints the current state of the skeleton.

The Magic of SoT in Action

Let’s break down what happens when we run this code:

  1. Initial Skeleton Creation
    • The AI generates a high-level outline for the given topic (e.g., “The Impact of AI on Future Job Markets”).
    • This outline includes main points and subpoints, formatted as a numbered list.
  2. Point Expansion
    • We can dive deeper into specific points (e.g., point 1) using the expand_point method.
    • The AI provides more detailed information about the chosen point.
  3. CrossLinking
    • Connections between different parts of the skeleton are established using add_cross_link.
    • For example, we can link points 1 and 2, generating an explanation of their relationship.
  4. Iterative Development
    • The skeleton can be continuously refined by expanding points or adding crosslinks.
    • Each operation updates the skeleton dictionary, allowing for cumulative knowledge building.
  5. Robust Error Handling
    • The implementation includes extensive error checking and debugging output.
    • This helps identify and address issues with API calls or parsing.

This structured yet flexible approach allows for a more comprehensive and interconnected exploration of complex topics. Using AI to generate content and the programmatic structure for organizing and linking ideas creates a powerful tool for thought development and analysis.

Also, Read these Prompt Engineering articles:

ArticleSource
Implementing the Tree of Thoughts Method in AILink
What are Delimiters in Prompt Engineering?Link
What is Self-Consistency in Prompt Engineering?Link
What is Temperature in Prompt Engineering?Link

Check more articles here – Prompt Engineering.

Advantages of the Skeleton of Thoughts

Here are the advantages of the skeleton of thoughts:

  1. Clarity and Organization: Ideas are structured in a logical, easy-to-follow manner.
  2. Scalability: The skeleton can be as simple or as complex as the task requires.
  3. Flexibility: Easy to adapt and refine as new insights emerge.
  4. Holistic Understanding: Crosslinking promotes a more comprehensive grasp of the topic.
  5. Efficient Exploration: Provides a roadmap for thorough investigation without getting lost in details.

Real World Applications

Here are the real-world applications:

  1. Academic Research: Imagine using SoT to outline a complex research paper. The skeleton could provide the main sections and subsections, with the ability to expand on key points and establish connections between different parts of the argument.
  2. Product Development: In the tech industry, SoT could be used to map out the features and components of a new product, allowing teams to visualize the overall structure while diving into specific details as needed.
  3. Strategic Planning: Businesses could use SoT to develop comprehensive strategies, outlining main objectives, potential challenges, and action plans, with the flexibility to expand and connect different aspects of the strategy.

Challenges and Considerations

While the Skeleton of Thoughts offers exciting possibilities, it’s important to consider:

  1. Balancing Structure and Flexibility: Finding the right balance between providing a clear structure and allowing for creative exploration.
  2. Managing Complexity: Managing the skeleton’s growth and interconnections can become challenging for very large or intricate topics.
  3. AI Limitations: The skeleton’s quality and expansions are still bound by the capabilities of the underlying language model.

The Future of Prompt Engineering

As AI advances, techniques like the Skeleton of Thoughts will enhance our ability to tackle complex problems and explore intricate topics. Providing a structured yet flexible framework for AI-assisted thinking opens up new possibilities for knowledge organization, problem-solving, and creative exploration.

Conclusion

The Skeleton of Thoughts represents a significant step in structuring and guiding AI-assisted thought processes. By providing a flexible scaffolding for ideas, we can explore complex topics with unprecedented clarity and depth while maintaining the ability to adapt and refine our approach as new insights emerge.

Whether you’re a researcher, a business strategist, or someone who loves exploring ideas, the Skeleton of Thoughts approach offers a powerful new tool for organizing and developing your thoughts. So why not give it a try? You might just discover a new way of thinking that transforms how you approach complex topics and challenges!

Frequently Asked Questions

Q1. What is the Skeleton of Thoughts approach in prompt engineering?

Ans. The Skeleton of Thoughts is a prompt engineering technique that provides a structured outline or framework for an AI model to follow when generating responses. It breaks down complex tasks into a logical sequence of steps or components, guiding the AI’s thought process without fully specifying each step’s content. This approach helps maintain coherence and structure in longer or more complex outputs.

Q2. How does the Skeleton of Thoughts differ from the Algorithm of Thoughts?

Ans. While both techniques aim to guide AI reasoning, the Skeleton of Thoughts provides a higher-level structure or outline, leaving more room for the AI to fill in details. The Algorithm of Thoughts, on the other hand, typically specifies more detailed step-by-step instructions. The Skeleton approach is often more flexible and useful for creative or open-ended tasks.

Q3. What are other types of “thoughts” techniques in prompt engineering?

Ans. Several “thoughts” techniques have been developed in prompt engineering to enhance AI reasoning and output quality. Some notable ones include:
a) Tree of Thoughts: This method creates a tree-like structure of potential reasoning paths, allowing the AI to explore multiple lines of thought and select the most promising one.
b) Chain of Thought: This technique encourages the AI to show its step-by-step reasoning process, improving transparency and allowing for more complex problem-solving.
c) Graph of Thoughts: This is similar to the Tree of Thoughts but allows for more complex interconnections between ideas. It is useful for tasks requiring non-linear thinking.
d) Reflexion: This approach involves the AI critiquing and refining its outputs, leading to improved accuracy and coherence.
e) ReAct: Combines reasoning and acting, allowing the AI to iteratively reason about a task and take actions based on that reasoning.

Shikha Sen 17 Jul, 2024

With 4 years of experience in model development and deployment, I excel in optimizing machine learning operations. I specialize in containerization with Docker and Kubernetes, enhancing inference through techniques like quantization and pruning. I am proficient in scalable model deployment, leveraging monitoring tools such as Prometheus, Grafana, and the ELK stack for performance tracking and anomaly detection. My skills include setting up robust data pipelines using Apache Airflow and ensuring data quality with stringent validation checks. I am experienced in establishing CI/CD pipelines with Jenkins and GitHub Actions, and I manage model versioning using MLflow and DVC. Committed to data security and compliance, I ensure adherence to regulations like GDPR and CCPA. My expertise extends to performance tuning, optimizing hardware utilization for GPUs and TPUs. I actively engage with the LLMOps community, staying abreast of the latest advancements to continually improve large language model deployments. My goal is to drive operational efficiency and scalability in AI systems.

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,