向 DeepSeek 模型發出請求

使用 Python 操作 DeepSeek

James Chapman

AI Curriculum Lead, DataCamp

重點回顧

電腦向 Together.ai API 發送請求,內含模型、資料與模型參數。

使用 Python 操作 DeepSeek

建立請求

from openai import OpenAI

# To send to DeepSeek API: base_url="https://api.deepseek.com" client = OpenAI(api_key="<TogetherAI API Key>", base_url="https://api.together.xyz/v1")
  • 使用 openai 函式庫建立請求
  • OpenAI:AI 模型與應用開發者(例如 ChatGPT
  • base_url:將請求「改送」至 DeepSeek 的模型供應商
  • api_key:API 驗證(請見供應商文件)
1 https://platform.deepseek.com/api_keys
使用 Python 操作 DeepSeek

建立請求

from openai import OpenAI
# To send to DeepSeek API: base_url="https://api.deepseek.com"
client = OpenAI(api_key="<TogetherAI API Key>", base_url="https://api.together.xyz/v1")


response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4.1-Flash",
messages=[{"role": "user", "content": "In one sentence, what is hallucination in AI?"}]
)
print(response)
使用 Python 操作 DeepSeek

回應內容

ChatCompletion(id='onVWeEx-gBAtS-a09121fcae54578c', choices=[Choice(finish_reason='stop', index=0,
logprobs=None, message=ChatCompletionMessage(content='Hallucination in AI is when a model confidently
generates information that sounds plausible but is factually incorrect, misleading, or entirely
fabricated.', refusal=None, role='assistant', annotations=None, audio=None, function_call=None,
tool_calls=[]), seed=None)], created=1781018557, model='deepseek-ai/DeepSeek-V4.1-Flash',
object='chat.completion', moderation=None, service_tier=None, system_fingerprint='default',
usage=CompletionUsage(completion_tokens=28, prompt_tokens=15, total_tokens=43,
completion_tokens_details=None, prompt_tokens_details=PromptTokensDetails(audio_tokens=None,
cached_tokens=0)), prompt=[])
使用 Python 操作 DeepSeek

回應內容

ChatCompletion(id='onVWeEx-gBAtS-a09121fcae54578c',
               choices=[Choice(finish_reason='stop', index=0, logprobs=None,
                               message=ChatCompletionMessage(content='Hallucination in AI refers...',
                               refusal=None, role='assistant', annotations=None, audio=None,
                               function_call=None, tool_calls=[]), seed=None)],
               created=1781018557,
               model='deepseek-ai/DeepSeek-V4.1-Flash',
               object='chat.completion',
               moderation=None,
               service_tier=None,
               system_fingerprint='default',
               usage=CompletionUsage(completion_tokens=28, prompt_tokens=15, total_tokens=43,
                                     completion_tokens_details=None,
                                     prompt_tokens_details=PromptTokensDetails(audio_tokens=None,
                                          cached_tokens=0)),
               prompt=[])
使用 Python 操作 DeepSeek

解讀回應

print(response.choices)
[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(
content='Hallucination in AI refers to the generation of false or nonsensical information
that is presented as factual, often due to limitations in training data, model biases, or
inference errors.', refusal=None, role='assistant', annotations=None, audio=None,
function_call=None, tool_calls=[]), seed=None)]
使用 Python 操作 DeepSeek

解讀回應

print(response.choices[0])
Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(
content='Hallucination in AI refers to the generation of false or nonsensical information
that is presented as factual, often due to limitations in training data, model biases, or
inference errors.', refusal=None, role='assistant', annotations=None, audio=None,
function_call=None, tool_calls=[]), seed=None)
使用 Python 操作 DeepSeek

解讀回應

print(response.choices[0].message)
ChatCompletionMessage(content='Hallucination in AI refers to the generation of false or
nonsensical information that is presented as factual, often due to limitations in training
data, model biases, or inference errors.', refusal=None, role='assistant', annotations=None,
audio=None, function_call=None, tool_calls=[])
print(response.choices[0].message.content)
Hallucination in AI refers to the generation of false or nonsensical information that is
presented as factual, often due to limitations in training data, model biases, or inference
errors.
使用 Python 操作 DeepSeek

API 使用成本

 

  • 針對 DeepSeek:
    • 供應商(例如 together.ai)
    • 模型
    • 輸入與輸出越大=費用越高

收費高速公路。

1 https://api-docs.deepseek.com/quick_start/pricing
使用 Python 操作 DeepSeek

一起來練習吧!

使用 Python 操作 DeepSeek

Preparing Video For Download...