Skip to content

Commit

Permalink
add ai prompt fine-tuning methods
Browse files Browse the repository at this point in the history
  • Loading branch information
timiredmind committed Oct 15, 2024
1 parent 2101227 commit b6a57c8
Show file tree
Hide file tree
Showing 24 changed files with 850 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/crowdin/client/Client.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.crowdin.client;

import com.crowdin.client.ai.AIApi;
import com.crowdin.client.applications.ApplicationsApi;
import com.crowdin.client.branches.BranchesApi;
import com.crowdin.client.bundles.BundlesApi;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class Client extends CrowdinApi {
private final ApplicationsApi applicationsApi;
private final ClientsApi clientsApi;
private final BranchesApi branchesApi;
private final AIApi aiApi;

public Client(Credentials credentials) {
super(credentials);
Expand Down Expand Up @@ -107,6 +109,7 @@ public Client(Credentials credentials) {
this.applicationsApi = new ApplicationsApi(credentials);
this.clientsApi = new ClientsApi(credentials);
this.branchesApi = new BranchesApi(credentials);
this.aiApi = new AIApi(credentials);
}

public Client(Credentials credentials, ClientConfig clientConfig) {
Expand Down Expand Up @@ -143,6 +146,7 @@ public Client(Credentials credentials, ClientConfig clientConfig) {
this.applicationsApi = new ApplicationsApi(credentials, clientConfig);
this.clientsApi = new ClientsApi(credentials, clientConfig);
this.branchesApi = new BranchesApi(credentials, clientConfig);
this.aiApi = new AIApi(credentials, clientConfig);
}

}
150 changes: 150 additions & 0 deletions src/main/java/com/crowdin/client/ai/AIApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.crowdin.client.ai;

import com.crowdin.client.ai.model.FineTuningDatasetDownload;
import com.crowdin.client.ai.model.FineTuningDatasetDownloadResponse;
import com.crowdin.client.ai.model.FineTuningDatasetRequest;
import com.crowdin.client.ai.model.FineTuningDatasetResponse;
import com.crowdin.client.ai.model.FineTuningDatasetResponse.FineTuningDatasetData;
import com.crowdin.client.ai.model.FineTuningEvent;
import com.crowdin.client.ai.model.FineTuningEventResponseList;
import com.crowdin.client.ai.model.FineTuningJob;
import com.crowdin.client.ai.model.FineTuningJobRequest;
import com.crowdin.client.ai.model.FineTuningJobResponseList;
import com.crowdin.client.ai.model.FineTuningJobResponseObject;
import com.crowdin.client.core.CrowdinApi;
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.ResponseList;
import com.crowdin.client.core.model.ResponseObject;

public class AIApi extends CrowdinApi {

public AIApi(Credentials credentials) {
super(credentials);
}

public AIApi(Credentials credentials, ClientConfig clientConfig) {
super(credentials, clientConfig);
}

/**
* @param userId user identifier
* @param aiPromptId AI prompt identifier
* @param jobIdentifier AI prompt fine-tuning dataset generation identifier, consists of 36 characters
* @return dataset generation status
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.users.ai.prompts.fine-tuning.datasets.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.prompts.fine-tuning.datasets.get" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
* */

public ResponseObject<FineTuningDatasetData> getFineTuningDatasetGenerationStatus(
final long userId, final long aiPromptId, final String jobIdentifier) {
String url = String.format("%s/users/%d/ai/prompts/%d/fine-tuning/datasets/%s", this.url, userId, aiPromptId, jobIdentifier);
FineTuningDatasetResponse response = this.httpClient.get(url, new HttpRequestConfig(), FineTuningDatasetResponse.class);
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param aiPromptId AI prompt identifier
* @param request request body
* @return fine-tuning dataset
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.datasets.post" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.datasets.post" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
* */
public ResponseObject<FineTuningDatasetData> generateFineTuningDataset(
final long userId, final long aiPromptId, final FineTuningDatasetRequest request) {
String url = String.format("%s/users/%d/ai/prompts/%d/fine-tuning/datasets", this.url, userId, aiPromptId);
FineTuningDatasetResponse response = this.httpClient.post(url, request, new HttpRequestConfig(), FineTuningDatasetResponse.class);
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param aiPromptId AI prompt identifier
* @param jobIdentifier AI prompt fine-tuning dataset generation identifier, consists of 36 characters
* @return fine-tuning event list
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.events.getMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.events.getMany" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
* */

public ResponseList<FineTuningEvent> getFineTuningEventList(
final long userId, final long aiPromptId, final String jobIdentifier) {
String url = String.format("%s/users/%d/ai/prompts/%d/fine-tuning/jobs/%s/events", this.url, userId, aiPromptId, jobIdentifier);
FineTuningEventResponseList responseList = this.httpClient.get(url, new HttpRequestConfig(), FineTuningEventResponseList.class);
return FineTuningEventResponseList.to(responseList);
}

/**
* @param userId user identifier
* @return fine-tuning job list
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.getMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.getMany" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
* */

public ResponseList<FineTuningJob> getFineTuningJobList(final long userId) {
String url = String.format("%s/users/%d/ai/prompts/fine-tuning/jobs", this.url, userId);
FineTuningJobResponseList responseList = this.httpClient.get(url, new HttpRequestConfig(), FineTuningJobResponseList.class);
return FineTuningJobResponseList.to(responseList);
}
/**
* @param userId user identifier
* @param aiPromptId AI prompt identifier
* @param request request body
* @return AI Prompt Fine-Tuning Dataset Job
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.post" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.post" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
* */

public ResponseObject<FineTuningJob> createFineTuningJob(final long userId, final long aiPromptId, final FineTuningJobRequest request) {
String url = String.format("%s/users/%d/ai/prompts/%d/fine-tuning/jobs", this.url, userId, aiPromptId);
FineTuningJobResponseObject responseObject = this.httpClient.post(url, request, new HttpRequestConfig(), FineTuningJobResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

/**
* @param userId user identifier
* @param aiPromptId AI prompt identifier
* @param jobIdentifier AI prompt fine-tuning dataset generation identifier, consists of 36 characters
* @return AI Prompt Fine-Tuning Dataset Job Status
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.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.prompts.fine-tuning.jobs.get" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
* */

public ResponseObject<FineTuningJob> getFineTuningJobStatus(final long userId, final long aiPromptId, final String jobIdentifier) {
String url = String.format("%s/users/%d/ai/prompts/%d/fine-tuning/jobs/%s", this.url, userId, aiPromptId, jobIdentifier);
FineTuningJobResponseObject responseObject = this.httpClient.get(url, new HttpRequestConfig(), FineTuningJobResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

/**
* @param userId user identifier
* @param aiPromptId AI prompt identifier
* @param jobIdentifier AI prompt fine-tuning dataset generation identifier, consists of 36 characters
* @return AI Prompt Fine-Tuning Dataset Download URL
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/api/v2/#tag/AI/operation/api.ai.prompts.fine-tuning.jobs.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.prompts.fine-tuning.jobs.get" target="_blank"><b>Enterprise API Documentation</b></li>
* </ul>
* */

public ResponseObject<FineTuningDatasetDownload> downloadFineTuningDatasetDownload(final long userId, final long aiPromptId, final String jobIdentifier) {
String url = String.format("%s/users/%d/ai/prompts/%d/fine-tuning/datasets/%s/download", this.url, userId, aiPromptId, jobIdentifier);
FineTuningDatasetDownloadResponse response = this.httpClient.get(url, new HttpRequestConfig(), FineTuningDatasetDownloadResponse.class);
return ResponseObject.of(response.getData());
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.crowdin.client.ai.model;

import java.util.Date;
import lombok.Data;

@Data
public class FineTuningDatasetDownload {
private String url;
private Date expireIn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crowdin.client.ai.model;

import java.util.Date;
import lombok.Data;

@Data
public class FineTuningDatasetDownloadResponse {
private FineTuningDatasetDownload data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.crowdin.client.ai.model;

import java.util.Date;
import java.util.List;
import lombok.Data;

@Data
public class FineTuningDatasetRequest {
private List<Long> projectIds;
private List<Long> tmIds;
private String purpose;
private Date dateFrom;
private Date dateTo;
private Long maxFileSize;
private Long minExamplesCount;
private Long maxExamplesCount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.crowdin.client.ai.model;

import java.util.Date;
import java.util.List;
import lombok.Data;

@Data
public class FineTuningDatasetResponse {
private FineTuningDatasetData data;

@Data
public static class FineTuningDatasetData {
private String identifier;
private String status;
private Long progress;
private DatasetAttributes attributes;
private Date createdAt;
private Date updatedAt;
private Date startedAt;
}

@Data
public static class DatasetAttributes {
private List<Long> projectIds;
private List<Long> tmIds;
private String purpose;
private Date dateFrom;
private Date dateTo;
private Long maxFileSize;
private Long minExamplesCount;
private Long maxExamplesCount;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/crowdin/client/ai/model/FineTuningEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.crowdin.client.ai.model;

import java.util.Date;
import lombok.Data;

@Data
public class FineTuningEvent {
private String id;
private String type;
private String message;
private FineTuningEventData data;
private Date createdAt;

@Data
public static class FineTuningEventData {
private Long step;
private Long totalSteps;
private Double trainingLoss;
private Double validationLoss;
private Double fullValidationLoss;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.crowdin.client.ai.model;

import com.crowdin.client.core.model.Pagination;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;

@Data
public class FineTuningEventResponseList {
private List<FineTuningEventResponseObject> data;
private Pagination pagination;

public static ResponseList<FineTuningEvent> to(FineTuningEventResponseList eventResponseList) {
return ResponseList.of(
eventResponseList.getData()
.stream()
.map(FineTuningEventResponseObject::getData)
.map(ResponseObject::of).collect(Collectors.toList()),
eventResponseList.getPagination()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.crowdin.client.ai.model;

import lombok.Data;

@Data
public class FineTuningEventResponseObject {
private FineTuningEvent data;
}
56 changes: 56 additions & 0 deletions src/main/java/com/crowdin/client/ai/model/FineTuningJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.crowdin.client.ai.model;

import java.util.Date;
import java.util.List;
import lombok.Data;

@Data
public class FineTuningJob {
private String identifier;
private String status;
private Long progress;
private JobAttribute attributes;
private Date createdAt;
private Date updatedAt;
private Date startedAt;
private Date finishedAt;

@Data
public static class JobAttribute {
private Boolean dryRun;
private Long aiPromptId;
private Hyperparameters hyperparameters;
private Options trainignOptions;
private Options validationOptions;
private String baseModel;
private String fineTunedModel;
private Long trainedTokensCount;
private String trainingDatasetUrl;
private String validationDatasetUrl;
private Metadata metadata;
}

@Data
public static class Hyperparameters {
private Long batchSize;
private Long learningRateMultiplier;
private Long nEpochs;
}

@Data
public static class Options {
private List<Long> projectIds;
private List<Long> tmIds;
private Date dateFrom;
private Date dateTo;
private Long maxFileSize;
private Long minExamplesCount;
private Long maxExamplesCount;
}

@Data
public static class Metadata {
private Double cost;
private String costCurrency;
}
}
Loading

0 comments on commit b6a57c8

Please sign in to comment.