academic program
This section of the course needs to be executed in a code environment and focuses on practicing how to construct the correct message interface format, which can be skipped.
The Claude The for Sheets extension provides A few featuresYou can use these functions to call Claude. one of these functions is CLAUDEMESSAGES(), which is constructed to reflect the Messaging API Structure.
CLAUDEMESSAGES() can take multiple arguments in the following order:
- Your tips, in quotes.
- Model versions, in quotes
- whicheverOptional additional parameters, such as temperature, system alerts, maximum number of markers, etc.
Attention: Temperature is related to the variability of Claude's answers. In these exercises, we set Temperature to 0. In Chapter 8, we will explore temperature in more depth.
Call Claude from any location using the CLAUDEMESSAGES() formula. The basic formula structure is as follows:=CLAUDEMESSAGES("{PROMPT}", "{MODEL_VERSION}", "system", "{SYSTEM_PROMPT}")
For example, to invoke the prompt call Claude 3 Haiku located in cell A1, you would write: =CLAUDEMESSAGES(A1, "claude-3-haiku-20240307", "system", "Respond in Esperanto only")
How to relate the CLAUDEMESSAGES() structure to the Messages API structure? Let's take a look at some sample hints sent to CLAUDEMESSAGES() and their underlying formulas.
Parameter description
Anthropic offers two APIs, the traditionalText Completion APIand the currentMessaging API. In this tutorial, we will only use the Messaging API.
At a minimum, the following parameters are required to call Claude using the Messages API:
model
: The model you intend to call theAPI Model Namemax_tokens
: The maximum number of markers to generate before stopping. Note that Claude may stop before reaching this maximum. This parameter only specifies the absolute maximum number of markers to generate. In addition, it is ahard stopThis means that it may cause Claude to stop being generated in the middle of a word or sentence.messages
: input message array. Our model is trained on alternatinguser
cap (a poem)assistant
conversation rounds to run on. When creating a newMessage
When you use the messages parameter to specify the previous dialog round, the model generates the next dialog round in the dialogMessage
The- Each input message must be a file containing the
character
cap (a poem)element
of the object. You can specify a separatesubscribers
-role message, or can contain multiplesubscribers
cap (a poem)helper
messages (if any, they must alternate). The first message must always use the usercharacter
The
- Each input message must be a file containing the
Some optional parameters also exist, e.g:
system
: System Tips - more on this below.temperature
: The degree of variation in Claude's response. For these lessons and exercises, we willtemp
Set to 0.
For a complete list of all API parameters, visit ourAPI DocumentationThe
typical example
Let's see how Claude responds to some correctly formatted hints. Don't worry about how the answers are generated.
Example 1
Input:
User: Hi Claude, how are you?
OUTPUT.
I'm fine, thanks for asking! As an AI assistant, I don't have the same emotions as a human, but I'm functioning fine and ready to answer your questions or help you with any task. How can I help you today?
Example 2
Input:
User: Can you tell me the color of the ocean?
Output:
The color of the ocean can vary depending on several factors:
- Depth - The ocean appears blue in deeper waters due to the way sunlight interacts with water molecules. Shallower waters may appear greener, lime green, or even brown, depending on the seafloor.
- Suspended particles - Substances such as plankton, sediment, or algae in the water can affect the color of the ocean, making it appear greener, browner, or even reddish.
- Weather conditions - The color of the sky can be reflected on the surface of the ocean, making it look grayer, whiter, or even darker on cloudy days.
- Geographic location - Different seas can take on slightly different hues depending on factors such as latitude, ocean currents and local marine life.
Overall, the dominant color of the open ocean is a deep, rich blue. This is due to the interaction of sunlight with water and the lack of suspended particles in the ocean depths. However, the color of the ocean does vary depending on specific conditions.
Example 3
Input:
User: What year was Celine Dion born?
Output:
Dionne Celine was born on 3/30/1968.
Now let's look at some incorrectly formatted hints. The CLAUDEMESSAGES() function returns an error for these incorrectly formatted prompts. Here's one that's missing the beginning of "User:".
Example of error 1
Enter:
What year was Celine Dion born?
Output:
⚠️=CLAUDEMESSAGES prompt should be in ""User: ... Assistant: ..."" format, with ""User: ..."" first. For newlines, press Ctrl/Cmd+Enter⚠️
Here's a tip for failing to alternate user and helper roles.
Error example 2
Input:
User: What year was Celine Dion born?
User: Also, can you tell me some other facts about her?
Output:
#ERROR!
Here's a tip with too many line breaks at the beginning.
Example of error 3
Input:
User: What year was Celine Dion born?
Output:
⚠️=CLAUDEMESSAGES prompt should be in "User: ... Assistant: ..." format, with "User: ..." first. For newlines, press Ctrl/Cmd+Enter⚠️
"User" and "Helper" messages must alternateand the message must begin with "User:".
When using CLAUDEMESSAGES(), be sure to distinguish messages (a message is a separate user or helper round) by inserting line breaks between each message. If you do not do this, Claude will not return an error, but Claude will treat everything not separated by newlines as a single message.
You can include multiple "User:"/"Assistant:" conversations in a single prompt (like simulating a multi-round conversation). You can also put some words in the last "Assistant:" message to let Claude pick up where you left off (we'll discuss this in more detail in a later section).
You can use System Prompts to give Claude instructions and guidelines. A system prompt is a "user" round that takes place before a question or task is asked of Claude.Provide context, instructions and guidanceOne way to do this.
System hints are structurally separate from the user and helper message lists, and therefore are a separate "system hint" parameter when using CLAUDEMESSAGES(). To make things easier for you in this tutorial, we have provided system cue input boxes that are used to enter a complete CLAUDEMESSAGES() formula for calling Claude.
Example 1
Input:
System Prompt
Your response should always be a series of critical thinking questions that drive the conversation deeper (do not provide answers to these questions). Do not answer the user's questions directly.
Prompt
User: Why is the sky blue?
Output:
Here are some critical thinking questions for further exploring why the sky is blue:
- Why does the sky appear blue?
- How does the composition of the Earth's atmosphere contribute to the blue color of the sky?
- What role does the wavelength of visible light play in making the sky blue?
- How do the scattering properties of different wavelengths of light affect the color of the sky?
- What other factors, such as the time of day or weather conditions, affect the appearance of the sky?
- How have scientists historically explained and studied the phenomenon of blue skies?
- What alternative ideas or theories have been proposed to explain why the sky is blue?
By exploring these types of questions, we can gain a deeper understanding of the scientific principles and mechanisms that lead to the familiar blue hue of the sky.
Why use system tips?A well-designed system cue can enhance Claude's performance in a number of ways, such as increasing Claude's ability to follow rules and instructions.
For more information, please visit our website onHow to Use System AlertsDocumentation for use with Claude.
If you are ready to try the exercises in this chapter, click on the links below. If you want to try any of the examples in this course, scroll down!
practice
Exercise 1.1 - Counting to Three
Using the correct user/assistant format, write a prompt in the yellow cell below asking Claude to count to three.
Remember, if your answer is correct (meaning the formatting and prompts are correct), Claude's response cell will turn green.
Example:
Input:
User: Count to 3.
Output:
1, 2, 3.
❓ If you need a reminder, click on the plus sign (➕) on the far left of this line.
Exercise 1.2 - System Tips
Modify the system prompt in the orange cell so that Claude responds like a 3-year-old.
Attention: Claude's reply may be much longer than shown. To see the full response, select the response cell, then copy and paste it into a text editor.
Example:
Input:
System Prompt
Write like a 3-year-old.
Prompt
User: How big is the sky?
Output:
The sky is so big! It's there, so high, so high! You can't even see the end of it! It's blue and there are so many, many clouds in it. I want to jump up and touch the sky, but I can't reach it. It's too high! The sky is so big and beautiful that I want to stare at it all the time!
❓ If you need a reminder, click on the plus sign (➕) on the far left of this line.