DeepSeek safety and content filtering
PREREQUISITES
Python 3.8+DeepSeek API keypip install openai>=1.0
Setup
Install the openai Python package to interact with DeepSeek's OpenAI-compatible API. Set your DeepSeek API key as an environment variable for secure authentication.
pip install openai>=1.0 Step by step
Use the deepseek-chat model with safety filters enabled by default. You can also implement custom filtering by checking the response content for flagged terms or using moderation endpoints if available.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
messages = [{"role": "user", "content": "Tell me a joke."}]
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages
)
text = response.choices[0].message.content
print("AI response:", text)
# Basic content filtering example
blocked_keywords = ["hate", "violence", "drugs"]
if any(word in text.lower() for word in blocked_keywords):
print("Warning: Response contains potentially unsafe content.")
else:
print("Response passed safety check.") AI response: Why did the scarecrow win an award? Because he was outstanding in his field! Response passed safety check.
Common variations
You can use asynchronous calls with the OpenAI SDK or switch to other DeepSeek models like deepseek-reasoner for reasoning tasks. For stricter safety, integrate third-party moderation APIs or implement advanced keyword filtering.
import asyncio
import os
from openai import OpenAI
async def async_chat():
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
response = await client.chat.completions.acreate(
model="deepseek-chat",
messages=[{"role": "user", "content": "Explain quantum computing simply."}]
)
print("Async AI response:", response.choices[0].message.content)
asyncio.run(async_chat()) Async AI response: Quantum computing uses quantum bits, or qubits, which can be both 0 and 1 at the same time, allowing computers to solve certain problems much faster.
Troubleshooting
If you receive unexpected or unsafe content, verify your filtering logic and consider using additional moderation layers. Check your API key and endpoint URL if requests fail. For rate limits or errors, consult DeepSeek's official documentation or support.
Key Takeaways
- DeepSeek's deepseek-chat model includes built-in safety filters to reduce harmful content.
- Implement additional keyword or moderation checks to enhance content safety in your application.
- Use asynchronous API calls for scalable and responsive AI integrations.
- Always secure your API key via environment variables and verify endpoint URLs.
- Consult DeepSeek documentation for updates on safety features and best practices.