File size: 1,981 Bytes
253d94a
 
 
 
bdea572
d4c5755
253d94a
 
 
 
 
d4c5755
253d94a
 
bdea572
253d94a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ebe73e
38c1fba
bdea572
 
38c1fba
 
0ebe73e
bdea572
 
 
 
38c1fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Pinecone team has been making a lot of changes to there code and here is how it should be used going forward :)
from pinecone import Pinecone as PineconeClient
#from langchain.vectorstores import Pinecone     #This import has been replaced by the below one :)
from langchain_community.vectorstores import Pinecone
from transformers import pipeline

#from langchain.llms import OpenAI #This import has been replaced by the below one :)
from langchain_openai import OpenAI
from langchain.chains.question_answering import load_qa_chain
#from langchain.callbacks import get_openai_callback #This import has been replaced by the below one :)
from langchain_community.callbacks import get_openai_callback
from langchain_community.embeddings import SentenceTransformerEmbeddings
import joblib

qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")

#Function to pull index data from Pinecone...
def pull_from_pinecone(pinecone_apikey,pinecone_environment,pinecone_index_name,embeddings):

    PineconeClient(
    api_key=pinecone_apikey,
    environment=pinecone_environment
    )

    index_name = pinecone_index_name

    index = Pinecone.from_existing_index(index_name, embeddings)
    return index

def create_embeddings():
    embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
    return embeddings

#This function will help us in fetching the top relevent documents from our vector store - Pinecone Index
def get_similar_docs(index,query,k=2):

    similar_docs = index.similarity_search(query, k=k)
    return similar_docs


def get_answer(docs, user_input):
    # Concatenate all the documents into one large context
    # Assuming 'doc.page_content' is how the content is stored in your 'Document' object
    context = " ".join([doc.page_content for doc in docs])

    # Use Hugging Face's QA model to get the answer
    response = qa_pipeline(question=user_input, context=context)

    return response['answer']