-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (53 loc) · 2.05 KB
/
app.py
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
from flask import Flask, render_template, jsonify, request
from src.helper import download_hugging_face_embeddings
from langchain.vectorstores import Pinecone
import pinecone
from langchain.prompts import PromptTemplate
from langchain.llms import CTransformers
from langchain.chains import RetrievalQA
from dotenv import load_dotenv
from src.prompt import *
import os
#initializing the flask
app=Flask(__name__)
load_dotenv()
PINECONE_API_KEY=os.environ.get('PINECONE_API_KEY')
PINECONE_API_ENV=os.environ.get('PINECONE_API_ENV')
os.environ["PINECONE_API_KEY"] = PINECONE_API_KEY
os.environ["OPENAI_API_KEY"] = OPENAI_AP
#download the embeddings
embeddings=download_hugging_face_embeddings()
#Initializing the Pinecone
pinecone.init(api_key=PINECONE_API_KEY,
environment=PINECONE_API_ENV)
#give index name
index_name="medichatbot"
# Embed each chunk and upsert the embeddings into your Pinecone index.
docsearch = PineconeVectorStore.from_existing_index(
index_name=index_name,
embedding=embeddings
)
retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k":3})
llm = OpenAI(temperature=0.4, max_tokens=500)
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm, prompt)
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
@app.route("/")
def index():
return render_template('chat.html') #it will open the chat.html file
#final route
@app.route("/get", methods=["GET", "POST"])
def chat():
msg = request.form["msg"] #when user will give msg . msg is taking in backedn
input = msg #set the msg inninput variable
print(input) #print the msg
response = rag_chain.invoke({"input": msg}) #sending the msg to qa object which we defind
print("Response : ", response["answer"]) #give the response
return str(response["answer"]) #response print in my terminal as well as send to my UI
if __name__ == '__main__':
app.run(host="0.0.0.0",port=8080,debug=True)