v2.5KI & Machine Learning

LLM Integration

Das llm-Modul bietet eine High-Level Schnittstelle zu Sprachmodellen wie GPT-4, Claude und Llama.

Features

Chat Completion

Konversationen mit Kontext-Management

Strukturierte Extraktion

Text zu typisierten Structs

Streaming

Echtzeit-Antworten

Chat-Schnittstellen

Für Chatbots oder Assistenten, die einen Kontext über mehrere Nachrichten behalten müssen:

use llm

struct ChatMessage { role: string, content: string }

fn chatSession() {
    let history: List<ChatMessage> = [
        { role: "system", content: "Du bist ein hilfreicher Support-Bot für Velisch." }
    ];
    
    while (true) {
        let userInput = console.read();
        history.push({ role: "user", content: userInput });
        
        let response = llm.chat(history);
        
        log.info("Bot: " + response.content);
        history.push({ role: "assistant", content: response.content });
    }
}

@POST("/api/chat")
fn chatEndpoint(message: string, history: List<ChatMessage>): ChatMessage {
    history.push({ role: "user", content: message });
    
    let response = llm.chat(history);
    
    return { role: "assistant", content: response.content };
}

Strukturierte Datenextraktion

Eine der mächtigsten Funktionen: Verwandeln Sie unstrukturierten Text in typisierte Structs. Das ist oft zuverlässiger als Regex.

use llm

struct MeetingInfo {
    topic: string,
    participants: List<string>,
    date: string,
    priority: string
}

fn parseEmail(emailBody: string): MeetingInfo {
    // Velisch zwingt das LLM, valides JSON zu generieren, 
    // das genau diesem Struct entspricht.
    return llm.extract_entities(emailBody, MeetingInfo);
}

// Beispielaufruf
let email = "Hi Team, lasst uns morgen um 14 Uhr über das Q3 Budget sprechen. Alice und Bob müssen dabei sein. Es ist dringend!";
let info = parseEmail(email);

log.info(info.topic);      // "Q3 Budget"
log.info(info.priority);   // "High"
log.info(info.participants); // ["Alice", "Bob"]

Sentiment-Analyse & Klassifizierung

use llm

@POST("/api/support/analyze")
fn analyzeTicket(ticketText: string): TicketAnalysis {
    // Sentiment (Stimmung): "positive", "negative", "neutral"
    let sentiment = llm.sentiment(ticketText);
    
    // Zero-Shot Klassifizierung in Kategorien
    let category = llm.classify(ticketText, [
        "Rechnungsproblem", 
        "Technischer Bug", 
        "Feature Request", 
        "Sonstiges"
    ]);
    
    // Dringlichkeit erkennen
    let urgent = llm.classify(ticketText, ["dringend", "normal"]) == "dringend";
    
    return TicketAnalysis {
        sentiment: sentiment,
        category: category,
        urgent: urgent,
    };
}

Streaming Responses

use llm

@GET("/api/stream")
fn streamResponse(prompt: string): Stream<string> {
    // Gibt Tokens in Echtzeit zurück
    return llm.stream(prompt, {
        temperature: 0.7,
        maxTokens: 1000,
    });
}

// Client-seitig (JavaScript)
// const eventSource = new EventSource('/api/stream?prompt=...');
// eventSource.onmessage = (e) => console.log(e.data);

Vollständiges Beispiel: AI-gestützter Support-Bot

use llm
use db

struct Ticket {
    id: string,
    content: string,
    sentiment: string,
    category: string,
    aiResponse: string,
    createdAt: string,
}

@POST("/api/support/ticket")
fn createTicket(content: string): Ticket {
    // 1. Analyse
    let sentiment = llm.sentiment(content);
    let category = llm.classify(content, [
        "Billing", "Technical", "Feature", "Other"
    ]);
    
    // 2. AI-Antwort generieren
    let systemPrompt = "Du bist ein freundlicher Support-Bot. Antworte kurz und hilfreich.";
    let aiResponse = llm.chat([
        { role: "system", content: systemPrompt },
        { role: "user", content: content }
    ]).content;
    
    // 3. Speichern
    let ticket = Ticket {
        id: utils.uuid(),
        content: content,
        sentiment: sentiment,
        category: category,
        aiResponse: aiResponse,
        createdAt: datetime.now(),
    };
    
    db.save(ticket);
    
    // 4. Bei negativem Sentiment eskalieren
    if (sentiment == "negative") {
        notifyManager(ticket);
    }
    
    return ticket;
}

Best Practices

Caching nutzen

LLM-Aufrufe sind teuer - nutze @Cache für wiederholte Anfragen

Graceful Degradation

Fallbacks einbauen, falls die AI-API nicht erreichbar ist

Prompt Engineering

Prompts in Konfigurationsdateien auslagern

Rate Limiting

LLM-Endpunkte mit @RateLimit schützen