Skip to content

Commit

Permalink
apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyuan-he committed Dec 11, 2024
1 parent da8e4fa commit 1c4fe0e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.awssdk.v1_11;

import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

public class BedrockJsonParser {

Expand Down Expand Up @@ -46,7 +51,9 @@ private String readString() {
StringBuilder result = new StringBuilder();
while (currentChar() != '"') {
// Handle escaped quotes within the string
if (currentChar() == '\\' && position + 1 < json.length() && json.charAt(position + 1) == '"') {
if (currentChar() == '\\'
&& position + 1 < json.length()
&& json.charAt(position + 1) == '"') {
result.append('"');
position += 2;
} else {
Expand Down Expand Up @@ -88,7 +95,8 @@ private Number readScopedNumber() {
int start = position;

// Consume digits and the optional decimal point
while (position < json.length() && (Character.isDigit(json.charAt(position)) || json.charAt(position) == '.')) {
while (position < json.length()
&& (Character.isDigit(json.charAt(position)) || json.charAt(position) == '.')) {
position++;
}

Expand All @@ -97,7 +105,8 @@ private Number readScopedNumber() {
if (number.contains(".")) {
double value = Double.parseDouble(number);
if (value < 0.0 || value > 1.0) {
throw new IllegalArgumentException("Value out of bounds for Bedrock Floating Point Attribute: " + number);
throw new IllegalArgumentException(
"Value out of bounds for Bedrock Floating Point Attribute: " + number);
}
return value;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ private static String approximateTokenCount(Map<String, Object> jsonBody, String
static String getMaxTokens(Object target) {
Map<String, Object> jsonBody = getJsonBody(target);
return findFirstMatchingPath(
jsonBody,
"/max_tokens",
"/max_gen_len",
"/textGenerationConfig/maxTokenCount"
);
jsonBody, "/max_tokens", "/max_gen_len", "/textGenerationConfig/maxTokenCount");
}

// Model -> Path Mapping:
Expand All @@ -124,11 +120,7 @@ static String getMaxTokens(Object target) {
@Nullable
static String getTemperature(Object target) {
Map<String, Object> jsonBody = getJsonBody(target);
return findFirstMatchingPath(
jsonBody,
"/temperature",
"/textGenerationConfig/temperature"
);
return findFirstMatchingPath(jsonBody, "/temperature", "/textGenerationConfig/temperature");
}

// Model -> Path Mapping:
Expand All @@ -142,12 +134,7 @@ static String getTemperature(Object target) {
@Nullable
static String getTopP(Object target) {
Map<String, Object> jsonBody = getJsonBody(target);
return findFirstMatchingPath(
jsonBody,
"/top_p",
"/p",
"/textGenerationConfig/topP"
);
return findFirstMatchingPath(jsonBody, "/top_p", "/p", "/textGenerationConfig/topP");
}

// Model -> Path Mapping:
Expand All @@ -166,13 +153,13 @@ static String getInputTokens(Object target) {
}

// Try direct token counts first
String directCount = findFirstMatchingPath(
jsonBody,
"/inputTextTokenCount",
"/prompt_token_count",
"/usage/input_tokens",
"/usage/prompt_tokens"
);
String directCount =
findFirstMatchingPath(
jsonBody,
"/inputTextTokenCount",
"/prompt_token_count",
"/usage/input_tokens",
"/usage/prompt_tokens");

if (directCount != null && !directCount.equals("null")) {
return directCount;
Expand All @@ -198,13 +185,13 @@ static String getOutputTokens(Object target) {
}

// Try direct token counts first
String directCount = findFirstMatchingPath(
jsonBody,
"/generation_token_count",
"/results/0/tokenCount",
"/usage/output_tokens",
"/usage/completion_tokens"
);
String directCount =
findFirstMatchingPath(
jsonBody,
"/generation_token_count",
"/results/0/tokenCount",
"/usage/output_tokens",
"/usage/completion_tokens");

if (directCount != null && !directCount.equals("null")) {
return directCount;
Expand All @@ -225,15 +212,15 @@ static String getOutputTokens(Object target) {
@Nullable
static String getFinishReasons(Object target) {
Map<String, Object> jsonBody = getJsonBody(target);
String finishReason = findFirstMatchingPath(
jsonBody,
"/finish_reason",
"/stop_reason",
"/results/0/completionReason",
"/generations/0/finish_reason",
"/choices/0/finish_reason",
"/outputs/0/stop_reason"
);
String finishReason =
findFirstMatchingPath(
jsonBody,
"/finish_reason",
"/stop_reason",
"/results/0/completionReason",
"/generations/0/finish_reason",
"/choices/0/finish_reason",
"/outputs/0/stop_reason");

return finishReason != null ? "[" + finishReason + "]" : null;
}
Expand Down

0 comments on commit 1c4fe0e

Please sign in to comment.