|
| 1 | +from typing import Optional, Sequence |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +from ..config import LLMConfigurationError |
| 6 | +from ..errors import LLMImportError |
| 7 | +from . import LLMClient |
| 8 | +from .base import ChatMessage |
| 9 | + |
| 10 | +try: |
| 11 | + import boto3 # noqa: F401 |
| 12 | +except ImportError as err: |
| 13 | + raise LLMImportError( |
| 14 | + flavor="llm", msg="To use Bedrock models, please install the `boto3` package with `pip install boto3`" |
| 15 | + ) from err |
| 16 | + |
| 17 | + |
| 18 | +class ClaudeBedrockClient(LLMClient): |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + bedrock_runtime_client, |
| 22 | + model: str = "anthropic.claude-3-sonnet-20240229-v1:0", |
| 23 | + anthropic_version: str = "bedrock-2023-05-31", |
| 24 | + ): |
| 25 | + self._client = bedrock_runtime_client |
| 26 | + self.model = model |
| 27 | + self.anthropic_version = anthropic_version |
| 28 | + |
| 29 | + def complete( |
| 30 | + self, |
| 31 | + messages: Sequence[ChatMessage], |
| 32 | + temperature: float = 1, |
| 33 | + max_tokens: Optional[int] = 1000, |
| 34 | + caller_id: Optional[str] = None, |
| 35 | + seed: Optional[int] = None, |
| 36 | + format=None, |
| 37 | + ) -> ChatMessage: |
| 38 | + # only supporting claude 3 to start |
| 39 | + if "claude-3" not in self.model: |
| 40 | + raise LLMConfigurationError(f"Only claude-3 models are supported as of now, got {self.model}") |
| 41 | + |
| 42 | + # extract system prompt from messages |
| 43 | + system_prompt = "" |
| 44 | + if len(messages) > 1: |
| 45 | + if messages[0].role.lower() == "user" and messages[1].role.lower() == "user": |
| 46 | + system_prompt = messages[0].content |
| 47 | + messages = messages[1:] |
| 48 | + |
| 49 | + # Create the messages format needed for bedrock specifically |
| 50 | + input_msg_prompt = [] |
| 51 | + for msg in messages: |
| 52 | + if msg.role.lower() == "assistant": |
| 53 | + input_msg_prompt.append({"role": "assistant", "content": [{"type": "text", "text": msg.content}]}) |
| 54 | + else: |
| 55 | + input_msg_prompt.append({"role": "user", "content": [{"type": "text", "text": msg.content}]}) |
| 56 | + |
| 57 | + # create the json body to send to the API |
| 58 | + body = json.dumps( |
| 59 | + { |
| 60 | + "anthropic_version": "bedrock-2023-05-31", |
| 61 | + "max_tokens": max_tokens, |
| 62 | + "temperature": temperature, |
| 63 | + "system": system_prompt, |
| 64 | + "messages": input_msg_prompt, |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + # invoke the model and get the response |
| 69 | + try: |
| 70 | + accept = "application/json" |
| 71 | + contentType = "application/json" |
| 72 | + response = self._client.invoke_model(body=body, modelId=self.model, accept=accept, contentType=contentType) |
| 73 | + completion = json.loads(response.get("body").read()) |
| 74 | + except RuntimeError as err: |
| 75 | + raise LLMConfigurationError("Could not get response from Bedrock API") from err |
| 76 | + |
| 77 | + self.logger.log_call( |
| 78 | + prompt_tokens=completion["usage"]["input_tokens"], |
| 79 | + sampled_tokens=completion["usage"]["input_tokens"], |
| 80 | + model=self.model, |
| 81 | + client_class=self.__class__.__name__, |
| 82 | + caller_id=caller_id, |
| 83 | + ) |
| 84 | + |
| 85 | + msg = completion["content"][0]["text"] |
| 86 | + return ChatMessage(role="assistant", content=msg) |
0 commit comments