Skip to content

Prompt Template

Alfred Nutile edited this page Jun 13, 2023 · 1 revision

Using this library here we can start to build out a prompt for the LLM

Prompt Templates

As noted earlier instead of a more complex and error prone

$question = <<<'EOD'
The book title is %s and the context of the chapters prior to this one is %s.
%s
as a helpful assistant can you please write the next chapter using the same style
as the tldrs of the ones included.
EOD;

        $prompt = sprintf($question,
            $this->book->title,
            $this->getSuggestionAsPartOfPrompt(),
            $this->finalTldrOfExistingChapters,
        );

We can use the Spatie Laravel Data Package https://spatie.be/docs/laravel-data/v3/introduction

do this instead:

$template = <<<'EOD'
I want you to act as a naming consultant for new companies.
What is a good name for a company that makes {product}?
EOD;

    $input_variables = [
        new PromptToken(
            'product',
            'colorful socks'
        ),
    ];
    $dto = new PromptTemplate(
        $input_variables,
        $template
    );

$expectedOutput = 'I want you to act as a naming consultant for new companies.
What is a good name for a company that makes colorful socks?'

Or if you have multiple Prompts

$template = <<<'EOD'
I want you to act as a naming consultant for new companies.
What is a good name for a company that makes {product1}, {product2} and {product3}?
EOD;

    $input_variables = [
        new PromptToken(
            'product1',
            'colorful socks'
        ),
        new PromptToken(
            'product2',
            'colorful hats'
        ),
        new PromptToken(
            'product3',
            'colorful headbands'
        ),
    ];
    $dto = new PromptTemplate(
        $input_variables,
        $template
    );

    assertEquals('I want you to act as a naming consultant for new companies.
What is a good name for a company that makes colorful socks, colorful hats and colorful headbands?', $dto->format());

Pass few shot examples to a prompt template

Few-shot refers to a machine learning paradigm where a model is trained on a small number of examples, typically less than a hundred, and is then tested on a different but related task. This approach is in contrast to the more traditional machine learning approach where a model is trained on a large dataset of examples, often in the order of thousands or millions.

In few-shot learning, the goal is to develop models that can learn to generalize from a small number of examples, which can be useful in scenarios where collecting large amounts of labeled data is expensive or time-consuming. Few-shot learning has been applied to various tasks, including image classification, object detection, and natural language processing.

This test shows one example of suggesting two sets of training data, adding a prefix and suffix.

The suffix contains that last bit of data that results in the question

it('test few show', function () {
    $prefix = 'Give the antonym of every input';

    $template = <<<'EOD'
Word: {word}
Antonym: {antonym}
EOD;

    $input_variables = [
        new PromptToken(
            'word',
            'happy'
        ),
        new PromptToken(
            'antonym',
            'sad'
        ),
    ];

    $promptTemplates1 = new PromptTemplate(
        $input_variables,
        $template
    );

    $input_variables = [
        new PromptToken(
            'word',
            'tall'
        ),
        new PromptToken(
            'antonym',
            'short'
        ),
    ];

    $promptTemplates2 = new PromptTemplate(
        $input_variables,
        $template
    );

    $input_variables = [
        new PromptToken(
            'word',
            'big'
        ),
        new PromptToken(
            'antonym',
            ''
        ),
    ];

    $template = <<<'EOD'
Word: {word}
Antonym:
EOD;

    $suffix = new PromptTemplate($input_variables, $template);

    $dto = new FewShotPromptTemplate(
        [$promptTemplates1, $promptTemplates2],
        $prefix,
        $suffix
    );

    $expected = <<<'EOD'
Give the antonym of every input
Word: happy
Antonym: sad
Word: tall
Antonym: short
Word: big
Antonym:
EOD;

    assertEquals($expected, $dto->format('big'));
});

## Using the Library

https://github.com/alnutile/larachain-prompt-templates

Is the start of this library

it('can replace 1 item', function () {
    $template = <<<'EOD'
I want you to act as a naming consultant for new companies.
What is a good name for a company that makes {product}?
EOD;

    $input_variables = [
        new PromptToken(
            'product',
            'colorful socks'
        ),
    ];
    $dto = new PromptTemplate(
        $input_variables,
        $template
    );

    assertEquals('I want you to act as a naming consultant for new companies.
What is a good name for a company that makes colorful socks?', $dto->format());
});

This shows a test with it working.

References

https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html