Skip to content

[Enhancement] - Enable passing of requestMetadata to the Bedrock Converse API. #4110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class BedrockChatOptions implements ToolCallingChatOptions {
@JsonProperty("presencePenalty")
private Double presencePenalty;

@JsonIgnore
private Map<String, String> requestParameters = new HashMap<>();

@JsonProperty("stopSequences")
private List<String> stopSequences;

Expand Down Expand Up @@ -87,6 +90,7 @@ public static BedrockChatOptions fromOptions(BedrockChatOptions fromOptions) {
.frequencyPenalty(fromOptions.getFrequencyPenalty())
.maxTokens(fromOptions.getMaxTokens())
.presencePenalty(fromOptions.getPresencePenalty())
.requestParameters(new HashMap<>(fromOptions.getRequestParameters()))
.stopSequences(
fromOptions.getStopSequences() != null ? new ArrayList<>(fromOptions.getStopSequences()) : null)
.temperature(fromOptions.getTemperature())
Expand Down Expand Up @@ -126,6 +130,12 @@ public void setMaxTokens(Integer maxTokens) {
this.maxTokens = maxTokens;
}

public Map<String, String> getRequestParameters() { return this.requestParameters; }

public void setRequestParameters(Map<String, String> requestParameters) {
this.requestParameters = requestParameters;
}

@Override
public Double getPresencePenalty() {
return this.presencePenalty;
Expand Down Expand Up @@ -241,6 +251,7 @@ public boolean equals(Object o) {
return Objects.equals(this.model, that.model) && Objects.equals(this.frequencyPenalty, that.frequencyPenalty)
&& Objects.equals(this.maxTokens, that.maxTokens)
&& Objects.equals(this.presencePenalty, that.presencePenalty)
&& Objects.equals(this.requestParameters, that.requestParameters)
&& Objects.equals(this.stopSequences, that.stopSequences)
&& Objects.equals(this.temperature, that.temperature) && Objects.equals(this.topK, that.topK)
&& Objects.equals(this.topP, that.topP) && Objects.equals(this.toolCallbacks, that.toolCallbacks)
Expand All @@ -250,8 +261,9 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(this.model, this.frequencyPenalty, this.maxTokens, this.presencePenalty, this.stopSequences,
this.temperature, this.topK, this.topP, this.toolCallbacks, this.toolNames, this.toolContext,
return Objects.hash(this.model, this.frequencyPenalty, this.maxTokens, this.presencePenalty,
this.requestParameters, this.stopSequences, this.temperature, this.topK, this.topP,
this.toolCallbacks, this.toolNames, this.toolContext,
this.internalToolExecutionEnabled);
}

Expand Down Expand Up @@ -279,6 +291,11 @@ public Builder presencePenalty(Double presencePenalty) {
return this;
}

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

public Builder stopSequences(List<String> stopSequences) {
this.options.stopSequences = stopSequences;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,16 @@ else if (message.getMessageType() == MessageType.TOOL) {
Document additionalModelRequestFields = ConverseApiUtils
.getChatOptionsAdditionalModelRequestFields(this.defaultOptions, prompt.getOptions());

Map<String, String> requestMetadata = ConverseApiUtils.getRequestMetadata(prompt.getUserMessage().getMetadata());

return ConverseRequest.builder()
.modelId(updatedRuntimeOptions.getModel())
.inferenceConfig(inferenceConfiguration)
.messages(instructionMessages)
.system(systemMessages)
.additionalModelRequestFields(additionalModelRequestFields)
.toolConfig(toolConfiguration)
.requestMetadata(requestMetadata)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,26 @@ else if (value instanceof Map mapValue) {
}
}

@SuppressWarnings("unchecked")
public static Map<String, String> getRequestMetadata(Map<String, Object> metadata) {

if (metadata.isEmpty()) {
return Map.of();
}

Map<String, String> result = new HashMap<>();
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();

if (key != null && value != null) {
result.put(key, value.toString());
}
}

return result;
}

private static Document convertMapToDocument(Map<String, Object> value) {
Map<String, Document> attr = value.entrySet()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ void testBuilderWithAllFields() {
.frequencyPenalty(0.0)
.maxTokens(100)
.presencePenalty(0.0)
.requestParameters(Map.of("requestId", "1234"))
.stopSequences(List.of("stop1", "stop2"))
.temperature(0.7)
.topP(0.8)
.topK(50)
.build();

assertThat(options)
.extracting("model", "frequencyPenalty", "maxTokens", "presencePenalty", "stopSequences", "temperature",
"topP", "topK")
.containsExactly("test-model", 0.0, 100, 0.0, List.of("stop1", "stop2"), 0.7, 0.8, 50);
.extracting("model", "frequencyPenalty", "maxTokens", "presencePenalty", "requestParameters",
"stopSequences", "temperature", "topP", "topK")
.containsExactly("test-model", 0.0, 100, 0.0, Map.of("requestId", "1234"), List.of("stop1", "stop2"), 0.7, 0.8, 50);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ void call() {
.system(s -> s.text(this.systemTextResource)
.param("name", "Bob")
.param("voice", "pirate"))
.user("Tell me about 3 famous pirates from the Golden Age of Piracy and what they did")
.user(u -> u.text("Tell me about 3 famous pirates from the Golden Age of Piracy and what they did")
.param("requestId", "1234")
)
.call()
.chatResponse();
// @formatter:on
Expand Down