A Python package for interacting with the DeepSeek AI chat API. This package provides a clean interface to interact with DeepSeek's chat model, with support for streaming responses, thinking process visibility, and web search capabilities.
- and reverse wasm like it was required here
- whop.com/reverser-academy (beta)
β οΈ Service Notice: DeepSeek API is currently experiencing high load. Work is in progress to integrate additional API providers. Please expect intermittent errors.
- π Streaming Responses: Real-time interaction with token-by-token output
- π€ Thinking Process: Optional visibility into the model's reasoning steps
- π Web Search: Optional integration for up-to-date information
- π¬ Session Management: Persistent chat sessions with conversation history
- β‘ Efficient PoW: WebAssembly-based proof of work implementation
- π‘οΈ Error Handling: Comprehensive error handling with specific exceptions
- β±οΈ No Timeouts: Designed for long-running conversations without timeouts
- 𧡠Thread Support: Parent message tracking for threaded conversations
- Clone the repository:
git clone https://github.com/yourusername/deepseek4free.git
cd deepseek4free
- Install dependencies:
pip install -r requirements.txt
To use this package, you need a DeepSeek auth token. Here's how to obtain it:
![image](https://private-user-images.githubusercontent.com/98614666/407590604-b4e11650-3d1b-4638-956a-c67889a9f37e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4OTQ1MzksIm5iZiI6MTczODg5NDIzOSwicGF0aCI6Ii85ODYxNDY2Ni80MDc1OTA2MDQtYjRlMTE2NTAtM2QxYi00NjM4LTk1NmEtYzY3ODg5YTlmMzdlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA3VDAyMTAzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQxOTEwMTdhYTNjMTBlNzY4YmFmOTNmYjQ0ZTkzZjU0MDZhMjBkOTFjNmFmMGVlMzA5MDYxYzdkYTczYmVlYWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Jf-DqEkK6QIKulBd1fmwfbcx6VrWf1u1l-hG2Q4Fy8Y)
- Visit chat.deepseek.com
- Log in to your account
- Open browser developer tools (F12 or right-click > Inspect)
- Go to Application tab (if not visible, click >> to see more tabs)
- In the left sidebar, expand "Local Storage"
- Click on "https://chat.deepseek.com"
- Find the key named
userToken
- Copy
"value"
- this is your authentication token
Alternatively, you can get the token from network requests:
- Visit chat.deepseek.com
- Log in to your account
- Open browser developer tools (F12)
- Go to Network tab
- Make any request in the chat
- Find the request headers
- Copy the
authorization
token (without 'Bearer ' prefix)
from dsk.api import DeepSeekAPI
# Initialize with your auth token
api = DeepSeekAPI("YOUR_AUTH_TOKEN")
# Create a new chat session
chat_id = api.create_chat_session()
# Simple chat completion
prompt = "What is Python?"
for chunk in api.chat_completion(chat_id, prompt):
if chunk['type'] == 'text':
print(chunk['content'], end='', flush=True)
The thinking process shows the model's reasoning steps:
# With thinking process enabled
for chunk in api.chat_completion(
chat_id,
"Explain quantum computing",
thinking_enabled=True
):
if chunk['type'] == 'thinking':
print(f"π€ Thinking: {chunk['content']}")
elif chunk['type'] == 'text':
print(chunk['content'], end='', flush=True)
Enable web search for up-to-date information:
# With web search enabled
for chunk in api.chat_completion(
chat_id,
"What are the latest developments in AI?",
thinking_enabled=True,
search_enabled=True
):
if chunk['type'] == 'thinking':
print(f"π Searching: {chunk['content']}")
elif chunk['type'] == 'text':
print(chunk['content'], end='', flush=True)
Create threaded conversations by tracking parent messages:
# Start a conversation
chat_id = api.create_chat_session()
# Send initial message
parent_id = None
for chunk in api.chat_completion(chat_id, "Tell me about neural networks"):
if chunk['type'] == 'text':
print(chunk['content'], end='', flush=True)
elif 'message_id' in chunk:
parent_id = chunk['message_id']
# Send follow-up question in the thread
for chunk in api.chat_completion(
chat_id,
"How do they compare to other ML models?",
parent_message_id=parent_id
):
if chunk['type'] == 'text':
print(chunk['content'], end='', flush=True)
The package provides specific exceptions for different error scenarios:
from dsk.api import (
DeepSeekAPI,
AuthenticationError,
RateLimitError,
NetworkError,
APIError
)
try:
api = DeepSeekAPI("YOUR_AUTH_TOKEN")
chat_id = api.create_chat_session()
for chunk in api.chat_completion(chat_id, "Your prompt here"):
if chunk['type'] == 'text':
print(chunk['content'], end='', flush=True)
except AuthenticationError:
print("Authentication failed. Please check your token.")
except RateLimitError:
print("Rate limit exceeded. Please wait before making more requests.")
except NetworkError:
print("Network error occurred. Check your internet connection.")
except APIError as e:
print(f"API error occurred: {str(e)}")
For cleaner output handling, you can use helper functions like in example.py
:
def print_response(chunks):
"""Helper function to print response chunks in a clean format"""
thinking_lines = []
text_content = []
for chunk in chunks:
if chunk['type'] == 'thinking':
if chunk['content'] not in thinking_lines:
thinking_lines.append(chunk['content'])
print(f"π€ {chunk['content']}")
elif chunk['type'] == 'text':
text_content.append(chunk['content'])
print(chunk['content'], end='', flush=True)
The API returns chunks in the following format:
{
'type': str, # 'thinking' or 'text'
'content': str, # The actual content
'finish_reason': str, # 'stop' when response is complete
'message_id': str # (optional) For threaded conversations
}
Contributions are welcome! Please feel free to submit a Pull Request. Here are some ways you can contribute:
- π Report bugs
- β¨ Request features
- π Improve documentation
- π§ Submit bug fixes
- π¨ Add examples
This project is licensed under the MIT License - see the LICENSE file for details.
This package is unofficial and not affiliated with DeepSeek. Use it responsibly and in accordance with DeepSeek's terms of service.
- DeepSeek Chat - Official DeepSeek chat interface
- Example Projects - More usage examples