node-red-contrib-dotnsf-watsonxai
Version:
173 lines (159 loc) • 6.35 kB
JavaScript
var axiosBase = require( 'axios' );
var env_location = 'LOCATION' in process.env ? process.env.LOCATION : 'us-south';
var env_apikey = 'APIKEY' in process.env ? process.env.APIKEY : '';
var env_project_id = 'PROJECT_ID' in process.env ? process.env.PROJECT_ID : '';
var env_model_id = 'MODEL_ID' in process.env ? process.env.MODEL_ID : 'ibm/granite-13b-chat-v2';
module.exports = function( RED ){
async function getAccessToken( apikey ){
return new Promise( function( resolve, reject ){
if( apikey ){
var axios = axiosBase.create({
baseURL: 'https://iam.cloud.ibm.com',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
});
var params = new URLSearchParams();
params.append( 'grant_type', 'urn:ibm:params:oauth:grant-type:apikey' );
params.append( 'apikey', apikey );
axios.post( '/identity/token', params )
.then( function( result ){
if( result && result.data && result.data.access_token ){
//console.log( 'access_token = ' + result.data.access_token );
resolve( { status: true, access_token: result.data.access_token } );
}else{
resolve( { status: false, error: 'no access_token retrieved.' } );
}
}).catch( function( err ){
console.log( {err} );
resolve( { status: false, error: err } );
});
}else{
resolve( { status: false, error: 'no apikey provided.' } );
}
});
}
async function generateText( access_token, project_id, model_id, input, max_new_tokens, location, deployment_id ){
return new Promise( function( resolve, reject ){
if( access_token ){
if( input && max_new_tokens ){
var axios = axiosBase.create({
baseURL: 'https://' + location + '.ml.cloud.ibm.com',
responseType: 'json',
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
var data = {
//'model_id': model_id,
//'project_id': project_id,
'parameters': {
"decoding_method": "greedy",
"max_new_tokens": max_new_tokens,
"min_new_tokens": 0,
"stop_sequences": [],
"repetition_penalty": 1
},
'input': input
};
if( !deployment_id && model_id && project_id ){
data.model_id = model_id;
data.project_id = project_id;
}
var url_path = '/ml/v1/' + ( deployment_id ? 'deployments/' + deployment_id + '/' : '' ) + 'text/generation?version=2023-05-29';
//axios.post( '/ml/v1-beta/generation/text?version=2023-05-29', data )
//axios.post( '/ml/v1/generation/text?version=2023-05-29', data )
axios.post( url_path, data )
.then( function( result ){
//console.log( {result} );
if( result && result.data && result.data.results ){
resolve( { status: true, results: result.data.results } );
}else{
resolve( { status: false, error: 'no results found.' } );
}
}).catch( function( err ){
console.log( {err} );
resolve( { status: false, error: err } );
});
}else{
resolve( { status: false, error: 'Parameter input, and/or max_new_tokens are not provided.' } );
}
}else{
resolve( { status: false, error: 'access_token is null.' } );
}
});
}
function main( config ){
RED.nodes.createNode( this, config );
var node = this;
node.on( 'input', async function( msg ){
node.status( { fill: "green", shape: "dot", text: "..." } );
var max_new_tokens = 900;
var text = msg.payload;
var location = config.location;
var apikey = config.apikey;
var model_id = config.model_id;
var project_id = config.project_id;
var deployment_id = config.deployment_id;
var only_firstline = config.only_firstline;
if( config.max_new_tokens ){
try{
var mnt = parseInt( config.max_new_tokens );
if( mnt ){
max_new_tokens = mnt;
}
}catch( e ){
}
}
/*
if( !location ){ location = env_location; }
if( !apikey ){ apikey = env_apikey; }
if( !project_id ){ project_id = env_project_id; }
if( !model_id ){ model_id = env_model_id; }
*/
//console.log( {apikey} );
if( apikey ){
var result0 = await getAccessToken( apikey );
if( result0 && result0.status && result0.access_token ){
var result = await generateText( result0.access_token, project_id, model_id, text, max_new_tokens, location, deployment_id );
if( result && result.status ){
var results = result.results;
console.log( {results} );
if( results && results[0] && results[0].generated_text ){
var generated_text = results[0].generated_text;
if( only_firstline ){
var tmp = generated_text.split( '\\n\\n' );
if( tmp.length > 1 ){
generated_text = tmp[0];
}
}
msg.payload = generated_text;
node.status( {} );
node.send( msg );
}else{
msg.payload = 'No generated text.';
node.status( {} );
node.send( msg );
}
}else{
msg.payload = JSON.stringify( result.error );
node.status( {} );
node.send( msg );
}
}else{
msg.payload = JSON.stringify( result0.error );
node.status( {} );
node.send( msg );
}
}else{
msg.payload = 'API Key and/or Project ID missing.';
node.status( {} );
node.send( msg );
}
});
};
RED.nodes.registerType( 'watsonx.ai', main );
}