文本生成

在 Python 中使用 DeepSeek

James Chapman

AI Curriculum Lead, DataCamp

输出如何生成?

  • 文本将最可能补全这段对话
response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",

messages=[{"role": "user", "content": "Hi! I'm James and I'm a Curriculum Manager at DataCamp."}]
) print(response.choices[0].message.content)
Hi James! It's great to meet you. As a **Curriculum Manager at DataCamp**, you must
play a key role in shaping the learning experience for data science and analytics
students. How can I assist you today?
  • 响应是非确定性的(本质上存在随机性)
在 Python 中使用 DeepSeek

一个客服聊天机器人收到两条语义相似的请求。

在 Python 中使用 DeepSeek

控制响应随机性

  • temperature:控制从确定性到随机性的程度,0(高度确定)到 2(非常随机)
  • 启用推理时将被静默忽略 → 需禁用方可生效
response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    extra_body={"thinking": {"type": "disabled"}},
    messages=[{"role": "user",
             "content": "Hi! I'm James and I'm a Curriculum Manager at DataCamp."}],

temperature=2
) print(response.choices[0].message.content)
Hi James! Welcome to DataCamp - it's great to connect with someone who works on
shaping learning experiences for data skills. What does your role typically involve
as a Curriculum Manager at **DataCamp?** Perhaps one-on95 kWielpulses.Z.blog.from...
在 Python 中使用 DeepSeek

文本生成:营销

prompt = "Generate a powerful tagline for a new electric vehicle 
that highlights innovation and sustainability."
response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
**"Drive the Future - Zero Emissions, Infinite Innovation."**  
该标语体现了前沿科技与环保意识的核心...
在 Python 中使用 DeepSeek

清理输出

prompt = "Generate a powerful tagline for a new electric vehicle 
that highlights innovation and sustainability. Return only the tagline
without formatting."

# Create a request to the chat model
# ...
"Drive the Future - Clean, Smart, Electric."
在 Python 中使用 DeepSeek

文本生成:产品描述

prompt = """Write a compelling e-commmerce product description for the UltraFit
Smartwatch in paragraph form. Highlight its key features:

10-day battery life, 24/7 heart rate and sleep tracking, built-in GPS, water
resistance up to 50 meters, and lightweight design.

Use a persuasive and engaging tone to appeal to fitness enthusiasts and busy
professionals.
"""

response = client.chat.completions.create( model="deepseek-ai/DeepSeek-V4-Pro", messages=[{"role": "user", "content": prompt}])
在 Python 中使用 DeepSeek

文本生成:产品描述

print(response.choices[0].message.content)
"UltraFit 智能手表是您的全能健康与健身伙伴。
24/7 心率与睡眠监测助您掌握健康状况,
内置 GPS 让跑步不偏航。
10 天续航,无需频繁充电。
轻量机身与 50 米防水,既适合训练也适合日常佩戴,是积极生活方式的理想之选。"
  • 迭代!在提示中加入细节并重新运行请求

  • 下个视频 → 向模型提供示例

在 Python 中使用 DeepSeek

Vamos praticar!

在 Python 中使用 DeepSeek

Preparing Video For Download...