Anfragen an DeepSeek-Modelle stellen

Arbeiten mit DeepSeek in Python

James Chapman

AI Curriculum Lead, DataCamp

Rückblick

Ein Computer sendet eine Anfrage an die Together.ai-API, die das Modell sowie Daten und Modellparameter enthält.

Arbeiten mit DeepSeek in Python

Eine Anfrage erstellen

from openai import OpenAI

# An DeepSeek-API senden: base_url="https://api.deepseek.com" client = OpenAI(api_key="<TogetherAI API Key>", base_url="https://api.together.xyz/v1")
  • Anfragen mit der Bibliothek openai erstellen
  • OpenAI: Entwickler von KI-Modellen und -Apps (z. B. ChatGPT)
  • base_url: Anfrage vom OpenAI- zum DeepSeek-Provider umleiten
  • api_key: API-Authentifizierung (siehe Doku des Providers)
1 https://platform.deepseek.com/api_keys
Arbeiten mit DeepSeek in Python

Eine Anfrage erstellen

from openai import OpenAI
# An DeepSeek-API senden: 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)
Arbeiten mit DeepSeek in Python

Die Antwort

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=[])
Arbeiten mit DeepSeek in Python

Die Antwort

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=[])
Arbeiten mit DeepSeek in Python

Die Antwort interpretieren

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)]
Arbeiten mit DeepSeek in Python

Die Antwort interpretieren

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)
Arbeiten mit DeepSeek in Python

Die Antwort interpretieren

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.
Arbeiten mit DeepSeek in Python

API-Nutzungskosten

 

  • Für DeepSeek:
    • Provider (z. B. together.ai)
    • Modell
    • Größere Eingaben + Ausgaben = höhere Kosten

Eine Mautstraße.

1 https://api-docs.deepseek.com/quick_start/pricing
Arbeiten mit DeepSeek in Python

Lass uns üben!

Arbeiten mit DeepSeek in Python

Preparing Video For Download...