E-learning: KI, LLM, MCP und Tensoren

Praktische Konzepte mit ausfuhrbaren Python-Beispielen auf dieser Seite.

Die Beispiele sind vordefiniert und werden auf dem Server ausgefuhrt, um Sicherheit und Vorhersehbarkeit zu gewahrleisten.

Requirements for full ML examples

pip install -r requirements.txt
# Includes: torch, tensorflow-cpu
Academy tracks
SQL & Datenbanken — Vollständiger Kurs Python-Datenanalyse — Professioneller Kurs Python Fundamentals Machine Learning with Python Django Web Development

KI (Kunstliche Intelligenz)

KI ermoglicht Maschinen Aufgaben auszufuhren, die normalerweise menschliche Intelligenz erfordern, wie Klassifikation und Vorhersage.

# Simple threshold classifier
loans = [1200, 3400, 900, 5100, 2200]
threshold = 2500
labels = ["high" if x >= threshold else "low" for x in loans]
print("Loans:", loans)
print("Risk labels:", labels)
Schritt fur Schritt
  1. Define a rule and a threshold.
  2. Compute one label per observation.
  3. Compare output labels with your expected risk classes.
  4. This is a deterministic classifier baseline before neural models.
Mathematik dahinter
y = 1 if x >= t else 0
Erwartete Ausgabe: Risk labels: ['low', 'high', 'low', 'high', 'low']

LLM (Large Language Model)

Ein LLM ist ein Sprachmodell, das mit grossen Textmengen trainiert wurde, um Inhalte zu erzeugen, zusammenzufassen und zu transformieren.

# Tiny toy language model with bigram counts
text = "ai helps teams make better decisions with data"
words = text.split()
bigrams = {}
for i in range(len(words) - 1):
    pair = (words[i], words[i + 1])
    bigrams[pair] = bigrams.get(pair, 0) + 1
print("Bigrams:")
for pair, count in sorted(bigrams.items()):
    print(f"{pair}: {count}")
Schritt fur Schritt
  1. Split text into tokens.
  2. Count each adjacent token pair.
  3. Use counts to estimate next-word likelihood.
  4. This is the conceptual bridge to autoregressive LLM decoding.
Mathematik dahinter
P(w_t|w_{t-1}) ~= count(w_{t-1}, w_t) / count(w_{t-1})
Erwartete Ausgabe: Bigramme mit Token-Paaren und Anzahlen, z.B. ('ai', 'helps'): 1

MCP (Model Context Protocol)

MCP standardisiert, wie Werkzeuge Kontext an Modelle liefern, damit Integrationen sicherer und vorhersagbarer werden.

# MCP-like context payload (tool + request + constraints)
context = {
    "user_intent": "summarize monthly sales",
    "tool": "sql_query",
    "constraints": ["read_only", "tenant_isolation"],
}
print("Context keys:", list(context.keys()))
print("Tool selected:", context["tool"])
Schritt fur Schritt
  1. Collect user intent.
  2. Attach tool and constraints metadata.
  3. Pass context to model and execute deterministic tool call.
  4. Observe that routing quality depends on context quality.
Mathematik dahinter
Decision = argmax score(tool_i | context)
Erwartete Ausgabe: Context keys: ['user_intent', 'tool', 'constraints'] Tool selected: sql_query

Tensoren

Ein Tensor ist eine n-dimensionale Struktur zur Darstellung von Daten in KI und Deep Learning (Vektoren, Matrizen und Volumen).

# Tensor basics using nested lists (2D matrix)
matrix = [
    [1, 2, 3],
    [4, 5, 6],
]
rows = len(matrix)
cols = len(matrix[0])
col_sums = [sum(matrix[r][c] for r in range(rows)) for c in range(cols)]
print("Shape:", (rows, cols))
print("Column sums:", col_sums)
Schritt fur Schritt
  1. Represent data as matrix X (rows, cols).
  2. Compute summaries per axis.
  3. Reuse same structure for transformations.
  4. The same concept scales to 3D/4D tensors for batches and channels.
Mathematik dahinter
X in R^(m x n), s_j = sum_i X_ij
Erwartete Ausgabe: Shape: (2, 3) Column sums: [5, 7, 9]

PyTorch (minimales neuronales Netz)

Einfaches Beispiel mit linearen Schichten fur Eingabe- und Ausgabetensoren.

# Minimal PyTorch forward pass
try:
    import torch
except Exception as e:
    print("PyTorch not available:", e)
else:
    torch.manual_seed(0)
    x = torch.randn(4, 3)
    model = torch.nn.Sequential(
        torch.nn.Linear(3, 4),
        torch.nn.ReLU(),
        torch.nn.Linear(4, 1),
    )
    y = model(x)
    print("Input shape:", tuple(x.shape))
    print("Output shape:", tuple(y.shape))
    print("First output:", float(y[0, 0]))
Schritt fur Schritt
  1. Create input tensor x with shape [batch, features].
  2. Apply linear layer, non-linearity, then output layer.
  3. Inspect output shape and first prediction value.
Mathematik dahinter
y = W2 * ReLU(W1 * x + b1) + b2
Erwartete Ausgabe: Input shape: (4, 3) Output shape: (4, 1) First output: numerischer Wert

TensorFlow (minimales Keras)

Einfaches Sequential-Beispiel zum Verstehen von Batch-Inferenz.

# Minimal TensorFlow/Keras forward pass
try:
    import tensorflow as tf
except Exception as e:
    print("TensorFlow not available:", e)
else:
    tf.random.set_seed(0)
    x = tf.random.normal((4, 3))
    model = tf.keras.Sequential([
        tf.keras.layers.Dense(4, activation="relu"),
        tf.keras.layers.Dense(1),
    ])
    y = model(x)
    print("Input shape:", x.shape)
    print("Output shape:", y.shape)
    print("First output:", float(y[0, 0]))
Schritt fur Schritt
  1. Build a Sequential model with Dense layers.
  2. Feed batch tensor and run forward inference.
  3. Read tensor shape and sample output.
Mathematik dahinter
Dense(x) = activation(x * W + b)
Erwartete Ausgabe: Input shape: (4, 3) Output shape: (4, 1) First output: numerischer Wert

Kleines Lern-LLM

Sehr kleines Sprachmodell (Bigramme), um Tokens, Logits und Wahrscheinlichkeiten zu lehren.

# Tiny LLM teaching model (char-level bigram logits)
try:
    import torch
except Exception as e:
    print("PyTorch not available:", e)
else:
    text = "hello llm"
    vocab = sorted(set(text))
    stoi = {c: i for i, c in enumerate(vocab)}
    itos = {i: c for c, i in stoi.items()}

    ids = torch.tensor([stoi[c] for c in text], dtype=torch.long)
    model = torch.nn.Embedding(len(vocab), len(vocab))
    logits = model(ids)
    probs = torch.softmax(logits[-1], dim=-1)

    top_p, top_i = torch.topk(probs, k=min(3, len(vocab)))
    print("Vocab:", vocab)
    print("Last input token:", repr(itos[int(ids[-1])]))
    print("Top next-token probabilities:")
    for p, i in zip(top_p, top_i):
        print(itos[int(i)], round(float(p), 4))
Schritt fur Schritt
  1. Build a character vocabulary and token IDs.
  2. Use embedding table as learnable logits producer.
  3. Apply softmax and read top-k next-token probabilities.
Mathematik dahinter
p(next) = softmax(logits_last_token)
Erwartete Ausgabe: Vocab: [...] Last input token: ... Top next-token probabilities: 3 Token-Wahrscheinlichkeitszeilen

RAG (Retrieval-Augmented Generation)

RAG kombiniert Retrieval + Generation fur prazisere und auditierbare Antworten.

# Tiny RAG demo (keyword overlap retrieval + prompt assembly)
docs = [
    "LGPD requires lawful basis, consent management, and data subject rights.",
    "Dedicated AI servers provide tenant isolation and private GPU workloads.",
    "RAG improves answer grounding by injecting retrieved context into prompts.",
]
query = "How to run AI servers with LGPD compliance?"

q_terms = set(w.strip('.,!?').lower() for w in query.split())
scored = []
for d in docs:
    d_terms = set(w.strip('.,!?').lower() for w in d.split())
    score = len(q_terms & d_terms)
    scored.append((score, d))

scored.sort(reverse=True, key=lambda x: x[0])
top_doc = scored[0][1]
prompt = f"Question: {query}\nContext: {top_doc}\nAnswer:"

print("Query:", query)
print("Top document:", top_doc)
print("Prompt preview:", prompt[:120] + "...")
Schritt fur Schritt
  1. 1) Indexieren Sie eine kleine Wissensbasis (Dokumente).
  2. 2) Holen Sie den besten Kontext fur die Anfrage.
  3. 3) Bauen Sie den finalen Prompt mit Frage + abgerufenem Kontext.
Mathematik dahinter
argmax_d sim(query, doc_d) -> prompt = [query + top_doc]
Erwartete Ausgabe: Query: ... Top document: ... Prompt preview: ...

Dedizierte KI-Services

Dedizierte KI-Server: private GPU, Tenant-Isolation, Monitoring, SLA und MLOps.

LGPD by design: Datenminimierung, Retention Policy, Audit-Trail, Consent und Recht auf Loschung.

Wir konnen Ihren dedizierten AI-Server mit LGPD-Compliance bereitstellen.

Contact