eliasangels commited on
Commit
d36c237
·
verified ·
1 Parent(s): 43d8ca7

chatbot infrastructure + basic rag outline

Browse files
Files changed (1) hide show
  1. model.py +69 -1
model.py CHANGED
@@ -1,4 +1,72 @@
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
+ from huggingface_hub import InferenceClient
5
+
6
+ # opening the file when it's ready should go here
7
+ # finlitText = file.read()
8
+
9
+ model = SentenceTransformer('all-MiniLM-L6-v2')
10
+
11
+ def preprocessText(text):
12
+ cleanedText = text.strip()
13
+ chunks = cleanedText.strip("\n")
14
+ cleanedChunks = [chunk.strip() for chunk in chunks if chunk is not None]
15
+ return cleanedChunks
16
+
17
+ def createEmbeddings(textChunks):
18
+ chunkEmbeddings = model.encode(cleanedChunks, convert_to_tensor = True)
19
+ return chunkEmbeddings
20
+
21
+ def getTopChunks(query, chunkEmbeddings, textChunks):
22
+ queryEmbedding = model.encode(query, convert_to_tensor = True)
23
+ queryEmbeddingNormalized = queryEmbedding / queryEmbedding.norm()
24
+ chunkEmbeddingsNormalized = chunkEmbeddings / chunkEmbeddings.norm(dim = 1, keepdim = True)
25
+ similarities = torch.matmul(chunkEmbeddingsNormalized, queryEmbeddingNormalized)
26
+ topIndices = torch.topk(similarities, k=3).indices
27
+ topChunks = [textChunks[i] for i in topIndices]
28
+ return topChunks
29
+
30
+ # cleanedChunks = preprocessText(finlitText)
31
+ # chunkEmbeddings = createEmbeddings(cleanedChunks)
32
+
33
+ client = InferenceClient("???")
34
+
35
+ def respond(message, history):
36
+ messages = [{"role": "system",
37
+ "content": "You are a friendly, approachable AI assistant whose main goal is to help high school and college students to learn more about productivity, setting goals for their education, and financial literacy."
38
+ "Keep responses between 200-300 words unless asked for more detail about your suggestions from the user."
39
+ "Explanations should be clear with examples and always include actionable steps, following this format:"
40
+ "User: What is the 50/30/20 rule when it comes to budgeting?"
41
+ "AI: Great question! The 50/30/20 rule states that you should spend 50% of your income on needs, 30% of your income on wants, and 20% of your income on investing. For example, if you make $4,000 each month, you should spend $2,000 on your needs, $1,200 on your wants, and $800 on investing. That way, you can set aside money to take care of yourself while still making progress towards saving up for the things that aren't as essential."}]
42
+
43
+ if history:
44
+ messages.extend(history)
45
+
46
+ # topResults = getTopChunks(message, chunkEmbeddings, cleanedChunks)
47
+ # context = "\n\n".join(topResults)
48
+
49
+ # messages.append({"role": "system",
50
+ # "content": context})
51
+
52
+ messages.append({"role": "user",
53
+ "content": message})
54
+
55
+ response = ""
56
+
57
+ responseStream = client.chat_completion(
58
+ messages, stream = True, max_tokens = 1024, temperature = 0.4
59
+ )
60
+
61
+ for segment in responseStream:
62
+ token = segment.choices[0].delta.content
63
+ if token is not None:
64
+ response += token
65
+ yield response
66
+
67
+ chatbot = gr.ChatInterface(response, title = "Student Formula Bot 🔬",
68
+ description = "Welcome to the core component of our project, The Student Formula: the RAG chatbot! With the ability to act as a finance tutor, accountability buddy, and goal-setting partner all in one, it's designed to best suit your needs on the way to productivity and success.")
69
+
70
+ chatbot.launch()
71
+
72
+