Ein LLM aus Python aufrufen
Prompts senden und Antworten verarbeiten
Ein LLM aus Python aufrufen ist eine kostenlose NLP Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des NLP Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der NLP Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Talk to a Model in Code
You do not run giant models on your laptop. Instead you send text to a hosted model over an API and get a reply back. 🌐
Install a Client
Most providers ship a Python client library so you can call the model with a few clean lines instead of raw HTTP.
pip install openaiKeep Your Key Secret
Calls are authenticated with an API key. Store it in an environment variable, never hard-coded in your source files.
import os
key = os.environ["OPENAI_API_KEY"]Create the Client
You start by building a client object. It reads your key and handles the network details for every request you make.
from openai import OpenAI
client = OpenAI()Messages, Not Just Text
Chat models take a list of messages, each tagged with a role like system, user, or assistant.
The System Role
A system message sets the model's behavior up front, like telling it to answer briefly or act as a helpful tutor.
Send Your Prompt
You put your question in a user message and send the whole list to the model in a single call.
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hi!"}])Parse the Response
The reply is a structured object. The text you want sits inside the first choice, ready to read or store.
text = resp.choices[0].message.content
print(text)Control With Temperature
The temperature setting controls randomness. Low values give steady answers; high values give more creative, varied ones.
Cap the Output
Setting max tokens limits how long the reply can be, which keeps responses tidy and your costs predictable.
Handle Failures
Networks fail and limits get hit, so wrap calls in try/except and retry gracefully when an error comes back.
Quick Check
Where do you find the model's text in a chat completion response?
Recap
Install a client, load your key from the environment, send role-tagged messages, then read the text from the first choice. ✅
Häufig gestellte Fragen
Ist die Lektion „Ein LLM aus Python aufrufen“ kostenlos?
Ja — der vollständige Text von „Ein LLM aus Python aufrufen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des NLP Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der NLP Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Ein LLM aus Python aufrufen“?
Prompts senden und Antworten verarbeiten Du übst NLP Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um NLP Academy zu starten?
Keine Vorkenntnisse erforderlich. NLP Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Ein LLM aus Python aufrufen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser NLP Academy-Lektion Code schreiben und ausführen?
Ja. Jede NLP Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Was ein Modell groß macht
- Ein LLM aus Python aufrufen
- Zero-Shot- und Few-Shot-Prompting
- Strukturierte Ausgaben und Guardrails