ShellyMimo/Chatbot
0
1from flask import Flask, render_template, jsonify, request2from src.helper import download_hugging_face_embeddings3from langchain_pinecone import PineconeVectorStore4from pinecone import Pinecone5from langchain.prompts import PromptTemplate6from langchain.llms import Replicate7from langchain.chains import RetrievalQA8from dotenv import load_dotenv9from src.prompt import *10import os11 12 13app = Flask(__name__)14 15load_dotenv()16 17PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')18PINECONE_API_ENV = os.environ.get('PINECONE_API_ENV')19 20 21embeddings = download_hugging_face_embeddings()22 23#Initializing the Pinecone24pinecone_instance = Pinecone(api_key=PINECONE_API_KEY, environment='us-east-1')25 26index_name="medical-bot"27 28#Loading the index29docsearch=PineconeVectorStore.from_existing_index(index_name, embeddings)30 31 32PROMPT=PromptTemplate(template=prompt_template, input_variables=["context", "question"])33 34chain_type_kwargs={"prompt": PROMPT}35 36llm =Replicate(model="meta/llama-2-7b-chat")37 38 39qa=RetrievalQA.from_chain_type(40 llm=llm, 41 chain_type="stuff", 42 retriever=docsearch.as_retriever(search_kwargs={'k': 2}),43 return_source_documents=True, 44 chain_type_kwargs=chain_type_kwargs)45 46 47 48@app.route("/")49def index():50 return render_template('chat.html')51 52 53 54@app.route("/get", methods=["GET", "POST"])55def chat():56 msg = request.form["msg"]57 input = msg58 print(input)59 result=qa({"query": input})60 print("Response : ", result["result"])61 return str(result["result"])62 63 64 65if __name__ == '__main__':66 app.run(host="0.0.0.0", port= 8080, debug= True)67 68 69 