|
32 | 32 | "%pip install -qU pinecone-notebooks pinecone-client[grpc]"
|
33 | 33 | ]
|
34 | 34 | },
|
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "from pinecone_notebooks.colab import Authenticate\n", |
| 42 | + "\n", |
| 43 | + "Authenticate()" |
| 44 | + ] |
| 45 | + }, |
35 | 46 | {
|
36 | 47 | "cell_type": "code",
|
37 | 48 | "execution_count": null,
|
|
64 | 75 | "dimension = index.describe_index_stats()['dimension']\n",
|
65 | 76 | "# Function to fetch all vectors from a namespace\n",
|
66 | 77 | "def fetch_all_vectors(namespace):\n",
|
67 |
| - " count = index.describe_index_stats()['namespaces'][namespace]['vector_count']\n", |
| 78 | + " try:\n", |
| 79 | + " count = index.describe_index_stats()['namespaces'][namespace]['vector_count']\n", |
| 80 | + " if count > 10000:\n", |
| 81 | + " raise ValueError(\"Namespaces larger than 10000 vectors need to be handled iteratively.\")\n", |
| 82 | + " except ValueError as e:\n", |
| 83 | + " print(f\"Error: {e}\")\n", |
| 84 | + " return []\n", |
68 | 85 | " random_vecs = [random.random() for _ in range(dimension)]\n",
|
69 | 86 | " response = index.query(\n",
|
70 | 87 | " namespace=namespace,\n",
|
71 | 88 | " vector=random_vecs,\n",
|
72 | 89 | " top_k=count,\n",
|
73 |
| - " include_values=True\n", |
| 90 | + " include_values=True,\n", |
| 91 | + " include_metadata=True\n", |
74 | 92 | " )\n",
|
75 | 93 | " return response['matches']\n",
|
76 | 94 | "\n",
|
|
87 | 105 | "source": [
|
88 | 106 | "# Convert fetched vectors to the required upsert format\n",
|
89 | 107 | "def format_vectors_for_upsert(fetched_vectors):\n",
|
90 |
| - " return [{\"id\": match['id'], \"values\": match['values']} for match in fetched_vectors]\n", |
| 108 | + " return [{\"id\": match['id'], \n", |
| 109 | + " \"values\": match['values'], \n", |
| 110 | + " \"metadata\": match['metadata']} for match in fetched_vectors]\n", |
91 | 111 | "\n",
|
92 | 112 | "formatted_vectors_ns1 = format_vectors_for_upsert(vectors_ns1)\n",
|
93 | 113 | "formatted_vectors_ns2 = format_vectors_for_upsert(vectors_ns2)\n",
|
|
131 | 151 | }
|
132 | 152 | ],
|
133 | 153 | "metadata": {
|
| 154 | + "kernelspec": { |
| 155 | + "display_name": "Python 3", |
| 156 | + "language": "python", |
| 157 | + "name": "python3" |
| 158 | + }, |
134 | 159 | "language_info": {
|
135 |
| - "name": "python" |
| 160 | + "name": "python", |
| 161 | + "version": "3.11.6" |
136 | 162 | }
|
137 | 163 | },
|
138 | 164 | "nbformat": 4,
|
|
0 commit comments