课程链接:https://www.deeplearning.ai/short-courses/ai-python-for-beginners/
大家好,我是章北海
最近利用零碎时间刷完了吴恩达的新课《AI Python for Beginners》
相信本公众号读者的
Python
肯定是过关的,可能觉得不需要学这门课了
我也是奔着学习课程开发的想法看的这门课
感觉收获还是蛮大的,推荐理由如下:
1、吴恩达亲自授课
deeplearning
官网很多短课吴恩达只是漏了脸,而这门课是吴恩达亲自授课
吴恩达的教学风格以清晰、通俗易懂而著称,他能够将复杂的概念以易于理解的方式传授给学习者。
课程内容深入浅出,注重实践,通过互动测验、实践项目和真实案例,帮助学生将理论知识应用于实际场景中。
2、LLM「助教」
LLM 当「助教」,随时可以唤出 chatbot
,可以询问它 Python
相关问题
这个课定位不是 Python
入门,而是 Python 与大模型的结合
比如 Writing code with chatbots
这一章,我就蛮受启发的
前文【教程】用大模型做数据分析,可视化,仅需一键和我做了一个AI数据分析网站 就是这种思路
3、学会学习
在《how to succeed in coding》
一章中,吴恩达提出了一些学习方法(这些方法不限于学习 Python):
- 实践代码:要亲自动手运行代码,不要只看视频。要暂停视频去运行代码,通过改变代码并观察结果来测试自己对代码行的理解,比如思考如果遗漏引号或有多余括号会发生什么。
- 完成练习:鼓励完成实践练习,还可以尽可能多地使用聊天机器人,将其视为编程伙伴,思考”如果”和”为什么”的问题,并向聊天机器人寻求解答。可询问代码解释、工作原理等问题,专业软件开发者也会这样做。
- 正确对待错误:代码中出现错误是完全正常的,就像刚开始学习新语言时不会立刻说对所有内容一样,学习新的编程语言也不会第一次就全对。如果第一次运行代码不成功,可以尝试不同方法或向聊天机器人求助,聊天机器人还能帮助理解错误信息。
- 深入理解:不要只是从聊天机器人那里复制粘贴答案到代码中,而是阅读其回复以获得新视角和知识,从而加深对编码的理解。
- 自主学习节奏:可以按照自己的节奏学习,随时暂停视频进行思考,想继续时再回来,可快可慢,确保理解概念,享受学习编码的乐趣。
总结,吴恩达关于如何学习编程核心观点是:通过实践、提问、利用 AI 辅助工具、接受错误、深入理解和自主学习,可以更有效地学习编程,并享受这一过程。
4 chatbots 彩蛋
课程中很多章节中有一些内置的函数,其实可以新建 cell
,然后执行 linux
命令看到.py
和 system_prompt
具体内容
学习大佬是如何写 Prompt 的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# helper_functions.py
import os
from openai import OpenAI
from dotenv import load_dotenv
import random
#Get the OpenAI API key from the .env file
load_dotenv('.env', override=True)
openai_api_key = os.getenv('OPENAI_API_KEY')
# Set up the OpenAI client
client = OpenAI(api_key=openai_api_key)
def print_llm_response(prompt):
"""This function takes as input a prompt, which must be a string enclosed in quotation marks,
and passes it to OpenAI's GPT3.5 model. The function then prints the response of the model.
"""
llm_response = get_llm_response(prompt)
print(llm_response)
def get_llm_response(prompt):
"""This function takes as input a prompt, which must be a string enclosed in quotation marks,
and passes it to OpenAI's GPT3.5 model. The function then saves the response of the model as
a string.
"""
try:
if not isinstance(prompt, str):
raise ValueError("Input must be a string enclosed in quotes.")
completion = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{
"role": "system",
"content": "You are a helpful but terse AI assistant who gets straight to the point.",
},
{"role": "user", "content": prompt},
],
temperature=0.0,
)
response = completion.choices[0].message.content
return response
except TypeError as e:
print("Error:", str(e))
def get_chat_completion(prompt, history):
history_string = "\n\n".join(["\n".join(turn) for turn in history])
prompt_with_history = f"{history_string}\n\n{prompt}"
completion = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{
"role": "system",
"content": "You are a helpful but terse AI assistant who gets straight to the point.",
},
{"role": "user", "content": prompt_with_history},
],
temperature=0.0,
)
response = completion.choices[0].message.content
return response
# def open_chatbot():
# """This function opens a Gradio chatbot window that is connected to OpenAI's GPT3.5 model."""
# gr.close_all()
# gr.ChatInterface(fn=get_chat_completion).launch(quiet=True)
def get_dog_age(human_age):
"""This function takes one parameter: a person's age as an integer and returns their age if
they were a dog, which is their age divided by 7. """
return human_age / 7
def get_goldfish_age(human_age):
"""This function takes one parameter: a person's age as an integer and returns their age if
they were a dog, which is their age divided by 5. """
return human_age / 5
def get_cat_age(human_age):
if human_age <= 14:
# For the first 14 human years, we consider the age as if it's within the first two cat years.
cat_age = human_age / 7
else:
# For human ages beyond 14 years:
cat_age = 2 + (human_age - 14) / 4
return cat_age
def get_random_ingredient():
"""
Returns a random ingredient from a list of 20 smoothie ingredients.
The ingredients are a bit wacky but not gross, making for an interesting smoothie combination.
Returns:
str: A randomly selected smoothie ingredient.
"""
ingredients = [
"rainbow kale", "glitter berries", "unicorn tears", "coconut", "starlight honey",
"lunar lemon", "blueberries", "mermaid mint", "dragon fruit", "pixie dust",
"butterfly pea flower", "phoenix feather", "chocolate protein powder", "grapes", "hot peppers",
"fairy floss", "avocado", "wizard's beard", "pineapple", "rosemary"
]
return random.choice(ingredients)
def get_random_number(x, y):
"""
Returns a random integer between x and y, inclusive.
Args:
x (int): The lower bound (inclusive) of the random number range.
y (int): The upper bound (inclusive) of the random number range.
Returns:
int: A randomly generated integer between x and y, inclusive.
"""
return random.randint(x, y)
def calculate_llm_cost(characters, price_per_1000_tokens=0.015):
tokens = characters / 4
cost = (tokens / 1000) * price_per_1000_tokens
return f"${cost:.4f}"
sysyem_prompt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
You are the friendly AI assistant for a beginner python programming class.
You are available to help learners with questions they might have about computer programming,
python, artificial intelligence, the internet, and other related topics.
You should assume zero to very little prior experience of coding when you reply to questions.
You should only use python and not mention other programming languages (unless the question is
about how computers work, where you may mention assembly or machine code if it is relevant to
the answer).
Only write code if you are asked directly by the learner. If you do write any code, it should
be as simple and easy to read as possible - name variables things that are easy to understand,
and avoid pythonic conventions like list comprehensions to help the learner stick to foundations
like for loops and if statements.
Keep your answers to questions short, offering as little explanation as is necessary to answer
the question. Let the learner ask follow up questions to dig deeper.
If the learner asks unrelated questions, respond with a brief reminder: "Please, focus on your programming for AI journey"
你是面向 Python 编程初学者课程的友好人工智能助手。你可以帮助学习者解答他们可能在计算机编程、Python、人工智能、互联网以及其他相关主题方面的问题。
当你回复问题时,应假设学习者几乎没有任何编程经验。除非问题是关于计算机如何工作的(在这种情况下,如果与答案相关,你可以提及汇编语言或机器代码),否则你只能使用 Python,不要提及其他编程语言。
只有在学习者直接要求时才编写代码。如果你编写代码,应尽可能简单易读 —— 给变量取易于理解的名称,并避免使用诸如列表推导式之类的 Python 惯用法,以帮助学习者专注于诸如 for 循环和 if 语句等基础知识。
回答问题要简短,提供回答问题所需的最少解释。让学习者提出后续问题以深入了解。
如果学习者问不相关的问题,用简短的提醒回复:“请专注于你的人工智能编程之旅。”