anikapro commited on
Commit
1db403f
·
verified ·
1 Parent(s): dd331e2

Add RAG using semantic search method and knowledge base

Browse files
Files changed (1) hide show
  1. app.py +43 -1
app.py CHANGED
@@ -1,9 +1,51 @@
1
  import gradio as gr
2
  import random
3
  from huggingface_hub import InferenceClient
 
 
4
 
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", provider='hf-inference')
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def respond(message, history):
8
 
9
  system_message = "You are a knowledgable and friendly chatbot that gives good information."
@@ -14,7 +56,7 @@ def respond(message, history):
14
  if history:
15
  messages.extend(history)
16
 
17
- messages.append({"role": "user", "content": message})
18
 
19
  response = ""
20
 
 
1
  import gradio as gr
2
  import random
3
  from huggingface_hub import InferenceClient
4
+ from sentence_transformers import SentenceTransformer
5
+ import torch
6
 
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", provider='hf-inference')
8
 
9
+ #loading and processing knowledge base
10
+ with open("bookbans.txt", "r", encoding="utf-8") as file:
11
+ book_bans_text = file.read()
12
+
13
+ #cleaning and chunking text
14
+ cleaned_text = book_bans_text.strip()
15
+ chunks = cleaned_text.split("\n")
16
+ cleaned_chunks = []
17
+
18
+ for chunk in chunks:
19
+ stripped_chunk = chunk.strip()
20
+ if stripped_chunk:
21
+ cleaned_chunks.append(stripped_chunk)
22
+
23
+ #importing model for embeddings
24
+ model = SentenceTransformer('all-MiniLM-L6-v2')
25
+
26
+ chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
27
+
28
+ #function to get top chunks that are most similar to query by calculating similarity scores based off of embeddings
29
+ def get_top_chunk(message):
30
+ query_embedding = model.encode(message, convert_to_tensor=True)
31
+ query_embedding_normalized = query_embedding / query_embedding.norm()
32
+
33
+ chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
34
+
35
+ similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
36
+
37
+ top_indices = torch.topk(similarities, k=1).indices
38
+
39
+ top_chunks = []
40
+
41
+ for i in top_indices:
42
+ chunk = chunks[i]
43
+ top_chunks.append(chunk)
44
+
45
+ return top_chunks
46
+
47
+
48
+
49
  def respond(message, history):
50
 
51
  system_message = "You are a knowledgable and friendly chatbot that gives good information."
 
56
  if history:
57
  messages.extend(history)
58
 
59
+ messages.append({"role": "user", "content": message, get_top_chunk(message)})
60
 
61
  response = ""
62