Skip to main content

Custom Models

Integrate Trix with self-hosted or custom AI models.

Overview

Trix works with any AI model that accepts text input. This guide covers integration patterns for custom or self-hosted models.

Ollama Integration

ollama.ts
import { Trix } from '@trixdb/client';

const trix = new Trix({ apiKey: process.env.TRIX_API_KEY });

async function chat(userMessage, userId) {
  // Get relevant memories
  const results = await trix.memories.list({ q: userMessage, spaceId: userId });
  const context = results.data.map(m => m.content).join('\n');

  // Call Ollama
  const response = await fetch('http://localhost:11434/api/generate', {
    method: 'POST',
    body: JSON.stringify({
      model: 'llama2',
      prompt: `Context:\n${context}\n\nUser: ${userMessage}\nAssistant:`
    })
  });

  return response.json();
}

Hugging Face Inference

huggingface.ts
import { HfInference } from '@huggingface/inference';
import { Trix } from '@trixdb/client';

const trix = new Trix({ apiKey: process.env.TRIX_API_KEY });
const hf = new HfInference(process.env.HF_TOKEN);

async function chat(userMessage, userId) {
  const results = await trix.memories.list({ q: userMessage, spaceId: userId });

  const response = await hf.textGeneration({
    model: 'mistralai/Mistral-7B-Instruct-v0.2',
    inputs: buildPrompt(results.data, userMessage)
  });

  return response.generated_text;
}

Custom Embeddings

For Enterprise plans, you can bring your own embeddings:

custom-embeddings.ts
const trix = new Trix({
  apiKey: process.env.TRIX_API_KEY,
  embeddings: {
    provider: 'custom',
    endpoint: 'https://your-embedding-service.com/embed',
    headers: {
      Authorization: `Bearer ${process.env.EMBEDDING_KEY}`
    }
  }
});

Custom embedding endpoints must return vectors compatible with Trix's similarity search. Contact support for specifications.

OpenAI-Compatible APIs

Models that expose an OpenAI-compatible API work seamlessly:

openai-compatible.ts
import OpenAI from 'openai';

// Point to your custom endpoint
const client = new OpenAI({
  baseURL: 'http://localhost:8080/v1',
  apiKey: 'not-needed'
});

// Use exactly like OpenAI
const response = await client.chat.completions.create({
  model: 'local-model',
  messages: [...]
});