Getting Started with Basic Generation in Guidance

First: what is Guidance and why should you care? In short, it’s an open-source library for Python that allows you to write natural language generation models using recursive grammars. But don’t let the fancy jargon scare you off we promise it’s not as scary as it sounds!

Let’s start with a basic example: generating a simple sentence based on some input text. Here’s how you can do that in Guidance:

# Import the `gen` and `select` functions from the `guidance` library
from guidance import gen, select

# Define the input text as a string
input_text = "The quick brown fox jumps over the lazy dog."

# Use the `gen` function to generate a sentence, with a period as the stop condition and output set to True
sentence = gen(stop='.', output=True)

# Use the `select` function to choose between the options "jumped" or "ran"
# Then concatenate it with some text from our input, starting from the 10th character
sentence += ' ' + select(['jumped', 'ran']) + ' ' + input_text[10:]

# Print the generated sentence
print(sentence)

# Output: The quick brown fox ran over the lazy dog.

# Explanation:
# The `gen` function generates a sentence using recursive grammars, with a period as the stop condition and output set to True.
# The `select` function chooses between the options "jumped" or "ran".
# The `input_text[10:]` slices the input text starting from the 10th character.
# The `+=` operator concatenates the generated sentence with the selected option and the sliced input text.
# The `print` function outputs the final generated sentence.

This will generate a sentence like “The lazy dog was jumped over by the quick brown fox.” Pretty cool, right?

But what if we want to be more specific about how our generated sentences are constructed? That’s where recursive grammars come in. Let’s say we have a grammar that looks something like this:

# Import the necessary module "guidance" which contains functions for generating sentences and selecting specific parts of speech
from guidance import gen, select

# Define a function called "my_grammar" that will return a list of dictionaries representing the grammar rules
def my_grammar():
    # The grammar will consist of 5 rules, each represented by a dictionary
    return [
        # The first rule is for a "sentence" and has two children: "subject" and "verb"
        {
            'name': 'sentence',
            'children': [
                {'name': 'subject'},
                {'name': 'verb'}
            ]
        },
        # The second rule is for a "subject" and has one child: "noun_phrase"
        {
            'name': 'subject',
            'children': [
                {'name': 'noun_phrase'}
            ]
        },
        # The third rule is for a "verb" and has one child: "verb_phrase"
        {
            'name': 'verb',
            'children': [
                {'name': 'verb_phrase'}
            ]
        },
        # The fourth rule is for a "noun_phrase" and has three children: "article", "adjective", and "noun"
        {
            'name': 'noun_phrase',
            'children': [
                {'name': 'article'},
                {'name': 'adjective'},
                {'name': 'noun'}
            ]
        },
        # The fifth rule is for a "verb_phrase" and has two children: "verb" and "object_clause"
        {
            'name': 'verb_phrase',
            'children': [
                {'name': 'verb'},
                {'name': 'object_clause'}
            ]
        }
    ]

This grammar defines a simple sentence structure with a subject, verb, and object clause. We can use this grammar to generate sentences like “The quick brown fox jumps over the lazy dog.” Here’s how:

# Importing necessary modules
from guidance import gen, select # Importing the `gen` and `select` functions from the `guidance` module
from my_grammar import * # Importing all functions from the `my_grammar` module

# Defining the main function
def main():
    # Generating a sentence using the `gen` function and concatenating it with a randomly selected verb from the list
    model = gen(stop='.', output=True) + ' ' + select(['jumped', 'ran']) + ' ' + gen('sentence')
    print(model) # Printing the generated sentence

# Checking if the script is being run directly
if __name__ == "__main__":
    main() # Calling the main function to execute the script

This will generate sentences like “The quick brown fox jumps over the lazy dog.” or “The lazy dog ran under the table.” depending on which option we choose for the verb.

SICORPS