Curranj commited on
Commit
7f28337
1 Parent(s): 9f50973

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -54
app.py CHANGED
@@ -13,80 +13,63 @@ def find_closest_neighbors(vector1, dictionary_of_vectors):
13
  """
14
  Takes a vector and a dictionary of vectors and returns the three closest neighbors
15
  """
16
-
17
- # Convert the input string to a vector
18
  vector = openai.Embedding.create(
19
  input=vector1,
20
  engine="text-embedding-ada-002"
21
  )['data'][0]['embedding']
22
-
23
  vector = np.array(vector)
24
 
25
- # Finds cosine similarities between the vector and values in the dictionary and Creates a dictionary of cosine similarities with its text key
26
  cosine_similarities = {}
27
  for key, value in dictionary_of_vectors.items():
28
  cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
29
 
30
- # Sorts the dictionary by value and returns the three highest values
31
  sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
32
  match_list = sorted_cosine_similarities[0:4]
33
- web = str(sorted_cosine_similarities[0][0])
34
- return match_list
35
-
36
- # Connect to the database
37
- conn = sqlite3.connect('QRIdatabase7.db')
38
-
39
- # Create a cursor
40
- cursor = conn.cursor()
41
-
42
- # Select the text and embedding from the chunks table
43
- cursor.execute('''SELECT text, embedding FROM chunks''')
44
-
45
- # Fetch the rows
46
- rows = cursor.fetchall()
47
-
48
- # Create a dictionary to store the text and embedding for each row
49
- dictionary_of_vectors = {}
50
 
51
- # Iterate through the rows and add them to the dictionary
52
- for row in rows:
53
- text = row[0]
54
- embedding_str = row[1]
55
- # Convert the embedding string to a NumPy array
56
- embedding = np.fromstring(embedding_str, sep=' ')
57
- dictionary_of_vectors[text] = embedding
58
 
59
- # Close the connection
60
- conn.close()
61
-
62
- def context_gpt_response(question):
63
- """
64
- Takes a question and returns an answer
65
- """
 
 
 
 
 
 
 
66
 
67
  # Find the closest neighbors
68
- match_list = find_closest_neighbors(question, dictionary_of_vectors)
69
-
70
- # Create a string of the text from the closest neighbors
71
  context = ''
72
  for match in match_list:
73
- context += str(match[0])
74
  context = context[:-1500]
75
 
76
- 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: {question} A: "
77
- # Generate an answer
78
- response = openai.Completion.create(
79
- engine="gpt-4",
80
- prompt=prep,
81
- temperature=0.7,
82
- max_tokens=220,
83
- )
84
-
85
 
86
- # Return the answer
87
- return response['choices'][0]['text']
 
 
 
88
 
89
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- iface = gr.Interface(fn=context_gpt_response, inputs="text", outputs="text",title="Qualia Research Institute GPTbot", description="Ask any question and get QRI specific answers!", examples=[["What is QRI?"], ["What is the Symmetry Theory of Valence?"], ["Explain Logarithmic scales of pain and pleasure"]])
92
- iface.launch()
 
13
  """
14
  Takes a vector and a dictionary of vectors and returns the three closest neighbors
15
  """
 
 
16
  vector = openai.Embedding.create(
17
  input=vector1,
18
  engine="text-embedding-ada-002"
19
  )['data'][0]['embedding']
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):
33
+ # Connect to the database
34
+ conn = sqlite3.connect('QRIdatabase7.db')
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]
42
+ embedding_str = row[1]
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[:-1500]
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: {message} A: "
 
 
 
 
 
 
 
 
55
 
56
+ history_openai_format = []
57
+ for human, assistant in history:
58
+ history_openai_format.append({"role": "user", "content": human })
59
+ history_openai_format.append({"role": "assistant", "content":assistant})
60
+ history_openai_format.append({"role": "user", "content": prep})
61
 
62
+ response = openai.ChatCompletion.create(
63
+ model='gpt-3.5-turbo',
64
+ messages= history_openai_format,
65
+ temperature=1.0,
66
+ stream=True
67
+ )
68
+
69
+ partial_message = ""
70
+ for chunk in response:
71
+ if len(chunk['choices'][0]['delta']) != 0:
72
+ partial_message = partial_message + chunk['choices'][0]['delta']['content']
73
+ yield partial_message
74
 
75
+ gr.ChatInterface(predict).queue().launch()