Hacker News new | ask | show | jobs
Show HN: Open-source code search with OpenAI's function calling (github.com)
19 points by narenkmano 1091 days ago
We're excited to share a tool we've been working on called gpt-code-search. It allows you to search any codebase using natural language locally on your machine. We leverage OpenAI's GPT-4 and function calling to retrieve, search, and answer queries about your code.

All you need to do is to install the package with `pip install gpt-code-search`, set up your `OPENAI_API_KEY` as an environment variable, and start asking questions with `gpt-code-search query <your question>`.

E.g. You can ask questions like "How do I use the analytics module?" or "Document all the API routes related to authentication."

This is still early and hacked together in the past week, but we wanted to get it out there and get feedback.

We utilize OpenAI's function calling to let GPT-4 call certain predefined functions in our library. You do not need to implement any of these functions yourself. These functions are designed to interact with your codebase and return enough context for the LLM to perform code searches without pre-indexing it or uploading your repo to a third party other than OpenAI. So, you only need to run the tool from the directory you want to search.

The functions currently available for the LLM to call are:

`search_codebase` - searches the codebase using a TF-IDF vectorizer

`get_file_tree` - provides the file tree of the codebase

`get_file_contents` - provides the contents of a file

These functions are implemented in `gpt-code-search` and are triggered by chat completions. The LLM is prompted to utilize the search_codebase and get_file_tree function as needed to find the necessary context to answer your query and then loops as needed to collect more context with the get_file_contents until the LLM responds.

A couple of limitations of this approach, GPT cannot load context across multiple files in a single prompt since we are passing in the contents of a single file in each function call. So, GPT repeatedly calls the get_file_contents function to load context from multiple files. This increases the latency and cost of the tool.

Another thing we realized as we were building is that the level of search and retrieval is limited by the context window, which refers to the scope of the search conducted by the tool, meaning that we can only search five levels deep in the file system and can only pass in the contents of one file at a time. So it would be best to run the tool from the package/directory closest to the code you want to search.

We plan to add support for local vector embeddings to improve search and retrieval. Combining the vector embeddings with function calling should result in much faster and higher quality results.

Also, support for other models, chat interactions in the command line, and generating code is already on our backlog!

Please check out gpt-code-search and let me know your thoughts, feedback, or suggestions.

3 comments

This looks super interesting, thanks for sharing. I like that you're exploiting the new functions API to give GPT agent-style access to explore a codebase. I have played with that previously with gpt-3.5 and plan to do some more experiments with gpt-4 someday soon.

I am also working on an open source CLI tool in this space [0]. I've taken a different approach, more focused on chatting with GPT to have it edit the code in your local git repo.

But my tool also provides GPT with a semantic map of your repo and the ability to ask to see particular files, etc. I use it to answer questions about unknown codebases all the time, and then start asking it to make changes. I have a chat transcript that illustrates that here [1]. As another example I needed a new feature in the glow tool and was able to make a PR [2] for it, even though I don't know anything about that codebase or even how to write golang.

Also, there's a small discord [3] where a few of us working on "AI coding tools" have been sharing ideas. You might be interested in joining the conversation over there.

[0] https://github.com/paul-gauthier/aider

[1] https://aider.chat/examples/2048-game.html

[2] https://github.com/charmbracelet/glow/pull/502

[3] https://discord.gg/fHcgCRGu

Thanks! Yeah, it's been a lot of fun hacking around with these models. I just checked out your open-source project; looks super interesting! It has a lot of things I've been wanting to implement. I particularly love the command completion and concept of a repository map.

The approach you've taken is the one I use the most in my workflow nowadays. I manually provide context to GPT4 and ask it to translate/fix issues in my code (at least provide high-level ideas about resolving issues). Pretty cool to see a CLI version of that.

Having the human in the loop has been crucial in practice. Your approach of having the LLM interact with your code base and collaboratively make edits is interesting.

Joining the discord; thanks for the invite!

You're saying on the documentation:

> nor send your code to another third-party service.

But aren't you actually doing that? Sending things to OpenAI to use as context?

You are right. We do send it to OpenAI, and it's mentioned in a couple of lines below. I meant to update the readme to mention it everywhere but forgot! I just pushed a fix.

This is still far better than uploading your codebase to a third-party service for indexing and embedding generation. This tool intends to solve that right now with OpenAI's function calling.

However, the dream goal is to eventually run an LLM locally on your machines and support search without talking to any third-party service - https://gpt4all.io/index.html. But they are yet to arrive; none of the open-source LLMs are even close to the level of performance or quality as GPT 4 yet.

There's a demo at https://wolfia.com/ that lets you try out their code search on some popular repos and see other people's questions and answers.
Yes! Although please note that the version at wolfia.com is more sophisticated and use embeddings to retrieve contextual snippets of code, vs this open source tool doesn't require indexing your codebase and instead leverage Open AI functions calling to let the LLM browse the code.
Thanks for the clarification. I was confused since the About link at the top of the linked-to repo has the URL https://wolfia.com prominently displayed. Both projects are very interesting and cool. Thanks!