| 55 | ] |
| 56 | |
| 57 | class CodeReranker(Reranker): |
| 58 | def __init__(self, model: str) -> None: |
| 59 | super().__init__(model) |
| 60 | def wrap_query_results(self, query_results: List[Dict]) -> str: |
| 61 | wrapped_query_results = "" |
| 62 | for result in query_results: |
| 63 | wrapped_query_results += f"File: {result['file']}\n" |
| 64 | wrapped_query_results += f"Content: {result['content'][:300]}...\n" |
| 65 | wrapped_query_results += "---" |
| 66 | return wrapped_query_results |
| 67 | def wrap_reranked_results(self, reranked_paths: List[str]) -> str: |
| 68 | wrapped_reranked_results = "[Referenced code files]:" |
| 69 | for path in reranked_paths: |
| 70 | wrapped_reranked_results += f"Code path: {path}\n" |
| 71 | try: |
| 72 | with open(path, 'r', encoding='utf-8') as file: |
| 73 | content = file.read() |
| 74 | wrapped_reranked_results += f"Code content:\n{content}\n" |
| 75 | except Exception as e: |
| 76 | wrapped_reranked_results += f"Error reading file: {str(e)}\n" |
| 77 | wrapped_reranked_results += "---\n" |
| 78 | return wrapped_reranked_results |
| 79 | def parse_results(self, reranked_results: str) -> List[str]: |
| 80 | lines = reranked_results.strip().split('\n') |
| 81 | |
| 82 | # get the last 5 lines |
| 83 | last_lines = lines[-5:] |
| 84 | |
| 85 | # remove the number and dot at the beginning of each line |
| 86 | cleaned_lines = [re.sub(r'^\d+\.\s*', '', line.strip()) for line in last_lines] |
| 87 | unique_lines = list(dict.fromkeys(cleaned_lines)) |
| 88 | |
| 89 | return unique_lines |
| 90 | def rerank(self, query_text: str, query_results: List[Dict]) -> List[Dict]: |
| 91 | system_prompt = \ |
| 92 | """ |
| 93 | You are a helpful assistant that reranks the given code files (containing the path of files and Overview of the content of files) based on the query. |
| 94 | You should rerank the code files based on the query, and the most relevant code files should be ranked on the top. |
| 95 | You should select the top 5 code files to answer the query, by giving the file path of the code files. |
| 96 | |
| 97 | Example: |
| 98 | [Query]: "The definition of 'BaseAgent'" |
| 99 | [Code files]: |
| 100 | File: /Users/tangjiabin/Documents/reasoning/SelfAgent/sa/agents/__init__.py |
| 101 | Content: from .ABCAgent import ABCAgent |
| 102 | from .BaseAgent import BaseAgent |
| 103 | from .ManagerAgent import ManagerAge... |
| 104 | --- |
| 105 | File: /Users/tangjiabin/Documents/reasoning/SelfAgent/sa/agents/__init__.py |
| 106 | Content: from .ABCAgent import ABCAgent |
| 107 | from .BaseAgent import BaseAgent |
| 108 | from .ManagerAgent import ManagerAge... |
| 109 | --- |
| 110 | File: /Users/tangjiabin/Documents/reasoning/SelfAgent/sa/agents/__init__.py |
| 111 | Content: from .ABCAgent import ABCAgent |
| 112 | from .BaseAgent import BaseAgent |
| 113 | from .ManagerAgent import ManagerAge... |
| 114 | --- |