How to integrate a GitHub Repository with Large Language Models (LLMs)
Published: 2023-09-27 13:40:00

In this guide, we will explore how to combine the capabilities of AI with your GitHub repository, allowing you to query your codebase efficiently.

## Prerequisites:

Ensure you have the `pickle`, `os`, and `llama_hub` libraries installed.
Get your API keys ready for both OpenAI and GitHub.
##Step-by-Step Guide:
###1. Environment and Library Initialization:
Start by importing necessary libraries.

```python
import pickle
import os
```
###2. Configuring OpenAI:
Before diving deep, you need to set the API key for OpenAI.

```python
os.environ["OPENAI_API_KEY"] = "<YOUR_OPENAI_API_KEY>"
```
###3. Load Llama Index:
The Llama Index is responsible for fetching and indexing data. Ensure you've downloaded the loader for the Github repository.
```python
from llama_index import download_loader, GPTVectorStoreIndex
download_loader("GithubRepositoryReader")
```
###4. Setting Up the GitHub Client:
For connecting with your GitHub repository, initialize the GitHub client.
```python
from llama_hub.github_repo import GithubClient, GithubRepositoryReader
```
###5. Fetching Repository Data:
Use the GitHub client to fetch the data from your repository. Here, we focus on the repository [sec-insights](https://github.com/run-llama/sec-insights/) owned by Llama Index team, specifically extracting Python files from the `backend` directory.
```python
# File path and parameters
file_path = "docs.pkl"
github_personal_key = "<YOUR_GITHUB_PERSONAL_KEY>"
repository_owner = "run-llama"
repository_name = "sec-insights"
directories_to_include = ["backend"]
file_extensions_to_include = [".py"]
github_branch = "main"

# Load existing data if file exists
docs = None
if os.path.exists(file_path):
    with open(file_path, "rb") as f:
        docs = pickle.load(f)

# If data not loaded, fetch from GitHub and save
if docs is None:
    github_client = GithubClient(github_personal_key)
    loader = GithubRepositoryReader(
        github_client,
        owner=repository_owner,
        repo=repository_name,
        filter_directories=(directories_to_include, GithubRepositoryReader.FilterType.INCLUDE),
        filter_file_extensions=(file_extensions_to_include, GithubRepositoryReader.FilterType.INCLUDE),
        verbose=True,
        concurrent_requests=10,
    )
    docs = loader.load_data(branch=github_branch)
    
    # Save fetched data for future use
    with open(file_path, "wb") as f:
        pickle.dump(docs, f)
```
###6. Indexing Data
Once your data is fetched, it's time to employ the powers of AI. We use the GPTVectorStoreIndex to index our documents. This tool converts our documents into vectors which can be efficiently searched later.

```python
index = GPTVectorStoreIndex.from_documents(docs)
```
###7. Querying Your Repository:
With everything in place, you can now query your indexed data. For example, to gain insights into your API endpoints, simply execute:

```python
query_engine = index.as_query_engine()
response = query_engine.query("Retrieve all API endpoints")

print(response)
```

##Security
When working with OpenAI APIs, it's important to understand that your data will be processed and enhanced through their systems. If security is a concern, consider switching to local models like [llama.cpp](https://gpt-index.readthedocs.io/en/latest/examples/llm/llama_2_llama_cpp.html) and [Hugging Face embeddings](https://gpt-index.readthedocs.io/en/stable/examples/embeddings/huggingface.html). 

Using these local solutions ensures your data remains within your infrastructure. Just ensure your hardware is up to the task to efficiently handle these models.

##Conclusion:
By integrating AI capabilities with your GitHub repository, you can derive insights, query specific parts of your codebase, and enhance your development workflow. It's a leap towards smarter code management and comprehension. 

Remember to keep your API keys confidential and adjust the repository specifics to your needs. Happy coding!

Do you want to discuss AI or Startups? DM me on [X](https://twitter.com/manelferreira_)(previously Twitter) or [LinkedIn](https://www.linkedin.com/in/emanuelcferreira/) 

## References
https://github.com/EmanuelCampos/gh-index
https://llamahub.ai/l/github_repo
https://gpt-index.readthedocs.io/