0

Create a rerank request

by
Published Oct 17, 2025

Query a reranker model

Script togetherai Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Togetherai = {
3
  api_key: string;
4
};
5
/**
6
 * Create a rerank request
7
 * Query a reranker model
8
 */
9
export async function main(
10
  auth: Togetherai,
11
  body: {
12
    model: string;
13
    query: string;
14
    documents: {}[] | string[];
15
    top_n?: number;
16
    return_documents?: false | true;
17
    rank_fields?: string[];
18
  },
19
) {
20
  const url = new URL(`https://api.together.xyz/v1/rerank`);
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      "Content-Type": "application/json",
26
      Authorization: "Bearer " + auth.api_key,
27
    },
28
    body: JSON.stringify(body),
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36