Semantic code search tool allows you to search for code using natural language. It's useful when the query is a sentance, semantic and vague. If exact search such as code search failed after multiple tries, try this Args: path (_type_): relative path to the repo lang
(self, path, language: str="python", db_path: Optional[str] = None, build: bool = False)
| 469 | |
| 470 | class SemanticCodeSearchTool(Tool): |
| 471 | def __init__(self, path, language: str="python", db_path: Optional[str] = None, build: bool = False): |
| 472 | """Semantic code search tool allows you to search for code using natural language. It's useful when the query is a sentance, semantic and vague. If exact search such as code search failed after multiple tries, try this |
| 473 | |
| 474 | Args: |
| 475 | path (_type_): relative path to the repo |
| 476 | language (str, optional): we have 4 options: python, rust, csharp, java. Defaults to "python". |
| 477 | """ |
| 478 | if db_path == None: |
| 479 | # randomize db_path |
| 480 | db_path = os.path.join(SEMANTIC_CODE_SEARCH_DB_PATH, str(uuid.uuid4())) |
| 481 | if not os.path.exists(db_path): |
| 482 | os.makedirs(db_path) |
| 483 | if build: |
| 484 | extension = identify_extension(language) |
| 485 | splitter = RecursiveCharacterTextSplitter.from_language( |
| 486 | language=language, chunk_size=400, chunk_overlap=100 |
| 487 | ) |
| 488 | |
| 489 | non_utf8_files = find_non_utf8_files(path) |
| 490 | |
| 491 | loader = GenericLoader.from_filesystem( |
| 492 | path, |
| 493 | suffixes=[extension], |
| 494 | exclude=non_utf8_files, |
| 495 | parser=LanguageParser(language=language, parser_threshold=500), |
| 496 | show_progress=True, |
| 497 | ) |
| 498 | |
| 499 | documents = loader.load() |
| 500 | texts = splitter.split_documents(documents) |
| 501 | db = Chroma.from_documents(texts, CohereEmbeddings(model="embed-english-light-v3.0", cohere_api_key=os.getenv("COHERE_API_KEY")), persist_directory=db_path) |
| 502 | db.persist() |
| 503 | else: |
| 504 | db = Chroma(persist_directory=db_path, embedding_function=CohereEmbeddings(model="embed-english-light-v3.0", cohere_api_key=os.getenv("COHERE_API_KEY"))) |
| 505 | |
| 506 | def semantic_code_search(query): |
| 507 | retrieved_docs = db.similarity_search(query, k=3) |
| 508 | return [doc.page_content for doc in retrieved_docs] |
| 509 | |
| 510 | super().__init__( |
| 511 | name="Semantic Code Search", |
| 512 | func=semantic_code_search, |
| 513 | description="useful for when the query is a sentance, semantic and vague. If exact search such as code search failed after multiple tries, try this", |
| 514 | ) |
| 515 | |
| 516 | class FindFileArgs(BaseModel): |
| 517 | file_name: str = Field(..., description="The name of the file you want to find") |
nothing calls this directly
no test coverage detected