Запросы к моделям DeepSeek

Работа с DeepSeek на Python

James Chapman

AI Curriculum Lead, DataCamp

Повторение

Компьютер отправляет запрос к API Together.ai, содержащий модель, данные и параметры модели.

Работа с 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: разработчик ИИ-моделей и приложений (например, 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)
    • Модель
    • Чем больше входных и выходных данных — тем выше стоимость

Платная дорога.

1 https://api-docs.deepseek.com/quick_start/pricing
Работа с DeepSeek на Python

Давайте потренируемся!

Работа с DeepSeek на Python

Preparing Video For Download...