logo
Tutorials

Integrating with ChatGPT

Welcome to this hands-on tutorial where we'll delve into Retrieval Augmented Generation (RAG) by merging the capabilities of GroundX's search results with OpenAI's ChatGPT.

In this tutorial, we will use Python to show you how to seamlessly extract relevant content from GroundX and then channel it into ChatGPT for enhanced, context-aware conversational experiences. This will not only broaden the knowledge base of the ChatGPT model but also leverage the precise search and retrieval capabilities of GroundX.

By the end of this tutorial, you'll possess the skills to create a more informed and enriched conversational agent that can draw upon your private data. Let's get started!

Step 1: Set Up Your Environment

You will need a GroundX API Key, which you can find in your GroundX account.

You will also need a projectId, groupId, or bucketId for a project, group, or bucket that contains ingested content. This can also be found in the GroundX dashboard.

For this tutorial, you will also need an OpenAI API Key, which can be found in your OpenAI account.

Finally, you will need to install the OpenAI and GroundX SDKs, either in TypeScript or Python, using the package manager of your choice. Here are examples using pip and npm.

pip install groundx-python-sdk openai
npm install groundx-typescript-sdk openai --save

Step 2: Initialize Your Clients

Initialize both OpenAI and GroundX clients with the appropriate API keys:

groundx = Groundx(
api_key=groundxKey,
)
openai.api_key = openaiKey
const groundx = new Groundx({
apiKey: groundxKey,
});
const openai = new OpenAI({
apiKey: openaiKey,
});

Replace groundxKey with your GroundX API key and openaiKey with your OpenAI key.

Step 3: Search Your Content on GroundX

Next, you will submit a query to the GroundX Search API. A request will look something like this:

content_response = groundx.search.content(id=groundxId, query=query)
results = content_response.body["search"]
const result = await groundx.search.content({
id: groundxId,
query: query
});

Replace groundxId with your projectId, groupId, or bucketId and query with the query you want to use to search your content.

Your search results should look something like this:

{
"search": {
"count": <int_number_of_results>,
"query": "<your_query>"
"score": <float_highest_relevance_score_in_results>,
"text": "<combined_text_of_search_results>",
"nextToken": "<token_for_next_set_of_results>",
"results":[
{
"documentId": "<unique_system_generated_id>",
"score": <float_relevance_score_of_result>,
"sourceUrl": "<source_document_url>",
"text": "<text_of_result>"
}
]
}
}

Step 4: Curate the Search Results

Once you have the results from your GroundX search, you will want to curate the relevant content to send to OpenAI for completion. You will need to be cognizant of the token limits for OpenAI models and reserve space for the completion, since the token limits apply to the combined total of tokens used for the request and completion.

Here is an example of what that could look like:

llmText = results["text"]
let llmText = result.data.search.text;

Use the search.text field from the response. It contains the suggested context for LLM completion. If search.text is too long for your model context limits, either truncate search.text or iterate search.results and use the suggestedText field of each search result.

Reference the OpenAI documentation for the most current model token limits. Keep in mind that a token is roughly equivalent to 3.5 characters and that token limits apply to the combined size of the request and completion response.

Step 5: Submit a Completion Request to OpenAI

Once you have relevant text for your request, you will need to combine the text with instructions and submit them to OpenAI.

Here is an example of what a completion instruction could look like:

You are a helpful virtual assistant that answers questions
using the content below. Your task is to create detailed answers
to the questions by combining your understanding of the world
with the content provided below. Do not share links.

Combine your completion instructions with your curated GroundX search results:

completion = openai.ChatCompletion.create(
model=openaiModel,
messages=[
{
"role": "system",
"content": """%s
===
%s
===
"""
% (instruction, llmText),
},
{"role": "user", "content": query},
],
)
const completion = await openai.chat.completions.create({
model: openaiModel,
messages: [
{
"role": "system",
"content": `${instruction}
===
${llmText}
===
`
},
{"role": "user", "content": query},
],
});

Replace openaiModel with your preferred model, instruction with your completion instructions, llmText with your curated GroundX search results, and query with your query.

Print the results of your request and you're done!