Rank Documents by Relevance to a Prompt

The following sample ranks four short documents by their relevance to a question about a damaged item. It uses all six llm.reRank(options) parameters and returns all four results by setting options.topN to the number of documents.

The damaged-item document should rank highest. Inspect rankedDocuments in the debugger or the debug log to compare the results. rank indicates the result's position in relevance order, and index identifies its position in the original input array. Exact scores and ordering can vary.

          /**
 * @NApiVersion 2.1
 */
require(['N/llm', 'N/log'], (llm, log) => {
    const documents = [
        'Shipping: Standard delivery takes three to five business days. Track your order using the shipping confirmation email.',
        'Returns: Unused items can be returned within 30 days. Include your receipt and the original packaging.',
        'Damaged items: If an item arrives damaged, contact customer support with your order number and a photo to request a refund or replacement.',
        'Account settings: Update your email address and password on your account profile page.'
    ];

    const response = llm.reRank({
        documents: documents,
        prompt: 'My item arrived damaged. How can I get a refund?',
        reRankModelFamily: llm.ReRankModelFamily.COHERE_RERANK,
        topN: documents.length, // Return ranks for all four documents.
        maxTokensPerDocument: 512, // Truncate documents that exceed this limit.
        timeout: 30000 // Wait up to 30,000 milliseconds.
    });

    // Associate each result with its original document, preserving relevance order.
    const rankedDocuments = response.documentRanks.map((documentRank, position) => ({
        rank: position + 1,
        index: documentRank.index,
        relevance: documentRank.relevance,
        document: documents[documentRank.index]
    }));

    log.debug({
        title: 'Documents ranked by relevance',
        details: {
            model: response.model,
            documents: rankedDocuments
        }
    });
}); 

        

General Notices