-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.py
66 lines (51 loc) · 1.71 KB
/
server.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
from pathlib import Path
from langchain.text_splitter import CharacterTextSplitter
import faiss
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
import pickle
from langchain import OpenAI, LLMChain
from langchain.prompts import Prompt
from flask import Flask, request, jsonify
import os
index = faiss.read_index("training.index")
with open("faiss.pkl", "rb") as f:
store = pickle.load(f)
store.index = index
with open("training/prompt/master.txt", "r") as f:
promptTemplate = f.read()
prompt = Prompt(template=promptTemplate, input_variables=["history", "context", "question"])
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
return "API Online"
@app.route("/", methods=["POST"])
def ask():
reqData = request.get_json()
if reqData['secret'] == os.environ["API_SECRET"]:
try:
llmChain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0.25, model_name="text-davinci-003", openai_api_key=os.environ["OPENAI_API_KEY"]))
def onMessage(question, history):
docs = store.similarity_search(question)
contexts = []
for i, doc in enumerate(docs):
contexts.append(f"Context {i}:\n{doc.page_content}")
answer = llmChain.predict(question=question, context="\n\n".join(contexts), history=history)
return answer
return jsonify({
"answer": onMessage(reqData['question'], reqData['history']),
"success": True
})
except:
return jsonify({
"answer": None,
"success": False,
"message": "Error"
}), 400
else:
return jsonify({
"answer": None,
"success": False,
"message": "Unauthorised"
})
app.run(host="0.0.0.0", port=3000)