DEV Community

drake
drake

Posted on

调用Deepseek API

# Please install OpenAI SDK first: `pip3 install openai`
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get('DEEPSEEK_API_KEY'),
    base_url="https://api.deepseek.com"
    )

text = """
China now adds more solar capacity every month than the U.S. has installed since Edison. By 2035, Shenzhen alone is projected to be producing 97% of global EV batteries, 85% of industrial robots, and roughly 900 times as many drones as the rest of the world combined. Shipyards in Dalian and Shanghai are already delivering the equivalent of one Royal Navy per quarter.

But it’s not all doom and gloom — on the other side of the ledger, the United States maintains a commanding lead in Twitch streamers and podcast hours per capita. The balance of global power, as such, is set to remain uncertain as ever.
"""
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a translation assistant."},
        {"role": "user", "content": f"将该文本翻译成中文: {text}"}
    ],
    stream=False
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)