AI Personal Learning
and practical guidance
CyberKnife Drawing Mirror

Anthropic Prompts Engineering Course - Chapter 1: Basic Prompt Structures

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:


  1. Your tips, in quotes.
  2. Model versions, in quotes
  3. 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 provides 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 Name
  • max_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 alternatingusercap (a poem)assistantconversation rounds to run on. When creating a newMessageWhen you use the messages parameter to specify the previous dialog round, the model generates the next dialog round in the dialogMessageThe
    • Each input message must be a file containing the 角色 cap (a poem) 内容 of the object. You can specify a separate 用户-role message, or can contain multiple 用户 cap (a poem) 助手 messages (if any, they must alternate). The first message must always use the user 角色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 will温度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

输入:
用户:嗨,克劳德,你好吗?

输出:
我很好,谢谢关心!作为人工智能助手,我没有人类那样的情感,但我运行正常,随时准备回答您的问题或帮助您完成任何任务。今天我能帮您什么忙?

Example 2

输入:
用户:你能告诉我海洋的颜色吗?

输出:
海洋的颜色会因几个因素而有所不同:  

- 深度 - 海洋在较深的水域中呈现蓝色,这是因为阳光与水分子相互作用的方式。较浅的水域可能呈现更绿色、青绿色,甚至棕色,这取决于海底。  

- 悬浮颗粒 - 水中的浮游生物、沉积物或藻类等物质会影响海洋的颜色,使其看起来更绿、更棕甚至呈红色。  

- 天气状况 - 天空的颜色可以反射在海洋表面,使它在阴天时看起来更灰、更白,甚至更黑。  

- 地理位置——不同海域可以根据纬度、洋流和当地海洋生物等因素呈现出略微不同的色调。  

总体来说,开阔海洋的占主导地位的颜色是深邃、丰富的蓝色。这是由于阳光与水的相互作用以及海洋深处缺乏悬浮颗粒。但海洋的颜色确实会根据具体条件而有所不同。

Example 3

输入:
用户:Celine Dion 是哪一年出生的?

输出:
迪奥娜·塞琳出生于 1968 年 3 月 30 日。

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

输入:
Celine Dion 是哪一年出生的?

输出:
⚠️=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

输入:
用户:Celine Dion 是哪一年出生的?
用户:还有,你能告诉我一些关于她的其他事实吗?

输出:
#ERROR!

Here's a tip with too many line breaks at the beginning.

Example of error 3

输入:
用户:Celine Dion 是哪一年出生的?

输出:
⚠️=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

输入:
	System Prompt
	你的回答应始终是一系列推动对话深入的批判性思考问题(不要为这些问题提供答案)。请勿直接回答用户的问题。

	Prompt
	用户:为什么天空是蓝色的?

输出:
以下是一些用于进一步探讨为什么天空是蓝色的批判性思维问题:
- 为什么天空呈现蓝色?
- 地球大气成分如何导致天空呈现蓝色?
- 可见光的波长在使天空呈现蓝色中扮演什么角色?
- 不同波长的光散射特性如何影响天空的颜色?
- 其他哪些因素,如一天中的时间或天气状况,会影响天空的外观?
- 科学家们历史上是如何解释和研究蓝天现象的?
- 有哪些替代观点或理论被提出来说明天空为什么是蓝色的?
通过探索这些类型的问题,我们可以更深入地了解导致天空熟悉蓝色调的科学原理和机制。

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:

输入:
User: 数到 3。

输出:
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:

输入:
	System Prompt
	像 3 岁的孩子一样写作。

	Prompt
	User: 天空有多大?

输出:
天空好大哦!它在那儿,好高好高哦!你都看不到它的尽头呢!它是蓝蓝的,里面有好多好多云朵。我好想跳起来摸摸天空,但我够不到。它太高了!天空又大又漂亮,我想一直盯着它看!

❓ If you need a reminder, click on the plus sign (➕) on the far left of this line.

May not be reproduced without permission:Chief AI Sharing Circle " Anthropic Prompts Engineering Course - Chapter 1: Basic Prompt Structures
en_USEnglish