Skip to content

Commit

Permalink
feat (dotAI): support structured output for chatGTP (#31327)
Browse files Browse the repository at this point in the history
Includes support for "response_format" in the request to dotAI
completions
  • Loading branch information
dsilvam authored Feb 14, 2025
1 parent 8a67ede commit 4550215
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions dotCMS/src/main/java/com/dotcms/ai/AiKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class AiKeys {
public static final String LIMIT = "limit";
public static final String COUNT = "count";
public static final String INPUT = "input";
public static final String RESPONSE_FORMAT = "response_format";

private AiKeys() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ private JSONObject buildRequestJson(final CompletionsForm form, final List<Embed
json.put(AiKeys.MODEL, modelTuple._2.getName());
}

if (UtilMethods.isSet(form.responseFormat)) {
json.put(AiKeys.RESPONSE_FORMAT, form.responseFormat);
}

json.put(AiKeys.MAX_TOKENS, form.responseLengthTokens);

return json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public <T extends Serializable> void sendRequest(final AIRequest<T> request, fin
throw new DotAIModelNotOperationalException(String.format("Model [%s] is not operational", modelName));
}


final long sleep = lastRestCall.computeIfAbsent(aiModel, m -> 0L)
+ aiModel.minIntervalBetweenCalls()
- System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class CompletionsForm {
public final String operator;
public final String site;
public final User user;
public final Map<String, Object> responseFormat;

@Override
public boolean equals(final Object o) {
Expand Down Expand Up @@ -122,6 +123,7 @@ private CompletionsForm(final Builder builder) {
}
this.model = UtilMethods.isSet(builder.model) ? builder.model : ConfigService.INSTANCE.config().getModel().getCurrentModel();
this.user = builder.user;
this.responseFormat = builder.responseFormat;
}

private String validateBuilderQuery(final String query) {
Expand Down Expand Up @@ -153,7 +155,8 @@ public static Builder copy(final CompletionsForm form) {
.indexName(form.indexName)
.threshold(form.threshold)
.stream(form.stream)
.user(form.user);
.user(form.user)
.responseFormat(form.responseFormat);
}

public static final class Builder {
Expand Down Expand Up @@ -188,6 +191,8 @@ public static final class Builder {
private String site;
@JsonSetter(nulls = Nulls.SKIP)
private User user;
@JsonSetter(nulls = Nulls.SKIP)
private Map<String, Object> responseFormat;

public Builder prompt(String queryOrPrompt) {
this.prompt = queryOrPrompt;
Expand Down Expand Up @@ -269,6 +274,11 @@ public Builder user(User user) {
return this;
}

public Builder responseFormat(Map<String, Object> responseFormat) {
this.responseFormat = responseFormat;
return this;
}

public CompletionsForm build() {
return new CompletionsForm(this);
}
Expand Down

0 comments on commit 4550215

Please sign in to comment.