Making requests to DeepSeek models

Working with DeepSeek in Python

James Chapman

Curriculum Manager, DataCamp

Recap

A computer sending a request to the Together.ai API, which contains the model and any data and model parameters.

Working with DeepSeek in Python

Creating a request

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")
  • Create requests using openai library
  • OpenAI: AI model and app developers (e.g., ChatGPT)
  • base_url: divert the request from OpenAI to DeepSeek model provider
  • api_key: API authentication (see the provider's documentation)
1 https://platform.deepseek.com/api_keys
Working with DeepSeek in Python

Creating a request

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)
Working with DeepSeek in Python

The response

ChatCompletion(id='ns1zcjp-zqrih-937de597af0fd643', 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=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=[])
Working with DeepSeek in Python

The response

ChatCompletion(id='ns1zcjp-zqrih-937de597af0fd643',
               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=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=[])
Working with DeepSeek in Python

Interpreting the response

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=12469585682595789000)]
Working with DeepSeek in Python

Interpreting the response

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=12469585682595789000)
Working with DeepSeek in Python

Interpreting the response

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.
Working with DeepSeek in Python

API usage costs

 

  • For DeepSeek:
    • Provider (e.g., together.ai)
    • Model
    • Larger inputs + outputs = Greater cost

A toll road.

1 https://api-docs.deepseek.com/quick_start/pricing
Working with DeepSeek in Python

Let's practice!

Working with DeepSeek in Python

Preparing Video For Download...