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   * @param requestBody Request body of createAiAsk method
045   */
046  public AiResponseFull createAiAsk(AiAsk requestBody) {
047    return createAiAsk(requestBody, new CreateAiAskHeaders());
048  }
049
050  /**
051   * Sends an AI request to supported LLMs and returns an answer specifically focused on the user's
052   * question given the provided context.
053   *
054   * @param requestBody Request body of createAiAsk method
055   * @param headers Headers of createAiAsk method
056   */
057  public AiResponseFull createAiAsk(AiAsk requestBody, CreateAiAskHeaders headers) {
058    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
059    FetchResponse response =
060        this.networkSession
061            .getNetworkClient()
062            .fetch(
063                new FetchOptions.Builder(
064                        String.join(
065                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/ask"),
066                        "POST")
067                    .headers(headersMap)
068                    .data(JsonManager.serialize(requestBody))
069                    .contentType("application/json")
070                    .responseFormat(ResponseFormat.JSON)
071                    .auth(this.auth)
072                    .networkSession(this.networkSession)
073                    .build());
074    if (convertToString(response.getStatus()).equals("204")) {
075      return null;
076    }
077    return JsonManager.deserialize(response.getData(), AiResponseFull.class);
078  }
079
080  /**
081   * Sends an AI request to supported Large Language Models (LLMs) and returns generated text based
082   * on the provided prompt.
083   *
084   * @param requestBody Request body of createAiTextGen method
085   */
086  public AiResponse createAiTextGen(AiTextGen requestBody) {
087    return createAiTextGen(requestBody, new CreateAiTextGenHeaders());
088  }
089
090  /**
091   * Sends an AI request to supported Large Language Models (LLMs) and returns generated text based
092   * on the provided prompt.
093   *
094   * @param requestBody Request body of createAiTextGen method
095   * @param headers Headers of createAiTextGen method
096   */
097  public AiResponse createAiTextGen(AiTextGen requestBody, CreateAiTextGenHeaders headers) {
098    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
099    FetchResponse response =
100        this.networkSession
101            .getNetworkClient()
102            .fetch(
103                new FetchOptions.Builder(
104                        String.join(
105                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/text_gen"),
106                        "POST")
107                    .headers(headersMap)
108                    .data(JsonManager.serialize(requestBody))
109                    .contentType("application/json")
110                    .responseFormat(ResponseFormat.JSON)
111                    .auth(this.auth)
112                    .networkSession(this.networkSession)
113                    .build());
114    return JsonManager.deserialize(response.getData(), AiResponse.class);
115  }
116
117  /**
118   * Get the AI agent default config.
119   *
120   * @param queryParams Query parameters of getAiAgentDefaultConfig method
121   */
122  public AiAgent getAiAgentDefaultConfig(GetAiAgentDefaultConfigQueryParams queryParams) {
123    return getAiAgentDefaultConfig(queryParams, new GetAiAgentDefaultConfigHeaders());
124  }
125
126  /**
127   * Get the AI agent default config.
128   *
129   * @param queryParams Query parameters of getAiAgentDefaultConfig method
130   * @param headers Headers of getAiAgentDefaultConfig method
131   */
132  public AiAgent getAiAgentDefaultConfig(
133      GetAiAgentDefaultConfigQueryParams queryParams, GetAiAgentDefaultConfigHeaders headers) {
134    Map<String, String> queryParamsMap =
135        prepareParams(
136            mapOf(
137                entryOf("mode", convertToString(queryParams.getMode())),
138                entryOf("language", convertToString(queryParams.getLanguage())),
139                entryOf("model", convertToString(queryParams.getModel()))));
140    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
141    FetchResponse response =
142        this.networkSession
143            .getNetworkClient()
144            .fetch(
145                new FetchOptions.Builder(
146                        String.join(
147                            "",
148                            this.networkSession.getBaseUrls().getBaseUrl(),
149                            "/2.0/ai_agent_default"),
150                        "GET")
151                    .params(queryParamsMap)
152                    .headers(headersMap)
153                    .responseFormat(ResponseFormat.JSON)
154                    .auth(this.auth)
155                    .networkSession(this.networkSession)
156                    .build());
157    return JsonManager.deserialize(response.getData(), AiAgent.class);
158  }
159
160  /**
161   * Sends an AI request to supported Large Language Models (LLMs) and extracts metadata in form of
162   * key-value pairs. In this request, both the prompt and the output can be freeform. Metadata
163   * template setup before sending the request is not required.
164   *
165   * @param requestBody Request body of createAiExtract method
166   */
167  public AiResponse createAiExtract(AiExtract requestBody) {
168    return createAiExtract(requestBody, new CreateAiExtractHeaders());
169  }
170
171  /**
172   * Sends an AI request to supported Large Language Models (LLMs) and extracts metadata in form of
173   * key-value pairs. In this request, both the prompt and the output can be freeform. Metadata
174   * template setup before sending the request is not required.
175   *
176   * @param requestBody Request body of createAiExtract method
177   * @param headers Headers of createAiExtract method
178   */
179  public AiResponse createAiExtract(AiExtract requestBody, CreateAiExtractHeaders headers) {
180    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
181    FetchResponse response =
182        this.networkSession
183            .getNetworkClient()
184            .fetch(
185                new FetchOptions.Builder(
186                        String.join(
187                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/ai/extract"),
188                        "POST")
189                    .headers(headersMap)
190                    .data(JsonManager.serialize(requestBody))
191                    .contentType("application/json")
192                    .responseFormat(ResponseFormat.JSON)
193                    .auth(this.auth)
194                    .networkSession(this.networkSession)
195                    .build());
196    return JsonManager.deserialize(response.getData(), AiResponse.class);
197  }
198
199  /**
200   * Sends an AI request to supported Large Language Models (LLMs) and returns extracted metadata as
201   * a set of key-value pairs.
202   *
203   * <p>To define the extraction structure, provide either a metadata template or a list of fields.
204   * To learn more about creating templates, see [Creating metadata templates in the Admin
205   * Console](https://support.box.com/hc/en-us/articles/360044194033-Customizing-Metadata-Templates)
206   * or use the [metadata template API](https://developer.box.com/guides/metadata/templates/create).
207   *
208   * <p>This endpoint also supports [Enhanced Extract
209   * Agent](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured#enhanced-extract-agent).
210   *
211   * <p>For information about supported file formats and languages, see the [Extract metadata from
212   * file
213   * (structured)](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured)
214   * API guide.
215   *
216   * @param requestBody Request body of createAiExtractStructured method
217   */
218  public AiExtractStructuredResponse createAiExtractStructured(AiExtractStructured requestBody) {
219    return createAiExtractStructured(requestBody, new CreateAiExtractStructuredHeaders());
220  }
221
222  /**
223   * Sends an AI request to supported Large Language Models (LLMs) and returns extracted metadata as
224   * a set of key-value pairs.
225   *
226   * <p>To define the extraction structure, provide either a metadata template or a list of fields.
227   * To learn more about creating templates, see [Creating metadata templates in the Admin
228   * Console](https://support.box.com/hc/en-us/articles/360044194033-Customizing-Metadata-Templates)
229   * or use the [metadata template API](https://developer.box.com/guides/metadata/templates/create).
230   *
231   * <p>This endpoint also supports [Enhanced Extract
232   * Agent](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured#enhanced-extract-agent).
233   *
234   * <p>For information about supported file formats and languages, see the [Extract metadata from
235   * file
236   * (structured)](https://developer.box.com/guides/box-ai/ai-tutorials/extract-metadata-structured)
237   * API guide.
238   *
239   * @param requestBody Request body of createAiExtractStructured method
240   * @param headers Headers of createAiExtractStructured method
241   */
242  public AiExtractStructuredResponse createAiExtractStructured(
243      AiExtractStructured requestBody, CreateAiExtractStructuredHeaders headers) {
244    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
245    FetchResponse response =
246        this.networkSession
247            .getNetworkClient()
248            .fetch(
249                new FetchOptions.Builder(
250                        String.join(
251                            "",
252                            this.networkSession.getBaseUrls().getBaseUrl(),
253                            "/2.0/ai/extract_structured"),
254                        "POST")
255                    .headers(headersMap)
256                    .data(JsonManager.serialize(requestBody))
257                    .contentType("application/json")
258                    .responseFormat(ResponseFormat.JSON)
259                    .auth(this.auth)
260                    .networkSession(this.networkSession)
261                    .build());
262    return JsonManager.deserialize(response.getData(), AiExtractStructuredResponse.class);
263  }
264
265  public Authentication getAuth() {
266    return auth;
267  }
268
269  public NetworkSession getNetworkSession() {
270    return networkSession;
271  }
272
273  public static class Builder {
274
275    protected Authentication auth;
276
277    protected NetworkSession networkSession;
278
279    public Builder() {}
280
281    public Builder auth(Authentication auth) {
282      this.auth = auth;
283      return this;
284    }
285
286    public Builder networkSession(NetworkSession networkSession) {
287      this.networkSession = networkSession;
288      return this;
289    }
290
291    public AiManager build() {
292      if (this.networkSession == null) {
293        this.networkSession = new NetworkSession();
294      }
295      return new AiManager(this);
296    }
297  }
298}