"Vector dimension 8 does not match the dimension of the index 1536"

I am working with Pinecone where I have already made the embeddings for my information. Now, I have this code to help my chatbot refine the questions and provide the answers. Initially, I encountered a problem with not being able to make the embeddings due to a mismatch between the dimensions of Pinecone (1536) and the model I was using. Currently, I use text-embedding-ada-002 which, according to my research, matches the dimensions, as it also supports 1536. Now when I run my chatbot, I encounter this new error:

“Vector dimension 8 does not match the dimension of the index 1536”

I don’t understand where it comes from and I’m not able to resolve it. Any ideas?

from sentence_transformers import SentenceTransformer
from transformers import GPT2TokenizerFast
import pinecone
import openai
import streamlit as st
openai.api_key = "API_KEY"
model = GPT2TokenizerFast.from_pretrained('Xenova/text-embedding-ada-002')


pinecone.init(api_key='API_KEY', environment='gcp-starter')
index = pinecone.Index('test')

def find_match(input):
    input_em = list(model.encode(input))
    result = index.query(input_em, top_k=2, includeMetadata=True)
    return result['matches'][0]['metadata']['text']+"\n"+result['matches'][1]['metadata']['text']

def query_refiner(conversation, query):
    response = openai.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt=f"Given the following user query and conversation log, formulate a question that would be the most relevant to provide the user with an answer from a knowledge base.\n\nCONVERSATION LOG: \n{conversation}\n\nQuery: {query}\n\nRefined Query:",
    temperature=0.7,
    max_tokens=256,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
    )
    return response.choices[0].text

def get_conversation_string():
    conversation_string = ""
    for i in range(len(st.session_state['responses'])-1):
        
        conversation_string += "Human: "+st.session_state['requests'][i] + "\n"
        conversation_string += "Bot: "+ st.session_state['responses'][i+1] + "\n"
    return conversation_string

Hi @garciafaciog. Just to clarify, Pinecone does not have a set dimensionality for indexes. You can set it to be whatever you need it to be when you create the index. 1536 happens to be the most common because that’s used by OpenAI’s Ada002, but if you’re using a model that only has 8 dimensions, you’ll need to create an index that also uses 8 dimensions.

You can double-check the dimensionality of your model by just running a simple len() on the input_em and confirm whether it’s returning 8 or 1536 dimensions.

Also, you might want to upgrade your Python client to the 3.0 version as it comes with a number of improvements. It will require some refactoring of your code to make use of, though. For instance, there’s no longer an init() method, you just instantiate a Pinecone object directly. I recommend reviewing our migration guide prior to running the upgrade.

pip install -U pinecone-client
1 Like

Hi @garciafaciog

check the length of this variable you are querying by. I think this line might be the problem. I don’t think .encode will create a list of correct length for your query. Run it in debug and see long long the input_em variable is.

Hope this helps

1 Like

Hi, cory!

I have reviewed according to your recommendations, which confirms that indeed, there is no match between the dimensions of my vectors and my index. Taking this into account, is there something I can do from my Pinecone code to specify that I want my vectors to have a dimension of 384, for example?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.