Skip to content

Commit

Permalink
feat: Add AI Setting Methods (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
vedantnd111 authored Oct 21, 2024
1 parent 5676d7b commit b818b6a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/main/java/com/crowdin/client/ai/AIApi.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.crowdin.client.ai;

import com.crowdin.client.ai.model.AiSettingResponse;
import com.crowdin.client.ai.model.AiSetting;
import com.crowdin.client.ai.model.FineTuningDatasetDownload;
import com.crowdin.client.ai.model.FineTuningDatasetDownloadResponse;
import com.crowdin.client.ai.model.FineTuningDatasetRequest;
Expand All @@ -15,8 +17,11 @@
import com.crowdin.client.core.http.HttpRequestConfig;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.core.model.PatchRequest;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;

import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -150,6 +155,33 @@ public ResponseObject<FineTuningDatasetDownload> downloadFineTuningDataset(final
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @return AI settings
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.users.ai.settings.get" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/AI/operation/api.ai.settings.get" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
*/
public ResponseObject<AiSetting> getAiSetting(Long userId) {
String url = getAIPath(userId, "ai/settings");
AiSettingResponse aiSettingResponse = this.httpClient.get(url, new HttpRequestConfig(), AiSettingResponse.class);
return ResponseObject.of(aiSettingResponse.getData());
}

/**
* @param userId user identifier
* @return updated AI settings
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.users.ai.settings.patch" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/AI/operation/api.ai.settings.patch" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<AiSetting> editAiSetting(Long userId, List<PatchRequest> request) {
String url = getAIPath(userId, "ai/settings");
AiSettingResponse aiSettingResponse = this.httpClient.patch(url, request, new HttpRequestConfig(), AiSettingResponse.class);
return ResponseObject.of(aiSettingResponse.getData());
}

private String getAIPath(Long userId, String path) {
return userId != null ? String.format("%s/users/%d/%s", this.url, userId, path) : String.format("%s/%s", this.url, path);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/crowdin/client/ai/model/AiSetting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.crowdin.client.ai.model;

import lombok.Data;

import java.util.List;

@Data
public class AiSetting {

private Integer assistActionAiPromptId;
private Integer editorSuggestionAiPromptId;

@Data
private static class ShortCut {
private String name;
private String prompt;
private Boolean isEnabled;
}

private List<ShortCut> shortcuts;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crowdin.client.ai.model;

import lombok.Data;

@Data
public class AiSettingResponse {

private AiSetting data;
}
30 changes: 29 additions & 1 deletion src/test/java/com/crowdin/client/ai/AIApiTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.crowdin.client.ai;

import com.crowdin.client.ai.model.AiSetting;
import com.crowdin.client.ai.model.FineTuningDatasetDownload;
import com.crowdin.client.ai.model.FineTuningDatasetRequest;
import com.crowdin.client.ai.model.FineTuningDatasetResponse.FineTuningDatasetData;
Expand All @@ -8,6 +9,8 @@
import com.crowdin.client.ai.model.FineTuningJobRequest;
import com.crowdin.client.ai.model.FineTuningJobRequest.Hyperparameters;
import com.crowdin.client.core.model.Pagination;
import com.crowdin.client.core.model.PatchRequest;
import com.crowdin.client.core.model.PatchOperation;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
Expand All @@ -20,6 +23,7 @@
import java.util.List;
import java.util.TimeZone;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -53,6 +57,7 @@ public class AIApiTest extends TestClient {
private static final String CREATE_FINE_TUNING_JOB_PATH = "%s/users/%d/ai/prompts/%d/fine-tuning/jobs";
private static final String GET_FINE_TUNING_JOB_STATUS_PATH = "%s/users/%d/ai/prompts/%d/fine-tuning/jobs/%s";
private static final String FINE_TUNING_DATASET_DOWNLOAD_PATH = "%s/users/%d/ai/prompts/%d/fine-tuning/datasets/%s/download";
private static final String GET_SETTINGS = "%s/users/%d/ai/settings";

private Date getDateTime(int year, int month, int date, int hour, int minute, int second) {
calendar.set(year, month, date, hour, minute, second);
Expand All @@ -68,7 +73,9 @@ public List<RequestMock> getMocks() {
RequestMock.build(String.format(GET_FINE_TUNING_JOB_LIST_PATH, this.url, userId), HttpGet.METHOD_NAME, "api/ai/fineTuningJobListResponse.json"),
RequestMock.build(String.format(CREATE_FINE_TUNING_JOB_PATH, this.url, userId, aiPromptId), HttpPost.METHOD_NAME, "api/ai/fineTuningJobRequest.json", "api/ai/fineTuningJobResponse.json"),
RequestMock.build(String.format(GET_FINE_TUNING_JOB_STATUS_PATH, this.url, userId, aiPromptId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/fineTuningJobStatusResponse.json"),
RequestMock.build(String.format(FINE_TUNING_DATASET_DOWNLOAD_PATH, this.url, userId, aiPromptId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadFineTuningDataset.json")
RequestMock.build(String.format(FINE_TUNING_DATASET_DOWNLOAD_PATH, this.url, userId, aiPromptId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadFineTuningDataset.json"),
RequestMock.build(String.format(GET_SETTINGS, this.url, userId), HttpGet.METHOD_NAME, "api/ai/getAiSettingResponse.json"),
RequestMock.build(String.format(GET_SETTINGS, this.url, userId), HttpPatch.METHOD_NAME, "api/ai/editAiSettingRequest.json", "api/ai/getAiSettingResponse.json")
);
}

Expand Down Expand Up @@ -175,4 +182,25 @@ public void downloadFineTuningDatasetTest() {
assertEquals(responseObject.getData().getExpireIn(), dateCreated);
assertNotNull(responseObject.getData().getUrl());
}

@Test
public void getAiSettingTest() {
AiSetting aiSetting = this.getAiApi().getAiSetting(userId).getData();
assertNotNull(aiSetting);
assertEquals(aiSetting.getAssistActionAiPromptId(), 2);
assertEquals(aiSetting.getEditorSuggestionAiPromptId(), 5);
assertEquals(aiSetting.getShortcuts().size(), 1);
}

@Test
public void editAiSettingTest() {
PatchRequest request = new PatchRequest();
request.setOp(PatchOperation.REPLACE);
request.setPath("/assistActionAiPromptId");
ResponseObject<AiSetting> aiSettingResponseObject =
this.getAiApi().editAiSetting(userId, Collections.singletonList(request));
assertNotNull(aiSettingResponseObject.getData());
assertEquals(aiSettingResponseObject.getData().getAssistActionAiPromptId(), 2);
assertEquals(aiSettingResponseObject.getData().getEditorSuggestionAiPromptId(), 5);
}
}
6 changes: 6 additions & 0 deletions src/test/resources/api/ai/editAiSettingRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"op": "replace",
"path": "/assistActionAiPromptId"
}
]
13 changes: 13 additions & 0 deletions src/test/resources/api/ai/getAiSettingResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data": {
"assistActionAiPromptId": 2,
"editorSuggestionAiPromptId": 5,
"shortcuts": [
{
"name": "My first shortcut",
"prompt": "Make translation shorter",
"enabled": true
}
]
}
}

0 comments on commit b818b6a

Please sign in to comment.