Spaces:
Sleeping
Sleeping
File size: 5,408 Bytes
6e443ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"print(\"hello world\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\Ritesh.Thawkar\\Desktop\\website-demos\\env\\Lib\\site-packages\\pinecone\\data\\index.py:1: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from tqdm.autonotebook import tqdm\n"
]
}
],
"source": [
"from pinecone import Pinecone\n",
"\n",
"# Initialize the Pinecone client\n",
"pc = Pinecone(api_key='ca8e6a33-7355-453f-ad4b-80c8a1c6a9c7')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Define your index name\n",
"index_name = 'vector-store-index'\n",
"index = pc.Index(index_name)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'dimension': 768,\n",
" 'index_fullness': 0.0,\n",
" 'namespaces': {'': {'vector_count': 16294}},\n",
" 'total_vector_count': 16294}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index.describe_index_stats()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"vector_ids = [str(i) for i in range(1, 16295)]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from concurrent.futures import ThreadPoolExecutor, as_completed"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def update_vector(vector_id):\n",
" try:\n",
" # Fetch the vector\n",
" vector = index.fetch([vector_id])\n",
" \n",
" # Check if the vector exists\n",
" if vector and vector['vectors']:\n",
" prev_text = vector['vectors'][vector_id].metadata['text']\n",
" \n",
" # Update the vector's metadata\n",
" index.update(\n",
" id=vector_id, \n",
" set_metadata={\"context\": prev_text},\n",
" )\n",
" return f\"Updated vector {vector_id}.\"\n",
" else:\n",
" return f\"Vector {vector_id} not found.\"\n",
" except Exception as e:\n",
" return f\"Error updating vector {vector_id}: {e}\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Use ThreadPoolExecutor for parallel execution\n",
"with ThreadPoolExecutor(max_workers=10) as executor: # Adjust max_workers as needed\n",
" future_to_vector_id = {executor.submit(update_vector, vector_id): vector_id for vector_id in vector_ids}\n",
" \n",
" for future in as_completed(future_to_vector_id):\n",
" vector_id = future_to_vector_id[future]\n",
" try:\n",
" result = future.result()\n",
" except Exception as e:\n",
" print(f\"Error processing vector {vector_id}: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Specify the ID of the vector you want to update\n",
"vector_ids = [str(i) for i in range(1, 16295)]\n",
"\n",
"for vector_id in vector_ids:\n",
" # Fetch the vector\n",
" vector = index.fetch([vector_id])\n",
"\n",
" prev_text = vector['vectors'][vector_id].metadata['text']\n",
"\n",
" index.update(\n",
" id=vector_id, \n",
" set_metadata={\"context\": prev_text},\n",
" )\n",
"\n",
"# Check if the vector exists\n",
"# if vector and vector.ids:\n",
"# # Get the current metadata\n",
"# current_metadata = vector.vectors[vector_id].metadata\n",
" \n",
"# # Update the key name in the metadata\n",
"# if 'text' in current_metadata:\n",
"# current_metadata['context'] = current_metadata.pop('text')\n",
" \n",
"# # Upsert the updated vector back to the index\n",
"# index.upsert(vectors=[(vector_id, vector.vectors[vector_id].values, current_metadata)])\n",
"# print(f\"Updated metadata for vector {vector_id}.\")\n",
"# else:\n",
"# print(f\"Vector with ID {vector_id} not found.\")\n",
"\n",
"# Optionally, close the index\n",
"# index.close()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|