Code
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Pinecone
from langchain.llms import HuggingFaceHub
from dotenv import load_dotenv
import os
from pinecone import Pinecone, ServerlessSpec
from langchain import PromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
from langchain_pinecone import PineconeVectorStore
from openai import OpenAI
from langchain_openai import ChatOpenAI
import logging
import uuid
import pinecone
import requests
import json
Setup logging
logging.basicConfig(level=logging.INFO)
load_dotenv()
Initialize the OpenAI embeddings model
embeddings = OpenAIEmbeddings(openai_api_key=os.getenv(“OPENAI_API_KEY”))
Initialize Pinecone instance
pc = Pinecone(api_key= os.getenv(‘PINECONE_API_KEY’))
source_index_name = “foaps-aws”
Define target index (where merged data will be stored)
target_index_name = ‘foaps-merged’
if source_index_name not in pc.list_indexes().names():
pc.create_index(
name=source_index_name,
dimension=1536,
metric=“cosine”,
spec=ServerlessSpec(
cloud=“aws”,
region=“us-east-1”
)
)
if target_index_name not in pc.list_indexes().names():
pc.create_index(
name=target_index_name,
dimension=1536,
metric=“cosine”,
spec=ServerlessSpec(
cloud=“aws”,
region=“us-east-1”
)
)
index = pc.Index(source_index_name)
def fetch_data_from_index(index_name):
index = pinecone.Index(index_name, host=“”)
total_vectors = index.describe_index_stats()[‘total_vector_count’]
ids = [str(i) for i in range(total_vectors)]
fetch_response = index.fetch(ids=ids)
print(f"Fetched data from {index_name}: {fetch_response}")
return fetch_response[‘vectors’]
def process_and_merge_data(vectors):
merged_data = {}
for vector_id, vector in vectors.items():
metadata = vector[‘metadata’]
stream_type = metadata.get(‘_ab_stream’)
if stream_type == 'public_combos':
merged_data[vector_id] = {
"combo_id": metadata["id"],
"combo_name": metadata["name"],
"combo_amount": metadata.get("amount", ""),
"restaurant_id": metadata.get("restaurant_id", ""),
"location_id": metadata.get("location_id", "")
}
elif stream_type == 'public_restaurants':
if vector_id not in merged_data:
merged_data[vector_id] = {"restaurant_id": metadata["id"], "restaurant_name": metadata["name"]}
else:
merged_data[vector_id]["restaurant_name"] = metadata["name"]
elif stream_type == 'public_locations':
if vector_id not in merged_data:
merged_data[vector_id] = {"location_id": metadata["id"], "location_name": metadata["name"]}
else:
merged_data[vector_id]["location_name"] = metadata["name"]
print(f"Merged data: {merged_data}")
return merged_data
def upsert_to_target_index(merged_data):
target_index = pinecone.Index(target_index_name, host=“”)
vectors_to_upsert =
for vector_id, data in merged_data.items():
# Create a vector with metadata; values should be actual embedding vectors
vector_values = [0.1] * 1536 # Placeholder for actual vector values
vectors_to_upsert.append((vector_id, vector_values, data))
print(f"Vectors to upsert: {vectors_to_upsert}")
vectors_to_upsert = []
for vector_id, data in merged_data.items():
# Create a vector with metadata; values should be actual embedding vectors
vector_values = [0.1] * 1536 # Placeholder for actual vector values
vectors_to_upsert.append({"id": vector_id, "values": vector_values, "metadata": data})
print(f"Vectors to upsert: {vectors_to_upsert}")
if vectors_to_upsert:
target_index.upsert(vectors=vectors_to_upsert)
print("Data upsert complete.")
else:
print("No data to upsert.")
def main():
vectors = fetch_data_from_index(source_index_name)
merged_data = process_and_merge_data(vectors)
upsert_to_target_index(merged_data)
if name == “main”:
main()
Exception has occurred: ForbiddenException
(403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({‘Date’: ‘Tue, 13 Aug 2024 06:41:02 GMT’, ‘Content-Type’: ‘text/plain’, ‘Content-Length’: ‘9’, ‘Connection’: ‘keep-alive’, ‘x-pinecone-auth-rejected-reason’: ‘Wrong API key’, ‘www-authenticate’: ‘Wrong API key’, ‘server’: ‘envoy’})
HTTP response body: Forbidden
File “C:\Users\FARAZ\Desktop\Projects\FIxed Bot\foaps.py”, line 64, in fetch_data_from_index
total_vectors = index.describe_index_stats()[‘total_vector_count’]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\FARAZ\Desktop\Projects\FIxed Bot\foaps.py”, line 123, in main
vectors = fetch_data_from_index(source_index_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\FARAZ\Desktop\Projects\FIxed Bot\foaps.py”, line 128, in
main()
pinecone.core.openapi.shared.exceptions.ForbiddenException: (403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({‘Date’: ‘Tue, 13 Aug 2024 06:41:02 GMT’, ‘Content-Type’: ‘text/plain’, ‘Content-Length’: ‘9’, ‘Connection’: ‘keep-alive’, ‘x-pinecone-auth-rejected-reason’: ‘Wrong API key’, ‘www-authenticate’: ‘Wrong API key’, ‘server’: ‘envoy’})
HTTP response body: Forbidden
How to solve this error