|
13 | 13 | "3. Vertex AI\n", |
14 | 14 | "4. Cohere\n", |
15 | 15 | "5. Mistral AI\n", |
16 | | - "6. Bringing your own vectorizer\n", |
| 16 | + "6. Amazon Bedrock\n", |
| 17 | + "7. Bringing your own vectorizer\n", |
17 | 18 | "\n", |
18 | 19 | "Before running this notebook, be sure to\n", |
19 | 20 | "1. Have installed ``redisvl`` and have that environment active for this notebook.\n", |
|
541 | 542 | "# print(test[:10])" |
542 | 543 | ] |
543 | 544 | }, |
| 545 | + { |
| 546 | + "cell_type": "markdown", |
| 547 | + "metadata": {}, |
| 548 | + "source": [ |
| 549 | + "### Amazon Bedrock\n", |
| 550 | + "\n", |
| 551 | + "Amazon Bedrock provides fully managed foundation models for text embeddings. Install the required dependencies:\n", |
| 552 | + "\n", |
| 553 | + "```bash\n", |
| 554 | + "pip install 'redisvl[bedrock]' # Installs boto3\n", |
| 555 | + "```" |
| 556 | + ] |
| 557 | + }, |
| 558 | + { |
| 559 | + "cell_type": "markdown", |
| 560 | + "metadata": {}, |
| 561 | + "source": [ |
| 562 | + "#### Configure AWS credentials:" |
| 563 | + ] |
| 564 | + }, |
| 565 | + { |
| 566 | + "cell_type": "code", |
| 567 | + "execution_count": null, |
| 568 | + "metadata": {}, |
| 569 | + "outputs": [], |
| 570 | + "source": [ |
| 571 | + "import os\n", |
| 572 | + "import getpass\n", |
| 573 | + "\n", |
| 574 | + "if \"AWS_ACCESS_KEY_ID\" not in os.environ:\n", |
| 575 | + " os.environ[\"AWS_ACCESS_KEY_ID\"] = getpass.getpass(\"Enter AWS Access Key ID: \")\n", |
| 576 | + "if \"AWS_SECRET_ACCESS_KEY\" not in os.environ:\n", |
| 577 | + " os.environ[\"AWS_SECRET_ACCESS_KEY\"] = getpass.getpass(\"Enter AWS Secret Key: \")\n", |
| 578 | + "\n", |
| 579 | + "os.environ[\"AWS_REGION\"] = \"us-east-1\" # Change as needed" |
| 580 | + ] |
| 581 | + }, |
| 582 | + { |
| 583 | + "cell_type": "markdown", |
| 584 | + "metadata": {}, |
| 585 | + "source": [ |
| 586 | + "#### Create embeddings:" |
| 587 | + ] |
| 588 | + }, |
| 589 | + { |
| 590 | + "cell_type": "code", |
| 591 | + "execution_count": null, |
| 592 | + "metadata": {}, |
| 593 | + "outputs": [], |
| 594 | + "source": [ |
| 595 | + "from redisvl.utils.vectorize import BedrockTextVectorizer\n", |
| 596 | + "\n", |
| 597 | + "bedrock = BedrockTextVectorizer(\n", |
| 598 | + " model=\"amazon.titan-embed-text-v2:0\"\n", |
| 599 | + ")\n", |
| 600 | + "\n", |
| 601 | + "# Single embedding\n", |
| 602 | + "text = \"This is a test sentence.\"\n", |
| 603 | + "embedding = bedrock.embed(text)\n", |
| 604 | + "print(f\"Vector dimensions: {len(embedding)}\")\n", |
| 605 | + "\n", |
| 606 | + "# Multiple embeddings\n", |
| 607 | + "sentences = [\n", |
| 608 | + " \"That is a happy dog\",\n", |
| 609 | + " \"That is a happy person\",\n", |
| 610 | + " \"Today is a sunny day\"\n", |
| 611 | + "]\n", |
| 612 | + "embeddings = bedrock.embed_many(sentences)" |
| 613 | + ] |
| 614 | + }, |
544 | 615 | { |
545 | 616 | "cell_type": "markdown", |
546 | 617 | "metadata": {}, |
|
691 | 762 | }, |
692 | 763 | { |
693 | 764 | "cell_type": "code", |
694 | | - "execution_count": 17, |
| 765 | + "execution_count": null, |
695 | 766 | "metadata": {}, |
696 | 767 | "outputs": [ |
697 | 768 | { |
|
710 | 781 | "source": [ |
711 | 782 | "# load expects an iterable of dictionaries where\n", |
712 | 783 | "# the vector is stored as a bytes buffer\n", |
| 784 | + "from redisvl.redis.utils import array_to_buffer\n", |
713 | 785 | "\n", |
714 | 786 | "data = [{\"text\": t,\n", |
715 | | - " \"embedding\": v}\n", |
| 787 | + " \"embedding\": array_to_buffer(v, dtype=\"float32\")}\n", |
716 | 788 | " for t, v in zip(sentences, embeddings)]\n", |
717 | 789 | "\n", |
718 | 790 | "index.load(data)" |
|
0 commit comments