001package com.box.sdkgen.managers.ai; 002 003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString; 004import static com.box.sdkgen.internal.utils.UtilsManager.entryOf; 005import static com.box.sdkgen.internal.utils.UtilsManager.mapOf; 006import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps; 007import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams; 008 009import com.box.sdkgen.networking.auth.Authentication; 010import com.box.sdkgen.networking.fetchoptions.FetchOptions; 011import com.box.sdkgen.networking.fetchoptions.ResponseFormat; 012import com.box.sdkgen.networking.fetchresponse.FetchResponse; 013import com.box.sdkgen.networking.network.NetworkSession; 014import com.box.sdkgen.schemas.aiagent.AiAgent; 015import com.box.sdkgen.schemas.aiask.AiAsk; 016import com.box.sdkgen.schemas.aiextract.AiExtract; 017import com.box.sdkgen.schemas.aiextractstructured.AiExtractStructured; 018import com.box.sdkgen.schemas.aiextractstructuredresponse.AiExtractStructuredResponse; 019import com.box.sdkgen.schemas.airesponse.AiResponse; 020import com.box.sdkgen.schemas.airesponsefull.AiResponseFull; 021import com.box.sdkgen.schemas.aitextgen.AiTextGen; 022import com.box.sdkgen.serialization.json.JsonManager; 023import java.util.Map; 024 025public class AiManager { 026 027 public Authentication auth; 028 029 public NetworkSession networkSession; 030 031 public AiManager() { 032 this.networkSession = new NetworkSession(); 033 } 034 035 protected AiManager(Builder builder) { 036 this.auth = builder.auth; 037 this.networkSession = builder.networkSession; 038 } 039 040 /** 041 * Sends an AI request to supported LLMs and returns an answer specifically focused on the user's 042 * question given the provided context. 043 * 044 * <p>You can ask a question about a single file, several files, or the entire contents of a Box 045 * Hub. To search across and ask questions about everything in a Box Hub, send a single item with 046 * `type` set to `hubs` and the Hub's ID as the `id`. Box AI answers the question using the 047 * indexed content of all files in that Hub. 048 * 049 * <p>Asking questions about a Box Hub requires Box AI for Hubs to be enabled in the Admin Console 050 * before the Hub is created, so that its content is indexed. 051 * 052 * @param requestBody Request body of createAiAsk method 053 */ 054 public AiResponseFull createAiAsk(AiAsk requestBody) { 055 return createAiAsk(requestBody, new CreateAiAskHeaders()); 056 } 057 058 /** 059 * Sends an AI request to supported LLMs and returns an answer specifically focused on the user's 060 * question given the provided context. 061 * 062 * <p>You can ask a question about a single file, several files, or the entire contents of a Box 063 * Hub. To search across and ask questions about everything in a Box Hub, send a single item with 064 * `type` set to `hubs` and the Hub's ID as the `id`. Box AI answers the question using the 065 * indexed content of all files in that Hub. 066 * 067 * <p>Asking questions about a Box Hub requires Box AI for Hubs to be enabled in the Admin Console 068 * before the Hub is created, so that its content is indexed. 069 * 070 * @param requestBody Request body of createAiAsk method 071 * @param headers Headers of createAiAsk method 072 */ 073 public AiResponseFull createAiAsk(AiAsk requestBody, CreateAiAskHeaders headers) { 074 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 075 FetchResponse response = 076 this.networkSession 077 .getNetworkClient() 078 .fetch( 079 new FetchOptions.Builder( 080 String.join( 081 "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/ask"), 082 "POST") 083 .headers(headersMap) 084 .data(JsonManager.serialize(requestBody)) 085 .contentType("application/json") 086 .responseFormat(ResponseFormat.JSON) 087 .auth(this.auth) 088 .networkSession(this.networkSession) 089 .build()); 090 if (convertToString(response.getStatus()).equals("204")) { 091 return null; 092 } 093 return JsonManager.deserialize(response.getData(), AiResponseFull.class); 094 } 095 096 /** 097 * Sends an AI request to supported Large Language Models (LLMs) and returns generated text based 098 * on the provided prompt. 099 * 100 * @param requestBody Request body of createAiTextGen method 101 */ 102 public AiResponse createAiTextGen(AiTextGen requestBody) { 103 return createAiTextGen(requestBody, new CreateAiTextGenHeaders()); 104 } 105 106 /** 107 * Sends an AI request to supported Large Language Models (LLMs) and returns generated text based 108 * on the provided prompt. 109 * 110 * @param requestBody Request body of createAiTextGen method 111 * @param headers Headers of createAiTextGen method 112 */ 113 public AiResponse createAiTextGen(AiTextGen requestBody, CreateAiTextGenHeaders headers) { 114 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 115 FetchResponse response = 116 this.networkSession 117 .getNetworkClient() 118 .fetch( 119 new FetchOptions.Builder( 120 String.join( 121 "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/text_gen"), 122 "POST") 123 .headers(headersMap) 124 .data(JsonManager.serialize(requestBody)) 125 .contentType("application/json") 126 .responseFormat(ResponseFormat.JSON) 127 .auth(this.auth) 128 .networkSession(this.networkSession) 129 .build()); 130 return JsonManager.deserialize(response.getData(), AiResponse.class); 131 } 132 133 /** 134 * Get the AI agent default config. 135 * 136 * @param queryParams Query parameters of getAiAgentDefaultConfig method 137 */ 138 public AiAgent getAiAgentDefaultConfig(GetAiAgentDefaultConfigQueryParams queryParams) { 139 return getAiAgentDefaultConfig(queryParams, new GetAiAgentDefaultConfigHeaders()); 140 } 141 142 /** 143 * Get the AI agent default config. 144 * 145 * @param queryParams Query parameters of getAiAgentDefaultConfig method 146 * @param headers Headers of getAiAgentDefaultConfig method 147 */ 148 public AiAgent getAiAgentDefaultConfig( 149 GetAiAgentDefaultConfigQueryParams queryParams, GetAiAgentDefaultConfigHeaders headers) { 150 Map<String, String> queryParamsMap = 151 prepareParams( 152 mapOf( 153 entryOf("mode", convertToString(queryParams.getMode())), 154 entryOf("language", convertToString(queryParams.getLanguage())), 155 entryOf("model", convertToString(queryParams.getModel())))); 156 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 157 FetchResponse response = 158 this.networkSession 159 .getNetworkClient() 160 .fetch( 161 new FetchOptions.Builder( 162 String.join( 163 "", 164 this.networkSession.getBaseUrls().getBaseUrl(), 165 "/2.0/ai_agent_default"), 166 "GET") 167 .params(queryParamsMap) 168 .headers(headersMap) 169 .responseFormat(ResponseFormat.JSON) 170 .auth(this.auth) 171 .networkSession(this.networkSession) 172 .build()); 173 return JsonManager.deserialize(response.getData(), AiAgent.class); 174 } 175 176 /** 177 * Sends an AI request to supported Large Language Models (LLMs) and extracts metadata in form of 178 * key-value pairs. In this request, both the prompt and the output can be freeform. Metadata 179 * template setup before sending the request is not required. 180 * 181 * @param requestBody Request body of createAiExtract method 182 */ 183 public AiResponse createAiExtract(AiExtract requestBody) { 184 return createAiExtract(requestBody, new CreateAiExtractHeaders()); 185 } 186 187 /** 188 * Sends an AI request to supported Large Language Models (LLMs) and extracts metadata in form of 189 * key-value pairs. In this request, both the prompt and the output can be freeform. Metadata 190 * template setup before sending the request is not required. 191 * 192 * @param requestBody Request body of createAiExtract method 193 * @param headers Headers of createAiExtract method 194 */ 195 public AiResponse createAiExtract(AiExtract requestBody, CreateAiExtractHeaders headers) { 196 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 197 FetchResponse response = 198 this.networkSession 199 .getNetworkClient() 200 .fetch( 201 new FetchOptions.Builder( 202 String.join( 203 "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/extract"), 204 "POST") 205 .headers(headersMap) 206 .data(JsonManager.serialize(requestBody)) 207 .contentType("application/json") 208 .responseFormat(ResponseFormat.JSON) 209 .auth(this.auth) 210 .networkSession(this.networkSession) 211 .build()); 212 return JsonManager.deserialize(response.getData(), AiResponse.class); 213 } 214 215 /** 216 * Sends an AI request to supported Large Language Models (LLMs) and returns extracted metadata as 217 * a set of key-value pairs. 218 * 219 * <p>To define the extraction structure, provide either a metadata template or a list of fields. 220 * To learn more about creating templates, see [Creating metadata templates in the Admin 221 * Console](https://support.box.com/hc/en-us/articles/360044194033-Customizing-Metadata-Templates) 222 * or use the [metadata template API](https://developer.box.com/guides/metadata/templates/create). 223 * 224 * <p>This endpoint also supports [Enhanced Extract 225 * Agent](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured#enhanced-extract-agent). 226 * 227 * <p>For information about supported file formats and languages, see the [Extract metadata from 228 * file 229 * (structured)](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured) 230 * API guide. 231 * 232 * @param requestBody Request body of createAiExtractStructured method 233 */ 234 public AiExtractStructuredResponse createAiExtractStructured(AiExtractStructured requestBody) { 235 return createAiExtractStructured(requestBody, new CreateAiExtractStructuredHeaders()); 236 } 237 238 /** 239 * Sends an AI request to supported Large Language Models (LLMs) and returns extracted metadata as 240 * a set of key-value pairs. 241 * 242 * <p>To define the extraction structure, provide either a metadata template or a list of fields. 243 * To learn more about creating templates, see [Creating metadata templates in the Admin 244 * Console](https://support.box.com/hc/en-us/articles/360044194033-Customizing-Metadata-Templates) 245 * or use the [metadata template API](https://developer.box.com/guides/metadata/templates/create). 246 * 247 * <p>This endpoint also supports [Enhanced Extract 248 * Agent](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured#enhanced-extract-agent). 249 * 250 * <p>For information about supported file formats and languages, see the [Extract metadata from 251 * file 252 * (structured)](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured) 253 * API guide. 254 * 255 * @param requestBody Request body of createAiExtractStructured method 256 * @param headers Headers of createAiExtractStructured method 257 */ 258 public AiExtractStructuredResponse createAiExtractStructured( 259 AiExtractStructured requestBody, CreateAiExtractStructuredHeaders headers) { 260 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 261 FetchResponse response = 262 this.networkSession 263 .getNetworkClient() 264 .fetch( 265 new FetchOptions.Builder( 266 String.join( 267 "", 268 this.networkSession.getBaseUrls().getBaseUrl(), 269 "/2.0/ai/extract_structured"), 270 "POST") 271 .headers(headersMap) 272 .data(JsonManager.serialize(requestBody)) 273 .contentType("application/json") 274 .responseFormat(ResponseFormat.JSON) 275 .auth(this.auth) 276 .networkSession(this.networkSession) 277 .build()); 278 return JsonManager.deserialize(response.getData(), AiExtractStructuredResponse.class); 279 } 280 281 public Authentication getAuth() { 282 return auth; 283 } 284 285 public NetworkSession getNetworkSession() { 286 return networkSession; 287 } 288 289 public static class Builder { 290 291 protected Authentication auth; 292 293 protected NetworkSession networkSession; 294 295 public Builder() {} 296 297 public Builder auth(Authentication auth) { 298 this.auth = auth; 299 return this; 300 } 301 302 public Builder networkSession(NetworkSession networkSession) { 303 this.networkSession = networkSession; 304 return this; 305 } 306 307 public AiManager build() { 308 if (this.networkSession == null) { 309 this.networkSession = new NetworkSession(); 310 } 311 return new AiManager(this); 312 } 313 } 314}