0
Conversational Rag
One script reply has been approved by the moderators Verified
Created by hugo697 32 days ago Viewed 2819 times
0
Submitted by hugo697 Bun
Verified 32 days ago
1
//native
2
type Ai21 = {
3
  apiKey: string;
4
};
5
/**
6
 * Conversational Rag
7
 *
8
 */
9
export async function main(
10
  auth: Ai21,
11
  body: {
12
    messages: { role: "assistant" | "user"; content: string }[];
13
    path?: string;
14
    labels?: string[];
15
    file_ids?: string[];
16
    max_segments?: number;
17
    retrieval_strategy?: "default" | "segments" | "add_neighbors" | "full_doc";
18
    retrieval_similarity_threshold?: number;
19
    max_neighbors?: number;
20
    hybrid_search_alpha?: number;
21
    response_language?:
22
      | "dutch"
23
      | "english"
24
      | "french"
25
      | "german"
26
      | "hebrew"
27
      | "italian"
28
      | "portuguese"
29
      | "spanish";
30
  },
31
) {
32
  const url = new URL(`https://api.ai21.com/studio/v1/conversational-rag`);
33

34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      "Content-Type": "application/json",
38
      Authorization: "Bearer " + auth.apiKey,
39
    },
40
    body: JSON.stringify(body),
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48