|
608 | 608 | "source": [
|
609 | 609 | "from langchain_google_alloydb_pg import Column\n",
|
610 | 610 | "\n",
|
| 611 | + "\n", |
611 | 612 | "# Set table name\n",
|
612 | 613 | "TABLE_NAME = \"vectorstore_custom\"\n",
|
613 | 614 | "# SCHEMA_NAME = \"my_schema\"\n",
|
|
649 | 650 | "all_texts = [\"Apples and oranges\", \"Cars and airplanes\", \"Pineapple\", \"Train\", \"Banana\"]\n",
|
650 | 651 | "metadatas = [{\"len\": len(t)} for t in all_texts]\n",
|
651 | 652 | "ids = [str(uuid.uuid4()) for _ in all_texts]\n",
|
652 |
| - "await custom_store.aadd_texts(all_texts, metadatas=metadatas, ids=ids)\n", |
| 653 | + "await custom_store.aadd_texts(all_texts, metadatas=metadatas, ids=ids)" |
| 654 | + ] |
| 655 | + }, |
| 656 | + { |
| 657 | + "cell_type": "markdown", |
| 658 | + "metadata": {}, |
| 659 | + "source": [ |
| 660 | + "#### For v0.13.0+\n", |
653 | 661 | "\n",
|
| 662 | + "**Important Update:** Support for string filters has been deprecated. Please use dictionaries to add filters." |
| 663 | + ] |
| 664 | + }, |
| 665 | + { |
| 666 | + "cell_type": "code", |
| 667 | + "execution_count": null, |
| 668 | + "metadata": {}, |
| 669 | + "outputs": [], |
| 670 | + "source": [ |
| 671 | + "# Use filter on search\n", |
| 672 | + "docs = await custom_store.asimilarity_search(query, filter={\"len\": {\"$gte\": 6}})\n", |
| 673 | + "\n", |
| 674 | + "print(docs)" |
| 675 | + ] |
| 676 | + }, |
| 677 | + { |
| 678 | + "cell_type": "markdown", |
| 679 | + "metadata": {}, |
| 680 | + "source": [ |
| 681 | + "#### For v0.12.0 and under\n", |
| 682 | + "\n", |
| 683 | + "You can make use of the string filters to filter on metadata" |
| 684 | + ] |
| 685 | + }, |
| 686 | + { |
| 687 | + "cell_type": "code", |
| 688 | + "execution_count": null, |
| 689 | + "metadata": {}, |
| 690 | + "outputs": [], |
| 691 | + "source": [ |
654 | 692 | "# Use filter on search\n",
|
655 | 693 | "docs = await custom_store.asimilarity_search(query, filter=\"len >= 6\")\n",
|
656 | 694 | "\n",
|
|
765 | 803 | "Since price_usd is one of the metadata_columns, we can use price filter while searching"
|
766 | 804 | ]
|
767 | 805 | },
|
| 806 | + { |
| 807 | + "cell_type": "markdown", |
| 808 | + "metadata": {}, |
| 809 | + "source": [ |
| 810 | + "#### For v0.13.0+\n", |
| 811 | + "\n", |
| 812 | + "**Important Update:** Support for string filters has been deprecated. Please use dictionaries to add filters." |
| 813 | + ] |
| 814 | + }, |
| 815 | + { |
| 816 | + "cell_type": "code", |
| 817 | + "execution_count": null, |
| 818 | + "metadata": {}, |
| 819 | + "outputs": [], |
| 820 | + "source": [ |
| 821 | + "import uuid\n", |
| 822 | + "\n", |
| 823 | + "docs = await custom_store.asimilarity_search(query, filter={\"price_usd\": {\"$gte\": 100}})\n", |
| 824 | + "\n", |
| 825 | + "print(docs)" |
| 826 | + ] |
| 827 | + }, |
| 828 | + { |
| 829 | + "cell_type": "markdown", |
| 830 | + "metadata": {}, |
| 831 | + "source": [ |
| 832 | + "#### For v0.12.0 and under\n", |
| 833 | + "\n", |
| 834 | + "You can make use of the string filters to filter on metadata" |
| 835 | + ] |
| 836 | + }, |
768 | 837 | {
|
769 | 838 | "cell_type": "code",
|
770 | 839 | "execution_count": null,
|
|
782 | 851 | "cell_type": "markdown",
|
783 | 852 | "metadata": {},
|
784 | 853 | "source": [
|
785 |
| - "### Search for documents with json filter\n", |
786 |
| - "Since category is added in json metadata, we can use filter on JSON fields while searching\n" |
| 854 | + "### Search for documents with json filter\n" |
| 855 | + ] |
| 856 | + }, |
| 857 | + { |
| 858 | + "cell_type": "markdown", |
| 859 | + "metadata": {}, |
| 860 | + "source": [ |
| 861 | + "#### For v0.13.0+\n", |
| 862 | + "\n", |
| 863 | + "**Important Update:** Support for string filters has been deprecated. To filter data on the JSON metadata, you must first create a new column for the specific key you wish to filter on. Use the following SQL command to set this up." |
| 864 | + ] |
| 865 | + }, |
| 866 | + { |
| 867 | + "cell_type": "code", |
| 868 | + "execution_count": null, |
| 869 | + "metadata": { |
| 870 | + "vscode": { |
| 871 | + "languageId": "sql" |
| 872 | + } |
| 873 | + }, |
| 874 | + "outputs": [], |
| 875 | + "source": [ |
| 876 | + "ALTER TABLE vectorstore_table ADD COLUMN category VARCHAR;\n", |
| 877 | + "UPDATE vectorstore_table\n", |
| 878 | + "SET\n", |
| 879 | + " category = metadata ->> 'category';" |
| 880 | + ] |
| 881 | + }, |
| 882 | + { |
| 883 | + "cell_type": "markdown", |
| 884 | + "metadata": {}, |
| 885 | + "source": [ |
| 886 | + "Now that you've added the new column, you must update the Vectorstore instance to recognize it. After which the new column is available for filtering operations." |
| 887 | + ] |
| 888 | + }, |
| 889 | + { |
| 890 | + "cell_type": "code", |
| 891 | + "execution_count": null, |
| 892 | + "metadata": {}, |
| 893 | + "outputs": [], |
| 894 | + "source": [ |
| 895 | + "import uuid\n", |
| 896 | + "\n", |
| 897 | + "docs = await custom_store.asimilarity_search(query, filter={\"category\": \"Electronics\"})\n", |
| 898 | + "\n", |
| 899 | + "print(docs)" |
| 900 | + ] |
| 901 | + }, |
| 902 | + { |
| 903 | + "cell_type": "markdown", |
| 904 | + "metadata": {}, |
| 905 | + "source": [ |
| 906 | + "\n", |
| 907 | + "#### For v0.12.0 and under\n", |
| 908 | + "\n", |
| 909 | + "Since category is added in json metadata, we can use filter on JSON fields using string filters while searching." |
787 | 910 | ]
|
788 | 911 | },
|
789 | 912 | {
|
|
0 commit comments