Docs · Guides

Search your own documents.

Find passages by meaning rather than by keyword — “can I bring my dog?” finds the clause about pets — and give them to a chat model so it answers from your documents. Everything runs inside Canner: the embeddings model, your Postgres database and the chat model.

1. Turn on pgvector

pgvector needs the project’s own Postgres instance (the Dedicated Postgres add-on): on the shared cluster only Canner can install extensions. Install it from Database → Extensions or the CLI:

canner db extensions <database> install vector
CREATE TABLE passages (
  id        bigserial PRIMARY KEY,
  document  text NOT NULL,
  content   text NOT NULL,
  embedding vector(1024) NOT NULL
);
CREATE INDEX ON passages USING hnsw (embedding vector_cosine_ops);

2. Embed your documents

Split long documents into passages of a few hundred words; one passage is one row. A 300-page document is about 150,000 tokens: roughly 3 minutes and 180 credits to embed, once.

import OpenAI from 'openai';
import pg from 'pg';

const ai = new OpenAI();   // OPENAI_BASE_URL + OPENAI_API_KEY from the attached endpoint
const db = new pg.Pool({ connectionString: process.env.DATABASE_URL });

async function addDocument(name, passages) {
  for (let i = 0; i < passages.length; i += 64) {
    const batch = passages.slice(i, i + 64);
    const r = await ai.embeddings.create({ model: 'bge-m3', input: batch });
    for (const [k, d] of r.data.entries()) {
      await db.query('INSERT INTO passages (document, content, embedding) VALUES ($1, $2, $3)',
        [name, batch[k], JSON.stringify(d.embedding)]);
    }
  }
}

3. Search, then answer

async function ask(question) {
  const q = await ai.embeddings.create({ model: 'bge-m3', input: question });
  const { rows } = await db.query(
    'SELECT document, content FROM passages ORDER BY embedding <=> $1 LIMIT 5',
    [JSON.stringify(q.data[0].embedding)]);
  const context = rows.map((r) => `[${r.document}] ${r.content}`).join('\n\n');
  const r = await ai.chat.completions.create({
    model: 'gemma-4-26b-a4b',
    messages: [
      { role: 'system', content: 'Answer only from the passages given. Name the document you used. If the answer is not there, say so.' },
      { role: 'user', content: `${context}\n\nQuestion: ${question}` },
    ],
  });
  return r.choices[0].message.content;
}

English and French live in the same index: a French question finds the English passage that answers it. In our tests every query found its passage, in either language.

To flag answers a person should check, call /v1/uncertainty with the same messages instead of the chat endpoint and look at agreement (see AI endpoints).