Opens the specified file and returns its content. Args: relative_file_path (str): The relative path to the file. max_new_line (int, optional): The maximum number of lines to include in the returned content. Defaults to 500. Returns: str:
(self, relative_file_path: str, start_line: Optional[int] = None, end_line: Optional[int] = None, keywords: Optional[List[str]] = [], preview_size: int = 8, max_num_result: int = 5, semantic_query: str = "")
| 333 | self.model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-code', trust_remote_code=True) |
| 334 | |
| 335 | def _run(self, relative_file_path: str, start_line: Optional[int] = None, end_line: Optional[int] = None, keywords: Optional[List[str]] = [], preview_size: int = 8, max_num_result: int = 5, semantic_query: str = ""): |
| 336 | """ |
| 337 | Opens the specified file and returns its content. |
| 338 | |
| 339 | Args: |
| 340 | relative_file_path (str): The relative path to the file. |
| 341 | max_new_line (int, optional): The maximum number of lines to include in the returned content. Defaults to 500. |
| 342 | |
| 343 | Returns: |
| 344 | str: The content of the file. |
| 345 | |
| 346 | """ |
| 347 | if len(keywords) == 0 and start_line is None and end_line is None: |
| 348 | return "Please specify the keyword or start and end line to view the content of the file." |
| 349 | |
| 350 | abs_path = find_matching_abs_path(self.path, relative_file_path) |
| 351 | # abs_path = os.path.join(self.path, relative_file_path) |
| 352 | source = open(abs_path, "r").read() |
| 353 | lines = source.split("\n") |
| 354 | |
| 355 | cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b)) |
| 356 | |
| 357 | def chunk_list(lst, chunk_size): |
| 358 | return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)] |
| 359 | |
| 360 | if semantic_query != "": |
| 361 | embeddings = self.modle.encode( |
| 362 | [ |
| 363 | "\n".join(segment_lst) for segment_lst in chunk_list(lines, 50) |
| 364 | ] |
| 365 | ) |
| 366 | |
| 367 | query = self.model.encode([semantic_query]) |
| 368 | similarities = [] |
| 369 | for emb in embeddings: |
| 370 | similarities.append(cos_sim(query, emb)) |
| 371 | |
| 372 | sorted_idx = sorted(range(len(similarities)), key=lambda x: similarities[x], reverse=True) |
| 373 | |
| 374 | line_ranges = sorted_idx[:max_num_result] |
| 375 | line_ranges = [(i*50, (i+1)*50) for i in line_ranges] |
| 376 | returned_source = [] |
| 377 | for start, end in line_ranges: |
| 378 | expanded_source = "\n".join(lines[start:end]) |
| 379 | expanded_source = add_num_line(expanded_source, start+1) |
| 380 | returned_source.append(expanded_source) |
| 381 | |
| 382 | semantic_query_result = "\n--------------\n".join(returned_source) |
| 383 | |
| 384 | try: |
| 385 | if start_line is not None and end_line is not None and len(keywords) == 0: |
| 386 | if end_line - start_line > 90: |
| 387 | return f"The number of lines to show is limited at 90, the requested number of lines is {end_line - start_line}, please specify the start and end line again or using keyword instead. For example {start_line}:{start_line+90}" |
| 388 | |
| 389 | if start_line > len(lines): |
| 390 | return f"Invalid start line, the start line is greater than the total number of lines in the file, the total number of lines in the file is {len(lines)}" |
| 391 | |
| 392 | source = "\n".join(lines[start_line:end_line]) |
no test coverage detected