MCPcopy Create free account
hub / github.com/HKUDS/AutoAgent / code_rag

Function code_rag

autoagent/tools/rag_code.py:8–29  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

6from typing import Union
7@register_tool("code_rag")
8def 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

Callers

nothing calls this directly

Calls 10

add_code_filesMethod · 0.95
query_codeMethod · 0.95
rerankMethod · 0.95
LocalEnvClass · 0.90
CodeMemoryClass · 0.90
CodeRerankerClass · 0.90
compress_folderFunction · 0.90
get_file_md5Function · 0.90
getMethod · 0.80
countMethod · 0.80

Tested by

no test coverage detected