Aller au contenu

🤖 OpenAI API (Multi-Provider)

Typ: REST API (OpenAI-kompatibel)
Auth: API Key
Status: ✅ Flexibel (Cloud + Lokal)


Übersicht

OpenAI-kompatible API für: - Dokumenten-Zusammenfassungen - Vertragsanalyse - Mieter-Kommunikation (Entwürfe) - OCR-Nachbearbeitung

Strategie: OpenAI API-Format → Flexibel zwischen Providern wechseln


Unterstützte Provider

Provider Modelle Kosten Latenz
OpenAI GPT-4o, GPT-4-turbo €€€ Schnell
Anthropic Claude 3.5 Sonnet €€ Schnell
Groq Llama 3.1, Mixtral Sehr schnell
Together AI Llama, Mistral Schnell
Ollama Llama, Mistral, etc. Kostenlos Lokal
LM Studio Diverse Kostenlos Lokal

API Endpoints

Methode Endpunkt Beschreibung Cache TTL
POST /api/ai/chat Chat Completion -
POST /api/ai/summarize Dokument zusammenfassen 1h
POST /api/ai/analyze-contract Vertragsanalyse 1h
POST /api/ai/draft-email E-Mail-Entwurf -
POST /api/ai/extract-data Daten extrahieren 1h
GET /api/ai/models Verfügbare Modelle 1h

Unified AI Client

// src/integrations/ai-client.ts
import OpenAI from 'openai';

type AIProvider = 'openai' | 'anthropic' | 'groq' | 'together' | 'ollama';

interface AIConfig {
  provider: AIProvider;
  apiKey?: string;
  baseURL?: string;
  model: string;
}

const PROVIDER_CONFIG: Record<AIProvider, Partial<AIConfig>> = {
  openai: {
    baseURL: 'https://api.openai.com/v1',
    model: 'gpt-4o'
  },
  anthropic: {
    baseURL: 'https://api.anthropic.com/v1',
    model: 'claude-3-5-sonnet-20241022'
  },
  groq: {
    baseURL: 'https://api.groq.com/openai/v1',
    model: 'llama-3.1-70b-versatile'
  },
  together: {
    baseURL: 'https://api.together.xyz/v1',
    model: 'meta-llama/Llama-3.1-70B-Instruct-Turbo'
  },
  ollama: {
    baseURL: 'http://localhost:11434/v1',
    model: 'llama3.1'
  }
};

export function createAIClient(provider: AIProvider = 'openai'): OpenAI {
  const config = PROVIDER_CONFIG[provider];

  return new OpenAI({
    apiKey: process.env[`${provider.toUpperCase()}_API_KEY`] || 'ollama',
    baseURL: config.baseURL
  });
}

// Usage
const ai = createAIClient(process.env.AI_PROVIDER as AIProvider);

const response = await ai.chat.completions.create({
  model: PROVIDER_CONFIG[process.env.AI_PROVIDER].model,
  messages: [
    { role: 'system', content: 'Du bist ein Immobilien-Assistent.' },
    { role: 'user', content: 'Fasse diesen Mietvertrag zusammen: ...' }
  ]
});

Spezifische Use Cases

Vertragszusammenfassung

async function summarizeContract(contractText: string): Promise<string> {
  const response = await ai.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      {
        role: 'system',
        content: `Du bist ein Immobilien-Rechtsexperte. Fasse Mietverträge präzise zusammen.

Struktur:
- Parteien (Vermieter, Mieter)
- Objekt (Adresse, Größe, Ausstattung)
- Konditionen (Miete, NK, Kaution)
- Laufzeit (Beginn, Ende, Kündigungsfrist)
- Besonderheiten (Sondervereinbarungen)`
      },
      { role: 'user', content: contractText }
    ],
    temperature: 0.3
  });

  return response.choices[0].message.content;
}

Datenextraktion

interface ContractData {
  tenant: { name: string; address: string };
  property: { address: string; size: number; rooms: number };
  rent: { base: number; utilities: number; deposit: number };
  period: { start: string; end?: string; notice: string };
}

async function extractContractData(text: string): Promise<ContractData> {
  const response = await ai.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      {
        role: 'system',
        content: 'Extrahiere strukturierte Daten aus dem Mietvertrag. Antworte NUR mit validem JSON.'
      },
      { role: 'user', content: text }
    ],
    response_format: { type: 'json_object' },
    temperature: 0
  });

  return JSON.parse(response.choices[0].message.content);
}

Anthropic (Claude) direkt

import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY
});

const response = await anthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  max_tokens: 4096,
  messages: [
    { role: 'user', content: 'Analysiere diesen Vertrag...' }
  ]
});

Lokale AI (Ollama)

# Installation
curl -fsSL https://ollama.ai/install.sh | sh

# Modell laden
ollama pull llama3.1
ollama pull mistral

# Server starten (automatisch)
ollama serve
// Ollama ist OpenAI-kompatibel
const ollama = new OpenAI({
  apiKey: 'ollama',
  baseURL: 'http://localhost:11434/v1'
});

const response = await ollama.chat.completions.create({
  model: 'llama3.1',
  messages: [...]
});

Umgebungsvariablen

# Provider auswählen
AI_PROVIDER="openai"  # openai | anthropic | groq | together | ollama

# API Keys
OPENAI_API_KEY=""
ANTHROPIC_API_KEY=""
GROQ_API_KEY=""
TOGETHER_API_KEY=""

# Ollama (lokal)
OLLAMA_HOST="http://localhost:11434"

# Defaults
AI_DEFAULT_MODEL="gpt-4o"
AI_TEMPERATURE="0.3"
AI_MAX_TOKENS="4096"

Rate Limits

Provider RPM TPM
OpenAI (Tier 1) 500 30K
Anthropic 1000 100K
Groq 30 6K
Ollama

Kosten-Schätzung

Task Tokens GPT-4o Claude Llama (Groq)
Zusammenfassung ~2K $0.02 $0.01 $0.001
Vertragsanalyse ~5K $0.05 $0.03 $0.003
Datenextraktion ~3K $0.03 $0.02 $0.002

OpenAI-kompatibel • Multi-Provider • Lokal möglich