-
Notifications
You must be signed in to change notification settings - Fork 585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Amazon Bedrock support #483
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from retry import retry | ||
from pr_agent.config_loader import get_settings | ||
from pr_agent.log import get_logger | ||
import boto3 | ||
|
||
OPENAI_RETRIES = 5 | ||
|
||
|
@@ -24,6 +25,7 @@ def __init__(self): | |
Raises a ValueError if the OpenAI key is missing. | ||
""" | ||
self.azure = False | ||
self.aws_bedrock_client = None | ||
|
||
if get_settings().get("OPENAI.KEY", None): | ||
openai.api_key = get_settings().openai.key | ||
|
@@ -60,6 +62,12 @@ def __init__(self): | |
litellm.vertex_location = get_settings().get( | ||
"VERTEXAI.VERTEX_LOCATION", None | ||
) | ||
if get_settings().get("AWS.BEDROCK_REGION", None): | ||
litellm.AmazonAnthropicConfig.max_tokens_to_sample = 2000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need this because |
||
self.aws_bedrock_client = boto3.client( | ||
service_name="bedrock-runtime", | ||
region_name=get_settings().aws.bedrock_region, | ||
) | ||
|
||
@property | ||
def deployment_id(self): | ||
|
@@ -100,13 +108,16 @@ async def chat_completion(self, model: str, system: str, user: str, temperature: | |
if self.azure: | ||
model = 'azure/' + model | ||
messages = [{"role": "system", "content": system}, {"role": "user", "content": user}] | ||
response = await acompletion( | ||
model=model, | ||
deployment_id=deployment_id, | ||
messages=messages, | ||
temperature=temperature, | ||
force_timeout=get_settings().config.ai_timeout | ||
) | ||
kwargs = { | ||
"model": model, | ||
"deployment_id": deployment_id, | ||
"messages": messages, | ||
"temperature": temperature, | ||
"force_timeout": get_settings().config.ai_timeout, | ||
} | ||
if self.aws_bedrock_client: | ||
kwargs["aws_bedrock_client"] = self.aws_bedrock_client | ||
response = await acompletion(**kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding
|
||
except (APIError, Timeout, TryAgain) as e: | ||
get_logger().error("Error during OpenAI inference: ", e) | ||
raise | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,6 +290,7 @@ def _fix_key_value(key: str, value: str): | |
|
||
def load_yaml(response_text: str) -> dict: | ||
response_text = response_text.removeprefix('```yaml').rstrip('`') | ||
response_text = response_text.strip().rstrip().removeprefix('{').removesuffix('}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude tends to add a leading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is problematic. what if the message actually starts with '{' ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this line, as a seperate fallback, to 'try_fix_yaml' section. but this is not good for claude in terms of claude-vs-openai comparison :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi thanks. I moved them to |
||
try: | ||
data = yaml.safe_load(response_text) | ||
except Exception as e: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ GitPython==3.1.32 | |
PyYAML==6.0 | ||
starlette-context==0.3.6 | ||
litellm==0.12.5 | ||
boto3==1.28.25 | ||
boto3==1.33.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. litellm requires |
||
google-cloud-storage==2.10.0 | ||
ujson==5.8.0 | ||
azure-devops==7.1.0b3 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add support only for Claude for now because it's difficult to tune more parameters like
max_tokens_to_sample
specific for each model.