As the world of AI continues to advance, the need for advanced prompt engineering grows as well. In this post, we are going to have a sneak peak into the backend code of Claude, a prompt engineering tool by Anthropic.
YouTube Tutorial
The video that goes through the below content in streaming format is as follows:
This tool is built for building prompt templates that enables you to fill in additional context to make a complete prompt to input into a ChatBot. Below is an example.
Test Output
Below is a breakdown of the backend code for making the above function:
The above code starts by importing the anthropic library for accessing the Anthropic API and the re (regular expression) library for text processing.
An API key is defined for authenticating with the Anthropic API and the model ‘claude-3-opus-20240229’ is selected for use.
The above code also initializes a client for interacting with the Anthropic API, using the provided API key.
The above is a metaprompt that outlines how to instruct Claude to perform its tasks. The specific tasks include:
Choose an item from a menu for me given user preferences
Rate a resume according to a rubric
Explain a complex scientific concept in simple terms
Draft an email responding to a customer complaint
Design a marketing strategy for launching a new product
In the above code, the chosen task is in quotations, which users may change according to their own preferences. VARIABLES = [ ] specifies which variables Claude will need to complete the task. It’s empty by default, which means no specific variables are required unless specified.
The above code would generate a string of variables to be included in the prompt formatted according to requirements.
The above code creates the prompt for Claude by replacing the placeholders in the metaprompt with the actual task and variables.
assistant_partial is initialized with the value “<Inputs>”. This is the starting point for constructing the input section of the prompt. The if statement checks if ‘variable_string’, all the variables formatted as placeholders concatenated together is not empty.
If there are variables specified for the task and variable_string is not empty, the string of variables will be appended to ‘assistant_partial’, followed by a new line and the closing </Inputs> tag. Then <Instructions Structure> would be where the instructions for the AI on how to process the inputs would be inserted in the full prompt.
The above code sends the generated prompt to the Claude model, specifying the model name, maximum token limit, and the prompt itself. The temperature parameter controls the randomness of the response.
The above is the definition for the function for formatting messages for readability.
The above are the definitions for functions in extracting content between XML-like tags, removing empty tags, and extracting prompt from the <Instructions> section of the ‘metaprompt-response’ string using the extract_between_tags and the remove_empty_tags functions.
The extract_variables function is used to extract variables from the specific prompt.
The above extract prompt template and variables function processes the response from Claude to extract a template for the prompt and identify any variables used. The print statements as well as pretty_print would yield the following text.
Test the Prompt
The above user input field asks the user to input value for each of the extracted variables. The prompt_with_variables function replaces variable placeholders in the extracted prompt template with actual values provided by the user.
message = CLIENT.messages.create( sends the final prompt to Claude for processing.
print(“Claude’s output on your prompt:\n\n”) and pretty_print(message) would show the following result:
Our decimal system - or the Hindu-Arabic numeral system, as it’s also called-has the basic rhythm of 10. The binary number system, which all modern computers use, has 2 as its basic rhythm. Everything is written in combinations of two numbers: 0 and 1. Ancient Babylon, amusingly enough, has a number system with 60 as the basic rhythm. That system became important for calculating time-seconds, minutes, and hours-and also for measuring tangles in a circle. In just about any other context, the Babylonian number system is pretty impractical. It didn’t even have a sign for zero.
Udemy Python vs. R course
The below Python vs. R course will be launched on Udemy in the next few days. Currently it lays out the code in Python and R for creating pivot tables, detect anomalies using Benford’s Law, showing a heatmap, displaying a clustering plot, converting image to text, analyzing text data, machine learning decision tree, and the Plotly package in Python for plotting data structures. If there are topics you would like to see covered in the course, please let me know by commenting or replying to this email.