字符串索引必须是整数 Pinecone Embedding

问题描述 投票:0回答:1

我将索引更新到了松果上。我使用的尺寸为 1536,另一个索引为 768。当我使用开放式 AI 嵌入模型 ada 时,1536 运行得很好,但如果我使用任何其他仍然是 1536 的句子转换器,我会收到此错误。现在尝试使 768 工作,但在不使用 open ai 嵌入模型 ada 时仍然遇到相同的错误。

OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
PINECONE_API_KEY = os.environ['PINECONE_API_KEY']
PINECONE_ENVIRONMENT = os.environ['PINECONE_ENVIRONMENT']

class MeilenWebsiteRetriever():

def __init__(self, index_name, namespace):
    self.index_name = index_name
    self.namespace = namespace

def init_vectorstore(self):

    pc = Pinecone(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)

    if self.index_name not in pc.list_indexes().names():
        pc.create_index(
            name=self.index_name,
            dimension=768,
            metric="cosine"
        )

    #embedding_function = SentenceTransformer('BAAI/bge-large-en-v1.5')
    #embedding_function = OpenAIEmbeddings(model="text-embedding-ada-002",
                                #disallowed_special=())
    
    embedding_function = SentenceTransformer('all-mpnet-base-v2')

    print('1')
    vectorstore = LangchainPinecone.from_existing_index(
        index_name=self.index_name,
        embedding=embedding_function,
        namespace=self.namespace
    )
    print('2')
    
    return vectorstore

def init_retriever(self, k, return_source_documents=True):
    print('3')
    vectorstore = self.init_vectorstore()
    print('4')
    retriever = vectorstore.as_retriever(search_kwargs={"k": k},
                            return_source_documents=True)
    print('5')
    return retriever

class JSON_output_sources(BaseModel):
    doc_ids: list = Field(description="list of doc ids")

class MeilenRAGBot():


def __init__(self):
    
    self.retriever = MeilenWebsiteRetriever(
                      index_name="angelos-testing",
                      namespace="factoids-baseemb").init_retriever(k=2, return_source_documents=True)
    self.llm = ChatOpenAI(
                openai_api_key=OPENAI_API_KEY,
                model="gpt-4-1106-preview",
                temperature=0,
                max_tokens=512,
                streaming=True,
                model_kwargs={
                    "top_p": 0.95,
                  })
    self.conversational_memory = ConversationBufferWindowMemory(
                                  memory_key='chat_history',
                                  input_key="question",
                                  k=3,
                                  return_messages=True)
    self.prompt_template = self.init_prompt_template()
    self.bot = self.init_bot()

def init_prompt_template(self):

    template = """Your are a informative assistant answering questions about Meilen, a municipality in Switzerland.
        Only consider the following pieces of context and chat history to answer the questions. 

        Chat history: {chat_history}
        Context: {context}

        You can only make conversations based on the provided context. 
        If a response cannot be formed strictly using the context, politely say you don’t have knowledge about that topic. 
        If you don’t know the answer to a question, say you don’t know. 
        Ensure that your answers remain exclusively focused on Meilen and maintain language consistency with the incoming questions. 
        Prioritize providing detailed yet concise responses, and approach each question methodically. 
        Remember to take a deep breath and work through each step deliberately.

        Question: {question}
        Answer: """

    prompt_template = PromptTemplate(input_variables=["context", "chat_history", "question"],
                        template=template)

    return prompt_template
    
def init_bot(self):
    print('10')
    bot = RetrievalQA.from_chain_type(
        llm=self.llm,
        chain_type="stuff",
        retriever=self.retriever,
        chain_type_kwargs={"prompt": self.prompt_template,
                           "memory": self.conversational_memory
                          },
        return_source_documents=True,
        verbose=False)
        
    return bot
  
def isolate_sources(self, source_docs, answer):
    
    template = """You will be presented with a list of retrieved source documents and an LLM generated answer. Your task is to determine which source documents contributed to the answer.
        
        Approach this task step by step, take your time and do not skip any steps.

        1. Read the generated LLM answer.
        2. Read the source documents.
        3. Determine which source documents in the list of source documents contributed to the answer.
        4. Output a response as JSON with keys as follows:
            - "doc_ids": allowable values are a list of integers (eg. [0, 1, 3])

        Input source documents: {source_docs}

        LLM generated answer: {answer}
    """

    prompt_template = PromptTemplate(input_variables=["source_docs", "answer"], template=template)

    parser = PydanticOutputParser(pydantic_object=JSON_output_sources)

    prompt = prompt_template.format(source_docs=source_docs, answer=answer)
    res = self.llm([SystemMessage(content=prompt)])

    try:
        doc_ids = parser.parse(res.content).doc_ids
        relevant_sources = [source_docs[i] for i in doc_ids]
        relevant_sources = [{"page_content": source.page_content,
                             "metadata": source.metadata,
                             "uuid": str(uuid.uuid4())} for source in relevant_sources]
    except Exception as e:
        relevant_sources = []

    return relevant_sources

def run_query(self, query):
    print('6')
    
    res = self.bot({"query": query})
    print('7')
    # isolate relevant sources
    relevant_sources = self.isolate_sources(res["source_documents"], res["result"])
    print('8')
    return {
        "answer": res["result"],
        "source_documents": relevant_sources
        }
    
if __name__ == "__main__":

bot = MeilenRAGBot()

while True:
  
    query = input('>')
    
    # run query
    res = bot.run_query(query)

    print(res["answer"])
    print(res["source_documents"])

文件“C:\Users\site-packages\sentence_transformers\models\Transformer.py”,第 62 行,向前 trans_features = {'input_ids':特征['input_ids'],'attention_mask':特征['attention_mask']} 类型错误:字符串索引必须是整数

large-language-model sentence-transformers pinecone
1个回答
0
投票

您遇到的错误表明句子转换器模型的使用方式存在问题。具体来说,它表明字符串索引被视为整数索引,从而导致类型错误。

根据您的代码,错误可能发生在

LangchainPinecone.from_existing_index
方法中,该方法需要嵌入模型作为参数。然而,提供的嵌入模型(
SentenceTransformer('all-mpnet-base-v2')
)似乎与该方法的期望不兼容。

要解决此问题,您可能需要确保您使用的嵌入模型与

from_existing_index
方法兼容。此方法可能需要一个嵌入模型,该模型返回一个类似字典的对象,其中包含“input_ids”和“attention_mask”等键,这些键通常由 Hugging Face 的 Transformers 库模型返回。

您应该检查

LangchainPinecone
类和
from_existing_index
方法的文档,以了解嵌入模型的预期输入格式。然后,确保您使用的嵌入模型符合这些期望。

如果您使用自定义嵌入模型或来自不同库的模型,您可能需要对其进行调整以返回所需的格式或找到满足

LangchainPinecone
类要求的替代模型。

© www.soinside.com 2019 - 2024. All rights reserved.