Search data
Search data: Once the data is put into Tair, we will start querying the collection for the closest vectors. We may provide an additional parameter vector_name to switch from title to content-based search. Since the precomputed embeddings were created with text-embedding-ada-002 OpenAI model, we also have to use it during search. def query_tair(client, query, vector_name='title_vector', top_k=5): # Creates an embedding vector from the user query embedded_query = openai.Embedding.create( input=query, model='text-embedding-ada-002', )['data'][0]['embedding'] embedded_query = np.array(embedded_query) # Search for the top k approximate nearest neighbors of the vector in an index query_result = client.tvs_knnsearch(index=index+'_'+vector_name, k=top_k, vector=embedded_query) return query_result import openai import numpy as np query_result = query_tair(client=client, query='modern art in Europe', vector_name='title_vector') for i in range(len(query_result)): title = client.tvs_hmget(index+'_'+'content_vector', query_result[i][0].decode('utf-8'), 'title') print(f'{i + 1}. {title[0].decode('utf-8')} (Distance: {round(query_result[i][1],3)})') # This time we'll query using content vector query_result = query_tair(client=client, query='Famous battles in Scottish history', vector_name='content_vector') for i in range(len(query_result)): title = client.tvs_hmget(index+'_'+'content_vector', query_result[i][0].decode('utf-8'), 'title') print(f'{i + 1}. {title[0].decode('utf-8')} (Distance: {round(query_result[i][1],3)})')