1
from flask import Flask, render_template, jsonify, request
from src.helper import download_hugging_face_embeddings
# from langchain.vectorstores import Pinecone
import pinecone
from langchain.prompts import PromptTemplate
from langchain.llms import CTransformers
from langchain.chains import RetrievalQA
from dotenv import load_dotenv
from src.prompt import *
import os
from pinecone import Pinecone,ServerlessSpec
from langchain_pinecone import PineconeVectorStore

app = Flask(__name__)

load_dotenv()

PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
PINECONE_API_ENV = os.environ.get('PINECONE_API_ENV')


embeddings = download_hugging_face_embeddings()

#Initializing the Pinecone
# pinecone.init(api_key=PINECONE_API_KEY,
#               environment=PINECONE_API_ENV)
pc = Pinecone(
        api_key=os.environ.get("PINECONE_API_KEY")
    )
index_name="chatbot"

#Loading the index
# docsearch=pc.from_existing_index(index_name, embeddings)
docsearch = Pinecone.from_existing_index(index_name, embeddings)

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

chain_type_kwargs={"prompt": PROMPT}

llm=CTransformers(model="model/llama-2-7b-chat.ggmlv3.q4_0.bin",
                  model_type="llama",
                  config={'max_new_tokens':512,
                          'temperature':0.8})


qa=RetrievalQA.from_chain_type(
    llm=llm, 
    chain_type="stuff", 
    retriever=docsearch.as_retriever(search_kwargs={'k': 2}),
    return_source_documents=True, 
    chain_type_kwargs=chain_type_kwargs)



@app.route("/")
def index():
    return render_template('chat.html')



@app.route("/get", methods=["GET", "POST"])
def chat():
    msg = request.form["msg"]
    input = msg
    print(input)
    result=qa({"query": input})
    print("Response : ", result["result"])
    return str(result["result"])



if __name__ == '__main__':
    app.run(host="0.0.0.0", port= 8080, debug= True)

Why am I getting this error ? Traceback (most recent call last): File "C:\final_year_chatbot\End-to-end-Medical-Chatbot-using-Llama2-main\app.py", line 34, in docsearch = Pinecone.from_existing_index(index_name, embeddings) AttributeError: type object 'Pinecone' has no attribute 'from_existing_index'

I am trying to run this app.py but I am getting this error

2 Answers 2

0

I think your code was supposed to work for an earlier version of pinecone. I faced a similar issue and this code works for me:

from pinecone import Pinecone
from pinecone import ServerlessSpec
from langchain_pinecone import PineconeVectorStore
import os

# initialize connection to pinecone (get API key at app.pinecone.io)
api_key = PINECONE_API_KEY
# configure client
pc = Pinecone(api_key=api_key)

os.environ['PINECONE_API_KEY'] = PINECONE_API_KEY
index_name="chatbot"
index = pc.Index(index_name)
PineconeVectorStore(index_name=index_name,embedding=embeddings)

vectorstore_from_docs = PineconeVectorStore.from_documents(
    text_chunks,
    index_name=index_name,
    embedding=embeddings
)

# index.describe_index_stats()
vectorstore = PineconeVectorStore.from_existing_index(index_name=index_name, embedding=embeddings)
qa=RetrievalQA.from_chain_type(
    llm=llm, 
    chain_type="stuff", 
    retriever=vectorstore.as_retriever(search_kwargs={'k': 2}),
    return_source_documents=True, 
    chain_type_kwargs=chain_type_kwargs)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for answering but I'm getting this error now:NameError: name 'text_chunks' is not defined
Here's how I defined it for one of my projects: def load_pdf(data): loader = DirectoryLoader(data, glob="*pdf", loader_cls=PyPDFLoader) documents = loader.load() return documents extracted_data=load_pdf("data/") def text_split(extracted_data): text_splitter=RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap = 20) text_chunks=text_splitter.split_documents(extracted_data) return text_chunks text_chunks=text_split(extracted_data)
0

AttributeError: type object 'Pinecone' has no attribute 'from_existing_index'.

This happenes when there are two Pinecones that you installed when importing from langchain.vectorstore and also pinecone itself. so I the method so called 'from_existing_index' only exist in Pinecone from langchain. so when import from langchain ... use this:: 'from langchain.vectorstores import Pinecode as Pi' and then use Pi when you are trying to access the attribute 'from_existing_index'

I hope you understood it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.