การส่งคำขอไปยังโมเดล DeepSeek

การใช้งาน DeepSeek ใน Python

James Chapman

AI Curriculum Lead, DataCamp

ทบทวน

คอมพิวเตอร์ส่งคำขอไปยัง Together.ai API ซึ่งประกอบด้วยโมเดลและพารามิเตอร์ต่าง ๆ

การใช้งาน DeepSeek ใน Python

การสร้างคำขอ

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: เปลี่ยนทิศทาง คำขอจาก OpenAI ไปยังผู้ให้บริการโมเดล DeepSeek
  • api_key: การยืนยันตัวตนของ API (ดูเอกสารของผู้ให้บริการ)
1 https://platform.deepseek.com/api_keys
การใช้งาน DeepSeek ใน Python

การสร้างคำขอ

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-Pro",
messages=[{"role": "user", "content": "In one sentence, what is hallucination in AI?"}]
)
print(response)
การใช้งาน DeepSeek ใน Python

การตอบกลับ

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-Pro',
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=[])
การใช้งาน DeepSeek ใน Python

การตอบกลับ

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-Pro',
               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=[])
การใช้งาน DeepSeek ใน Python

การตีความการตอบกลับ

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)]
การใช้งาน DeepSeek ใน Python

การตีความการตอบกลับ

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)
การใช้งาน DeepSeek ใน Python

การตีความการตอบกลับ

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.
การใช้งาน DeepSeek ใน Python

ค่าใช้จ่ายในการใช้งาน API

 

  • สำหรับ DeepSeek:
    • ผู้ให้บริการ (เช่น together.ai)
    • โมเดล
    • Input + Output ยิ่งมาก = ค่าใช้จ่ายยิ่งสูง

ทางด่วนที่เก็บค่าผ่านทาง

1 https://api-docs.deepseek.com/quick_start/pricing
การใช้งาน DeepSeek ใน Python

มาฝึกกันเถอะ!

การใช้งาน DeepSeek ใน Python

Preparing Video For Download...