Prompt Builder

gllm-inference | Tutorial: Prompt Builder| Use Case: Utilize Language Model Request Processor | API Reference

What's a Prompt Builder?

The prompt builder is a module designed to manage prompt templates and seamlessly build a list of messages ready to be sent to a language model. In this tutorial, you'll learn how to utilize the PromptBuilder in just a few lines of code.

Prerequisites

This example specifically requires completion of all setup steps listed on the Prerequisites page.

Installation

# you can use a Conda environment
pip install --extra-index-url https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/ gllm-inference

Quickstart

Let’s jump into a basic example using PromptBuilder. Prompt builder cannot be empty, which means it must have at least a system template, a user template, or both.

from gllm_inference.prompt_builder import PromptBuilder

prompt_builder = PromptBuilder(
    system_template="Talk like a pirate.", 
    user_template="What is the capital city of Indonesia?",
)
messages = prompt_builder.format()
print(messages)

Expected Output

Prompt Variables

One of the most useful feature of the prompt builder is the ability to add prompt variables.

Basically, we can add a placeholder in the prompt templates using curly braces ({}). These placeholder can then be replaced with the actual value that we want during runtime. This allows us to easily customize just certain parts of the prompts, while the common parts can be stored in the prompt builder during initialization.

In the following example, let's try to assign {role} and {country} as prompt variables!

Expected Output

Adding History

Prompt builder allows adding history to handle multiturn conversations. History passed to the prompt builder will be positioned in between the system message and the user message.

Expected Output

Adding Extra Contents

Prompt builder also allows adding extra contents to be passed to the language model, such as extra texts, attachments, tool calls, and many more. Extra contents passed to the prompt builder will be added to the user message contents.

Expected Output

Last updated