Update 'Supportgpt/app.py'

This commit is contained in:
2024-01-08 04:57:52 +00:00
parent c5d5ea9a24
commit 7a6ec54ac4
+110 -110
View File
@@ -1,110 +1,110 @@
from flask import Flask, request, render_template from flask import Flask, request, render_template
# Import the relevant modules and functions from your code # Import the relevant modules and functions from your code
from langchain.llms import GooglePalm from langchain.llms import GooglePalm
from langchain.llms import LlamaCpp from langchain.llms import LlamaCpp
from transformers import pipeline from transformers import pipeline
from langchain.llms import HuggingFacePipeline from langchain.llms import HuggingFacePipeline
from langchain.embeddings import GooglePalmEmbeddings from langchain.embeddings import GooglePalmEmbeddings
from langchain.document_loaders.csv_loader import CSVLoader from langchain.document_loaders.csv_loader import CSVLoader
from langchain.embeddings import HuggingFaceInstructEmbeddings from langchain.embeddings import HuggingFaceInstructEmbeddings
from langchain.vectorstores import FAISS from langchain.vectorstores import FAISS
from langchain.prompts import PromptTemplate from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA from langchain.chains import RetrievalQA
from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.text_splitter import RecursiveCharacterTextSplitter
import torch import torch
app = Flask(__name__) app = Flask(__name__)
# Initialize Langchain components (put this code outside the app route) # Initialize Langchain components (put this code outside the app route)
#api_key = 'AIzaSyCZwsYvr3ht7ctxcrOLWvfppySP33ducmE' #api_key = 'AIzaSyCZwsYvr3ht7ctxcrOLWvfppySP33ducmE'
#llm = GooglePalm(google_api_key=api_key, temperature=0.1) #llm = GooglePalm(google_api_key=api_key, temperature=0.1)
from langchain.llms import CTransformers from langchain.llms import CTransformers
llm = CTransformers(model=r"C:\Aiproject\mainmodel\llama-2-7b-chat.ggmlv3.q8_0.bin",model_type="llama",temperature=0.1,gpu_layers=50,do_sample=True) llm = CTransformers(model=r"TheBloke/Llama-2-7B-Chat-GGML",model_type="llama",temperature=0.1,gpu_layers=50,do_sample=True)
# llm = CTransformers( # llm = CTransformers(
# model = r"C:\Aiproject\mistral\Mistral-7B-v0.1", # model = r"C:\Aiproject\mistral\Mistral-7B-v0.1",
# model_type="mistral", # model_type="mistral",
# max_new_tokens = 1048, # max_new_tokens = 1048,
# temperature = 0.1,gpu_layers=50 # temperature = 0.1,gpu_layers=50
# ) # )
# device=torch.device('cpu') # device=torch.device('cpu')
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# checkpoint = "MBZUAI/LaMini-T5-738M" # checkpoint = "MBZUAI/LaMini-T5-738M"
# tokenizer = AutoTokenizer.from_pretrained(checkpoint) # tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# base_model = AutoModelForSeq2SeqLM.from_pretrained( # base_model = AutoModelForSeq2SeqLM.from_pretrained(
# checkpoint, # checkpoint,
# device_map=device, # device_map=device,
# torch_dtype=torch.float32 # torch_dtype=torch.float32
# ) # )
# pipe = pipeline( # pipe = pipeline(
# 'text2text-generation', # 'text2text-generation',
# model = base_model, # model = base_model,
# tokenizer = tokenizer, # tokenizer = tokenizer,
# max_length = 256, # max_length = 256,
# do_sample = True, # do_sample = True,
# temperature = 0.1, # temperature = 0.1,
# top_p= 0.95 # top_p= 0.95
# ) # )
# llm = HuggingFacePipeline(pipeline=pipe) # llm = HuggingFacePipeline(pipeline=pipe)
# Initialize instructor embeddings using the Hugging Face model # Initialize instructor embeddings using the Hugging Face model
instructor_embeddings = HuggingFaceInstructEmbeddings(model_name=r"C:\Aiproject\SupportGpt\model") instructor_embeddings = HuggingFaceInstructEmbeddings(model_name=r"sentence-transformers/all-MiniLM-L6-v2")
# Load the data from CSV # Load the data from CSV
loader = CSVLoader(file_path=r"C:\Aiproject\codebasics_faqs.csv", encoding='iso-8859-1', source_column="Question") loader = CSVLoader(file_path=r"./supportqa.csv", encoding='iso-8859-1', source_column="Question")
data = loader.load() data = loader.load()
text_splitter=RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=50) text_splitter=RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=50)
texts=text_splitter.split_documents(data) texts=text_splitter.split_documents(data)
# Create a FAISS instance for vector database # Create a FAISS instance for vector database
vectordb = FAISS.from_documents(documents=texts, embedding=instructor_embeddings) vectordb = FAISS.from_documents(documents=texts, embedding=instructor_embeddings)
# Create a retriever for querying the vector database # Create a retriever for querying the vector database
retriever = vectordb.as_retriever(score_threshold=0.7) retriever = vectordb.as_retriever(score_threshold=0.7)
# Define the prompt template # Define the prompt template
# prompt_template = """Use the following pieces of information to answer the user's question,dont give additional answers. # prompt_template = """Use the following pieces of information to answer the user's question,dont give additional answers.
# If you don't know the answer, just say that you don't know, don't try to make up an answer. # If you don't know the answer, just say that you don't know, don't try to make up an answer.
prompt_template="""Use the following peices of information to answer the user's question and do not give any additional or extra answers that do not exist in the given information.If the answer doesn't exist in the given information, say "I dont know". If the answer exists in the information, give the exact answer as it exists in the given information.Users may use different keywords or synonyms, so use at least 10 keywords or synonyms from the user's question to identify exactly what the user wants to know from given information.Please refrain from making your own answers or modifying the answers. prompt_template="""Use the following peices of information to answer the user's question and do not give any additional or extra answers that do not exist in the given information.If the answer doesn't exist in the given information, say "I dont know". If the answer exists in the information, give the exact answer as it exists in the given information.Users may use different keywords or synonyms, so use at least 10 keywords or synonyms from the user's question to identify exactly what the user wants to know from given information.Please refrain from making your own answers or modifying the answers.
CONTEXT: {context} CONTEXT: {context}
QUESTION: {question}""" QUESTION: {question}"""
# Create a prompt template # Create a prompt template
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
chain_type_kwargs = {"prompt": PROMPT} chain_type_kwargs = {"prompt": PROMPT}
# Create the Langchain RetrievalQA chain # Create the Langchain RetrievalQA chain
chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, input_key="query", chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, input_key="query",
return_source_documents=True, chain_type_kwargs=chain_type_kwargs) return_source_documents=True, chain_type_kwargs=chain_type_kwargs)
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def index(): def index():
if request.method == 'POST': if request.method == 'POST':
question = request.form['question'] question = request.form['question']
if question: if question:
result = chain(question)['result'] result = chain(question)['result']
else: else:
result = None result = None
return render_template('index1.html', question=question, result=result) return render_template('index1.html', question=question, result=result)
return render_template('index1.html', question='', result=None) return render_template('index1.html', question='', result=None)
if __name__ == '__main__': if __name__ == '__main__':
app.run(host="0.0.0.0",debug=False) app.run(host="0.0.0.0",debug=False)