0

Extract metadata (structured)

by
Published Oct 17, 2025

Sends an AI request to supported Large Language Models (LLMs) and returns extracted metadata as a set of key-value pairs.

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 (structured)
7
 * Sends an AI request to supported Large Language Models (LLMs) and returns extracted metadata as a set of key-value pairs.
8
 */
9
export async function main(
10
  auth: Box,
11
  body: {
12
    items: { id: string; type: "file"; content?: string }[];
13
    metadata_template?: {
14
      template_key?: string;
15
      type?: "metadata_template";
16
      scope?: string;
17
    };
18
    fields?: {
19
      key: string;
20
      description?: string;
21
      displayName?: string;
22
      prompt?: string;
23
      type?: string;
24
      options?: { key: string }[];
25
    }[];
26
    ai_agent?: {
27
      type: "ai_agent_extract_structured";
28
      long_text?: {
29
        model?: string;
30
        num_tokens_for_completion?: number;
31
        llm_endpoint_params?:
32
          | {
33
              type: "openai_params";
34
              temperature?: number;
35
              top_p?: number;
36
              frequency_penalty?: number;
37
              presence_penalty?: number;
38
              stop?: string;
39
            }
40
          | {
41
              type: "google_params";
42
              temperature?: number;
43
              top_p?: number;
44
              top_k?: number;
45
            }
46
          | { type: "aws_params"; temperature?: number; top_p?: number };
47
      } & { system_message?: string; prompt_template?: string } & {
48
        embeddings?: {
49
          model?: string;
50
          strategy?: { id?: string; num_tokens_per_chunk?: number };
51
        };
52
      };
53
      basic_text?: {
54
        model?: string;
55
        num_tokens_for_completion?: number;
56
        llm_endpoint_params?:
57
          | {
58
              type: "openai_params";
59
              temperature?: number;
60
              top_p?: number;
61
              frequency_penalty?: number;
62
              presence_penalty?: number;
63
              stop?: string;
64
            }
65
          | {
66
              type: "google_params";
67
              temperature?: number;
68
              top_p?: number;
69
              top_k?: number;
70
            }
71
          | { type: "aws_params"; temperature?: number; top_p?: number };
72
      } & { system_message?: string; prompt_template?: string };
73
    };
74
  },
75
) {
76
  const url = new URL(`https://api.box.com/2.0/ai/extract_structured`);
77

78
  const response = await fetch(url, {
79
    method: "POST",
80
    headers: {
81
      "Content-Type": "application/json",
82
      Authorization: "Bearer " + auth.token,
83
    },
84
    body: JSON.stringify(body),
85
  });
86
  if (!response.ok) {
87
    const text = await response.text();
88
    throw new Error(`${response.status} ${text}`);
89
  }
90
  return await response.json();
91
}
92