import openai import sqlite3 import numpy as np from sklearn.metrics.pairwise import cosine_similarity import gradio as gr # Your OpenAI API Key openai.api_key = os.environ["Secret"] # Connect to the SQLite database db_path = "text_chunks_with_embeddings.db" # Update with the path to your database conn = sqlite3.connect(db_path) cursor = conn.cursor() # Fetch the rows from the database cursor.execute("SELECT text, embedding FROM chunks") rows = cursor.fetchall() # Create a dictionary to store the text and embedding for each row dictionary_of_vectors = {} for row in rows: text = row[0] embedding_str = row[1] embedding = np.fromstring(embedding_str, sep=' ') dictionary_of_vectors[text] = embedding # Close the connection conn.close() def find_closest_neighbors(vector): 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] sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True) return sorted_cosine_similarities[0:4] def generate_embedding(text): response = openai.Embedding.create( input=text, engine="text-embedding-ada-002" ) embedding = np.array(response['data'][0]['embedding']) return embedding def context_gpt_response(question): vector = generate_embedding(question) match_list = find_closest_neighbors(vector) context = '' for match in match_list: context += str(match[0]) context = context[:1500] # Limit context to the last 1500 characters prep = f"This is an OpenAI model designed to answer questions specific to grant-making applications for an aquarium. Here is some question-specific context: {context}. Q: {question} A: " response = openai.Completion.create( engine="gpt-4", prompt=prep, temperature=0.7, max_tokens=220, ) return response['choices'][0]['text'] iface = gr.Interface(fn=context_gpt_response, inputs="text", outputs="text", title="Aquarium Grant Application Chatbot", description="Context-specific chatbot for grant writing", examples=[["What types of projects are eligible for funding?"], ["Tell me more about the application process."], ["What will be the most impactful grant opportunities?"]]) iface.launch()