Working with DeepSeek in Python
James Chapman
Curriculum Manager, DataCamp
Extract thinking tokens
import re
match = re.search(r'<think>(.*?)<\/think>', response_content, re.DOTALL)
think_content = match.group(1).strip()
print(think_content)
Remove thinking tokens
final_response = re.sub(r'<think>[\s\S]*?<\/think>\s*', '', response_content, re.DOTALL)
print(final_response.strip())
"system"
messages shouldn't be used with reasoning modelsmessages = []
user_msgs = [code_to_debug, follow_up]
for q in user_qs: user_dict = {"role": "user", "content": q} messages.append(user_dict) response = client.chat.completions.create(model="deepseek-ai/DeepSeek-R1", messages=messages)
final_response = re.sub(r'<think>[\s\S]*?<\/think>\s*', '', response.choices[0].message.content, re.DOTALL) assistant_dict = {"role": "assistant", "content": final_response.strip()}
messages.append(assistant_dict)
Working with DeepSeek in Python