Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,31 +2,25 @@ import sklearn
|
|
2 |
import sqlite3
|
3 |
import numpy as np
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
-
import
|
6 |
import os
|
7 |
import gradio as gr
|
8 |
|
9 |
-
|
10 |
-
openai.api_key = os.environ["Secret"]
|
11 |
|
12 |
def find_closest_neighbors(vector1, dictionary_of_vectors):
|
13 |
-
"""
|
14 |
-
|
15 |
-
"""
|
16 |
-
vector = openai.Embedding.create(
|
17 |
input=vector1,
|
18 |
-
|
19 |
-
)
|
20 |
-
|
21 |
vector = np.array(vector)
|
22 |
-
|
23 |
cosine_similarities = {}
|
24 |
for key, value in dictionary_of_vectors.items():
|
25 |
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
|
26 |
-
|
27 |
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
|
28 |
match_list = sorted_cosine_similarities[0:4]
|
29 |
-
|
30 |
return match_list
|
31 |
|
32 |
def predict(message, history):
|
@@ -35,7 +29,6 @@ def predict(message, history):
|
|
35 |
cursor = conn.cursor()
|
36 |
cursor.execute('''SELECT text, embedding FROM chunks''')
|
37 |
rows = cursor.fetchall()
|
38 |
-
|
39 |
dictionary_of_vectors = {}
|
40 |
for row in rows:
|
41 |
text = row[0]
|
@@ -43,44 +36,47 @@ def predict(message, history):
|
|
43 |
embedding = np.fromstring(embedding_str, sep=' ')
|
44 |
dictionary_of_vectors[text] = embedding
|
45 |
conn.close()
|
46 |
-
|
47 |
# Find the closest neighbors
|
48 |
match_list = find_closest_neighbors(message, dictionary_of_vectors)
|
49 |
context = ''
|
50 |
for match in match_list:
|
51 |
context += str(match[0])
|
52 |
-
context = context[
|
53 |
-
|
54 |
-
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:
|
55 |
-
|
56 |
-
|
|
|
57 |
for human, assistant in history:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
model=
|
64 |
-
messages=
|
65 |
temperature=1.0,
|
66 |
stream=True
|
67 |
)
|
68 |
|
69 |
partial_message = ""
|
70 |
-
for chunk in
|
71 |
-
if
|
72 |
-
partial_message
|
73 |
-
yield partial_message
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
-
demo.launch()
|
|
|
2 |
import sqlite3
|
3 |
import numpy as np
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
+
from openai import OpenAI
|
6 |
import os
|
7 |
import gradio as gr
|
8 |
|
9 |
+
client = OpenAI(api_key=os.environ["Secret"])
|
|
|
10 |
|
11 |
def find_closest_neighbors(vector1, dictionary_of_vectors):
|
12 |
+
"""Takes a vector and a dictionary of vectors and returns the three closest neighbors"""
|
13 |
+
vector = client.embeddings.create(
|
|
|
|
|
14 |
input=vector1,
|
15 |
+
model="text-embedding-ada-002"
|
16 |
+
).data[0].embedding
|
17 |
+
|
18 |
vector = np.array(vector)
|
|
|
19 |
cosine_similarities = {}
|
20 |
for key, value in dictionary_of_vectors.items():
|
21 |
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
|
|
|
22 |
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
|
23 |
match_list = sorted_cosine_similarities[0:4]
|
|
|
24 |
return match_list
|
25 |
|
26 |
def predict(message, history):
|
|
|
29 |
cursor = conn.cursor()
|
30 |
cursor.execute('''SELECT text, embedding FROM chunks''')
|
31 |
rows = cursor.fetchall()
|
|
|
32 |
dictionary_of_vectors = {}
|
33 |
for row in rows:
|
34 |
text = row[0]
|
|
|
36 |
embedding = np.fromstring(embedding_str, sep=' ')
|
37 |
dictionary_of_vectors[text] = embedding
|
38 |
conn.close()
|
39 |
+
|
40 |
# Find the closest neighbors
|
41 |
match_list = find_closest_neighbors(message, dictionary_of_vectors)
|
42 |
context = ''
|
43 |
for match in match_list:
|
44 |
context += str(match[0])
|
45 |
+
context = context[:1500] # Limit context length
|
46 |
+
|
47 |
+
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: "
|
48 |
+
|
49 |
+
messages = []
|
50 |
+
# Convert history to the expected format
|
51 |
for human, assistant in history:
|
52 |
+
messages.append({"role": "user", "content": human})
|
53 |
+
messages.append({"role": "assistant", "content": assistant})
|
54 |
+
messages.append({"role": "user", "content": prep})
|
55 |
+
|
56 |
+
stream = client.chat.completions.create(
|
57 |
+
model="gpt-3.5-turbo",
|
58 |
+
messages=messages,
|
59 |
temperature=1.0,
|
60 |
stream=True
|
61 |
)
|
62 |
|
63 |
partial_message = ""
|
64 |
+
for chunk in stream:
|
65 |
+
if chunk.choices[0].delta.content is not None:
|
66 |
+
partial_message += chunk.choices[0].delta.content
|
67 |
+
yield partial_message
|
68 |
|
69 |
+
with gr.Blocks(title="QRI Research Assistant") as demo:
|
70 |
+
chatbot = gr.ChatInterface(
|
71 |
+
predict,
|
72 |
+
title="QRI Research Assistant",
|
73 |
+
description="Ask questions about consciousness, human experience, and phenomenology based on QRI research.",
|
74 |
+
examples=[
|
75 |
+
"What is consciousness?",
|
76 |
+
"How does QRI approach the study of phenomenology?",
|
77 |
+
"What are the key theories about qualia?"
|
78 |
+
]
|
79 |
+
)
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
+
demo.launch()
|