Spaces:
Runtime error
Runtime error
Commit ·
70cebce
1
Parent(s): 2c6308d
Create user_utils.py
Browse files- user_utils.py +43 -0
user_utils.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pinecone
|
| 2 |
+
from langchain.vectorstores import Pinecone
|
| 3 |
+
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
| 4 |
+
from langchain.llms import OpenAI
|
| 5 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 6 |
+
from langchain.callbacks import get_openai_callback
|
| 7 |
+
import joblib
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
#Function to pull index data from Pinecone
|
| 11 |
+
def pull_from_pinecone(pinecone_apikey,pinecone_environment,pinecone_index_name,embeddings):
|
| 12 |
+
|
| 13 |
+
pinecone.init(
|
| 14 |
+
api_key=pinecone_apikey,
|
| 15 |
+
environment=pinecone_environment
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
index_name = pinecone_index_name
|
| 19 |
+
|
| 20 |
+
index = Pinecone.from_existing_index(index_name, embeddings)
|
| 21 |
+
return index
|
| 22 |
+
|
| 23 |
+
def create_embeddings():
|
| 24 |
+
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 25 |
+
return embeddings
|
| 26 |
+
|
| 27 |
+
#This function will help us in fetching the top relevent documents from our vector store - Pinecone Index
|
| 28 |
+
def get_similar_docs(index,query,k=2):
|
| 29 |
+
|
| 30 |
+
similar_docs = index.similarity_search(query, k=k)
|
| 31 |
+
return similar_docs
|
| 32 |
+
|
| 33 |
+
def get_answer(docs,user_input):
|
| 34 |
+
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
| 35 |
+
with get_openai_callback() as cb:
|
| 36 |
+
response = chain.run(input_documents=docs, question=user_input)
|
| 37 |
+
return response
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def predict(query_result):
|
| 41 |
+
Fitmodel = joblib.load('modelsvm.pk1')
|
| 42 |
+
result=Fitmodel.predict([query_result])
|
| 43 |
+
return result[0]
|