-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
150 lines (135 loc) · 4.74 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from flask import Flask, request, jsonify, render_template_string
import os
import openai
import json
from openai import OpenAI
openai.api_key = os.getenv('OPENAI_API_KEY')
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
from pinecone import Pinecone
app = Flask(__name__)
pinecone_api_key = os.getenv('PINECONE_API_KEY')
pinecone_db = Pinecone(
api_key=pinecone_api_key
)
# Create a Pinecone index if it doesn't exist
index_name = 'example-index'
# Connect to the Pinecone index
index = pinecone_db.Index(index_name)
# Create a Pinecone index if it doesn't exist
index_name = 'example-index'
# HTML template for the form page
form_page = '''
<!DOCTYPE html>
<html>
<head>
<title>Query Form</title>
</head>
<body>
<h1>Submit Your Query</h1>
<form id="queryForm" action="/query">
<label for="query">Query:</label>
<input type="text" id="query" name="query">
<br/>
<br/>
<label for="top_k">Top N results:</label>
<input type="number" id="top_k" name="top_k">
<br/>
<br/>
<button type="submit">Submit</button>
</form>
<div id="result"></div>
<script>
document.getElementById('queryForm').addEventListener('submit', function(event) {
event.preventDefault();
var query = document.getElementById('query').value;
var topK = document.getElementById('top_k').value;
fetch('/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ query: query, top_k: topK })
})
.then(response => {
return response.json()
})
.then(data => {
var result = document.getElementById('result')
result.innerHTML = '';
var llmResponse = document.createElement('p')
llmResponse.textContent = data['response'];
var contextualHeader = document.createElement("h1");
contextualHeader.textContent = 'Augmented Results';
result.appendChild(llmResponse);
result.appendChild(contextualHeader);
var newList = document.createElement("ul");
newList.id = 'rag_data';
data['matches'].forEach(function(value){
console.log(value);
var newListItem = document.createElement("li");
var newLink = document.createElement("a");
newLink.href = value['id']
newLink.textContent = value['metadata']['source']
var newParagraph = document.createElement("p");
newParagraph.textContent = value['metadata']['content']
newListItem.appendChild(newLink);
newListItem.appendChild(newParagraph);
newList.appendChild(newListItem);
})
result.appendChild(newList);
})
.catch(error => {
console.error('Error:', error);
});
});
</script>
</body>
</html>
'''
@app.route('/')
def home():
return render_template_string(form_page)
def get_feedback(user_prompt: str) -> str:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """
You are a staff data engineer trying to teach people!
If you use any augmented information please provide the link!
"""
},
{"role": "user", "content": user_prompt},
],
temperature=0.2
)
return response.choices[0].message.content
@app.route('/query', methods=['POST'])
def query():
data = request.get_json()
if not data or 'query' not in data:
return jsonify({"error": "No query provided"}), 400
openai_embeddings = openai.embeddings.create(input=data['query'], model="text-embedding-ada-002")
vector = openai_embeddings.data[0].embedding
try:
top_k = int(data['top_k'])
except Exception as e:
top_k = 0
matches = []
if top_k > 0:
result = index.query(
vector=vector,
top_k=top_k,
include_metadata=True
)
matches = list(map(lambda x: {
'id': x['id'],
'metadata': x['metadata']
}, result['matches']))
jsonified_matches = '------- here is some augmented info: ' + str(json.dumps(matches)) if len(matches) > 0 else ''
prompt = str(data['query']) + str(jsonified_matches)
print(prompt)
response = get_feedback(prompt)
print(response)
return {'matches': matches, 'response': response}
if __name__ == '__main__':
app.run(debug=True)