Làm việc với DeepSeek bằng Python
James Chapman
Curriculum Manager, DataCamp

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")
openaibase_url: chuyển hướng yêu cầu từ OpenAI đến nhà cung cấp mô hình DeepSeekapi_key: Xác thực API (xem tài liệu nhà cung cấp)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(# On DeepSeek's API: model="deepseek-chat" model="deepseek-ai/DeepSeek-V3",messages=[{"role": "user", "content": "In one sentence, what is hallucination in AI?"}])print(response)
ChatCompletion(id='ns1zcjp-zqrih-937de597af0fd643', choices=[Choice(finish_reason='stop', index=0,
logprobs=None, message=ChatCompletionMessage(content='Hallucination trong AI là việc tạo ra
thông tin sai hoặc vô nghĩa nhưng được trình bày như sự thật, thường do hạn chế dữ liệu huấn luyện,
thiên lệch mô hình hoặc lỗi suy luận.', refusal=None, role='assistant', annotations=None, audio=None,
function_call=None, tool_calls=[]), seed=12469585682595789000)], created=1745920244,
model='deepseek-ai/DeepSeek-V3', object='chat.completion', service_tier=None, system_fingerprint=None,
usage=CompletionUsage(completion_tokens=38, prompt_tokens=14, total_tokens=52,
completion_tokens_details=None, prompt_tokens_details=None), prompt=[])
ChatCompletion(id='ns1zcjp-zqrih-937de597af0fd643',
choices=[Choice(finish_reason='stop', index=0, logprobs=None,
message=ChatCompletionMessage(content='Hallucination trong AI là việc
tạo ra thông tin sai hoặc vô nghĩa nhưng được trình bày như
sự thật, thường do hạn chế dữ liệu huấn luyện, thiên lệch mô hình
hoặc lỗi suy luận.',
refusal=None, role='assistant', annotations=None, audio=None,
function_call=None, tool_calls=[]), seed=12469585682595789000)],
created=1745920244,
model='deepseek-ai/DeepSeek-V3',
object='chat.completion',
service_tier=None,
system_fingerprint=None,
usage=CompletionUsage(completion_tokens=38, prompt_tokens=14, total_tokens=52,
completion_tokens_details=None, prompt_tokens_details=None),
prompt=[])
print(response.choices)
[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(
content='Hallucination trong AI là việc tạo ra thông tin sai hoặc vô nghĩa nhưng được trình bày
như sự thật, thường do hạn chế dữ liệu huấn luyện, thiên lệch mô hình hoặc lỗi suy luận.',
refusal=None, role='assistant', annotations=None, audio=None,
function_call=None, tool_calls=[]), seed=12469585682595789000)]
print(response.choices[0])
Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(
content='Hallucination trong AI là việc tạo ra thông tin sai hoặc vô nghĩa nhưng được trình bày
như sự thật, thường do hạn chế dữ liệu huấn luyện, thiên lệch mô hình hoặc lỗi suy luận.',
refusal=None, role='assistant', annotations=None, audio=None,
function_call=None, tool_calls=[]), seed=12469585682595789000)
print(response.choices[0].message)
ChatCompletionMessage(content='Hallucination trong AI là việc tạo ra thông tin sai hoặc
vô nghĩa nhưng được trình bày như sự thật, thường do hạn chế dữ liệu huấn luyện,
thiên lệch mô hình hoặc lỗi suy luận.', refusal=None, role='assistant', annotations=None,
audio=None, function_call=None, tool_calls=[])
print(response.choices[0].message.content)
Hallucination trong AI là việc tạo ra thông tin sai hoặc vô nghĩa nhưng được trình bày như sự thật,
thường do hạn chế dữ liệu huấn luyện, thiên lệch mô hình hoặc lỗi suy luận.

Làm việc với DeepSeek bằng Python