Summarizing and editing text

Working with DeepSeek in Python

James Chapman

Curriculum Manager, DataCamp

Recap...

  • Q&A
response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V3",
  messages=[{"role": "user", "content": "How many days are in October?"}]
)

print(response.choices[0].message.content)
October has **31 days**.  

It's one of the seven months in the Gregorian calendar with 31 days...
Working with DeepSeek in Python

Text editing

  • Example: updating the name, pronouns, and job title

$$

prompt = """
Update name to Maarten, pronouns to he/him, and job title to Senior Content Developer
in the following text:

Joanne is a Content Developer at DataCamp. Her favorite programming language is R,
which she uses for her statistical analyses.
"""
Working with DeepSeek in Python

Text editing

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",

messages=[{"role": "user", "content": prompt}]
) print(response.choices[0].message.content)
Here's the updated text with the requested changes:

Maarten is a Senior Content Developer at DataCamp. His favorite programming language
is R, which he uses for his statistical analyses.

Let me know if you'd like any further adjustments!
Working with DeepSeek in Python

Text summarization

  • Example: summary of customer chat transcripts

Customer support team

text = """
Customer: Hi, I'm trying to log into 
my account, but it keeps saying 
my password is incorrect. I'm sure 
I'm entering the right one.  

Support: I'm sorry to hear that! 
Have you tried resetting your password?  
...
"""
Working with DeepSeek in Python

Text summarization

prompt = f"""Summarize the customer support chat 
             in three concise key points: {text}"""


response = client.chat.completions.create( model="deepseek-ai/DeepSeek-V3", messages=[{"role": "user", "content": prompt}] ) print(response.choices[0].message.content)
1. **Login Issue**: Customer couldn't log in due to a password issue and missing reset link.  
2. **Password Reset Suggestion**: Support resent the reset email after confirming it was sent.  
3. **Prompt Assistance**: Customer resolved the issue by using Google sign-in.
Working with DeepSeek in Python

Controlling response length

  • max_tokens = 5
response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V3",
  messages=[{"role":"user",
      "content":"Write a haiku about AI."}],
  max_tokens=5
)

**Silent circuits hum
  • max_tokens = 30
response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=[{"role":"user",
      "content":"Write a haiku about AI."}],
    max_tokens=30
)

**Silent circuits hum,**  
**thoughts of light and logic bloom—**  
**minds beyond our own.**
Working with DeepSeek in Python

Understanding tokens

$$

  • Tokens: units of text that help the AI understand and interpret text

$$

The sentence, "How can the OpenAI API deliver business value?" with each token highlighted in a different color.

1 https://lunary.ai/deepseek-tokenizer
Working with DeepSeek in Python

Calculating the cost

 

  • API usage costs depend on the platform, model, and the number of tokens 💰

    • Models are priced by cost/tokens
    • Input and output tokens may have different costs
  • Increasing max_tokens increases cost 📈

Screenshot 2025-03-05 at 11.46.54.png

Working with DeepSeek in Python

Calculating the cost

prompt = f"""Summarize the customer support chat 
             in three concise key points: {text}"""

max_tokens = 500

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=[{"role": "user", "content": prompt}], 
    max_tokens=max_tokens
)
Working with DeepSeek in Python

Calculating the cost

# Define price per token
input_token_price = 1.25 / 1_000_000
output_token_price = 1.25 / 1_000_000

# Extract token usage input_tokens = response.usage.prompt_tokens
output_tokens = max_tokens
# Calculate cost cost = (input_tokens * input_token_price + output_tokens * output_token_price) print(f"Estimated cost: ${cost}")
Estimated cost: $0.00848
Working with DeepSeek in Python

Let's practice!

Working with DeepSeek in Python

Preparing Video For Download...