• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / box-java-sdk / #3992

06 Sep 2024 01:11PM UTC coverage: 71.703% (-0.7%) from 72.441%
#3992

push

github

web-flow
feat: Support AI Agent (#1265)

248 of 452 new or added lines in 14 files covered. (54.87%)

3 existing lines in 3 files now uncovered.

7921 of 11047 relevant lines covered (71.7%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.61
/src/main/java/com/box/sdk/BoxAI.java
1
package com.box.sdk;
2

3
import com.box.sdk.http.HttpMethod;
4
import com.eclipsesource.json.Json;
5
import com.eclipsesource.json.JsonArray;
6
import com.eclipsesource.json.JsonObject;
7
import java.net.URL;
8
import java.util.List;
9

10

11
public final class BoxAI {
12

13
    /**
14
     * Ask AI url.
15
     */
16
    public static final URLTemplate SEND_AI_REQUEST_URL = new URLTemplate("ai/ask");
1✔
17
    /**
18
     * Text gen AI url.
19
     */
20
    public static final URLTemplate SEND_AI_TEXT_GEN_REQUEST_URL = new URLTemplate("ai/text_gen");
1✔
21
    /**
22
     * AI agent default config url.
23
     */
24
    public static final URLTemplate AI_AGENT_DEFAULT_CONFIG_URL = new URLTemplate("ai_agent_default");
1✔
25

26
    private BoxAI() {
27
    }
28

29
    /**
30
     * Sends an AI request to supported LLMs and returns an answer specifically focused
31
     * on the user's question given the provided items.
32
     *
33
     * @param api    the API connection to be used by the created user.
34
     * @param prompt The prompt provided by the client to be answered by the LLM.
35
     * @param items  The items to be processed by the LLM, currently only files are supported.
36
     * @param mode   The mode specifies if this request is for a single or multiple items.
37
     * @return The response from the AI.
38
     */
39
    public static BoxAIResponse sendAIRequest(BoxAPIConnection api, String prompt, List<BoxAIItem> items, Mode mode) {
40
        return sendAIRequest(api, prompt, items, mode, null, null, null);
1✔
41
    }
42

43
    /**
44
     * Sends an AI request to supported LLMs and returns an answer specifically focused
45
     * on the user's question given the provided items.
46
     *
47
     * @param api             the API connection to be used by the created user.
48
     * @param prompt          The prompt provided by the client to be answered by the LLM.
49
     * @param items           The items to be processed by the LLM, currently only files are supported.
50
     * @param mode            The mode specifies if this request is for a single or multiple items.
51
     * @param dialogueHistory The history of prompts and answers previously passed to the LLM.
52
     *                        This provides additional context to the LLM in generating the response.
53
     * @param agent           The AI agent configuration to be used for the request.
54
     * @param includeCitations Whether to include citations in the response.
55
     * @return The response from the AI.
56
     */
57
    public static BoxAIResponse sendAIRequest(BoxAPIConnection api, String prompt, List<BoxAIItem> items, Mode mode,
58
                                              List<BoxAIDialogueEntry> dialogueHistory, BoxAIAgentAsk agent,
59
                                              Boolean includeCitations) {
60
        URL url = SEND_AI_REQUEST_URL.build(api.getBaseURL());
1✔
61
        JsonObject requestJSON = new JsonObject();
1✔
62
        requestJSON.add("mode", mode.toString());
1✔
63
        requestJSON.add("prompt", prompt);
1✔
64

65
        JsonArray itemsJSON = new JsonArray();
1✔
66
        for (BoxAIItem item : items) {
1✔
67
            itemsJSON.add(item.getJSONObject());
1✔
68
        }
1✔
69
        requestJSON.add("items", itemsJSON);
1✔
70

71
        if (dialogueHistory != null) {
1✔
72
            JsonArray dialogueHistoryJSON = new JsonArray();
1✔
73
            for (BoxAIDialogueEntry dialogueEntry : dialogueHistory) {
1✔
74
                dialogueHistoryJSON.add(dialogueEntry.getJSONObject());
1✔
75
            }
1✔
76
            requestJSON.add("dialogue_history", dialogueHistoryJSON);
1✔
77
        }
78
        if (agent != null) {
1✔
79
            requestJSON.add("ai_agent", agent.getJSONObject());
1✔
80
        }
81
        if (includeCitations != null) {
1✔
82
            requestJSON.add("include_citations", includeCitations);
1✔
83
        }
84

85
        BoxJSONRequest req = new BoxJSONRequest(api, url, HttpMethod.POST);
1✔
86
        req.setBody(requestJSON.toString());
1✔
87

88
        try (BoxJSONResponse response = req.send()) {
1✔
89
            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
1✔
90
            return new BoxAIResponse(responseJSON);
1✔
91
        }
92
    }
93

94
    /**
95
     * Sends an AI request to supported LLMs and returns an answer specifically focused on the creation of new text.
96
     *
97
     * @param api    the API connection to be used by the created user.
98
     * @param prompt The prompt provided by the client to be answered by the LLM.
99
     * @param items  The items to be processed by the LLM, currently only files are supported.
100
     * @return The response from the AI.
101
     */
102
    public static BoxAIResponse sendAITextGenRequest(BoxAPIConnection api, String prompt, List<BoxAIItem> items) {
103
        return sendAITextGenRequest(api, prompt, items, null);
1✔
104
    }
105

106
    /**
107
     * Sends an AI request to supported LLMs and returns an answer specifically focused on the creation of new text.
108
     *
109
     * @param api             the API connection to be used by the created user.
110
     * @param prompt          The prompt provided by the client to be answered by the LLM.
111
     * @param items           The items to be processed by the LLM, currently only files are supported.
112
     * @param dialogueHistory The history of prompts and answers previously passed to the LLM.
113
     *                        This provides additional context to the LLM in generating the response.
114
     * @return The response from the AI.
115
     */
116
    public static BoxAIResponse sendAITextGenRequest(BoxAPIConnection api, String prompt, List<BoxAIItem> items,
117
                                                     List<BoxAIDialogueEntry> dialogueHistory) {
118
        return sendAITextGenRequest(api, prompt, items, dialogueHistory, null);
1✔
119
    }
120

121
    /**
122
     * Sends an AI request to supported LLMs and returns an answer specifically focused on the creation of new text.
123
     *
124
     * @param api             the API connection to be used by the created user.
125
     * @param prompt          The prompt provided by the client to be answered by the LLM.
126
     * @param items           The items to be processed by the LLM, currently only files are supported.
127
     * @param dialogueHistory The history of prompts and answers previously passed to the LLM.
128
     *                        This provides additional context to the LLM in generating the response.
129
     * @param agent           The AI agent configuration to be used for the request.
130
     * @return The response from the AI.
131
     */
132
    public static BoxAIResponse sendAITextGenRequest(BoxAPIConnection api, String prompt, List<BoxAIItem> items,
133
                                                     List<BoxAIDialogueEntry> dialogueHistory,
134
                                                     BoxAIAgentTextGen agent) {
135
        URL url = SEND_AI_TEXT_GEN_REQUEST_URL.build(api.getBaseURL());
1✔
136
        JsonObject requestJSON = new JsonObject();
1✔
137
        requestJSON.add("prompt", prompt);
1✔
138

139
        JsonArray itemsJSON = new JsonArray();
1✔
140
        for (BoxAIItem item : items) {
1✔
141
            itemsJSON.add(item.getJSONObject());
1✔
142
        }
1✔
143
        requestJSON.add("items", itemsJSON);
1✔
144

145
        if (dialogueHistory != null) {
1✔
146
            JsonArray dialogueHistoryJSON = new JsonArray();
1✔
147
            for (BoxAIDialogueEntry dialogueEntry : dialogueHistory) {
1✔
148
                dialogueHistoryJSON.add(dialogueEntry.getJSONObject());
1✔
149
            }
1✔
150
            requestJSON.add("dialogue_history", dialogueHistoryJSON);
1✔
151
        }
152

153
        if (agent != null) {
1✔
154
            requestJSON.add("ai_agent", agent.getJSONObject());
1✔
155
        }
156

157
        BoxJSONRequest req = new BoxJSONRequest(api, url, HttpMethod.POST);
1✔
158
        req.setBody(requestJSON.toString());
1✔
159

160
        try (BoxJSONResponse response = req.send()) {
1✔
161
            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
1✔
162
            return new BoxAIResponse(responseJSON);
1✔
163
        }
164
    }
165

166
    /**
167
     * Get the default AI Agent use for the given mode.
168
     *
169
     * @param api  The API connection to be used by the created user.
170
     * @param mode The mode to filter the agent config to return.
171
     * @return A successful response including the default agent configuration.
172
     */
173
    public static BoxAIAgent getAiAgentDefaultConfig(BoxAPIConnection api, BoxAIAgent.Mode mode) {
NEW
174
        return getAiAgentDefaultConfig(api, mode, null, null);
×
175
    }
176

177
    /**
178
     * Get the default AI Agent use for the given mode.
179
     *
180
     * @param api      The API connection to be used by the created user.
181
     * @param mode     The mode to filter the agent config to return.
182
     * @param language The language to filter the agent config to return.
183
     * @param model    The model to filter the agent config to return.
184
     * @return A successful response including the default agent configuration.
185
     */
186
    public static BoxAIAgent getAiAgentDefaultConfig(BoxAPIConnection api,
187
                                                     BoxAIAgent.Mode mode,
188
                                                     String language,
189
                                                     String model) {
190
        QueryStringBuilder builder = new QueryStringBuilder();
1✔
191
        builder.appendParam("mode", mode.toString());
1✔
192
        if (language != null) {
1✔
193
            builder.appendParam("language", language);
1✔
194
        }
195
        if (model != null) {
1✔
196
            builder.appendParam("model", model);
1✔
197
        }
198
        URL url = AI_AGENT_DEFAULT_CONFIG_URL.buildWithQuery(api.getBaseURL(), builder.toString());
1✔
199
        BoxAPIRequest req = new BoxAPIRequest(api, url, HttpMethod.GET);
1✔
200
        try (BoxJSONResponse response = (BoxJSONResponse) req.send()) {
1✔
201
            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
1✔
202
            return BoxAIAgent.parse(responseJSON);
1✔
203
        }
204
    }
205

206
    public enum Mode {
1✔
207
        /**
208
         * Multiple items
209
         */
210
        MULTIPLE_ITEM_QA("multiple_item_qa"),
1✔
211

212
        /**
213
         * Single item
214
         */
215
        SINGLE_ITEM_QA("single_item_qa");
1✔
216

217
        private final String mode;
218

219
        Mode(String mode) {
1✔
220
            this.mode = mode;
1✔
221
        }
1✔
222

223
        static BoxAI.Mode fromJSONValue(String jsonValue) {
224
            if (jsonValue.equals("multiple_item_qa")) {
×
225
                return BoxAI.Mode.MULTIPLE_ITEM_QA;
×
226
            } else if (jsonValue.equals("single_item_qa")) {
×
227
                return BoxAI.Mode.SINGLE_ITEM_QA;
×
228
            } else {
229
                System.out.print("Invalid AI mode.");
×
230
                return null;
×
231
            }
232
        }
233

234
        String toJSONValue() {
235
            return this.mode;
×
236
        }
237

238
        public String toString() {
239
            return this.mode;
1✔
240
        }
241
    }
242
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc