GIZ/Development-Project-Synergy-Finder
2
1import faiss
2
3"""
4Semantic Search Function
5"""
6def search(query, model, embeddings, filtered_df, top_x=20):
7
8 filtered_df_indecies_list = filtered_df.index
9 filtered_embeddings = embeddings[filtered_df_indecies_list]
10
11 # Load or create FAISS index
12 dimension = filtered_embeddings.shape[1]
13 faiss_index = faiss.IndexFlatL2(dimension)
14 faiss_index.add(filtered_embeddings)
15
16 # Convert query to embedding
17 query_embedding = model.encode([query])[0].reshape(1, -1)
18
19 # Perform search
20 D, I = faiss_index.search(query_embedding, k=top_x) # Search for top x similar items
21
22 # Extract the sentences corresponding to the top indices
23 top_indecies = [i for i in I[0]]
24
25 return filtered_df.iloc[top_indecies] 