第 2 章:基本函数构建-Claude开发应用教程

news/2024/9/23 0:42:16/

设置

运行以下设置单元以加载您的 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)
> 
> #Note that we changed max_tokens to 4K just for this lesson to allow for longer completions in the exercises
> def get_completion(prompt: str, system_prompt=""):
>     message = client.messages.create(
>         model=MODEL_NAME,
>         max_tokens=4000,
>         temperature=0.0,
>         system=system_prompt,
>         messages=[
>           {"role": "user", "content": prompt}
>         ]
>     )
>     return message.content[0].text

课程

Claude 对明确而直接的指示反应最好。

将 Claude 视为任何其他刚上任的人。除了您直接告诉它的内容之外,Claude 不知道该做什么。就像您第一次指示人类完成任务时一样,您越是直截了当地向 Claude 解释您想要什么,Claude 的回应就会越好、越准确。”如有疑问,请遵循明确提示的黄金法则:

向同事或朋友展示您的提示,让他们自己按照说明操作,看看他们是否能产生您想要的结果。如果他们感到困惑,Claude 也会感到困惑。

示例

让我们以写诗这样的任务为例。(忽略任何音节不匹配 – LLM 还不擅长计算音节。)

> #Prompt
> PROMPT = "Write a haiku about robots."
> 
> #Print Claude's response
> print(get_completion(PROMPT))

这首俳句已经很不错了,但用户可能希望 Claude 直接进入诗歌,而不使用“这是一首俳句”的开场白。

我们如何实现这一点?我们要求这样做!

> #Prompt
> PROMPT = "Write a haiku about robots. Skip the preamble; go straight into the poem."
> 
> #Print Claude's response
> print(get_completion(PROMPT))

这是另一个例子。让我们问 Claude 谁是有史以来最优秀的篮球运动员。您可以在下面看到,虽然 Claude 列出了几个名字,但它并没有用明确的“最佳”来回答。

> #Prompt
> PROMPT = "Who is the best basketball player of all time?"
> 
> #Print Claude's response
> print(get_completion(PROMPT))

我们能让Claude下定决心,选出最佳球员吗?是的!只要问!

> #Prompt
> PROMPT = "Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?"
> 
> #Print Claude's response
> print(get_completion(PROMPT))

如果您想尝试课程提示而不更改上述任何内容,请一直滚动到课程笔记本的底部以访问示例操场。

练习

练习 2.1 – Spanish

修改 SYSTEM_PROMPT,让 Claude 以西班牙语输出答案。

> #System prompt - this is the only field you should chnage
> SYSTEM_PROMPT = "[Replace this text]"
> 
> #Prompt
> PROMPT = "Hello Claude, how are you?"
> 
> #Get Claude's response
> response = get_completion(PROMPT, SYSTEM_PROMPT)
> 
> #Function to grade exercise correctness
> def grade_exercise(text):
>     return "hola" in text.lower()
> 
> #Print Claude's response and the corresponding grade
> print(response)
> print("\n--------------------------- GRADING ---------------------------")
> print("This exercise has been correctly solved:", grade_exercise(response))

练习 2.2 – 仅限一名玩家

修改提示,使 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):
>     return text == "Michael Jordan"
> 
> #Print Claude's response and the corresponding grade
> print(response)
> print("\n--------------------------- GRADING ---------------------------")
> print("This exercise has been correctly solved:", grade_exercise(response))

练习 2.3 – 写一个故事

修改提示,让 Claude 尽可能长地回答。如果您的答案超过 800 个字,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):
>     trimmed = text.strip()
>     words = len(trimmed.split())
>     return words >= 800
> 
> #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 = "Write a haiku about robots."
> 
> #Print Claude's response
> print(get_completion(PROMPT))
> 
> #Prompt
> PROMPT = "Write a haiku about robots. Skip the preamble; go straight into the poem."
> 
> #Print Claude's response
> print(get_completion(PROMPT))
> #Prompt
> PROMPT = "Who is the best basketball player of all time?"
> 
> #Print Claude's response
> print(get_completion(PROMPT))
> 
> 
> #Prompt
> PROMPT = "Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?"
> 
> #Print Claude's response
> print(get_completion(PROMPT))

本文由AI技术博客平台 ClaudeAI http://assh83.com/发布!


http://www.ppmy.cn/news/1523867.html

相关文章

校篮球联赛系统小程序的设计

管理员账户功能包括:系统首页,个人中心,管理员管理,公告管理,基础数据管理,球队管理,球员管理,赛事信息管理,用户管理,轮播图信息 微信端账号功能包括&#…

通信工程学习:什么是ASK振幅键控、FSK频移键控、PSK相移键控

ASK振幅键控、FSK频移键控、PSK相移键控 ASK(振幅键控)、FSK(频移键控)和PSK(相移键控)是三种常见的数字调制技术,它们各自通过不同的方式改变载波的某个参数来传输数字信息。以下是对这三种调制…

Django 文件上传时出现 500 错误

在 Django 中,文件上传时出现 500 错误通常是服务器端未处理的异常。这类错误可能有多种原因,包括配置问题、权限问题或上传逻辑中的错误。以下是一些常见的导致 Django 文件上传失败并出现 500 错误的原因和解决方法。 1、问题背景 在 Django 中使用文…

uniapp,vite整合windicss

官方文档:https://weapp-tw.icebreaker.top/docs/quick-start/frameworks/hbuilderx 安装: npm i -D tailwindcss postcss autoprefixer # 初始化 tailwind.config.js 文件 npx tailwindcss initnpm i -D weapp-tailwindcss# 假如 tailwindcss 在 weap…

Express框架中的res

中间件由两部分组成,中间件方法和请求处理函数; 中间件方法由express提供,负责拦截请求,请求处理函数由开发人员提供,负责处理请求; app.get(‘请求路径’,‘处理函数’); //接收并处理get请求 app.post(‘请求路径’,‘处理函数’); //接收并处理post请求 可以对同一个请求…

【云备份】可视化客户端----QT开发➕QT数据库编程

文章目录 一、 需求分析二、 概念结构设计三、逻辑结构设计1. 用户表 (users)2. 客户端本地文件信息表 (upload_files)3. 备份记录表 (backup_records)4. 服务端备份文件信息表 (backup_files) 四、 开发工具五、具体实现(一) 客户端程序运行演示 一、 需…

后端Web之SpringBoot原理

目录 1.配置优先级 2.Bean 3.SpringBoot原理 1.配置优先级 SpringBoot中支持三种格式的配置文件: .application.properties、application.yml和application. yaml。它们的配置优先级分别降低。虽然springboot支持多种格式配置文件,但是在项目开发时,推荐统一使用…

系统性能分析工具sysstat之sar命令以及nginx中打开gzip使用配置gzip_http_version值为1.0和1.1时遇到的结果乱码问题

一、系统性能分析工具:sysstat之sar命令 服务器维护常用的管理命令有很多:top、ps,、pstree、vmstat、iostat、iotop等,但好像都不全面,这里有一个比较系统全面的性能分析工具sar(System Activity Reporter系统活动情况…