Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,87 +7,67 @@ import os
|
|
7 |
|
8 |
openai.api_key = os.environ["Secret"]
|
9 |
|
10 |
-
|
11 |
def find_closest_neighbors(vector1, dictionary_of_vectors):
|
12 |
"""
|
13 |
Takes a vector and a dictionary of vectors and returns the three closest neighbors
|
14 |
"""
|
15 |
-
|
16 |
-
# Convert the input string to a vector
|
17 |
vector = openai.Embedding.create(
|
18 |
input=vector1,
|
19 |
engine="text-embedding-ada-002"
|
20 |
)['data'][0]['embedding']
|
21 |
-
|
22 |
vector = np.array(vector)
|
23 |
|
24 |
-
# Finds cosine similarities between the vector and values in the dictionary and Creates a dictionary of cosine similarities with its text key
|
25 |
cosine_similarities = {}
|
26 |
for key, value in dictionary_of_vectors.items():
|
27 |
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
|
28 |
|
29 |
-
# Sorts the dictionary by value and returns the three highest values
|
30 |
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
|
31 |
match_list = sorted_cosine_similarities[0:4]
|
32 |
-
web = str(sorted_cosine_similarities[0][0])
|
33 |
-
return match_list
|
34 |
-
|
35 |
-
# Connect to the database
|
36 |
-
conn = sqlite3.connect('QRIdatabase7.db')
|
37 |
-
|
38 |
-
# Create a cursor
|
39 |
-
cursor = conn.cursor()
|
40 |
-
|
41 |
-
# Select the text and embedding from the chunks table
|
42 |
-
cursor.execute('''SELECT text, embedding FROM chunks''')
|
43 |
-
|
44 |
-
# Fetch the rows
|
45 |
-
rows = cursor.fetchall()
|
46 |
-
|
47 |
-
# Create a dictionary to store the text and embedding for each row
|
48 |
-
dictionary_of_vectors = {}
|
49 |
-
|
50 |
-
# Iterate through the rows and add them to the dictionary
|
51 |
-
for row in rows:
|
52 |
-
text = row[0]
|
53 |
-
embedding_str = row[1]
|
54 |
-
# Convert the embedding string to a NumPy array
|
55 |
-
embedding = np.fromstring(embedding_str, sep=' ')
|
56 |
-
dictionary_of_vectors[text] = embedding
|
57 |
|
58 |
-
|
59 |
-
conn.close()
|
60 |
|
61 |
-
def
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
# Find the closest neighbors
|
67 |
-
match_list = find_closest_neighbors(
|
68 |
-
|
69 |
-
# Create a string of the text from the closest neighbors
|
70 |
context = ''
|
71 |
for match in match_list:
|
72 |
-
context += str(match[0])
|
73 |
context = context[:-1500]
|
74 |
|
75 |
-
prep = f"This is an OpenAI model tuned to answer questions specific to the Qualia Research institute, a research institute that focuses on consciousness. Here is some question-specific context, and then the Question to answer, related to consciousness, the human experience, and phenomenology: {context}. Here is a question specific to QRI and consciousness in general Q: {
|
76 |
-
# Generate an answer
|
77 |
-
response = openai.ChatCompletion.create(
|
78 |
-
model="gpt-4",
|
79 |
-
messages=[
|
80 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
81 |
-
{"role": "user", "content": prep},
|
82 |
-
]
|
83 |
-
)
|
84 |
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
93 |
|
|
|
|
7 |
|
8 |
openai.api_key = os.environ["Secret"]
|
9 |
|
|
|
10 |
def find_closest_neighbors(vector1, dictionary_of_vectors):
|
11 |
"""
|
12 |
Takes a vector and a dictionary of vectors and returns the three closest neighbors
|
13 |
"""
|
|
|
|
|
14 |
vector = openai.Embedding.create(
|
15 |
input=vector1,
|
16 |
engine="text-embedding-ada-002"
|
17 |
)['data'][0]['embedding']
|
18 |
+
|
19 |
vector = np.array(vector)
|
20 |
|
|
|
21 |
cosine_similarities = {}
|
22 |
for key, value in dictionary_of_vectors.items():
|
23 |
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
|
24 |
|
|
|
25 |
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
|
26 |
match_list = sorted_cosine_similarities[0:4]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
return match_list
|
|
|
29 |
|
30 |
+
def predict(message, history):
|
31 |
+
# Connect to the database
|
32 |
+
conn = sqlite3.connect('QRIdatabase7.db')
|
33 |
+
cursor = conn.cursor()
|
34 |
+
cursor.execute('''SELECT text, embedding FROM chunks''')
|
35 |
+
rows = cursor.fetchall()
|
36 |
+
|
37 |
+
dictionary_of_vectors = {}
|
38 |
+
for row in rows:
|
39 |
+
text = row[0]
|
40 |
+
embedding_str = row[1]
|
41 |
+
embedding = np.fromstring(embedding_str, sep=' ')
|
42 |
+
dictionary_of_vectors[text] = embedding
|
43 |
+
conn.close()
|
44 |
|
45 |
# Find the closest neighbors
|
46 |
+
match_list = find_closest_neighbors(message, dictionary_of_vectors)
|
|
|
|
|
47 |
context = ''
|
48 |
for match in match_list:
|
49 |
+
context += str(match[0])
|
50 |
context = context[:-1500]
|
51 |
|
52 |
+
prep = f"This is an OpenAI model tuned to answer questions specific to the Qualia Research institute, a research institute that focuses on consciousness. Here is some question-specific context, and then the Question to answer, related to consciousness, the human experience, and phenomenology: {context}. Here is a question specific to QRI and consciousness in general Q: {message} A: "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
history_openai_format = []
|
55 |
+
for human, assistant in history:
|
56 |
+
history_openai_format.append({"role": "user", "content": human })
|
57 |
+
history_openai_format.append({"role": "assistant", "content":assistant})
|
58 |
+
history_openai_format.append({"role": "user", "content": prep})
|
59 |
|
60 |
+
response = openai.ChatCompletion.create(
|
61 |
+
model='gpt-3.5-turbo',
|
62 |
+
messages= history_openai_format,
|
63 |
+
temperature=1.0,
|
64 |
+
stream=True
|
65 |
+
)
|
66 |
+
|
67 |
+
partial_message = ""
|
68 |
+
for chunk in response:
|
69 |
+
if len(chunk['choices'][0]['delta']) != 0:
|
70 |
+
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
71 |
+
yield partial_message
|
72 |
|
73 |
+
gr.ChatInterface(predict).queue().launch()
|