Retrieve information from a code directory. Use this function when there is a need to search for information in the codebase. Args: query_text: Anything you want to search in the code directory, like a function name, a class name, a variable name, etc. Returns: A string
(query_text: str, context_variables)
| 6 | from typing import Union |
| 7 | @register_tool("code_rag") |
| 8 | def code_rag(query_text: str, context_variables) -> str: |
| 9 | """ |
| 10 | Retrieve information from a code directory. Use this function when there is a need to search for information in the codebase. |
| 11 | Args: |
| 12 | query_text: Anything you want to search in the code directory, like a function name, a class name, a variable name, etc. |
| 13 | Returns: |
| 14 | A string representation of the reranked results. |
| 15 | """ |
| 16 | env: Union[DockerEnv, LocalEnv] = context_variables.get("code_env", LocalEnv()) |
| 17 | code_memory = CodeMemory(project_path = './code_db', platform='OpenAI', api_key=os.getenv("OPENAI_API_KEY"),embedding_model='text-embedding-3-small') |
| 18 | code_reranker = CodeReranker(model="gpt-4o-2024-08-06") |
| 19 | code_path = f"{env.local_workplace}/autoagent" |
| 20 | compress_folder(code_path, f"{env.local_workplace}/", "autoagent.zip") |
| 21 | code_id = get_file_md5(f"{env.local_workplace}/autoagent.zip") |
| 22 | code_memory.collection_name = code_memory.collection_name + f"_{code_id}" |
| 23 | |
| 24 | if code_memory.count() == 0: |
| 25 | code_memory.add_code_files(f"{env.local_workplace}/autoagent", exclude_prefix=['__pycache__', 'code_db', '.git']) |
| 26 | |
| 27 | query_results = code_memory.query_code(query_text, n_results=20) |
| 28 | reranked_results = code_reranker.rerank(query_text, query_results) |
| 29 | return reranked_results |
| 30 | |
| 31 | |
| 32 |
nothing calls this directly
no test coverage detected