MulaiMulai sekarang secara gratis

Extracting the model’s thoughts

The model response content from the previous exercise's math_problem has been stored under response_content.

Note that this may differ from what you saw when you ran the exercise, as LLM outputs are inherently random (more on this in Chapter 2).

Your job is to separate thought from answer using regular expressions (RegEx).

Latihan ini adalah bagian dari kursus

Working with DeepSeek in Python

Lihat Kursus

Petunjuk latihan

  • Import the re built-in Python module.
  • Search for the model's thoughts in the response by looking for strings between two <think> tags using the RegEx provided.
  • Extract the first group from match using the .group() method ,and strip these thoughts of any leading and trailing whitespace.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import the re module
import re

# Search for strings between think tags
match = re.____(r'(.*?)', ____, re.DOTALL)

# Extract the group from the match and strip whitespace
think_content = match.____(1).____()
print(think_content)
Edit dan Jalankan Kode