tensorsoft/Mini-RAG-Chat-With-Your-Files-CPU-Only
0
1import numpy as np2 3def chunk_text(text: str, chunk_size: int = 200):4 """Split text into chunks of specified size."""5 words = text.split()6 chunks = []7 current_chunk = []8 current_length = 09 10 for word in words:11 current_length += len(word) + 112 if current_length > chunk_size:13 chunks.append(" ".join(current_chunk))14 current_chunk = [word]15 current_length = len(word) + 116 else:17 current_chunk.append(word)18 19 if current_chunk:20 chunks.append(" ".join(current_chunk))21 22 return chunks23 24def cosine_similarity(vec_a: np.ndarray, vec_b: np.ndarray):25 """Compute cosine similarity between two vectors."""26 dot_product = np.dot(vec_a, vec_b)27 norm_a = np.linalg.norm(vec_a)28 norm_b = np.linalg.norm(vec_b)29 return dot_product / (norm_a * norm_b) if norm_a * norm_b != 0 else 030 