from flask import Flask, request, render_template # Import the relevant modules and functions from your code from langchain.llms import GooglePalm from langchain.llms import LlamaCpp from transformers import pipeline from langchain.llms import HuggingFacePipeline from langchain.embeddings import GooglePalmEmbeddings from langchain.document_loaders.csv_loader import CSVLoader from langchain.embeddings import HuggingFaceInstructEmbeddings from langchain.vectorstores import FAISS from langchain.prompts import PromptTemplate from langchain.chains import RetrievalQA from langchain.text_splitter import RecursiveCharacterTextSplitter import torch app = Flask(__name__) # Initialize Langchain components (put this code outside the app route) #api_key = 'AIzaSyCZwsYvr3ht7ctxcrOLWvfppySP33ducmE' #llm = GooglePalm(google_api_key=api_key, temperature=0.1) 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"C:\Aiproject\mistral\Mistral-7B-v0.1", # model_type="mistral", # max_new_tokens = 1048, # temperature = 0.1,gpu_layers=50 # ) # device=torch.device('cpu') # from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # checkpoint = "MBZUAI/LaMini-T5-738M" # tokenizer = AutoTokenizer.from_pretrained(checkpoint) # base_model = AutoModelForSeq2SeqLM.from_pretrained( # checkpoint, # device_map=device, # torch_dtype=torch.float32 # ) # pipe = pipeline( # 'text2text-generation', # model = base_model, # tokenizer = tokenizer, # max_length = 256, # do_sample = True, # temperature = 0.1, # top_p= 0.95 # ) # llm = HuggingFacePipeline(pipeline=pipe) # Initialize instructor embeddings using the Hugging Face model instructor_embeddings = HuggingFaceInstructEmbeddings(model_name=r"C:\Aiproject\SupportGpt\model") # Load the data from CSV loader = CSVLoader(file_path=r"C:\Aiproject\codebasics_faqs.csv", encoding='iso-8859-1', source_column="Question") data = loader.load() text_splitter=RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=50) texts=text_splitter.split_documents(data) # Create a FAISS instance for vector database vectordb = FAISS.from_documents(documents=texts, embedding=instructor_embeddings) # Create a retriever for querying the vector database retriever = vectordb.as_retriever(score_threshold=0.7) # Define the prompt template # 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. 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} QUESTION: {question}""" # Create a prompt template PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) chain_type_kwargs = {"prompt": PROMPT} # Create the Langchain RetrievalQA chain 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) @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': question = request.form['question'] if question: result = chain(question)['result'] else: result = None return render_template('index1.html', question=question, result=result) return render_template('index1.html', question='', result=None) if __name__ == '__main__': app.run(host="0.0.0.0",debug=False)