@@ -56,41 +56,40 @@ export default {
5656 const accessKey = env .accessKey ;
5757 const secretKey = env .secretAccessKey ;
5858
59- const requestData = {
60- inputText: " What does ethereal mean?" ,
61- };
62-
63- const headers = {
64- " Content-Type" : " application/json" ,
65- };
66-
67- // sign the original request
68- const stockUrl = new URL (
69- " https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-embed-text-v1/invoke" ,
70- );
71-
7259 const awsClient = new AwsClient ({
7360 accessKeyId: accessKey ,
7461 secretAccessKey: secretKey ,
7562 region: region ,
7663 service: " bedrock" ,
7764 });
7865
66+ const requestBodyString = JSON .stringify ({
67+ inputText: " What does ethereal mean?" ,
68+ });
69+
70+ const stockUrl = new URL (
71+ ` https://bedrock-runtime.${region }.amazonaws.com/model/amazon.titan-embed-text-v1/invoke ` ,
72+ );
73+
74+ const headers = {
75+ " Content-Type" : " application/json" ,
76+ };
77+
78+ // sign the original request
7979 const presignedRequest = await awsClient .sign (stockUrl .toString (), {
8080 method: " POST" ,
8181 headers: headers ,
82+ body: requestBodyString ,
8283 });
8384
84- // change the signed request's host to AI Gateway
85- const stockUrlSigned = new URL (presignedRequest .url );
86- stockUrlSigned .host = " gateway.ai.cloudflare.com" ;
87- stockUrlSigned .pathname = ` /v1/${cfAccountId }/${gatewayName }/aws-bedrock/bedrock-runtime/${region }/model/amazon.titan-embed-text-v1/invoke ` ;
85+ // Gateway Url
86+ const gatewayUrl = new URL (` https://gateway.ai.cloudflare.com/v1/${cfAccountId }/${gatewayName }/aws-bedrock/bedrock-runtime/${region }/model/amazon.titan-embed-text-v1/invoke ` );
8887
89- // make request
90- const response = await fetch (stockUrlSigned , {
88+ // make the request through the gateway url
89+ const response = await fetch (gatewayUrl , {
9190 method: " POST" ,
9291 headers: presignedRequest .headers ,
93- body: JSON . stringify ( requestData ) ,
92+ body: requestBodyString ,
9493 });
9594
9695 if (
@@ -99,9 +98,9 @@ export default {
9998 ) {
10099 const data = await response .json ();
101100 return new Response (JSON .stringify (data ));
102- } else {
103- return new Response (" Invalid response" , { status: 500 });
104101 }
102+
103+ return new Response (" Invalid response" , { status: 500 });
105104 },
106105};
107106```
0 commit comments