MCPcopy Create free account
hub / github.com/AsyncFuncAI/deepwiki-open / call

Method call

api/bedrock_client.py:304–434  ·  view source on GitHub ↗

Make a synchronous call to the AWS Bedrock API.

(self, api_kwargs: Dict = None, model_type: ModelType = None)

Source from the content-addressed store, hash-verified

302 max_time=5,
303 )
304 def call(self, api_kwargs: Dict = None, model_type: ModelType = None) -> Any:
305 """Make a synchronous call to the AWS Bedrock API."""
306 api_kwargs = api_kwargs or {}
307
308 # Check if client is initialized
309 if not self.sync_client:
310 error_msg = "AWS Bedrock client not initialized. Check your AWS credentials and region."
311 log.error(error_msg)
312 return error_msg
313
314 if model_type == ModelType.LLM:
315 model_id = api_kwargs.get("model", "anthropic.claude-3-sonnet-20240229-v1:0")
316 provider = self._get_model_provider(model_id)
317
318 # Get the prompt from api_kwargs
319 prompt = api_kwargs.get("input", "")
320 messages = api_kwargs.get("messages")
321
322 # Format the prompt according to the provider
323 request_body = self._format_prompt_for_provider(provider, prompt, messages)
324
325 # Add model parameters if provided
326 if "temperature" in api_kwargs:
327 if provider == "anthropic":
328 request_body["temperature"] = api_kwargs["temperature"]
329 elif provider == "amazon":
330 request_body["textGenerationConfig"]["temperature"] = api_kwargs["temperature"]
331 elif provider == "cohere":
332 request_body["temperature"] = api_kwargs["temperature"]
333 elif provider == "ai21":
334 request_body["temperature"] = api_kwargs["temperature"]
335
336 if "top_p" in api_kwargs:
337 if provider == "anthropic":
338 request_body["top_p"] = api_kwargs["top_p"]
339 elif provider == "amazon":
340 request_body["textGenerationConfig"]["topP"] = api_kwargs["top_p"]
341 elif provider == "cohere":
342 request_body["p"] = api_kwargs["top_p"]
343 elif provider == "ai21":
344 request_body["topP"] = api_kwargs["top_p"]
345
346 # Convert request body to JSON
347 body = json.dumps(request_body)
348
349 try:
350 # Make the API call
351 response = self.sync_client.invoke_model(
352 modelId=model_id,
353 body=body
354 )
355
356 # Parse the response
357 response_body = json.loads(response["body"].read())
358
359 # Extract the generated text
360 generated_text = self._extract_response_text(provider, response_body)
361

Callers 1

acallMethod · 0.95

Calls 3

_get_model_providerMethod · 0.95

Tested by

no test coverage detected