Spaces:
Running
Running
import sklearn | |
import sqlite3 | |
import numpy as np | |
from sklearn.metrics.pairwise import cosine_similarity | |
import openai | |
import os | |
openai.api_key = os.environ["Secret"] | |
def find_closest_neighbors(vector1, dictionary_of_vectors): | |
""" | |
Takes a vector and a dictionary of vectors and returns the three closest neighbors | |
""" | |
# Convert the input string to a vector | |
vector = openai.Embedding.create( | |
input=vector1, | |
engine="text-embedding-ada-002" | |
)['data'][0]['embedding'] | |
vector = np.array(vector) | |
# Finds cosine similarities between the vector and values in the dictionary and Creates a dictionary of cosine similarities with its text key | |
cosine_similarities = {} | |
for key, value in dictionary_of_vectors.items(): | |
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0] | |
# Sorts the dictionary by value and returns the three highest values | |
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True) | |
match_list = sorted_cosine_similarities[0:4] | |
web = str(sorted_cosine_similarities[0][0]) | |
return match_list | |
# Connect to the database | |
conn = sqlite3.connect('QRIdatabase.db') | |
# Create a cursor | |
cursor = conn.cursor() | |
# Select the text and embedding from the chunks table | |
cursor.execute('''SELECT text, embedding FROM chunks''') | |
# Fetch the rows | |
rows = cursor.fetchall() | |
# Create a dictionary to store the text and embedding for each row | |
dictionary_of_vectors = {} | |
# Iterate through the rows and add them to the dictionary | |
for row in rows: | |
text = row[0] | |
embedding_str = row[1] | |
# Convert the embedding string to a NumPy array | |
embedding = np.fromstring(embedding_str, sep=' ') | |
dictionary_of_vectors[text] = embedding | |
# Close the connection | |
conn.close() | |
def context_gpt_response(question): | |
""" | |
Takes a question and returns an answer | |
""" | |
# Find the closest neighbors | |
match_list = find_closest_neighbors(question, dictionary_of_vectors) | |
# Create a string of the text from the closest neighbors | |
context = '' | |
for match in match_list: | |
context += str(match[0]) | |
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: " | |
# Generate an answer | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prep, | |
temperature=0.7, | |
max_tokens=200, | |
) | |
# Return the answer | |
return response['choices'][0]['text'] | |
import gradio as gr | |
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"]]) | |
iface.launch() | |