開始使用免費開始

使用推理模式進行程式撰寫

現在輪到你用 DeepSeek V4-Pro 的推理能力來寫程式了!你可能已經知道,開發可靠的程式碼很重要的一環,就是加入測試,先驗證中間步驟是否正確,再產生最後輸出。寫得好的測試能讓偵錯容易許多!

同事交接給你一支他們正在開發的 Python 指令稿,你發現裡面沒有任何測試。由於你不熟悉這支指令稿所用的函式庫與語法,你會依靠 V4-Pro 的預設推理,在指令稿中加入適當的 assert 陳述式作為測試。如果你想更深入了解 assert 陳述式,請參考[這篇 DataCamp 教學](https://www.datacamp.com/tutorial/understanding-the-python-assert-statement)。

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)
編輯並執行程式碼