-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
72 lines (43 loc) · 1.76 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
from langchain.vectorstores import Chroma
from src.helper import load_embedding
from dotenv import load_dotenv
import os
from src.helper import repo_ingestion
from flask import Flask, render_template, jsonify, request
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationSummaryMemory
from langchain.chains import ConversationalRetrievalChain
app = Flask(__name__)
load_dotenv()
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
embeddings = load_embedding()
persist_directory = "db"
# Now we can load the persisted database from disk, and use it as normal.
vectordb = Chroma(persist_directory=persist_directory,
embedding_function=embeddings)
llm = ChatOpenAI()
memory = ConversationSummaryMemory(llm=llm, memory_key = "chat_history", return_messages=True)
qa = ConversationalRetrievalChain.from_llm(llm, retriever=vectordb.as_retriever(search_type="mmr", search_kwargs={"k":8}), memory=memory)
@app.route('/', methods=["GET", "POST"])
def index():
return render_template('index.html')
@app.route('/chatbot', methods=["GET", "POST"])
def gitRepo():
if request.method == 'POST':
user_input = request.form['question']
repo_ingestion(user_input)
os.system("python store_index.py")
return jsonify({"response": str(user_input) })
@app.route("/get", methods=["GET", "POST"])
def chat():
msg = request.form["msg"]
input = msg
print(input)
if input == "clear":
os.system("rm -rf repo")
result = qa(input)
print(result['answer'])
return str(result["answer"])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)