美文网首页
第1章:基本提示结构-Claude应用开发教程

第1章:基本提示结构-Claude应用开发教程

作者: AI黑悟空 | 来源:发表于2024-09-09 00:20 被阅读0次

    设置

    运行以下设置单元以加载您的 API 密钥并建立 get_completion 辅助函数。

    !pip install anthropic
    
    # Import python's built-in regular expression library
    import re
    import anthropic
    
    # Retrieve the API_KEY & MODEL_NAME variables from the IPython store
    %store -r API_KEY
    %store -r MODEL_NAME
    
    client = anthropic.Anthropic(api_key=API_KEY)
    
    def get_completion(prompt: str, system_prompt=""):
        message = client.messages.create(
            model=MODEL_NAME,
            max_tokens=2000,
            temperature=0.0,
            system=system_prompt,
            messages=[
              {"role": "user", "content": prompt}
            ]
        )
        return message.content[0].text
    

    课程

    Anthropic 提供两个 API,即旧版文本完成 API 和当前消息 API。在本教程中,我们将专门使用消息 API。

    使用消息 API 调用 Claude 至少需要以下参数:

    • model:您打算调用的模型的 API 模型名称
    • max_tokens:停止前要生成的最大标记数。请注意,Claude 可能会在达到此最大值之前停止。此参数仅指定要生成的绝对最大标记数。此外,这是一个硬停止,这意味着它可能会导致 Claude 停止生成中间单词或中间句子。
    • messages:输入消息数组。我们的模型经过训练,可以在交替的用户和助手对话轮次上运行。创建新消息时,您可以使用 messages 参数指定先前的对话轮次,然后模型会在对话中生成下一条消息。

    每条输入消息都必须是具有角色和内容的对象。您可以指定单个用户角色消息,也可以包含多个用户和助手消息(如果是,它们必须交替出现)。第一条消息必须始终使用用户角色。

    还有可选参数,例如:

    • 系统:系统提示 – 更多信息见下文。
    • 温度:Claude 响应的变化程度。对于这些课程和练习,我们将温度设置为 0。
    • 有关所有 API 参数的完整列表,请访问我们的 API 文档。

    示例

    让我们看看 Claude 如何响应一些格式正确的提示。对于以下每个单元格,运行单元格(shift+enter),Claude 的响应将显示在块下方。

    # Prompt
    PROMPT = "Hi Claude, how are you?"
    
    # Print Claude's response
    print(get_completion(PROMPT))
    
    # Prompt
    PROMPT = "Can you tell me the color of the ocean?"
    # Print Claude's response
    print(get_completion(PROMPT))
    
    # Prompt
    PROMPT = "What year was Celine Dion born in?"
    
    # Print Claude's response
    print(get_completion(PROMPT))
    

    现在让我们来看看一些不包含正确 Messages API 格式的提示。对于这些格式错误的提示,Messages API 会返回错误。

    首先,我们有一个 Messages API 调用的示例,该调用的消息数组中缺少角色和内容字段。

    # Get Claude's response
    response = client.messages.create(
            model=MODEL_NAME,
            max_tokens=2000,
            temperature=0.0,
            messages=[
              {"Hi Claude, how are you?"}
            ]
        )
    
    # Print Claude's response
    print(response[0].text)
    

    这是用户与助手角色切换失败的提示。

    # Get Claude's response
    response = client.messages.create(
            model=MODEL_NAME,
            max_tokens=2000,
            temperature=0.0,
            messages=[
              {"role": "user", "content": "What year was Celine Dion born in?"},
              {"role": "user", "content": "Also, can you tell me some other facts about her?"}
            ]
        )
    
    # Print Claude's response
    print(response[0].text)
    

    用户和助手消息必须交替出现,并且消息必须以用户回合开始。您可以在提示中拥有多个用户和助手对(就像模拟多回合对话一样)。您还可以将单词放入终端助手消息中,以便 Claude 从您上次中断的地方继续(有关详细信息,请参阅后面的章节)。

    System Prompts

    您还可以使用系统提示。系统提示是一种在“用户”回合中向 Claude 提出问题或任务之前向其提供上下文、说明和指南的方法。

    从结构上讲,系统提示与用户和助手消息列表分开存在,因此属于单独的系统参数(请查看笔记本的“设置”部分中 get_completion 辅助函数的结构)。

    在本教程中,无论我们在哪里使用系统提示,我们都会在完成函数中为您提供系统字段。如果您不想使用系统提示,只需将 SYSTEM_PROMPT 变量设置为空字符串。

    System Prompt 示例

    # System prompt
    SYSTEM_PROMPT = "Your answer should always be a series of critical thinking questions that further the conversation (do not provide answers to your questions). Do not actually answer the user question."
    
    # Prompt
    PROMPT = "Why is the sky blue?"
    
    # Print Claude's response
    print(get_completion(PROMPT, SYSTEM_PROMPT))
    

    为什么要使用System Prompt?编写良好的System Prompt可以通过多种方式提高 Claude 的表现,例如提高 Claude 遵守规则和指令的能力。有关更多信息,请访问我们的文档,了解如何将系统提示与 Claude 结合使用。

    现在我们将深入研究一些练习。如果您想在不更改上述任何内容的情况下尝试课程提示,请一直滚动到课程笔记本的底部以访问示例操场。

    练习

    练习 1.1 – 数到三

    使用正确的用户/助手格式,编辑下面的提示,让 Claude 数到三。输出还将指示您的解决方案是否正确。

    # Prompt - this is the only field you should change
    PROMPT = "[Replace this text]"
    
    # Get Claude's response
    response = get_completion(PROMPT)
    
    # Function to grade exercise correctness
    def grade_exercise(text):
        pattern = re.compile(r'^(?=.*1)(?=.*2)(?=.*3).*$', re.DOTALL)
        return bool(pattern.match(text))
    
    # Print Claude's response and the corresponding grade
    print(response)
    print("\n--------------------------- GRADING ---------------------------")
    print("This exercise has been correctly solved:", grade_exercise(response))
    

    练习 1.2 – System Prompt

    修改 SYSTEM_PROMPT,让 Claude 像对待 3 岁小孩一样做出反应。

    # System prompt - this is the only field you should change
    SYSTEM_PROMPT = "[Replace this text]"
    
    # Prompt
    PROMPT = "How big is the sky?"
    
    # Get Claude's response
    response = get_completion(PROMPT, SYSTEM_PROMPT)
    
    # Function to grade exercise correctness
    def grade_exercise(text):
        return bool(re.search(r"giggles", text) or re.search(r"soo", text))
    
    # Print Claude's response and the corresponding grade
    print(response)
    print("\n--------------------------- GRADING ---------------------------")
    print("This exercise has been correctly solved:", grade_exercise(response))
    

    恭喜!

    如果您已经解决了到目前为止的所有练习,那么您就可以进入下一章了。祝您好运!

    示例游乐场

    这是一个供您自由试验本课中显示的提示示例的区域,并调整提示以查看它如何影响 Claude 的回答。

    # Prompt
    PROMPT = "Hi Claude, how are you?"
    
    # Print Claude's response
    print(get_completion(PROMPT))
    # Prompt
    PROMPT = "Can you tell me the color of the ocean?"
    
    # Print Claude's response
    print(get_completion(PROMPT))
    # Prompt
    PROMPT = "What year was Celine Dion born in?"
    
    # Print Claude's response
    print(get_completion(PROMPT))
    # Get Claude's response
    response = client.messages.create(
            model=MODEL_NAME,
            max_tokens=2000,
            temperature=0.0,
            messages=[
              {"Hi Claude, how are you?"}
            ]
        )
    
    # Print Claude's response
    print(response[0].text)
    
    # Get Claude's response
    response = client.messages.create(
            model=MODEL_NAME,
            max_tokens=2000,
            temperature=0.0,
            messages=[
              {"role": "user", "content": "What year was Celine Dion born in?"},
              {"role": "user", "content": "Also, can you tell me some other facts about her?"}
            ]
        )
    
    # Print Claude's response
    print(response[0].text)
    
    # System prompt
    SYSTEM_PROMPT = "Your answer should always be a series of critical thinking questions that further the conversation (do not provide answers to your questions). Do not actually answer the user question."
    
    # Prompt
    PROMPT = "Why is the sky blue?"
    
    # Print Claude's response
    print(get_completion(PROMPT, SYSTEM_PROMPT))
    

    更多AI教程请访问:第1章:基本提示结构-Claude应用开发教程 – 欢迎来到ClaudeAI博客网站 (assh83.com)

    相关文章

      网友评论

          本文标题:第1章:基本提示结构-Claude应用开发教程

          本文链接:https://www.haomeiwen.com/subject/fkveljtx.html