Kore.AI export knowledge base into batch testing

Small script in python to export Kore.AI knowledge base
python
Kore.AI
Published

March 29, 2021

How to export the knowledge base and transform into batch testing

We need to transform between 2 JSON files. The knowledge base can be extracted from the tool, and the Batch testing only needs some items from there.

We will be using the Google Fire library to wrap our code.

import json
import fire

def create_test(knowledge_base,outfile):
    with open(knowledge_base,'r',encoding='utf-8') as f:
        data = json.load(f)

    tests = {'testCases':[]}

    for term in data['faqs']:
        key = term['question']
        tests['testCases'].append({'input':key,'intent':key})
        for alt in term['alternateQuestions']:
            if not alt['question'].startswith('||'):
                tests['testCases'].append({'input':alt['question'],'intent':key})


    with open(outfile,'w',encoding='utf-8') as out:
        json.dump(tests,out)

if __name__ == '__main__':
  fire.Fire(create_test)

Resources

programmiz

stackoverflow

Python Fire

String comparison