Empowering Your Data Analytics with GPT-3 and SAP CAP

Answer free text questions against the DB using GPT-3.

Empowering Your Data Analytics with GPT-3 and SAP CAP
3 min read
Posted: by Kai Niklas
Tags: sap cap gpt

Introduction #

In this article, we will explore how GPT-3 can be used to give SAP CAP super-powers to answer free text questions against a database. By leveraging GPT-3, businesses can ask a free text question and get the result back in a human readable format. This approach is ideal for smaller ad-hoc questions without using heavy data analytics tools.

The Technology Stack #

GPT-3 will be used to generate SQL queries based on free text questions. GPT-3 is ideal to understand the question and transform it into a suitable SQL query. Further, given the SQL result, it can be used to format it into a human readable answer.

SAP CAP is used to connect to the data source, execute the query and integrate GPT-3. For this prototype we use the build-in SQLite DB. This works with SAP HANA or PostgreSQL as well.

Free Text Questions to SQL Queries #

GPT-3's natural language processing capabilities make it a great tool for converting free text questions into SQL queries. By analyzing and understanding the question and its intent, GPT-3 can generate queries that accurately capture the user's request.

The secret sauce is in the prompt design. It’s key to provide the right information, so that GPT knows what to do and has the required information at hand. In this prototype the complete DB structure is included in the request together with the question and further instructions. Only with this information the correct select and join conditions can be generated.

The used prompt to generate the SQL Query looks like this:

# Create a syntactical correct  query to answer the question using only the tables and properties listed below
# Tables and Properties:
# Question:
# Further Instructions: Prefix table names with . Select all required properties to answer the question.
SELECT

When calling the GPT-3 API it is important to use the right parameters, such as the "temperature". To not generate a complete random query, we use 0 as temperature which reduces noise and randomness and gives the most probable results.
Getting the tables and its properties can be retrieved by utilizing the entities in SAP CAP. The prototype uses the entities and it's elements excluding associations.

function getTableAndPropertiesString(entities) {
let entityArr = [];
Object.keys(entities).map((entity) => {
let entityString = "";
entityString += entity + "(";
let elements = Object.keys(db.entities[entity].elements).filter(
(e) => !(db.entities[entity].elements[e] instanceof cds.Association)
);
entityString += elements.toString() + ")";
entityArr.push(entityString);
});

return entityArr.toString();
};

The entities are from here:

const db = await cds.connect.to("db");
const entities = db.entities("sap.capire.bookshop");

SQL Execution and Result Retrieval
Once the SQL queries are generated, the SQL is executed. With SAP CAP this is easy to achieve:

let dbResult = await db.run(sqlQuery);

Hint: Proper database design and optimization are crucial for efficient query processing and fast response times. GPT-3 can do no wonders here ;)

GPT-3 Formulation of Human-Readable Answers #

Finally, GPT-3 is used to formulate human-readable answers based on the results of the SQL queries, the question and some additional instructions. By analyzing the data and applying natural language processing techniques, GPT-3 can generate responses that are accurate, informative, and easy to understand.

Again, the key is to formulate a proper prompt so that the answer makes sense. The following prompt was used:

Answer the question concisely in written form using the DB Query Result. Include all results.
DB Query Result:
Question:
Answer:

Conclusion & Discussion #

In conclusion, I believe the combination of GPT-3 and SAP CAP has the potential to revolutionize the way business handles free text questions. Especially, smaller, less complex ad-hoc queries are the perfect use case.

While this prototype can handle already quite a bit, further research and development is required. Especially, the question in the reliability of the answers has to be enhanced. How do we know if the output is correct? The prototype includes Further research and development is required.

3 min read
Posted: by Kai Niklas
Tags: sap cap gpt

kai-niklas.de - All rights reserved