{ "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 }