0

Ask question

by
Published Oct 17, 2025

Sends an AI request to supported LLMs and returns an answer specifically focused on the user's question given the provided context.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Ask question
7
 * Sends an AI request to supported LLMs and returns an answer specifically focused on the user's question given the provided context.
8
 */
9
export async function main(
10
  auth: Box,
11
  body: {
12
    mode: "multiple_item_qa" | "single_item_qa";
13
    prompt: string;
14
    items: { id: string; type: "file"; content?: string }[];
15
    dialogue_history?: {
16
      prompt?: string;
17
      answer?: string;
18
      created_at?: string;
19
    }[];
20
    include_citations?: false | true;
21
    ai_agent?: {
22
      type: "ai_agent_ask";
23
      long_text?: {
24
        model?: string;
25
        num_tokens_for_completion?: number;
26
        llm_endpoint_params?:
27
          | {
28
              type: "openai_params";
29
              temperature?: number;
30
              top_p?: number;
31
              frequency_penalty?: number;
32
              presence_penalty?: number;
33
              stop?: string;
34
            }
35
          | {
36
              type: "google_params";
37
              temperature?: number;
38
              top_p?: number;
39
              top_k?: number;
40
            }
41
          | { type: "aws_params"; temperature?: number; top_p?: number };
42
      } & { system_message?: string; prompt_template?: string } & {
43
        embeddings?: {
44
          model?: string;
45
          strategy?: { id?: string; num_tokens_per_chunk?: number };
46
        };
47
      };
48
      basic_text?: {
49
        model?: string;
50
        num_tokens_for_completion?: number;
51
        llm_endpoint_params?:
52
          | {
53
              type: "openai_params";
54
              temperature?: number;
55
              top_p?: number;
56
              frequency_penalty?: number;
57
              presence_penalty?: number;
58
              stop?: string;
59
            }
60
          | {
61
              type: "google_params";
62
              temperature?: number;
63
              top_p?: number;
64
              top_k?: number;
65
            }
66
          | { type: "aws_params"; temperature?: number; top_p?: number };
67
      } & { system_message?: string; prompt_template?: string };
68
      long_text_multi?: {
69
        model?: string;
70
        num_tokens_for_completion?: number;
71
        llm_endpoint_params?:
72
          | {
73
              type: "openai_params";
74
              temperature?: number;
75
              top_p?: number;
76
              frequency_penalty?: number;
77
              presence_penalty?: number;
78
              stop?: string;
79
            }
80
          | {
81
              type: "google_params";
82
              temperature?: number;
83
              top_p?: number;
84
              top_k?: number;
85
            }
86
          | { type: "aws_params"; temperature?: number; top_p?: number };
87
      } & { system_message?: string; prompt_template?: string } & {
88
        embeddings?: {
89
          model?: string;
90
          strategy?: { id?: string; num_tokens_per_chunk?: number };
91
        };
92
      };
93
      basic_text_multi?: {
94
        model?: string;
95
        num_tokens_for_completion?: number;
96
        llm_endpoint_params?:
97
          | {
98
              type: "openai_params";
99
              temperature?: number;
100
              top_p?: number;
101
              frequency_penalty?: number;
102
              presence_penalty?: number;
103
              stop?: string;
104
            }
105
          | {
106
              type: "google_params";
107
              temperature?: number;
108
              top_p?: number;
109
              top_k?: number;
110
            }
111
          | { type: "aws_params"; temperature?: number; top_p?: number };
112
      } & { system_message?: string; prompt_template?: string };
113
    };
114
  },
115
) {
116
  const url = new URL(`https://api.box.com/2.0/ai/ask`);
117

118
  const response = await fetch(url, {
119
    method: "POST",
120
    headers: {
121
      "Content-Type": "application/json",
122
      Authorization: "Bearer " + auth.token,
123
    },
124
    body: JSON.stringify(body),
125
  });
126
  if (!response.ok) {
127
    const text = await response.text();
128
    throw new Error(`${response.status} ${text}`);
129
  }
130
  return await response.json();
131
}
132