In this guide, you will use Python with the Ground API to create the following functions:

  1. upload documents to the RAG
  2. query against the documents to obtain similarity rankings
  3. input the similarity ranking into an LLM to get an answer So let’s get started.

You will need an API key from Ground (get it by following the first 3 steps in Quickstart), and an API key from OpenAI.

1. Upload documents

First thing, you’ll need requests to make API calls.

pip install requests

Let’s create a first file, upload_document.py:

upload_document.py
import requests

def upload_document(file_path, token):
    URL = "https://platform.ground.dev/api/documents"
    
    with open(file_path, 'rb') as file:
        files = {'document': file}
        headers = {
            'Authorization': f'Bearer {token}'
        }
        
        response = requests.request("POST", URL, headers=headers, files=files)
    
    # Check if the upload was successful
    if response.status_code == 200:
        print("File uploaded successfully.")
        print("Response:", response.text)
    else:
        print("Failed to upload file.")
        print("Status Code:", response.status_code)
        print("Response:", response.text)

if __name__ == "__main__":
    FILE_PATH = "path/to/file"
    TOKEN = "your_ground_api_key"
    
    upload_document(FILE_PATH, TOKEN)

You’re able to upload most kind of text documents to Ground.
They will be embedded and saved on our platform.

2. Get the RAG ranking

Now that you can upload files, let’s query information from them!
For this example, let’s use OpenAI (but you can use whatever model you want).

pip install openai

Create a search_documents.py file.
Our first function in this file will use the /search/rrf endpoint from Ground’s API.
It will find matches for your query in the files you uploaded, and return a list of answers.

search_documents.py
import requests
import json
import openai

def search_rrf(query, token):
    GROUND_URL = "https://platform.ground.dev/api"
    
    headers = {
        'Authorization': f'Bearer {token}'
    }
    params = {
        'query': query
    }
    
    search_url = GROUND_URL + "/search/rrf"
    response = requests.request("GET", search_url, headers=headers, params=params)
    
    # Check if the request was successful
    if response.status_code == 200:
        try:
            return response.text
        except ValueError:
            print("Error: Response is not in JSON format.")
    else:
        print("Failed to perform search.")
        print("Status Code:", response.status_code)
        print("Response:", response.text)

3. Pass the rankings in OpenAI

Now that Ground did it’s job, and you have a ranked list with results, input this list into ChatGPT 3.5 and let it come up with a nicely formatted answer.
For reference, here is the OpenAI documentation, and get your OpenAI API Key here.


The second function in search_documents.py takes the ranking results from Ground, and pass them to ChatGPT.
ChatGPT will use the rankings to come up with the answer to your question.
The prompt is very basic, asking to use the informations provided by the ranking, and to cite the parent_document_key.

search_documents.py
def answer_with_ranking(rag_rankings, query):
    client = openai.OpenAI(
        api_key="your_openai_api_key"
    )
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a customer support agent, helping users by following directives and answering questions.\n"},
            {"role": "system", "content": "Get your answer from context, the right answer is in one of the chunks.\n"},
            {"role": "system", "content": "Remove duplicate content.\n"},
            {"role": "system", "content": "Your response is a quote from source with relevant informations.\n"},
            {"role": "system", "content": "Only show the final response, do not provide explanations or details.\n"},
            {"role": "system", "content": "Provide the source document at the end of the response, as follows: source: <parent_document_key>.\n"},
            {"role": "system", "content": f"Context: '{rag_rankings}'\n\n"},
            {"role": "user", "content": f"'{query}'\n\n"},
        ],
        max_tokens=200,
        n=1,
        stop=None,
        temperature=0.8
    )
    
    # Extract the first generated answer
    return response.choices[0].message.content

if __name__ == "__main__":
    # Example query and token
    query = "put_your_question_here"
    GROUND_API_KEY = "your_ground_api_key"
    
    # Call the search function and turn the JSON answer into an object
    rag_rankings = json.loads(search_rrf(query, GROUND_API_KEY))

    answer = answer_with_ranking(rag_rankings, query)
    print(answer)

And voila!
That’s the gist of it.
You have a working ChatGPT using the context you provided to it.
You can change the code to use open source, fine tuned, or other models.
Modify and play around with the prompts to get results that fit your needs.

Storing the different keys in a .env file or secret manager is recommended. Don’t share them online.
Thank you for following this tutorial.
If you have questions feel free to contact us.