DSPy: The framework for programming—not prompting—language models
Python
Enable passing a non-default profile to dspy.Bedrock
Jorge Balazs7af2725a9062
diff --git a/dsp/modules/aws_lm.py b/dsp/modules/aws_lm.pyindex 00906282a..ffdb605eb 100644--- a/dsp/modules/aws_lm.py+++ b/dsp/modules/aws_lm.py@@ -4,10 +4,11 @@ A generalized AWS LLM.from __future__ import annotations-from abc import abstractmethod-import logging-from typing import Any, Literalimport json+import logging+from abc import abstractmethod+from typing import Any, Literal, Optional+from dsp.modules.lm import LM# Heuristic translating number of chars to tokens@@ -26,6 +27,7 @@ class AWSLM(LM):region_name: str,service_name: str,max_new_tokens: int,+ profile_name: Optional[str] = None,truncate_long_prompts: bool = False,input_output_ratio: int = 3,batch_n: bool = True,@@ -54,7 +56,12 @@ class AWSLM(LM):import boto3- self.predictor = boto3.client(service_name, region_name=region_name)+ if profile_name is None:+ self.predictor = boto3.client(service_name, region_name=region_name)+ else:+ self.predictor = boto3.Session(profile_name=profile_name).client(+ service_name, region_name=region_name+ )@abstractmethoddef _create_body(self, prompt: str, **kwargs):diff --git a/dsp/modules/bedrock.py b/dsp/modules/bedrock.pyindex 64277f65c..e9089f0f7 100644--- a/dsp/modules/bedrock.py+++ b/dsp/modules/bedrock.py@@ -1,6 +1,6 @@from __future__ import annotations-from typing import Any+from typing import Any, Optionalimport jsonfrom dsp.modules.aws_lm import AWSLM@@ -10,6 +10,7 @@ class Bedrock(AWSLM):self,region_name: str,model: str,+ profile_name: Optional[str] = None,input_output_ratio: int = 3,max_new_tokens: int = 1500,) -> None:@@ -27,6 +28,7 @@ class Bedrock(AWSLM):model=model,service_name="bedrock-runtime",region_name=region_name,+ profile_name=profile_name,truncate_long_prompts=False,input_output_ratio=input_output_ratio,max_new_tokens=max_new_tokens,