Claude on Vertex

Hello,

Having the code bellow, which is trying to do a query on claude.

 

$PROJECT_ID = "project_id";
            $LOCATION = "us-central1";
            $MODEL = "claude-3-haiku@20240307";
            $formattedEndpoint = PredictionServiceClient::projectLocationPublisherModelName($PROJECT_ID, $LOCATION, 'google', $MODEL);
            Log::info($formattedEndpoint);
            $predictionServiceClient = new PredictionServiceClient([
                'apiEndpoint' => $LOCATION.'-aiplatform.googleapis.com',
            ]);


            $payload = [
                "anthropic_version" => "vertex-2023-10-16",
                "messages"          => [["role" => "user", "content" => $prompt]],
                "max_tokens"        => 4000,
                "stream"            => false,
                "temperature"       => floatval(env('CLAUDE_TEMPERATURE')),
            ];

            $httpBody = new HttpBody();
            $httpBody->setContentType('application/json;');
            $httpBody->setData(json_encode($payload));

            $request = RawPredictRequest::build($formattedEndpoint, $httpBody);

            return $predictionServiceClient->rawPredict($request);

 

No matter how I set that payload I always get back the same error:

 

{
    "message": "messages: Field required",
    "code": 400,
    "status": "UNRECOGNIZED_STATUS",
    "details": []
}

 

Any help is highly appreciated!

1 1 89
1 REPLY 1

Hi @MariusC

Thank you for joining our community.

The error message "messages: Field required" indicates a missing field in your request. While your code includes the "messages" field, there might be an issue with how the message itself is constructed.

A common culprit is an empty $prompt variable within the "content" field. Ensure $prompt is defined and contains the data you want to send to Claude.

For reference on proper code structure, refer to this Anthropic - Vertex AI API article.

I hope I was able to provide you with useful insights.