|
| 1 | +# DeepSeek LLM API Documentation |
| 2 | + |
| 3 | +DeepSeek provides an API that is compatible with the OpenAI format, making it relatively easy to integrate for developers already familiar with OpenAI's services. This documentation provides a comprehensive overview of the DeepSeek API, including its endpoints, authentication methods, request formats, supported models, error codes, and example implementations. |
| 4 | + |
| 5 | +## Authentication |
| 6 | + |
| 7 | +DeepSeek API uses API key authentication, similar to many other API services. |
| 8 | + |
| 9 | +### Obtaining an API Key |
| 10 | + |
| 11 | +To use the DeepSeek API, you need to: |
| 12 | + |
| 13 | +1. Create or log in to your DeepSeek account |
| 14 | +2. Navigate to the API keys page |
| 15 | +3. Select "Create new secret key" (you can optionally name your key) |
| 16 | +4. Copy the generated API key for use in your applications[4] |
| 17 | + |
| 18 | +### Authentication Method |
| 19 | + |
| 20 | +Authentication is performed by including your API key in the request header: |
| 21 | + |
| 22 | +``` |
| 23 | +Authorization: Bearer |
| 24 | +``` |
| 25 | + |
| 26 | +This header must be included with every API request[1]. |
| 27 | + |
| 28 | +## API Endpoints |
| 29 | + |
| 30 | +### Base URL |
| 31 | + |
| 32 | +The DeepSeek API can be accessed through the following base URL: |
| 33 | + |
| 34 | +``` |
| 35 | +https://api.deepseek.com |
| 36 | +``` |
| 37 | + |
| 38 | +For OpenAI compatibility, you can also use: |
| 39 | + |
| 40 | +``` |
| 41 | +https://api.deepseek.com/v1 |
| 42 | +``` |
| 43 | + |
| 44 | +Note that the "v1" in this URL has no relationship with the model's version[1]. |
| 45 | + |
| 46 | +### Chat Completions |
| 47 | + |
| 48 | +**Endpoint:** `/chat/completions` |
| 49 | +**Method:** POST |
| 50 | +**URL:** `https://api.deepseek.com/chat/completions` |
| 51 | + |
| 52 | +This endpoint creates a model response for the given chat conversation[2]. |
| 53 | + |
| 54 | +## Supported Models |
| 55 | + |
| 56 | +DeepSeek currently offers the following models through their API: |
| 57 | + |
| 58 | +1. **deepseek-chat**: The general-purpose chat model, which has been upgraded to DeepSeek-V3. To invoke this model, specify `model='deepseek-chat'`[1]. |
| 59 | + |
| 60 | +2. **deepseek-reasoner**: The latest reasoning model, DeepSeek-R1, which is specialized for complex reasoning tasks. To invoke this model, specify `model='deepseek-reasoner'`[1][6]. |
| 61 | + |
| 62 | +## Request Format |
| 63 | + |
| 64 | +### Chat Completion Request |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "model": "deepseek-chat", |
| 69 | + "messages": [ |
| 70 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 71 | + {"role": "user", "content": "Hello!"} |
| 72 | + ], |
| 73 | + "stream": false |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Required Fields |
| 78 | + |
| 79 | +- **model** (string): Specifies which model to use (e.g., "deepseek-chat", "deepseek-reasoner")[1] |
| 80 | +- **messages** (array): A list of messages comprising the conversation, with at least one message required[2] |
| 81 | + |
| 82 | +### Message Object |
| 83 | + |
| 84 | +Each message in the "messages" array must include: |
| 85 | + |
| 86 | +- **role** (string): The role of the message author. Possible values: |
| 87 | + - "system": For system instructions |
| 88 | + - "user": For user messages |
| 89 | + - "assistant": For assistant responses |
| 90 | + - "tool": For tool messages/responses |
| 91 | +- **content** (string): The content of the message[2] |
| 92 | + |
| 93 | +### Optional Fields |
| 94 | + |
| 95 | +- **name** (string): An optional name for the participant to differentiate between participants of the same role[2] |
| 96 | +- **stream** (boolean): When set to true, provides a stream of responses; when false, returns the complete response[1] |
| 97 | +- **prefix** (boolean, Beta): When set to true for assistant messages, forces the model to start its answer with the supplied prefix content. Requires setting `base_url="https://api.deepseek.com/beta"`[2] |
| 98 | +- **reasoning_content** (string, Beta): Used for the `deepseek-reasoner` model in the Chat Prefix Completion feature. When using this feature, the `prefix` parameter must be set to `true`[2] |
| 99 | +- **stop** (array): Specifies sequences where the API will stop generating further tokens[3] |
| 100 | + |
| 101 | +## Response Format |
| 102 | + |
| 103 | +The response format follows a structure similar to OpenAI's API responses. When using the OpenAI SDK to interact with DeepSeek API, the response can be processed in the same way as OpenAI responses. |
| 104 | + |
| 105 | +For non-streaming responses, the response includes the generated content accessible through the response object[1]. |
| 106 | + |
| 107 | +## Error Codes |
| 108 | + |
| 109 | +When calling the DeepSeek API, you may encounter the following error codes: |
| 110 | + |
| 111 | +| CODE | DESCRIPTION | CAUSE | SOLUTION | |
| 112 | +|------|-------------|-------|----------| |
| 113 | +| 400 - Invalid Format | Invalid request body format | The request body doesn't follow the required format | Modify your request body according to the error message hints[5] | |
| 114 | +| 401 - Authentication Fails | Authentication failure | Wrong API key provided | Check your API key or create a new one[5] | |
| 115 | +| 402 - Insufficient Balance | Account balance depleted | You have run out of balance | Check your account balance and add funds if necessary[5] | |
| 116 | +| 422 - Invalid Parameters | Request contains invalid parameters | The request parameters don't meet the requirements | Adjust your request parameters according to the error message[5] | |
| 117 | +| 429 - Rate Limit Reached | Too many requests in a short time | Sending requests too quickly | Pace your requests reasonably or temporarily switch to alternative LLM service providers[5] | |
| 118 | +| 500 - Server Error | Server-side issue | Internal server problem | Retry after a brief wait; contact support if the issue persists[5] | |
| 119 | +| 503 - Server Overloaded | High traffic causing server overload | Too many concurrent requests to the server | Retry after a brief wait[5] | |
| 120 | + |
| 121 | +## Beta Features |
| 122 | + |
| 123 | +### Chat Prefix Completion |
| 124 | + |
| 125 | +This feature allows users to provide an assistant's prefix message that the model will complete. To use Chat Prefix Completion: |
| 126 | + |
| 127 | +1. Ensure the last message in the `messages` array has a `role` of `assistant` |
| 128 | +2. Set the `prefix` parameter of the last message to `True` |
| 129 | +3. Set the base URL to `https://api.deepseek.com/beta` |
| 130 | + |
| 131 | +Example Python code: |
| 132 | + |
| 133 | +```python |
| 134 | +from openai import OpenAI |
| 135 | + |
| 136 | +client = OpenAI( |
| 137 | + api_key="", |
| 138 | + base_url="https://api.deepseek.com/beta", |
| 139 | +) |
| 140 | + |
| 141 | +messages = [ |
| 142 | + {"role": "user", "content": "Please write quick sort code"}, |
| 143 | + {"role": "assistant", "content": "``` |
| 144 | +] |
| 145 | + |
| 146 | +response = client.chat.completions.create( |
| 147 | + model="deepseek-chat", |
| 148 | + messages=messages, |
| 149 | + stop=["```"], |
| 150 | +) |
| 151 | + |
| 152 | +print(response.choices[0].message.content) |
| 153 | +``` |
| 154 | + |
| 155 | +This example forces the model to output Python code by setting the prefix to the start of a Python code block and using the `stop` parameter to prevent additional explanations[3]. |
| 156 | + |
| 157 | +## Example API Requests |
| 158 | + |
| 159 | +### Curl Example |
| 160 | + |
| 161 | +```bash |
| 162 | +curl https://api.deepseek.com/chat/completions \ |
| 163 | + -H "Content-Type: application/json" \ |
| 164 | + -H "Authorization: Bearer " \ |
| 165 | + -d '{ |
| 166 | + "model": "deepseek-chat", |
| 167 | + "messages": [ |
| 168 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 169 | + {"role": "user", "content": "Hello!"} |
| 170 | + ], |
| 171 | + "stream": false |
| 172 | + }' |
| 173 | +``` |
| 174 | + |
| 175 | +### Python Example (using OpenAI SDK) |
| 176 | + |
| 177 | +```python |
| 178 | +# Install OpenAI SDK first: pip install openai |
| 179 | +from openai import OpenAI |
| 180 | + |
| 181 | +client = OpenAI(api_key="", base_url="https://api.deepseek.com") |
| 182 | + |
| 183 | +response = client.chat.completions.create( |
| 184 | + model="deepseek-chat", |
| 185 | + messages=[ |
| 186 | + {"role": "system", "content": "You are a helpful assistant"}, |
| 187 | + {"role": "user", "content": "Hello"}, |
| 188 | + ], |
| 189 | + stream=False |
| 190 | +) |
| 191 | + |
| 192 | +print(response.choices[0].message.content) |
| 193 | +``` |
| 194 | + |
| 195 | +### Node.js Example (using OpenAI SDK) |
| 196 | + |
| 197 | +```javascript |
| 198 | +// Install OpenAI SDK first: npm install openai |
| 199 | +import OpenAI from "openai"; |
| 200 | + |
| 201 | +const openai = new OpenAI({ |
| 202 | + baseURL: 'https://api.deepseek.com', |
| 203 | + apiKey: '' |
| 204 | +}); |
| 205 | + |
| 206 | +async function main() { |
| 207 | + const completion = await openai.chat.completions.create({ |
| 208 | + model: "deepseek-chat", |
| 209 | + messages: [ |
| 210 | + {"role": "system", "content": "You are a helpful assistant"}, |
| 211 | + {"role": "user", "content": "Hello"}, |
| 212 | + ], |
| 213 | + stream: false |
| 214 | + }); |
| 215 | + |
| 216 | + console.log(completion.choices[0].message.content); |
| 217 | +} |
| 218 | + |
| 219 | +main(); |
| 220 | +``` |
| 221 | + |
| 222 | +## Differences from OpenAI API |
| 223 | + |
| 224 | +The DeepSeek API is designed to be compatible with the OpenAI API format, allowing developers to use the OpenAI SDK or software compatible with the OpenAI API to access DeepSeek services with minimal changes[1]. Key differences include: |
| 225 | + |
| 226 | +1. **Base URL**: You need to set the base URL to `https://api.deepseek.com` or `https://api.deepseek.com/v1`[1] |
| 227 | +2. **Available Models**: DeepSeek provides its own models (`deepseek-chat` and `deepseek-reasoner`) rather than OpenAI's models[1] |
| 228 | +3. **Feature Support**: Currently, DeepSeek does not support Embedding or Function Calling features, although these are in their development plans[8] |
| 229 | +4. **Pricing**: DeepSeek has a different pricing structure, typically offering more competitive rates. For example, DeepSeek R1 offers free access for up to 50 daily messages, and its API pricing is generally lower than OpenAI's[7] |
| 230 | + |
| 231 | +## Limitations and Upcoming Features |
| 232 | + |
| 233 | +According to the available information: |
| 234 | + |
| 235 | +1. **No Embedding Support**: DeepSeek does not currently support embeddings of content, which limits certain RAG (Retrieval-Augmented Generation) implementation possibilities[8] |
| 236 | +2. **No Function Calling**: Unlike OpenAI, the DeepSeek API does not currently support function calling capabilities[8] |
| 237 | +3. **Beta Features**: Some features like Chat Prefix Completion are currently in beta and require using a specific beta endpoint[3] |
| 238 | + |
| 239 | +## Conclusion |
| 240 | + |
| 241 | +The DeepSeek API offers a familiar interface for developers who have worked with the OpenAI API, making it relatively straightforward to integrate or switch between services. With competitive pricing and specialized models like DeepSeek-R1 for reasoning tasks, it provides a viable alternative for many AI applications. |
| 242 | + |
| 243 | +For building a robust Python wrapper, you should account for the authentication process, endpoint structure, model options, and error handling as described in this documentation. Additionally, you should be aware of the current limitations around embeddings and function calling if these features are required for your implementation. |
| 244 | + |
| 245 | +As the DeepSeek API continues to evolve, staying updated with their official documentation would be beneficial for incorporating new features and capabilities as they become available. |
| 246 | + |
| 247 | +Citations: |
| 248 | +[1] https://api-docs.deepseek.com |
| 249 | +[2] https://api-docs.deepseek.com/api/create-chat-completion |
| 250 | +[3] https://api-docs.deepseek.com/guides/chat_prefix_completion |
| 251 | +[4] https://docs.n8n.io/integrations/builtin/credentials/deepseek/ |
| 252 | +[5] https://api-docs.deepseek.com/quick_start/error_codes |
| 253 | +[6] https://aws.amazon.com/blogs/big-data/use-deepseek-with-amazon-opensearch-service-vector-database-and-amazon-sagemaker/ |
| 254 | +[7] https://www.euclea-b-school.com/deepseek-ai-vs-open-ai-a-comprehensive-comparison/ |
| 255 | +[8] https://huggingface.co/deepseek-ai/deepseek-coder-6.7b-instruct/discussions/10 |
| 256 | +[9] https://platform.deepseek.com/docs/api-reference/chat/create |
| 257 | +[10] https://api-docs.deepseek.com/api/create-completion |
| 258 | +[11] https://api-docs.deepseek.com/quick_start/pricing |
| 259 | +[12] https://www.datacamp.com/tutorial/deepseek-api |
| 260 | +[13] https://www.reddit.com/r/SillyTavernAI/comments/1jbdccq/the_redacted_guide_to_deepseek_r1/ |
| 261 | +[14] https://api-docs.deepseek.com/api/deepseek-api |
| 262 | +[15] https://www.postman.com/ai-on-postman/deepseek/documentation/gr0i44z/deepseek-api |
| 263 | +[16] https://apidog.com/blog/how-to-use-deepseek-api-r1-v3/ |
| 264 | +[17] https://api-docs.deepseek.com/guides/fim_completion |
| 265 | +[18] https://docs.gitguardian.com/secrets-detection/secrets-detection-engine/detectors/specifics/deepseek_api_key |
| 266 | +[19] https://deepinfra.com/deepseek-ai/DeepSeek-R1/api |
| 267 | +[20] https://python.useinstructor.com/integrations/deepseek/ |
| 268 | +[21] https://www.byteplus.com/en/topic/375654 |
| 269 | +[22] https://blog.skypilot.co/deepseek-rag/ |
| 270 | +[23] https://meetrix.io/articles/deepseekcoder-developer-guide/ |
| 271 | +[24] https://api-docs.deepseek.com/faq |
| 272 | +[25] https://aws.amazon.com/blogs/big-data/use-deepseek-with-amazon-opensearch-service-vector-database-and-amazon-sagemaker/ |
| 273 | +[26] https://python.langchain.com/api_reference/deepseek/chat_models/langchain_deepseek.chat_models.ChatDeepSeek.html |
| 274 | +[27] https://www.datacamp.com/tutorial/deepseek-api |
| 275 | + |
| 276 | +--- |
| 277 | +Answer from Perplexity: pplx.ai/share |
0 commit comments