Zum Hauptinhalt springen

Documentation Index

Fetch the complete documentation index at: https://docs.aihubmix.com/llms.txt

Use this file to discover all available pages before exploring further.

1. Kern-Integrationsmuster

1.1 Einheitliche Authentifizierung und Routing

// Core logic: Replace API Key, Base URL, add APP-Code
const config = {
  apiKey: 'your-aihubmix-api-key',  // Replace with aihubmix API Key
  baseURL: 'https://aihubmix.com',   // Replace with aihubmix gateway
  headers: {
    'APP-Code': 'App Code'  // App Code can be obtained from https://aihubmix.com/appstore
  }
};

// Model routing rules
function routeModel(modelName: string) {
  if (modelName.startsWith('claude')) {
    // Claude models: Use Anthropic SDK
    return 'anthropic';
  } else if (modelName.startsWith('gemini') && !modelName.endsWith('-nothink') && !modelName.endsWith('-search')) {
    // Gemini models: Use Google SDK, endpoint https://aihubmix.com/gemini
    return 'gemini';
  } else {
    // Other models: Use OpenAI compatible interface
    return 'openai';
  }
}

1.2 Besondere Behandlungspunkte

  • Empty-Tools-Fix: Wenn tools=[] und tool_choice vorhanden ist, entfernen Sie tool_choice automatisch
  • Dateiendungen: Setzen Sie die richtigen Dateiendungen automatisch basierend auf mediaType
  • Cache-Steuerung: Unterstützt <cache>-Tags zur Cache-Steuerung

2. Einheitliche Integration

2.1 Kern-Client-Wrapper

class AihubmixModelClient {
  private config: {
    apiKey: string;
    baseURL: string;
    appCode: string;
  };

  constructor(apiKey: string) {
    this.config = {
      apiKey,
      baseURL: 'https://aihubmix.com',
      appCode: 'App Code can be obtained from https://aihubmix.com/appstore'
    };
  }

  async chatCompletion(model: string, messages: any[], options: any = {}) {
    // Automatically route to corresponding SDK based on model name
    if (model.startsWith('claude')) {
      return this.claudeCompletion(model, messages, options);
    } else if (model.startsWith('gemini')) {
      return this.geminiCompletion(model, messages, options);
    } else {
      return this.openaiCompletion(model, messages, options);
    }
  }

  private async claudeCompletion(model: string, messages: any[], options: any) {
    const { Anthropic } = await import('@anthropic-ai/sdk');
    const client = new Anthropic({
      apiKey: this.config.apiKey,
      baseURL: this.config.baseURL,
      defaultHeaders: { 'APP-Code': this.config.appCode }
    });
    return client.messages.create({ model, messages, ...options });
  }

  private async geminiCompletion(model: string, messages: any[], options: any) {
    const { GoogleGenerativeAI } = await import('@google/generative-ai');
    const genAI = new GoogleGenerativeAI(this.config.apiKey, {
      baseURL: `${this.config.baseURL}/gemini/v1beta`,
      defaultHeaders: { 'APP-Code': this.config.appCode }
    });
    const genModel = genAI.getGenerativeModel({ model });
    return genModel.generateContent(messages);
  }

  private async openaiCompletion(model: string, messages: any[], options: any) {
    const OpenAI = await import('openai');
    const client = new OpenAI.default({
      apiKey: this.config.apiKey,
      baseURL: `${this.config.baseURL}/v1`,
      defaultHeaders: { 'APP-Code': this.config.appCode }
    });
    return client.chat.completions.create({ model, messages, ...options });
  }
}

// Usage example
const client = new AihubmixModelClient('your-aihubmix-api-key');
await client.chatCompletion('gpt-4o-mini', messages);
await client.chatCompletion('claude-3-5-sonnet-20241022', messages);
await client.chatCompletion('gemini-2.5-flash', messages);

2.2 Spezielle Behandlung und Hilfsfunktionen

// Empty tools fix
function fixToolChoice(requestBody: any): any {
  if (requestBody.tools?.length === 0 && requestBody.tool_choice) {
    delete requestBody.tool_choice;
  }
  return requestBody;
}

// File extension mapping
function setFileExtension(mediaType: string): string {
  const mimeToExt: Record<string, string> = {
    'audio/mpeg': 'mp3', 'audio/wav': 'wav', 'audio/flac': 'flac'
  };
  return mimeToExt[mediaType] || 'bin';
}

// Cache control
function processCacheTags(content: string): { content: string; cacheControl?: any } {
  if (content.includes('<cache>')) {
    return { content: content.replace('<cache>', ''), cacheControl: { type: 'ephemeral' } };
  }
  return { content };
}

3. Deployment und Konfiguration

3.1 Umgebungsvariablen

const config = {
  apiKey: process.env.AIHUBMIX_API_KEY || '',
  baseURL: process.env.AIHUBMIX_BASE_URL || 'https://aihubmix.com',
  appCode: process.env.AIHUBMIX_APP_CODE || 'App Code can be obtained from https://aihubmix.com/appstore'
};

3.2 Fehlerbehandlung

class AihubmixError extends Error {
  constructor(message: string, public code?: string, public status?: number) {
    super(message);
    this.name = 'AihubmixError';
  }
}

function handleAihubmixErrors(error: any): AihubmixError {
  const message = error.message || 'Unknown error';
  if (message.toLowerCase().includes('rate limit')) {
    return new AihubmixError('Rate limit exceeded', 'RATE_LIMIT', 429);
  } else if (message.toLowerCase().includes('unauthorized')) {
    return new AihubmixError('Authentication failed', 'AUTH_ERROR', 401);
  } else {
    return new AihubmixError(message, error.code, error.status);
  }
}

4. Referenzimplementierungen und Abgleich-Checkliste

4.1 cherry-studio-Client-Referenz (TypeScript)

Die folgenden Kernpunkte aus cherry-studio’s AihubmixAPIClient.ts können als Vorlage für Drittanbieter-Frontend- bzw. Desktop-Anwendungen dienen, die aihubmix auf TypeScript-Seite integrieren:
  • Einheitliches Hinzufügen des Rabatt-Codes: extra_headers auf Provider-Ebene zusammenführen und APP-Code setzen (im Projekt wird MLTG2087 verwendet)
  • Multi-Client-Routing:
    • claude* → Anthropic-Client verwenden
    • gemini*/imagen*, die nicht auf -nothink/-search enden und kein embedding enthalten → Gemini-Client verwenden (apiHost: https://aihubmix.com/gemini)
    • OpenAI-Serie (außer gpt-oss) → OpenAI-kompatibler Response-Client
    • Sonstige → Fallback auf den Standard-OpenAI-Client
  • BaseURL-Ermittlung: aus dem aktuell gerouteten Client exportieren, um Endpoint-Unterschiede pro Anbieter zu erhalten

4.2 dify-plugin-aihubmix-Referenz (Python)

Die folgenden Kernpunkte aus der dify-plugin-aihubmix-Implementierung können als Vorlage für Drittanbieter-Python-Tools dienen, die aihubmix integrieren:
  • Einheitliches Hinzufügen des Rabatt-Codes: extra_headers auf Provider-Ebene zusammenführen und APP-Code setzen (im Projekt wird Dify2025 verwendet)
  • Multi-Client-Routing:
    • claude* → Anthropic-Client verwenden
    • gemini*/imagen*, die nicht auf -nothink/-search enden und kein embedding enthalten → Gemini-Client verwenden (apiHost: https://aihubmix.com/gemini)
    • OpenAI-Serie (außer gpt-oss) → OpenAI-kompatibler Response-Client
    • Sonstige → Fallback auf den Standard-OpenAI-Client
  • BaseURL-Ermittlung: aus dem aktuell gerouteten Client exportieren, um Endpoint-Unterschiede pro Anbieter zu erhalten

4.3 Abgleich-Checkliste

  • Provider-Eintrag vereinheitlicht das Zusammenführen von extra_headers und injiziert APP-Code
  • Gemini-Client verwendet https://aihubmix.com/gemini als apiHost
  • Routing-Regeln sind konsistent für claude*, gemini*/imagen*, OpenAI-Serie (außer gpt-oss)
  • Standard-Fallback auf OpenAI-Client unter Beibehaltung des OpenAI-kompatiblen Verhaltens
  • getBaseURL() exportiert immer aus dem aktuell gerouteten Client, ohne Hardcoding

5. Migrationsprüfliste

  • API-Schlüssel durch aihubmix-API-Schlüssel ersetzen
  • Base URL durch https://aihubmix.com ersetzen
  • Header APP-Code für den Rabatt hinzufügen
  • Modell-Routing-Logik implementieren (claude/gemini/openai)
  • tool_choice-Fix bei leerem tools behandeln
  • MIME-Type-Behandlung für File-Uploads konfigurieren
  • Verschiedene Modellaufrufe testen

Zuletzt aktualisiert: 2026-06-01