001package com.box.sdk; 002 003import com.box.sdk.http.HttpMethod; 004import com.eclipsesource.json.Json; 005import com.eclipsesource.json.JsonArray; 006import com.eclipsesource.json.JsonObject; 007import java.net.URL; 008import java.util.List; 009 010 011public final class BoxAI { 012 013 /** 014 * Ask AI url. 015 */ 016 public static final URLTemplate SEND_AI_REQUEST_URL = new URLTemplate("ai/ask"); 017 /** 018 * Text gen AI url. 019 */ 020 public static final URLTemplate SEND_AI_TEXT_GEN_REQUEST_URL = new URLTemplate("ai/text_gen"); 021 022 private BoxAI() { 023 } 024 025 /** 026 * Sends an AI request to supported LLMs and returns an answer specifically focused 027 * on the user's question given the provided items. 028 * @param api the API connection to be used by the created user. 029 * @param prompt The prompt provided by the client to be answered by the LLM. 030 * @param items The items to be processed by the LLM, currently only files are supported. 031 * @param mode The mode specifies if this request is for a single or multiple items. 032 * @return The response from the AI. 033 */ 034 public static BoxAIResponse sendAIRequest(BoxAPIConnection api, String prompt, List<BoxAIItem> items, Mode mode) { 035 URL url = SEND_AI_REQUEST_URL.build(api.getBaseURL()); 036 JsonObject requestJSON = new JsonObject(); 037 requestJSON.add("mode", mode.toString()); 038 requestJSON.add("prompt", prompt); 039 040 JsonArray itemsJSON = new JsonArray(); 041 for (BoxAIItem item : items) { 042 itemsJSON.add(item.getJSONObject()); 043 } 044 requestJSON.add("items", itemsJSON); 045 046 BoxJSONRequest req = new BoxJSONRequest(api, url, HttpMethod.POST); 047 req.setBody(requestJSON.toString()); 048 049 try (BoxJSONResponse response = req.send()) { 050 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 051 return new BoxAIResponse(responseJSON); 052 } 053 } 054 055 /** 056 * Sends an AI request to supported LLMs and returns an answer specifically focused on the creation of new text. 057 * @param api the API connection to be used by the created user. 058 * @param prompt The prompt provided by the client to be answered by the LLM. 059 * @param items The items to be processed by the LLM, currently only files are supported. 060 * @return The response from the AI. 061 */ 062 public static BoxAIResponse sendAITextGenRequest(BoxAPIConnection api, String prompt, List<BoxAIItem> items) { 063 return sendAITextGenRequest(api, prompt, items, null); 064 } 065 066 /** 067 * Sends an AI request to supported LLMs and returns an answer specifically focused on the creation of new text. 068 * @param api the API connection to be used by the created user. 069 * @param prompt The prompt provided by the client to be answered by the LLM. 070 * @param items The items to be processed by the LLM, currently only files are supported. 071 * @param dialogueHistory The history of prompts and answers previously passed to the LLM. 072 * This provides additional context to the LLM in generating the response. 073 * @return The response from the AI. 074 */ 075 public static BoxAIResponse sendAITextGenRequest( 076 BoxAPIConnection api, String prompt, List<BoxAIItem> items, List<BoxAIDialogueEntry> dialogueHistory 077 ) { 078 URL url = SEND_AI_TEXT_GEN_REQUEST_URL.build(api.getBaseURL()); 079 JsonObject requestJSON = new JsonObject(); 080 requestJSON.add("prompt", prompt); 081 082 JsonArray itemsJSON = new JsonArray(); 083 for (BoxAIItem item : items) { 084 itemsJSON.add(item.getJSONObject()); 085 } 086 requestJSON.add("items", itemsJSON); 087 088 if (dialogueHistory != null) { 089 JsonArray dialogueHistoryJSON = new JsonArray(); 090 for (BoxAIDialogueEntry dialogueEntry : dialogueHistory) { 091 dialogueHistoryJSON.add(dialogueEntry.getJSONObject()); 092 } 093 requestJSON.add("dialogue_history", dialogueHistoryJSON); 094 } 095 096 BoxJSONRequest req = new BoxJSONRequest(api, url, HttpMethod.POST); 097 req.setBody(requestJSON.toString()); 098 099 try (BoxJSONResponse response = req.send()) { 100 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 101 return new BoxAIResponse(responseJSON); 102 } 103 } 104 105 public enum Mode { 106 /** 107 * Multiple items 108 */ 109 MULTIPLE_ITEM_QA("multiple_item_qa"), 110 111 /** 112 * Single item 113 */ 114 SINGLE_ITEM_QA("single_item_qa"); 115 116 private final String mode; 117 118 Mode(String mode) { 119 this.mode = mode; 120 } 121 122 static BoxAI.Mode fromJSONValue(String jsonValue) { 123 if (jsonValue.equals("multiple_item_qa")) { 124 return BoxAI.Mode.MULTIPLE_ITEM_QA; 125 } else if (jsonValue.equals("single_item_qa")) { 126 return BoxAI.Mode.SINGLE_ITEM_QA; 127 } else { 128 System.out.print("Invalid AI mode."); 129 return null; 130 } 131 } 132 133 String toJSONValue() { 134 return this.mode; 135 } 136 137 public String toString() { 138 return this.mode; 139 } 140 } 141}