Skip to content

Python: Improve Bedrock service setup docs #10772

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

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"nopep",
"NOSQL",
"ollama",
"Onnx",
"onyourdatatest",
"OPENAI",
"opentelemetry",
Expand Down
31 changes: 31 additions & 0 deletions python/samples/concepts/agents/bedrock_agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@
1. You need to have an AWS account and [access to the foundation models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-permissions.html)
2. [AWS CLI installed](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and [configured](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration)

### Configuration

Follow this [guide](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration) to configure your environment to use the Bedrock API.

Please configure the `aws_access_key_id`, `aws_secret_access_key`, and `region` otherwise you will need to create custom clients for the services. For example:

```python
runtime_client=boto.client(
"bedrock-runtime",
aws_access_key_id="your_access_key",
aws_secret_access_key="your_secret_key",
region_name="your_region",
[...other parameters you may need...]
)
client=boto.client(
"bedrock",
aws_access_key_id="your_access_key",
aws_secret_access_key="your_secret_key",
region_name="your_region",
[...other parameters you may need...]
)

bedrock_agent = BedrockAgent.create_and_prepare_agent(
name="your_agent_name",
instructions="your_instructions",
runtime_client=runtime_client,
client=client,
[...other parameters you may need...]
)
```

## Samples

| Sample | Description |
Expand Down
8 changes: 5 additions & 3 deletions python/samples/concepts/setup/ALL_SETTINGS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## AI Service Settings used across SK:
# Semantic Kernel Settings

## AI Service Settings used across SK

| Provider | Service | Constructor Settings | Environment Variable | Required? | Settings Class |
| --- | --- | --- | --- | --- | --- |
Expand Down Expand Up @@ -36,7 +38,7 @@
| Onnx | [OnnxGenAIChatCompletion](../../../semantic_kernel/connectors/ai/onnx/services/onnx_gen_ai_chat_completion.py) | template, <br> ai_model_path | N/A, <br> ONNX_GEN_AI_CHAT_MODEL_FOLDER | Yes, <br> Yes | [OnnxGenAISettings](../../../semantic_kernel/connectors/ai/onnx/onnx_gen_ai_settings.py) |
| | [OnnxGenAITextCompletion](../../../semantic_kernel/connectors/ai/onnx/services/onnx_gen_ai_text_completion.py) | ai_model_path | ONNX_GEN_AI_TEXT_MODEL_FOLDER | Yes | |

## Memory Service Settings used across SK:
## Memory Service Settings used across SK

| Provider | Service | Constructor Settings | Environment Variable | Required? | Settings Class |
| --- | --- | --- | --- | --- | --- |
Expand All @@ -49,7 +51,7 @@
| Redis | [RedisMemoryService](../../../semantic_kernel/connectors/memory/redis/redis_memory_store.py) | connection_string | REDIS_CONNECTION_STRING | Yes | [RedisSettings](../../../semantic_kernel/connectors/memory/redis/redis_settings.py) |
| Weaviate | [WeaviateMemoryService](../../../semantic_kernel/connectors/memory/weaviate/weaviate_memory_store.py) | url, <br> api_key, <br> use_embed | WEAVIATE_URL, <br> WEAVIATE_API_KEY, <br> WEAVIATE_USE_EMBED | No, <br> No, <br> No | [WeaviateSettings](../../../semantic_kernel/connectors/memory/weaviate/weaviate_settings.py) |

## Other settings used:
## Other settings used

| Provider | Service | Constructor Settings | Environment Variable | Required? | Settings Class |
| --- | --- | --- | --- | --- | --- |
Expand Down
2 changes: 1 addition & 1 deletion python/semantic_kernel/connectors/ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
This directory contains the implementation of the AI connectors (aka AI services) that are used to interact with AI models.

Depending on the modality, the AI connector can inherit from one of the following classes:

- [`ChatCompletionClientBase`](./chat_completion_client_base.py) for chat completion tasks.
- [`TextCompletionClientBase`](./text_completion_client_base.py) for text completion tasks.
- [`AudioToTextClientBase`](./audio_to_text_client_base.py) for audio to text tasks.
- [`TextToAudioClientBase`](./text_to_audio_client_base.py) for text to audio tasks.
- [`TextToImageClientBase`](./text_to_image_client_base.py) for text to image tasks.
- [`EmbeddingGeneratorBase`](./embeddings/embedding_generator_base.py) for text embedding tasks.


All base clients inherit from the [`AIServiceClientBase`](../../services/ai_service_client_base.py) class.

## Existing AI connectors
Expand Down
21 changes: 21 additions & 0 deletions python/semantic_kernel/connectors/ai/bedrock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@

Follow this [guide](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration) to configure your environment to use the Bedrock API.

Please configure the `aws_access_key_id`, `aws_secret_access_key`, and `region` otherwise you will need to create custom clients for the services. For example:

```python
runtime_client=boto.client(
"bedrock-runtime",
aws_access_key_id="your_access_key",
aws_secret_access_key="your_secret_key",
region_name="your_region",
[...other parameters you may need...]
)
client=boto.client(
"bedrock",
aws_access_key_id="your_access_key",
aws_secret_access_key="your_secret_key",
region_name="your_region",
[...other parameters you may need...]
)

bedrock_chat_completion_service = BedrockChatCompletion(runtime_client=runtime_client, client=client)
```

## Supports

### Region
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from functools import partial
from typing import Any, ClassVar

import boto3

from semantic_kernel.kernel_pydantic import KernelBaseModel
from semantic_kernel.utils.async_utils import run_in_executor

Expand All @@ -19,6 +21,26 @@ class BedrockBase(KernelBaseModel, ABC):
# Client: Use for model management
bedrock_client: Any

def __init__(
self,
*,
runtime_client: Any | None = None,
client: Any | None = None,
**kwargs: Any,
) -> None:
"""Initialize the Amazon Bedrock Base Class.

Args:
runtime_client: The Amazon Bedrock runtime client to use.
client: The Amazon Bedrock client to use.
**kwargs: Additional keyword arguments.
"""
super().__init__(
bedrock_runtime_client=runtime_client or boto3.client("bedrock-runtime"),
bedrock_client=client or boto3.client("bedrock"),
**kwargs,
)

async def get_foundation_model_info(self, model_id: str) -> dict[str, Any]:
"""Get the foundation model information."""
response = await run_in_executor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from functools import partial
from typing import TYPE_CHECKING, Any, ClassVar

import boto3

if sys.version_info >= (3, 12):
from typing import override # pragma: no cover
else:
Expand Down Expand Up @@ -95,8 +93,8 @@ def __init__(
super().__init__(
ai_model_id=bedrock_settings.chat_model_id,
service_id=service_id or bedrock_settings.chat_model_id,
bedrock_runtime_client=runtime_client or boto3.client("bedrock-runtime"),
bedrock_client=client or boto3.client("bedrock"),
runtime_client=runtime_client,
client=client,
)

# region Overriding base class methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from functools import partial
from typing import TYPE_CHECKING, Any

import boto3
from pydantic import ValidationError

if sys.version_info >= (3, 12):
Expand Down Expand Up @@ -73,8 +72,8 @@ def __init__(
super().__init__(
ai_model_id=bedrock_settings.text_model_id,
service_id=service_id or bedrock_settings.text_model_id,
bedrock_runtime_client=runtime_client or boto3.client("bedrock-runtime"),
bedrock_client=client or boto3.client("bedrock"),
runtime_client=runtime_client,
client=client,
)

# region Overriding base class methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from functools import partial
from typing import TYPE_CHECKING, Any

import boto3
from numpy import array, ndarray
from pydantic import ValidationError

Expand Down Expand Up @@ -70,8 +69,8 @@ def __init__(
super().__init__(
ai_model_id=bedrock_settings.embedding_model_id,
service_id=service_id or bedrock_settings.embedding_model_id,
bedrock_runtime_client=runtime_client or boto3.client("bedrock-runtime"),
bedrock_client=client or boto3.client("bedrock"),
runtime_client=runtime_client,
client=client,
)

@override
Expand Down
Loading