UNPKG

node-red-contrib-dotnsf-watsonxai

Version:
113 lines (103 loc) 4.14 kB
//. test.js var axiosBase = require( 'axios' ); var env_location = 'LOCATION' in process.env ? process.env.LOCATION : 'jp-tok'; var env_apikey = 'APIKEY' in process.env ? process.env.APIKEY : 'JJfiS9fVs1IgNupg3OP5EY20MTasTx7l30Y7ZK9Xv4tr'; var env_project_id = 'PROJECT_ID' in process.env ? process.env.PROJECT_ID : '8507f70b-cf2a-4e50-83a6-a7d2976be46b'; var env_model_id = 'MODEL_ID' in process.env ? process.env.MODEL_ID : 'ibm/granite-13b-chat-v2'; 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 ){ return new Promise( function( resolve, reject ){ if( access_token ){ if( project_id && 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 config = { headers: { 'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json', 'Accept': 'application/json' } }; var data = { 'model_id': model_id, 'input': input, 'parameters': { "decoding_method": "greedy", "max_new_tokens": max_new_tokens, "min_new_tokens": 0, "stop_sequences": [], "repetition_penalty": 1 }, 'project_id': project_id }; console.log( {data} ); //axios.post( '/ml/v1-beta/generation/text?version=2023-05-29', data ) axiosBase.post( 'https://' + location + '.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29', data, config ) .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 project_id, model_id, input, and/or max_new_tokens are not provided.' } ); } }else{ resolve( { status: false, error: 'access_token is null.' } ); } }); } var max_new_tokens = 200; var text = 'Hello.'; var location = env_location; var apikey = env_apikey; var model_id = env_model_id; var project_id = env_project_id; getAccessToken( apikey ).then( async function( r0 ){ console.log( {r0} ); if( r0 && r0.status && r0.access_token ){ var r1 = await generateText( r0.access_token, project_id, model_id, text, max_new_tokens, location ); console.log( {r1} ); } });