Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I enable streaming output in the DeepSeek API


How do I enable streaming output in the DeepSeek API


To enable streaming output in the DeepSeek API, you need to modify the API call by setting the `stream` parameter to `true`. Here’s how you can do this in various programming environments:

1. Using cURL:
You can make a request to the DeepSeek API with the following command, ensuring that the `stream` parameter is set to `true`:

bash
   curl https://api.deepseek.com/chat/completions \
   -H "Content-Type: application/json" \
   -H "Authorization: Bearer " \
   -d '{
     "model": "deepseek-chat",
     "messages": [
       {"role": "system", "content": "You are a helpful assistant."},
       {"role": "user", "content": "Hello!"}
     ],
     "stream": true
   }'
   

2. Using Python:
If you are using Python, you can enable streaming by setting the `stream` parameter to `true` in your API call:

python
   from openai import OpenAI

   client = OpenAI(api_key="", base_url="https://api.deepseek.com")
   response = client.chat.completions.create(
       model="deepseek-chat",
       messages=[
           {"role": "system", "content": "You are a helpful assistant."},
           {"role": "user", "content": "Hello!"}
       ],
       stream=True
   )

   for message in response:
       print(message.choices[0].message.content)
   

3. Using Node.js:
In a Node.js environment, you can also set the `stream` parameter to `true`:

javascript
   import OpenAI from "openai";

   const openai = new OpenAI({
       baseURL: 'https://api.deepseek.com',
       apiKey: ''
   });

   async function main() {
       const completion = await openai.chat.completions.create({
           messages: [{ role: "system", content: "You are a helpful assistant." }],
           model: "deepseek-chat",
           stream: true
       });

       completion.on('data', (data) => {
           console.log(data.choices[0].message.content);
       });
   }

   main();
   

By setting the `stream` parameter to `true`, you will receive responses as they are generated, allowing you to process or display them incrementally rather than waiting for the entire response to complete[1][6][9].

Citations:
[1] https://api-docs.deepseek.com
[2] https://docs.llamaindex.ai/en/stable/module_guides/deploying/query_engine/streaming/
[3] https://www.youtube.com/watch?v=_IKz7sDQeVg
[4] https://github.com/Doriandarko/deepseek-engineer/blob/main/main.py
[5] https://python.useinstructor.com/integrations/deepseek/
[6] https://dev.to/auden/how-to-use-deepseek-api-and-enable-streaming-output-for-debugging-1ah9
[7] https://python.langchain.com/api_reference/deepseek/chat_models/langchain_deepseek.chat_models.ChatDeepSeek.html
[8] https://artificialanalysis.ai/models/deepseek-v2-5-sep-2024/providers
[9] https://deepinfra.com/deepseek-ai/DeepSeek-V3/api