0

Generate text

by
Published Oct 17, 2025

Sends an AI request to supported Large Language Models (LLMs) and returns generated text based on the provided prompt.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Generate text
7
 * Sends an AI request to supported Large Language Models (LLMs) and returns generated text based on the provided prompt.
8
 */
9
export async function main(
10
  auth: Box,
11
  body: {
12
    prompt: string;
13
    items: { id: string; type: "file"; content?: string }[];
14
    dialogue_history?: {
15
      prompt?: string;
16
      answer?: string;
17
      created_at?: string;
18
    }[];
19
    ai_agent?: {
20
      type: "ai_agent_text_gen";
21
      basic_gen?: {
22
        model?: string;
23
        num_tokens_for_completion?: number;
24
        llm_endpoint_params?:
25
          | {
26
              type: "openai_params";
27
              temperature?: number;
28
              top_p?: number;
29
              frequency_penalty?: number;
30
              presence_penalty?: number;
31
              stop?: string;
32
            }
33
          | {
34
              type: "google_params";
35
              temperature?: number;
36
              top_p?: number;
37
              top_k?: number;
38
            }
39
          | { type: "aws_params"; temperature?: number; top_p?: number };
40
      } & { system_message?: string; prompt_template?: string } & {
41
        embeddings?: {
42
          model?: string;
43
          strategy?: { id?: string; num_tokens_per_chunk?: number };
44
        };
45
      } & { content_template?: string };
46
    };
47
  },
48
) {
49
  const url = new URL(`https://api.box.com/2.0/ai/text_gen`);
50

51
  const response = await fetch(url, {
52
    method: "POST",
53
    headers: {
54
      "Content-Type": "application/json",
55
      Authorization: "Bearer " + auth.token,
56
    },
57
    body: JSON.stringify(body),
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65