{"id":13910627,"url":"https://github.com/sturdy-dev/semantic-code-search","last_synced_at":"2025-04-05T07:01:37.931Z","repository":{"id":63345902,"uuid":"563878125","full_name":"sturdy-dev/semantic-code-search","owner":"sturdy-dev","description":"Search your codebase with natural language • CLI • No data leaves your computer","archived":false,"fork":false,"pushed_at":"2023-05-14T13:53:27.000Z","size":1876,"stargazers_count":330,"open_issues_count":15,"forks_count":32,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-29T06:01:36.372Z","etag":null,"topics":["ai","codesearch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sturdy-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-09T14:27:02.000Z","updated_at":"2025-03-27T09:13:44.000Z","dependencies_parsed_at":"2024-11-25T19:45:22.577Z","dependency_job_id":null,"html_url":"https://github.com/sturdy-dev/semantic-code-search","commit_stats":{"total_commits":38,"total_committers":2,"mean_commits":19.0,"dds":0.02631578947368418,"last_synced_commit":"6434ef7a7987b3f3338c7eeb38c06078788dc331"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsemantic-code-search","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsemantic-code-search/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsemantic-code-search/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsemantic-code-search/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sturdy-dev","download_url":"https://codeload.github.com/sturdy-dev/semantic-code-search/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299828,"owners_count":20916190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","codesearch"],"created_at":"2024-08-07T00:01:38.924Z","updated_at":"2025-04-05T07:01:37.855Z","avatar_url":"https://github.com/sturdy-dev.png","language":"Python","readme":"# Semantic Code Search\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/sturdy-dev/semantic-code-search/main/docs/readme-banner.png\"\u003e\n\u003c/p\u003e\n\u003cp align='center'\u003e\n  Search your codebase with natural language. No data leaves your computer.\n\u003c/p\u003e\n\u003cp align='center'\u003e\n    \u003ca href=\"https://github.com/sturdy-dev/semantic-code-search/blob/main/LICENSE.txt\"\u003e\n        \u003cimg alt=\"GitHub\"\n        src=\"https://img.shields.io/github/license/sturdy-dev/semantic-code-search\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/semantic-code-search\"\u003e\n     \u003cimg alt=\"PyPi\"\n src=\"https://img.shields.io/pypi/v/semantic-code-search\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"#overview\"\u003e🔍 Overview\u003c/a\u003e •\n  \u003ca href=\"#installation\"\u003e🔧 Installation\u003c/a\u003e •\n  \u003ca href=\"#usage\"\u003e💻 Usage\u003c/a\u003e •\n  \u003ca href=\"#command-line-flags\"\u003e📖 Docs\u003c/a\u003e •\n  \u003ca href=\"#how-it-works\"\u003e🧠 How it works\u003c/a\u003e\n\u003c/p\u003e\n\n--------------------------------------------------------------------\n\n## Overview\n\n`sem` is a command line application which allows you to search your git repository using natural language. For example you can query for:\n\n- 'Where are API requests authenticated?'\n- 'Saving user objects to the database'\n- 'Handling of webhook events'\n- 'Where are jobs read from the queue?'\n\nYou will get a (visualized) list of code snippets and their `file:line` locations. You can use `sem` for exploring large codebases or, if you are as forgetfull as I am, even small ones.\n\nBasic usage:\n\n```bash\nsem 'my query'\n```\n\nThis will present you with a list of code snippets that most closely match your search. You can select one and press  `Return` to open it in your editor of choice.\n\nHow does this work? In a nutshell, it uses a neural network to generate code embeddings. More info [below](#how-it-works).\n\n\u003e NB: All processing is done on your hardware and no data is transmitted to the Internet.\n\n## Installation\n\nYou can install `semantic-code-search` via `pip`.\n\n### Pip (MacOS, Linux, Windows)\n\n```bash\npip3 install semantic-code-search\n```\n\n## Usage\n\nTL;DR:\n\n```bash\ncd /my/repo\nsem 'my query'\n```\n\nRun `sem --help` to see [all available options](#command-line-flags).\n\n### Searching for code\n\nInside your repo simply run\n\n```bash\nsem 'my query'\n```\n\n*(quotes can be omitted)*\n\n\u003e Note that you *need to* be  inside a git repository or provide a path to a repo with the `-p` argument.\n\nBefore you get your *first* search results, two things need to happen:\n\n- The app downloads its [model](#model) (~500 MB). This is done only once for the installation.\n- The app generates 'embeddings' of your code. This will be cached in an `.embeddings` file at the root of the repo and is reused in subsequent searches.\n\nDepending on the project size, the above can take from a couple of seconds to minutes. Once this is complete, querying is very fast.\n\nExample output:\n\n```bash session\nfoo@bar:~$ cd /my/repo\nfoo@bar:~$ sem 'parsing command line args'\nEmbeddings not found in /Users/kiril/src/semantic-code-search. Generating embeddings now.\nEmbedding 15 functions in 1 batches. This is done once and cached in .embeddings\nBatches: 100%|█████████████████████████████████████████████████████████| 1/1 [00:07\u003c00:00,  7.05s/it]\n```\n\n### Navigating search results\n\nBy default, a list of the top 5 matches are shown, containing :\n\n- Similarity score\n- File path\n- Line number\n- Code snippet\n\nYou can navigate the list using the `↑` `↓` arrow keys or `vim` bindings. Pressing `return` will open the relevant file at the line of the code snippet in your editor.\n\n\u003e NB: The editor used for opening can be set with the `--editor` argument.\n\nExample results:\n\n![example results](./docs/example-results.png)\n\n### Command line flags\n\n``` bash\nusage: sem [-h] [-p PATH] [-m MODEL] [-d] [-b BS] [-x EXT] [-n N]\n           [-e {vscode,vim}] [-c] [--cluster-max-distance THRESHOLD]\n           [--cluster-min-lines SIZE] [--cluster-min-cluster-size SIZE]\n           [--cluster-ignore-identincal]\n           ...\n\nSearch your codebase using natural language\n\npositional arguments:\n  query_text\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -p PATH, --path-to-repo PATH\n                        Path to the root of the git repo to search or embed\n  -m MODEL, --model-name-or-path MODEL\n                        Name or path of the model to use\n  -d, --embed           (Re)create the embeddings index for codebase\n  -b BS, --batch-size BS\n                        Batch size for embeddings generation\n  -x EXT, --file-extension EXT\n                        File extension filter (e.g. \"py\" will only return\n                        results from Python files)\n  -n N, --n-results N   Number of results to return\n  -e {vscode,vim}, --editor {vscode,vim}\n                        Editor to open selected result in\n  -c, --cluster         Generate clusters of code that is semantically\n                        similar. You can use this to spot near duplicates,\n                        results are simply printed to stdout\n  --cluster-max-distance THRESHOLD\n                        How close functions need to be to one another to be\n                        clustered. Distance 0 means that the code is\n                        identical, smaller values (e.g. 0.2, 0.3) are stricter\n                        and result in fewer matches\n  --cluster-min-lines SIZE\n                        Ignore clusters with code snippets smaller than this\n                        size (lines of code). Use this if you are not\n                        interested in smaller duplications (eg. one liners)\n  --cluster-min-cluster-size SIZE\n                        Ignore clusters smaller than this size. Use this if\n                        you want to find code that is similar and repeated\n                        many times (e.g. \u003e5)\n  --cluster-ignore-identincal\n                        Ignore identical code / exact duplicates (where\n                        distance is 0)\n```\n\n## How it works\n\nIn a nutshell, this application uses a [transformer](https://en.wikipedia.org/wiki/Transformer_(machine_learning_model)) machine learning model to generate embeddings of methods and functions in your codebase. Embeddings are information dense numerical representations of the semantics of the text/code they represent.\n\nHere is a great blog post by Jay Alammar which explains the concept really nicely:\n\u003e \u003chttps://jalammar.github.io/illustrated-word2vec/\u003e\n\nWhen the app is ran with the `--embed` argument, function and method definitions are first extracted from the source files and then used for sentence embedding. To avoid doing this for every query, the results are compressed and saved in an `.embeddings` file.\n\nWhen a query is being processed, embeddings are generated from the query text. This is then used in a 'nearest neighbor' search to discover function or methods with similar embeddings. We are basically comparing the [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity) between vectors.\n\n### Model\n\nThe application uses [sentence transformer](https://www.sbert.net/) model architecture to produce 'sentence' embeddings for functions and queries. The particular model is [krlvi/sentence-t5-base-nlpl-code_search_net](https://huggingface.co/krlvi/sentence-t5-base-nlpl-code_search_net) which is based of a [SentenceT5-Base](https://github.com/google-research/t5x_retrieval#released-model-checkpoints) checkpoint with 110M parameters and a pooling layer.\n\nIt has been further trained on the [code_search_net](https://huggingface.co/datasets/code_search_net) dataset of 'natural language' — 'programming language' pairs with a [MultipleNegativesRanking](https://github.com/UKPLab/sentence-transformers/blob/master/sentence_transformers/losses/MultipleNegativesRankingLoss.py) loss function.\n\nYou can experiment with your own sentence transformer models with the `--model` parameter.\n\n## Bugs and limitations\n\n- Currently, the `.embeddings` index is not updated when repository files change. As a temporary workaround, `sem embed` can be re-ran occasionally.\n- Supported languages: `{ 'python', 'javascript', 'typescript', 'ruby', 'go', 'rust', 'java', 'c', 'c++', 'kotlin' }`\n- Supported text editors for opening results in: `{ 'vscode', 'vim' }`\n\n## License\n\nSemantic Code Search is distributed under [AGPL-3.0-only](LICENSE.txt). For Apache-2.0 exceptions — \u003ckiril@codeball.ai\u003e\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsturdy-dev%2Fsemantic-code-search","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsturdy-dev%2Fsemantic-code-search","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsturdy-dev%2Fsemantic-code-search/lists"}