primerg
primerg

Reputation: 33

Bedrock API returns empty body

I'm usng AWS PHP SDK to call bedrock API. Here is the code:

$this->bedrockRuntimeClient = new BedrockRuntimeClient([
        'region' => $region,
        'version' => $version,
        'credentials' => $credentials
]);

$modelId = 'anthropic.claude-v2:1';

$body = [
          "prompt" => "\n\nHuman: Give me a short history of the US.\n\nAssistant:",
          "max_tokens_to_sample" => 300,
          "temperature" => 0.5,
          "top_k" => 250,
          "top_p" => 1,
          "stop_sequences" => [
            "\n\nHuman:"
          ],
          "anthropic_version" => "bedrock-2023-05-31"
];

$payload = [
          'contentType' => 'application/json',
          'accept' => '*/*',
          'body' => json_encode($body),
          'modelId' => $modelId,
];

$result = $this->bedrockRuntimeClient->invokeModel($payload);

Regardless of the modelID I used, I get an empty body. Here is an example:

{
    "body": {},
    "contentType": "application\/json",
    "@metadata": {
        "statusCode": 200,
        "effectiveUri": "https:\/\/bedrock-runtime.us-east-1.amazonaws.com\/model\/anthropic.claude-v2%3A1\/invoke",
        "headers": {
            "date": "Wed, 10 Jan 2024 11:03:10 GMT",
            "content-type": "application\/json",
            "content-length": "74",
            "connection": "keep-alive",
            "x-amzn-requestid": "1904e6b8-6c10-4279-a77c-7afeecbd629c",
            "x-amzn-bedrock-invocation-latency": "458",
            "x-amzn-bedrock-output-token-count": "6",
            "x-amzn-bedrock-input-token-count": "11"
        },
        "transferStats": {
            "http": [
                []
            ]
        }
    }
}

If I use a model that I don't have access to, I get an error. If I send a body that is not compliant to the model such as adding a new data, I also get an error.

When I send the above $payload, I get that result. I am assuming that the data I sent is correct but I'm not getting any result.

Upvotes: 0

Views: 845

Answers (1)

primerg
primerg

Reputation: 33

It turns out the body is not really empty. The way I use $result->__toString() to review the content did not return the actual body. Here is the actual content of the body

Array
(
    [body] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #13
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )

        )

    [contentType] => application/json
)

This is the line to extract the content

$result = $this->bedrockRuntimeClient->invokeModel($payload);
$content = $result->toArray();
$response_body = json_decode($content['body']->getContents());
$completion = $response_body->completion;

Upvotes: 1

Related Questions