0

Extract metadata (freeform)

by
Published Oct 17, 2025

Sends an AI request to supported Large Language Models (LLMs) and extracts metadata in form of key-value pairs. In this request, both the prompt and the output can be freeform. Metadata template setup before sending the request is not required.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Extract metadata (freeform)
7
 * Sends an AI request to supported Large Language Models (LLMs) and extracts metadata in form of key-value pairs.
8
In this request, both the prompt and the output can be freeform.
9
Metadata template setup before sending the request is not required.
10
 */
11
export async function main(
12
  auth: Box,
13
  body: {
14
    prompt: string;
15
    items: { id: string; type: "file"; content?: string }[];
16
    ai_agent?: {
17
      type: "ai_agent_extract";
18
      long_text?: {
19
        model?: string;
20
        num_tokens_for_completion?: number;
21
        llm_endpoint_params?:
22
          | {
23
              type: "openai_params";
24
              temperature?: number;
25
              top_p?: number;
26
              frequency_penalty?: number;
27
              presence_penalty?: number;
28
              stop?: string;
29
            }
30
          | {
31
              type: "google_params";
32
              temperature?: number;
33
              top_p?: number;
34
              top_k?: number;
35
            }
36
          | { type: "aws_params"; temperature?: number; top_p?: number };
37
      } & { system_message?: string; prompt_template?: string } & {
38
        embeddings?: {
39
          model?: string;
40
          strategy?: { id?: string; num_tokens_per_chunk?: number };
41
        };
42
      };
43
      basic_text?: {
44
        model?: string;
45
        num_tokens_for_completion?: number;
46
        llm_endpoint_params?:
47
          | {
48
              type: "openai_params";
49
              temperature?: number;
50
              top_p?: number;
51
              frequency_penalty?: number;
52
              presence_penalty?: number;
53
              stop?: string;
54
            }
55
          | {
56
              type: "google_params";
57
              temperature?: number;
58
              top_p?: number;
59
              top_k?: number;
60
            }
61
          | { type: "aws_params"; temperature?: number; top_p?: number };
62
      } & { system_message?: string; prompt_template?: string };
63
    };
64
  },
65
) {
66
  const url = new URL(`https://api.box.com/2.0/ai/extract`);
67

68
  const response = await fetch(url, {
69
    method: "POST",
70
    headers: {
71
      "Content-Type": "application/json",
72
      Authorization: "Bearer " + auth.token,
73
    },
74
    body: JSON.stringify(body),
75
  });
76
  if (!response.ok) {
77
    const text = await response.text();
78
    throw new Error(`${response.status} ${text}`);
79
  }
80
  return await response.json();
81
}
82