Skip to content

How to use PHP curl to access Nova Pro API

0

Attempting to write a PHP script to invoke request to Nova Pro LLM using curl. I have used the exact same PHP curl authentication method for all the Bedrock models I have accessed, including Titan and Anthropic models.

However, I am having difficulty using this same method to access the Nova Pro model. Attached is the PHP script: https://s3.us-west-2.amazonaws.com/docs.scbbs.com/docs/test/testNova04_debug.php

It just sends "Hello" to the Nova Pro 1.0 LLM.

This script also generates debug info to let me know where the failure is. This is the output:

root@ASUSET2410-Ubuntu:/home/ron/workarea/openai/php/aws/llms# php testNova04_debug.php cURL Verbose Output:

  • Trying 54.190.217.123:443...
  • TCP_NODELAY set
  • Connected to bedrock-runtime.us-west-2.amazonaws.com (54.190.217.123) port 443 (#0)
  • ALPN, offering h2
  • ALPN, offering http/1.1
  • successfully set certificate verify locations:
  • CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs
  • SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
  • ALPN, server accepted to use h2
  • Server certificate:
  • subject: CN=bedrock-runtime.us-west-2.amazonaws.com
  • start date: Aug 21 00:00:00 2024 GMT
  • expire date: Sep 20 23:59:59 2025 GMT
  • subjectAltName: host "bedrock-runtime.us-west-2.amazonaws.com" matched cert's "bedrock-runtime.us-west-2.amazonaws.com"
  • issuer: C=US; O=Amazon; CN=Amazon RSA 2048 M02
  • SSL certificate verify ok.
  • Using HTTP2, server supports multi-use
  • Connection state changed (HTTP/2 confirmed)
  • Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
  • Using Stream ID: 1 (easy handle 0x563d5e6adea0)

POST /model/arn:aws:bedrock:us-west-2:214280194226:inference-profile/us.amazon.nova-pro-v1:0/invoke HTTP/2 Host: bedrock-runtime.us-west-2.amazonaws.com content-type: application/json accept: application/json x-amz-date: 20241204T061725Z authorization: AWS4-HMAC-SHA256 Credential={credentials}/20241204/us-west-2/bedrock-runtime/aws4_request, SignedHeaders=host;x-amz-date, Signature={signature} content-length: 143

  • We are completely uploaded and fine

  • Connection state changed (MAX_CONCURRENT_STREAMS == 128)! < HTTP/2 200 < date: Wed, 04 Dec 2024 06:17:25 GMT < content-type: application/json < content-length: 90 < x-amzn-requestid: 950e3396-fecc-4747-a9ea-6a9fb2747b46 <

  • Connection #0 to host bedrock-runtime.us-west-2.amazonaws.com left intact Response: Array ( [Output] => Array ( [__type] => com.amazon.coral.service#UnknownOperationException )

    [Version] => 1.0 ) root@ASUSET2410-Ubuntu:/home/ron/workarea/openai/php/aws/llms#

Observations: Connection Successful:

The connection to the bedrock-runtime.us-west-2.amazonaws.com endpoint is established successfully. SSL/TLS handshake and HTTP/2 upgrade are working fine. Request Details:

Method: POST Endpoint: /model/arn:aws:bedrock:us-west-2:214280194226:inference-profile/us.amazon.nova-pro-v1:0/invoke Headers: content-type: application/json accept: application/json x-amz-date: 20241204T061725Z authorization: AWS4-HMAC-SHA256 Credential=..., Signature=... Payload: Correct JSON payload with content-length: 143. Response:

HTTP Status Code: 200 OK (No network or authorization issue). Content-Type: application/json Content-Length: 90 Error: UnknownOperationException Analysis: The UnknownOperationException is not caused by network or authentication problems but by the Bedrock API not recognizing the operation being requested. This means:

The endpoint is valid, but something about the request does not match what the API expects. The AWS Bedrock API is successfully invoked, but the API returns an error because: The operation (POST /model/.../invoke) is not supported for this ARN. There is an issue with the payload structure. Headers required for this operation are missing or incorrectly formatted. Likely Issues: ARN Formatting: The ARN being used may be incompatible with the /invoke operation. For Bedrock, ensure:

The ARN refers to a valid inference profile. The format arn:aws:bedrock:us-west-2:214280194226:inference-profile/us.amazon.nova-pro-v1:0 is correct. Headers: Some additional headers might be required, as seen in the successful BedrockRuntimeClient request:

X-Amz-User-Agent: aws-sdk-php/3.283.10 ... aws-sdk-invocation-id: A unique identifier for the request. Operation Mismatch: If the API expects a different path or parameters (e.g., a different inference profile or model ARN), it would result in this error.

Could someone please assist?

Region: us-west-2

asked a year ago303 views
2 Answers
0

Hello,

did you try invoking using the model ARN directly instead of the inference profile ARN?

I'd also open a ticket to AWS Support in parallel, Nova Pro is new, so there still could be some non documented specifics to take into account.

Best,

AWS
answered a year ago
0

It appears that the solution was to use the inference profile ID instead of the arn: https://s3.us-west-2.amazonaws.com/docs.scbbs.com/docs/test/testNova03.php

Code now executes as expected.

One thing, however. The documented API request code is incorrect: https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/model-catalog/serverless/amazon.nova-pro-v1:0

{
  "modelId": "amazon.nova-pro-v1:0",
  "contentType": "application/json",
  "accept": "application/json",
  "body": {
    "inferenceConfig": {
      "max_tokens": 1000
    },
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "this is where you place your input text"
          }
        ]
      }
    ]
  }
}

Using "type" in the request as documented generates an error:

{"message":"Malformed input request: #/messages/0/content/0: extraneous key [type] is not permitted, please reformat you (truncated...) ValidationException (client): Malformed input request: #/messages/0/content/0: extraneous key [type] is not permitted, please reformat your input and try again. - {"message":"Malformed input request: #/messages/0/content/0: extraneous key [type] is not permitted, please reformat your input and try again."}

I suspect this is either an error in the code or an error in the documentation.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.