forked from rrayatec/esearch-rag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
105 lines (87 loc) · 3.22 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
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
104
105
from flask import Flask, render_template, request, jsonify
from elasticsearch import Elasticsearch
from openai import OpenAI
import os
es_client = Elasticsearch(
os.environ["ES_ENDPOINT"],
api_key=os.environ["ES_API_KEY"]
)
openai_client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
)
index_source_fields = {
os.environ["ES_INDEX_NAME"]: [
"body_content",
"text"
]
}
client = OpenAI()
app = Flask(__name__)
def get_elasticsearch_results(query):
es_query = {
"retriever": {
"standard": {
"query": {
"knn": {
"field": "vector",
"num_candidates": 100,
"query_vector_builder": {
"text_embedding": {
"model_id": ".multilingual-e5-small_linux-x86_64",
"model_text": query
}
}
}
}
}
},
"size": 3
}
result = es_client.search(index=os.environ["ES_INDEX_NAME"], body=es_query)
return result["hits"]["hits"]
def create_openai_prompt(results):
context = ""
for hit in results:
inner_hit_path = f"{hit['_index']}.{index_source_fields.get(hit['_index'])[0]}"
## For semantic_text matches, we need to extract the text from the inner_hits
if 'inner_hits' in hit and inner_hit_path in hit['inner_hits']:
context += '\n --- \n'.join(inner_hit['_source']['text'] for inner_hit in hit['inner_hits'][inner_hit_path]['hits']['hits'])
else:
source_field = index_source_fields.get(hit["_index"])[1]
hit_context = hit["_source"][source_field]
context += f"{hit_context}\n"
prompt = f"""
Instructions:
- You are an assistant for question-answering tasks.
- Answer questions truthfully and factually using only the context presented.
- If you don't know the answer, just say that you don't know, don't make up an answer.
- You must always cite the document where the answer was extracted using inline academic citation style [], using the position.
- Use markdown format for code examples.
- You are correct, factual, precise, and reliable.
Context:
{context}
"""
return prompt
def generate_openai_completion(user_prompt, question):
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": user_prompt},
{"role": "user", "content": question},
]
)
return response.choices[0].message.content
@app.route("/")
def welcome():
return render_template('index.html', title='Chat App', message='Welcome to the chat!')
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
question =data.get('message')
elasticsearch_results = get_elasticsearch_results(question)
context_prompt = create_openai_prompt(elasticsearch_results)
openai_completion = generate_openai_completion(context_prompt, question)
print(openai_completion)
return jsonify({'response': openai_completion})
if __name__ == '__main__':
app.run(debug=True)