An unofficial free deepseek api wrapper written in python.
A totally free deepseek api(with latest R1 model) written in python.
Create an account in deepseek.com and visit chat.deepseek.com and copy the following cookies
smidV2
HMACCOUNT
Hm_lpvt_fb5acee01d9182aabb2b61eb816d24ff
intercom-session-guh50jw4
HWWAFSESTIME
Hm_lvt_fb5acee01d9182aabb2b61eb816d24ff
HWWAFSESID
.thumbcache_6b2e5483f9d858d7c661c5e276b6a6ae
Hm_lvt_1fff341d7a963a4043e858ef0e19a17c
intercom-device-id-guh50jw4
__cf_bm
ds_session_id
Hm_lpvt_1fff341d7a963a4043e858ef0e19a17c
and from Local Storage collect userToken
value part
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"X-Client-Platform": "web",
"X-Client-Version": "1.0.0-always",
"X-Client-Locale": "en_US",
"X-App-Version": "20241129.1",
"Authorization": "Bearer {userToken}", #collected from local storage
"Origin": "https://chat.deepseek.com",
"Referer": "https://chat.deepseek.com/", #change to 'https://chat.deepseek.com/a/chat/s/{chatroom_id}' after chatroom creation
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Priority": "u=0",
"Te": "trailers",
}
pip install -r requirements.txt
You can usage the terimnal wrapper this way
from deepseek import DeepSeek
import json
from rich.console import Console
from rich.markdown import Markdown
console = Console()
with open("cookies.json", "r") as f:
cookies = json.load(f)
f.close()
usertoken = "....." #collect from locla storage
deepseek_engiene:DeepSeek = DeepSeek(cookie=cookies,user_token=usertoken)
chatroom_id = deepseek_engiene.create_chatroom()
#you can visit the chatroom https://chat.deepseek.com/a/chat/s/{chtroom_id}
def ai_response(self,prompt:str,file_list:list=None,r1_enabled:bool=False,web_search:bool=False)->str:
"""
Get response from the ai
:param prompt: The prompt to send to the ai
:param file_list: List of files to send to the ai
:param r1_enabled: R1 enabled
:param web_search: Web search enabled
:return: The response from the ai
"""
markdown = Markdown(deepseek_engiene.ai_response(prompt=prompt))
console.print(markdown)
markdown = Markdown(deepseek_engiene.ai_response(prompt=prompt,r1_enabled=True))
console.print(markdown)
markdown = Markdown(deepseek_engiene.ai_response(prompt=prompt,web_search=True))
console.print(markdown)
markdown = Markdown(deepseek_engiene.ai_response(file_list=["assets/leetcode.png","assets/leetcode2.png"],prompt="solve the problems in python and complete the function"))
In the following example deepseek solves two leetcode question from screenshots
markdown = Markdown(deepseek_engiene.ai_response(file_list=["assets/leetcode.png","assets/leetcode2.png"],prompt="solve the problems in python and complete the function"))
-
Ai responses
The api endpoint for this is/api/v0/chat/completion
Use the File id
Use thefile_id
in theref_file_ids
array in the post request
1. Scrape for file upload
2. Code implementation in python.
3. chat history implementation
4. Start messaging in an already existing chatroom
5. Better error handling
The cloudflare __cf_bm cookie expires every thirty minutes. So if the requests aren't working then refresh the cookies