在推理模式下编写代码
现在轮到您使用 DeepSeek V4-Pro 的推理功能来编写代码了!正如您所知,开发可靠代码的一个关键环节是编写测试,用于在返回最终输出前验证中间过程是否正常运行。写得好的测试能让调试轻松许多!
您的同事交接给您一份他们正在编写的 Python 脚本,您注意到其中没有任何测试。由于您对脚本中使用的库或语法并不熟悉,您将借助 V4-Pro 的默认推理,为脚本添加合适的测试,即使用 assert 语句。如果您想进一步了解 assert 语句,请参阅这篇 DataCamp 教程。
import numpy as np
a = np.arange(5)
b = a.reshape(-1, 1)
c = np.ones((5, 3))
d = np.hstack((b, c))
col_means = d.mean(axis=0)
本练习是课程的一部分
在 Python 中使用 DeepSeek
练习说明
- 将提供的代码复制到已为您起好头的分隔提示中。
- 将
prompt发送到deepseek-ai/DeepSeek-V4.1-Flash。
交互式实操练习
通过完成这段示例代码来试试这个练习。
client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")
# Copy the code into the prompt
prompt = """[Task: Write Python assert statements for the following code.]
Code:
____
"""
# Send the prompt — V4-Pro reasons by default
response = client.chat.completions.create(model="deepseek-ai/DeepSeek-V4.1-Flash", messages=[{"role": "user", "content": ____}], max_tokens=400)
print(response.choices[0].message.content)