![](https://community.sap.com/html/assets/img_tile-default.png)
After Part1 & Part2, What's in Part 3?
|
without any further ado, let's read this wonderful blog on how to create a custom plugin in SAP CAP.
Reusable components for CAP with cds-plugin by @daniel_schlachter
Now I hope you understand the way I am thinking. Let's crack fingers and start code again...
1. Create a baseline with this -
mkdir cap_dox && cd cap_dox && npm init -y && cd ..
2. Modify your package.json with the below section
3. Now try to replicate the below screenshot.
4. Now, take no time to annotate my.Recipe like this -
5. Now have some magical code inside the cds-plugin.js.
6. Now let's add the secret JSON from default_key.json. It's the secret-key of your DOX service.
cds.cap_dox_key ->
Let's try to understand some of the APIs from SAP BTP AI services - Document information extraction service.
GET /document-information-extraction/v1/schemas?clientId=default
List of your all schemas in SAP-AI-DOX
GET /document-information-extraction/v1/schemas/{schemaId}?clientId=default
Details of SAP-AI-DOX schema fields and parameters
2. Details of your service - cf8 service default_document-information-extraction-trial
3. List of your service-keys for your service instance - cf8 service-keys default_document-information-extraction-trial
4. Get your service-key - cf8 service-key default_document-information-extraction-trial {service-key-name}
Bonus -
There is an interesting thing, that I found here. If you create the SAP-AI-DOX service from booster, it creates the service_key as this pattern -
defaultKey_{service-guid}
where this service-guid is - cf8 service default_document-information-extraction-trial --guid
Next is coding time. Which is boring. Hence, get a clone from this git repo - cap_plugin_genai_dox_demo.
Create a library lib_cap_dox.js where you can export the functions as you want -
module.exports = {
auth_token: async function() {
return await get_token();
},
schema_id: async function(){
return await get_schema();
},
post_job: async function(pdf, fileName, auth_token){
return await post_job(pdf, fileName, auth_token)
},
get_job_status: async function(job_id, auth_token){
return await get_job_status(job_id, auth_token);
},
entity_mapping_head: async function(dox_output, entity) {
await entity_mapping_head_def(dox_output, entity)
},
entity_mapping_item: async function(dox_output, entity) {
await entity_mapping_item_def(dox_output, entity)
}
}
Then also consume these functions file - cds-plugin.js then use this function to trigger your extractions.
Two things we should discuss here -
Why?
Custom schema based extraction in SAP AI Document Information extraction service can provide you the possible entity mapping automatically. If not, then anyway you can manage to change it via the Document Information extraction UI, or from the Custom CAP Application.
How?
It's just some dynamic codes like below -
async function entity_mapping_head_def(headerFields, entity){
for(let item of headerFields){
entity[item.name] = item.rawValue
}
return entity
}
async function entity_mapping_item_def(lineItems, entity){
let ingredients = [];
let ingredient = {};
for(let item of lineItems){
for(let item_properties of item){
ingredient[item_properties.name] = item_properties.rawValue
}
ingredients.push(ingredient)
}
entity.ingredients = ingredients
// log(entity)
return entity
}
You can also make the entity names dynamic and read it from the package.json cds-environments configurations.
Please comment on what could go wrong and what could have been better.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
9 | |
8 | |
6 | |
6 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 |