|
| 1 | +--- |
| 2 | +sidebar_position: 9 |
| 3 | +--- |
| 4 | + |
| 5 | +# dspy.AWSMistral, dspy.AWSAnthropic, dspy.AWSMeta |
| 6 | + |
| 7 | +### Usage |
| 8 | + |
| 9 | +```python |
| 10 | +# Notes: |
| 11 | +# 1. Install boto3 to use AWS models. |
| 12 | +# 2. Configure your AWS credentials with the AWS CLI before using these models |
| 13 | + |
| 14 | +# initialize the bedrock aws provider |
| 15 | +bedrock = dspy.Bedrock(region_name="us-west-2") |
| 16 | +# For mixtral on Bedrock |
| 17 | +lm = dspy.AWSMistral(bedrock, "mistral.mixtral-8x7b-instruct-v0:1", **kwargs) |
| 18 | +# For haiku on Bedrock |
| 19 | +lm = dspy.AWSAnthropic(bedrock, "anthropic.claude-3-haiku-20240307-v1:0", **kwargs) |
| 20 | +# For llama2 on Bedrock |
| 21 | +lm = dspy.AWSMeta(bedrock, "meta.llama2-13b-chat-v1", **kwargs) |
| 22 | + |
| 23 | +# initialize the sagemaker aws provider |
| 24 | +sagemaker = dspy.Sagemaker(region_name="us-west-2") |
| 25 | +# For mistral on Sagemaker |
| 26 | +# Note: you need to create a Sagemaker endpoint for the mistral model first |
| 27 | +lm = dspy.AWSMistral(sagemaker, "<YOUR_MISTRAL_ENDPOINT_NAME>", **kwargs) |
| 28 | + |
| 29 | +``` |
| 30 | + |
| 31 | +### Constructor |
| 32 | + |
| 33 | +The `AWSMistral` constructor initializes the base class `AWSModel` which itself inherits from the `LM` class. |
| 34 | + |
| 35 | +```python |
| 36 | +class AWSMistral(AWSModel): |
| 37 | + """Mistral family of models.""" |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + aws_provider: AWSProvider, |
| 42 | + model: str, |
| 43 | + max_context_size: int = 32768, |
| 44 | + max_new_tokens: int = 1500, |
| 45 | + **kwargs |
| 46 | + ) -> None: |
| 47 | +``` |
| 48 | + |
| 49 | +**Parameters:** |
| 50 | +- `aws_provider` (AWSProvider): The aws provider to use. One of `dspy.Bedrock` or `dspy.Sagemaker`. |
| 51 | +- `model` (_str_): Mistral AI pretrained models. For Bedrock, this is the Model ID in https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html#model-ids-arns. For Sagemaker, this is the endpoint name. |
| 52 | +- `max_context_size` (_Optional[int]_, _optional_): Max context size for this model. Defaults to 32768. |
| 53 | +- `max_new_tokens` (_Optional[int]_, _optional_): Max new tokens possible for this model. Defaults to 1500. |
| 54 | +- `**kwargs`: Additional language model arguments to pass to the API provider. |
| 55 | + |
| 56 | +### Methods |
| 57 | + |
| 58 | +```python |
| 59 | +def _format_prompt(self, raw_prompt: str) -> str: |
| 60 | +``` |
| 61 | +This function formats the prompt for the model. Refer to the model card for the specific formatting required. |
| 62 | + |
| 63 | +<br/> |
| 64 | + |
| 65 | +```python |
| 66 | +def _create_body(self, prompt: str, **kwargs) -> tuple[int, dict[str, str | float]]: |
| 67 | +``` |
| 68 | +This function creates the body of the request to the model. It takes the prompt and any additional keyword arguments and returns a tuple of the number of tokens to generate and a dictionary of keys including the prompt used to create the body of the request. |
| 69 | + |
| 70 | +<br/> |
| 71 | + |
| 72 | +```python |
| 73 | +def _call_model(self, body: str) -> str: |
| 74 | +``` |
| 75 | +This function calls the model using the provider `call_model()` function and extracts the generated text (completion) from the provider-specific response. |
| 76 | + |
| 77 | +<br/> |
| 78 | + |
| 79 | +The above model-specific methods are called by the `AWSModel::basic_request()` method, which is the main method for querying the model. This method takes the prompt and any additional keyword arguments and calls the `AWSModel::_simple_api_call()` which then delegates to the model-specific `_create_body()` and `_call_model()` methods to create the body of the request, call the model and extract the generated text. |
| 80 | + |
| 81 | + |
| 82 | +Refer to [`dspy.OpenAI`](https://dspy-docs.vercel.app/api/language_model_clients/OpenAI) documentation for information on the `LM` base class functionality. |
| 83 | + |
| 84 | +<br/> |
| 85 | + |
| 86 | +`AWSAnthropic` and `AWSMeta` work exactly the same as `AWSMistral`. |
0 commit comments