|
| 1 | +from textual.app import ComposeResult |
| 2 | +from textual.widgets import Input, Select |
| 3 | + |
| 4 | +from llama_index.embeddings.bedrock import BedrockEmbedding |
| 5 | +from llama_cloud import PipelineCreateEmbeddingConfig_BedrockEmbedding |
| 6 | + |
| 7 | +from ..base import ConfigurationScreen |
| 8 | + |
| 9 | + |
| 10 | +class BedrockEmbeddingScreen(ConfigurationScreen): |
| 11 | + """Configuration screen for Bedrock embeddings.""" |
| 12 | + |
| 13 | + def get_title(self) -> str: |
| 14 | + return "Bedrock Embedding Configuration" |
| 15 | + |
| 16 | + def get_form_elements(self) -> list[ComposeResult]: |
| 17 | + model_options = [] |
| 18 | + try: |
| 19 | + supported_models = BedrockEmbedding.list_supported_models() |
| 20 | + model_options = [ |
| 21 | + (f"{provider.title()}: {model_id.split('.')[-1]}", model_id) |
| 22 | + for provider, models in supported_models.items() |
| 23 | + for model_id in models |
| 24 | + ] |
| 25 | + except Exception as e: |
| 26 | + self.notify( |
| 27 | + f"Could not fetch Bedrock models: {e}", severity="error", timeout=10 |
| 28 | + ) |
| 29 | + |
| 30 | + return [ |
| 31 | + Select( |
| 32 | + options=model_options, |
| 33 | + prompt="Select Bedrock Model", |
| 34 | + id="model", |
| 35 | + classes="form-control", |
| 36 | + ), |
| 37 | + Input( |
| 38 | + placeholder="Region (e.g., us-east-1)", |
| 39 | + id="region", |
| 40 | + classes="form-control", |
| 41 | + ), |
| 42 | + Input( |
| 43 | + placeholder="Access Key ID (Optional)", |
| 44 | + id="access_key_id", |
| 45 | + classes="form-control", |
| 46 | + ), |
| 47 | + Input( |
| 48 | + placeholder="Secret Access Key (Optional)", |
| 49 | + password=True, |
| 50 | + id="secret_access_key", |
| 51 | + classes="form-control", |
| 52 | + ), |
| 53 | + ] |
| 54 | + |
| 55 | + def process_submission(self) -> None: |
| 56 | + model = self.query_one("#model", Select).value |
| 57 | + region = self.query_one("#region", Input).value |
| 58 | + access_key_id = self.query_one("#access_key_id", Input).value |
| 59 | + secret_access_key = self.query_one("#secret_access_key", Input).value |
| 60 | + |
| 61 | + if not all([model, region]): |
| 62 | + self.notify("All fields are required.", severity="error") |
| 63 | + return |
| 64 | + |
| 65 | + try: |
| 66 | + embed_model = BedrockEmbedding( |
| 67 | + model_name=model, |
| 68 | + region_name=region, |
| 69 | + aws_access_key_id=access_key_id, |
| 70 | + aws_secret_access_key=secret_access_key, |
| 71 | + ) |
| 72 | + embedding_config = PipelineCreateEmbeddingConfig_BedrockEmbedding( |
| 73 | + type="BEDROCK_EMBEDDING", component=embed_model |
| 74 | + ) |
| 75 | + |
| 76 | + self.app.config = embedding_config |
| 77 | + self.app.handle_completion(self.app.config) |
| 78 | + except Exception as e: |
| 79 | + self.notify(f"Error: {e}", severity="error") |
0 commit comments