第一周总目标
- ✅ 本地能跑 Python 脚本
- ✅ 能调用大模型 API 并拿到返回结果
- ✅ 对 Python 基础语法恢复“够用”的水平
每日任务拆解
第 1 天:环境搭建 + 第一个 Python 脚本
目标:能写、能跑 Python 代码
具体任务:
- 安装 Python
- 选一个编辑器:推荐 VS Code,装 Python 插件
- 新建一个文件夹,比如
rag-learning - 在终端验证:
python --version能看到版本号 - 写第一个脚本
hello.py,内容只有一行:print("Hello, RAG") - 运行:
python hello.py,看到输出
✅ 今日产出:能写、能跑 Python 脚本
第 2 天:Python 语法快速恢复(够用版)
目标:恢复最常用的语法,不用全学
具体任务(边敲边练):
在 hello.py 里依次练习:
# 1. 变量与打印
name = "RAG"
print(f"Hello, {name}")
# 2. 列表与循环
items = ["苹果", "香蕉", "橙子"]
for item in items:
print(item)
# 3. 字典
user = {"name": "张三", "age": 18}
print(user["name"])
# 4. 函数
def greet(name):
return f"Hello, {name}"
result = greet("World")
print(result)
# 5. if 判断
score = 85
if score >= 60:
print("及格")
else:
print("不及格")
不用背,知道怎么写、能看懂就行。
✅ 今日产出:以上 5 个语法块都能独立敲出来
第 3 天:第三方库 + requests 基础
目标:学会装库、用 requests 发请求
具体任务:
-
安装 requests 库:
pip install requests -
写一个脚本
test_request.py,调一个公共 API:
import requests
# 调用一个公开的测试接口
response = requests.get("https://api.github.com/zen")
print(response.text)
- 理解核心概念:
requests.get()发 GET 请求response.text拿返回内容response.json()如果返回 JSON 格式
✅ 今日产出:能成功调一个公共 API 并打印结果
第 4 天:注册 API 密钥 + 调大模型接口
目标:第一次成功调用大模型
具体任务:
- 注册一个 API 平台:
- 阿里百炼:新用户注册送余额,支持多种模型
- 拿到 API Key
- 写脚本调用:
import requests
API_KEY = "你的密钥"
url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "deepseek-v3",
"messages": [
{"role": "user", "content": "用一句话解释什么是 RAG"}
]
}
response = requests.post(url, headers=headers, json=data)
result = response.json()
print(result["choices"][0]["message"]["content"])
****这里url有个注意的点,阿里云官方文档明确说明了base url的默认值,北京地域为:https://dashscope.aliyuncs.com/compatible-mode/v1,国际地域是:https://dashscope-intl.aliyuncs.com/compatible-mode/v1,但是上面代码里的url是要用base url + /chat/completions拼接起来的.
- 运行,看到返回结果
✅ 今日产出:成功调用大模型,拿到回答
第 5 天:用 FastAPI 把调用包装成服务
目标:从脚本升级成“别人能调用的接口”
具体任务:
- 安装 FastAPI 和 uvicorn
pip install fastapi uvicorn - 写一个最简单的 API
app.py:
from fastapi import FastAPI
import requests
app = FastAPI()
@app.get("/ask")
def ask(question: str):
# 调用 阿里百炼 的代码(复用昨天的)
# 返回结果
return {"answer": "这里放大模型返回的内容"}
- 运行服务:
uvicorn app:app --reload - 测试:打开浏览器访问
http://127.0.0.1:8000/ask?question=你好
✅ 今日产出:有一个本地 HTTP 接口,能通过 URL 参数调大模型
***运行服务器的指令uvicorn app:app --reload这里有点要注意一下,app:app这里第二个app指的是你的项目名,第一个app指的是你的项目路径,像我是把项目app.py放在桌面上的,那我的启动服务的命令就是uvicorn Desktop.app:app --reload;还有这里在浏览器里面运行,得到的结果可能会乱码,看最后面的完整代码就行
几个提醒
- API Key 不要提交到 GitHub,先放代码里没关系,后面会教怎么用环境变量
- 遇到报错很正常,把报错信息复制到搜索引擎搜,这是必经之路
- 每天的任务如果完不成,就拆成两天,不要有压力
- 这周的目标是“能跑”,不是“理解透彻” ,理解可以留到后面
下面是完整的代码:
from fastapi import FastAPI, Response
import requests
API_KEY = "你的api_key"
url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
app = FastAPI()
@app.get("/ask")
def ask(question: str, response: Response):
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "deepseek-v3",
"messages": [
{"role": "user", "content": question}
]
}
response.headers["Content-Type"] = "application/json; charset=utf-8"#防止浏览器访问结果乱码
res = requests.post(url,headers=headers,json=data)
result = res.json()
print(result)
resStatus = res.status_code
if resStatus == 200:
answer = result["choices"][0]["message"]["content"]
return {"answer": answer}
else:
print("request fail")