File size: 3,182 Bytes
1cb79c4
 
9c712e1
1cb79c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9549d73
1cb79c4
9549d73
1cb79c4
 
0db5fea
1cb79c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c712e1
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from flask import Flask, render_template, jsonify, request
from src.helper import download_hugging_face_embeddings
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS

from langchain.prompts import PromptTemplate
from langchain_groq import ChatGroq
# from langchain.llms import CTransformers
from langchain.chains import RetrievalQA
from langchain_core.prompts import ChatPromptTemplate
from dotenv import load_dotenv
from src.prompt import *
import os
from store_index import create_vector_db
app = Flask(__name__)

# load_dotenv()

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


# DATA_PATH = '/kaggle/input/book-pdf'
# DB_FAISS_PATH = r'G:\Chatbot\data\vector'

# Create vector database
#create_vector_db()
print("###333")
# Call the function directly in the cell

'''#Initializing the Pinecone
pinecone.init(api_key=PINECONE_API_KEY,
              environment=PINECONE_API_ENV)

index_name="medical-bot"'''

#embeddings = download_hugging_face_embeddings()
# Load the FAISS vector database
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
# also i change here
# db = FAISS.load_local(DB_FAISS_PATH, embeddings)
DB_FAISS_PATH = "data/vector"
print("vector_base is loading from the folder")
db = FAISS.load_local(DB_FAISS_PATH, embeddings,
                      allow_dangerous_deserialization=True)
# Loading the index
# db = FAISS.load_local(r"G:\Chatbot\DB_FAISS_PATH",embeddings, allow_dangerous_deserialization=True)


# docsearch=Pinecone.from_existing_index(index_name, embeddings)

# Create a ChatPromptTemplate
prompt = ChatPromptTemplate.from_template(prompt_template)

chain_type_kwargs = {"prompt": prompt}

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

chain_type_kwargs = {"prompt": PROMPT}'''

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

# Initialize the LLM
# llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
# groq_api_key = ('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')
# Load the GROQ and OpenAI API keys
groq_api_key = os.getenv('GROQ_API_KEY')
# os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
# Initialize the LLM
llm = ChatGroq(groq_api_key=groq_api_key,
               model_name="Llama3-8b-8192")


qa = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=db.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=7860, debug=True)