Large language models (LLMs) are revolutionizing numerous industries, offering advanced capabilities in natural language processing, code generation, and more. Alibaba Cloud's Bailian platform provides access to a variety of LLMs, including the powerful DeepSeek series. This article delves into the specifics of using the DeepSeek R1 and V3 models via the Bailian API, including setup, example code, and troubleshooting tips.
The Bailian platform hosts the DeepSeek series of models, developed by DeepSeek. These models are not integrated through third-party services; instead, they are deployed directly on Alibaba Cloud servers, ensuring optimal performance and security. Key models include:
Alibaba Cloud offers a free tier for these models, making them accessible for initial experimentation and development.
Model Name | Context Length (Tokens) | Max Input (Tokens) | Max Output (Tokens) | Input Cost (Per 1k Tokens) | Output Cost (Per 1k Tokens) | Free Tier |
---|---|---|---|---|---|---|
deepseek-r1 (671B) | 65,792 | 57,344 | 32,768 | 0.002 CNY (Original 0.004) | 0.008 CNY (Original 0.016) | 1 Million Tokens (Valid for 180 days after Bailian activation) |
deepseek-v3 (671B) | N/A | N/A | 8,192 | 0.001 CNY (Original 0.002) | 0.004 CNY (Original 0.008) | N/A |
deepseek-r1-distill-qwen-1.5b | 32,768 | 32,768 | 16,384 | Limited-Time Free Trial | Limited-Time Free Trial | N/A |
deepseek-r1-distill-qwen-7b | N/A | N/A | N/A | 0.0005 CNY | 0.001 CNY | 1 Million Tokens (Valid for 180 days after Bailian activation) |
Note: Ensure you check the official pricing page for the most up-to-date information, as costs and free tiers are subject to change.
To begin using the DeepSeek API on Bailian, you need to meet certain prerequisites:
Here's a look at how to interact with the DeepSeek models using different programming languages and SDKs:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
completion = client.chat.completions.create(
model="deepseek-r1",
messages=[
{'role': 'user', 'content': '9.9和9.11谁大'}
]
)
print("思考过程:")
print(completion.choices[0].message.reasoning_content)
print("最终答案:")
print(completion.choices[0].message.content)
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
});
const completion = await openai.chat.completions.create({
model: "deepseek-r1",
messages: [
{ role: "user", content: "9.9和9.11谁大" }
],
});
console.log("思考过程:");
console.log(completion.choices[0].message.reasoning_content);
console.log("最终答案:");
console.log(completion.choices[0].message.content);
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "9.9和9.11谁大"
}
]
}'
These examples demonstrate sending a simple comparison query to the deepseek-r1
model and printing both the "reasoning_content" (the model's thought process) and the final answer.
The DeepSeek API on Bailian doesn't inherently remember past interactions. To enable multi-turn conversations (where the model retains context from previous exchanges), you must manage the conversation history manually. Here's an example using Python:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
messages = [
{'role': 'user', 'content': '你好'}
]
completion = client.chat.completions.create(
model="deepseek-r1",
messages=messages
)
print("="*20+"第一轮对话"+"="*20)
print("="*20+"思考过程"+"="*20)
print(completion.choices[0].message.reasoning_content)
print("="*20+"最终答案"+"="*20)
print(completion.choices[0].message.content)
messages.append({'role': 'assistant', 'content': completion.choices[0].message.content})
messages.append({'role': 'user', 'content': '你是谁'})
print("="*20+"第二轮对话"+"="*20)
completion = client.chat.completions.create(
model="deepseek-r1",
messages=messages
)
print("="*20+"思考过程"+"="*20)
print(completion.choices[0].message.reasoning_content)
print("="*20+"最终答案"+"="*20)
print(completion.choices[0].message.content)
In multi-turn conversations, add the assistant's content
field to the context.
DeepSeek-R1 models may produce lengthy reasoning processes, potentially causing delays or timeouts. To mitigate this, use streaming output. This allows you to receive and display the model's response in chunks as it's generated. Here’s a sample using the OpenAI-compatible Python library:
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
def main():
reasoning_content = ""
answer_content = ""
is_answering = False
stream = client.chat.completions.create(
model="deepseek-r1",
messages=[{"role": "user", "content": "9.9和9.11谁大"}],
stream=True
)
print("\n" + "=" * 20 + "思考过程" + "=" * 20 + "\n")
for chunk in stream:
if not getattr(chunk, 'choices', None):
print("\n" + "=" * 20 + "Token 使用情况" + "=" * 20 + "\n")
print(chunk.usage)
continue
delta = chunk.choices[0].delta
if not hasattr(delta, 'reasoning_content'):
continue
if not getattr(delta, 'reasoning_content', None) and not getattr(delta, 'content', None):
continue
if not getattr(delta, 'reasoning_content', None) and not is_answering:
print("\n" + "=" * 20 + "完整回复" + "=" * 20 + "\n")
is_answering = True
if getattr(delta, 'reasoning_content', None):
print(delta.reasoning_content, end='', flush=True)
reasoning_content += delta.reasoning_content
elif getattr(delta, 'content', None):
print(delta.content, end='', flush=True)
answer_content += delta.content
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"发生错误:{e}")
Keep the following in mind when working with DeepSeek models on Bailian:
temperature
, top_p
, presence_penalty
, and frequency_penalty
parameters aren't supported for DeepSeek-R1.https://dashscope.aliyuncs.com/compatible-mode/v1
) and your API key.Integrating DeepSeek R1 and V3 models via Alibaba Cloud's Bailian platform offers powerful AI capabilities for various applications. By following the steps outlined in this guide, developers can effectively set up, utilize, and troubleshoot their DeepSeek integrations. Ensure you stay updated with the latest features, pricing, and limitations to maximize the potential of these advanced language models.