Gửi yêu cầu tới các mô hình DeepSeek

Làm việc với DeepSeek trong Python

James Chapman

AI Curriculum Lead, DataCamp

Tóm tắt

Một máy tính gửi yêu cầu tới Together.ai API, bao gồm mô hình, dữ liệu và tham số mô hình.

Làm việc với DeepSeek trong Python

Tạo một yêu cầu

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")
  • Tạo yêu cầu bằng thư viện openai
  • OpenAI: Nhà phát triển mô hình và ứng dụng AI (vd: ChatGPT)
  • base_url: chuyển hướng yêu cầu từ OpenAI sang nhà cung cấp mô hình DeepSeek
  • api_key: Xác thực API (xem tài liệu của nhà cung cấp)
1 https://platform.deepseek.com/api_keys
Làm việc với DeepSeek trong Python

Tạo một yêu cầu

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)
Làm việc với DeepSeek trong Python

Phản hồi

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=[])
Làm việc với DeepSeek trong Python

Phản hồi

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=[])
Làm việc với DeepSeek trong Python

Diễn giải phản hồi

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)]
Làm việc với DeepSeek trong Python

Diễn giải phản hồi

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)
Làm việc với DeepSeek trong Python

Diễn giải phản hồi

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.
Làm việc với DeepSeek trong Python

Chi phí sử dụng API

 

  • Với DeepSeek:
    • Nhà cung cấp (vd: together.ai)
    • Mô hình
    • Đầu vào + đầu ra lớn hơn = Chi phí cao hơn

Một con đường thu phí.

1 https://api-docs.deepseek.com/quick_start/pricing
Làm việc với DeepSeek trong Python

Cùng luyện tập nào!

Làm việc với DeepSeek trong Python

Preparing Video For Download...