diff --git a/README.md b/README.md index 3b65577..6f3b544 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Here is a full example that summarizes a text using Facebook's Bart Large CNN mo ```js const NLPCloudClient = require('nlpcloud'); -const client = new NLPCloudClient('bart-large-cnn','4eC39HqLyjWDarjtT1zdp7dc') +const client = new NLPCloudClient({model:'bart-large-cnn', token:'4eC39HqLyjWDarjtT1zdp7dc'}) client.summarization(`One month after the United States began what has become a troubled rollout of a national COVID vaccination campaign, the effort is finally @@ -57,7 +57,7 @@ Here is a full example that does the same thing, but on a GPU: ```js const NLPCloudClient = require('nlpcloud'); -const client = new NLPCloudClient('bart-large-cnn','4eC39HqLyjWDarjtT1zdp7dc', true) +const client = new NLPCloudClient({model:'bart-large-cnn', token:'4eC39HqLyjWDarjtT1zdp7dc'}, true) client.summarization(`One month after the United States began what has become a troubled rollout of a national COVID vaccination campaign, the effort is finally @@ -85,7 +85,7 @@ Here is a full example that does the same thing, but on a French text: ```js const NLPCloudClient = require('nlpcloud'); -const client = new NLPCloudClient('bart-large-cnn','4eC39HqLyjWDarjtT1zdp7dc', true, 'fra_Latn') +const client = new NLPCloudClient({model:'bart-large-cnn', token:'4eC39HqLyjWDarjtT1zdp7dc', gpu:true, lang:'fra_Latn'}) client.summarization(`Sur des images aériennes, prises la veille par un vol de surveillance de la Nouvelle-Zélande, la côte d’une île est bordée d’arbres passés du vert @@ -130,31 +130,31 @@ Your token can be retrieved from your [NLP Cloud dashboard](https://nlpcloud.io/ ```js const NLPCloudClient = require('nlpcloud'); -const client = new NLPCloudClient('','') +const client = new NLPCloudClient({model:'', token:''}) ``` -If you want to use a GPU, pass `true` as the 3rd argument. +If you want to use a GPU, pass `true` as the gpu argument. ```js const NLPCloudClient = require('nlpcloud'); -const client = new NLPCloudClient('', '', true) +const client = new NLPCloudClient({model:'', token:'', gpu:true}) ``` -If you want to use the multilingual add-on in order to process non-English texts, set `lang=''` as the 4th argument. For example, if you want to process French text, you should set `lang='fra_Latn'` as the 4th argument. +If you want to use the multilingual add-on in order to process non-English texts, set `''` as the lang argument. For example, if you want to process French text, you should set `lang:'fra_Latn'`. ```js const NLPCloudClient = require('nlpcloud'); -const client = new NLPCloudClient('', '', false, '') +const client = new NLPCloudClient({model:'', token:'', lang:''}) ``` -If you want to make asynchronous requests, pass `true` as the 4th argument. +If you want to make asynchronous requests, pass `true` as the async argument. ```js const NLPCloudClient = require('nlpcloud'); -const client = new NLPCloudClient('', '', false, '', true) +const client = new NLPCloudClient({model:'', token:'', async:true) ``` If you are making asynchronous requests, you will always receive a quick response containing a URL. You should then poll this URL with `asyncResult()` on a regular basis (every 10 seconds for example) in order to check if the result is available. Here is an example: @@ -168,7 +168,7 @@ client.asyncResult('https://api.nlpcloud.io/v1/get-async-result/21718218-42e8-4b Call the `adGeneration()` method and pass the keywords you want to use to generate your ad or product description. ```js -client.adGeneration(['keyword 1', 'keyword 2', ...]) +client.adGeneration({keywords:['keyword 1', 'keyword 2', ...]}) ``` ### Automatic Speech Recognition (Speech to Text) Endpoint @@ -180,7 +180,7 @@ Call the `asr()` method and pass the following arguments: 1. (Optional) `inputLanguage`: the language of your file as ISO code ```js -client.asr('Your url') +client.asr({url:'Your url'}) ``` ### Chatbot Endpoint @@ -192,7 +192,7 @@ Call the `chatbot()` method and pass the following arguments: 1. (Optional) `history` The history of your previous exchanges with the model ```js -client.chatbot('') +client.chatbot({text:''}) ``` ### Classification Endpoint @@ -204,7 +204,7 @@ Call the `classification()` method and pass the following arguments: 1. (Optional) `multiClass` Whether the classification should be multi-class or not, as a boolean ```js -client.classification('', ['label 1', 'label 2', ...]) +client.classification({text:'', labels:['label 1', 'label 2', ...]}) ``` ### Code Generation Endpoint @@ -212,7 +212,7 @@ client.classification('', ['label 1', 'label 2', ...]) Call the `codeGeneration()` method and pass the instruction for the code you want to generate. ```js -client.codeGeneration('') +client.codeGeneration({instruction:''}) ``` ### Dependencies Endpoint @@ -220,7 +220,7 @@ client.codeGeneration('') Call the `dependencies()` method and pass the text you want to perform part of speech tagging (POS) + arcs on. ```js -client.dependencies('') +client.dependencies({text:''}) ``` ### Embeddings Endpoint @@ -228,7 +228,7 @@ client.dependencies('') Call the `embeddings()` method and pass an array of blocks of text that you want to extract embeddings from. ```js -client.embeddings(['', '', '', ...]) +client.embeddings({sentences:['', '', '', ...]}) ``` The above command returns a JSON object. @@ -238,7 +238,7 @@ The above command returns a JSON object. Call the `entities()` method and pass the text you want to perform named entity recognition (NER) on. ```js -client.entities('') +client.entities({text:''}) ``` ### Generation Endpoint @@ -260,7 +260,7 @@ Call the `generation()` method and pass the following arguments: 1. (Optional) `removeEndSequence`: Optional. Whether you want to remove the `endSequence` string from the result. Defaults to false. ```js -client.generation('') +client.generation({text:''}) ``` ### Grammar and Spelling Correction Endpoint @@ -268,7 +268,7 @@ client.generation('') Call the `gsCorrection()` method and pass the text you want to correct. ```js -client.gsCorrection('') +client.gsCorrection({text:''}) ``` ### Image Generation Endpoint @@ -276,7 +276,7 @@ client.gsCorrection('') Call the `imageGeneration()` method and pass the text you want to use to generate your image. ```js -client.imageGeneration('') +client.imageGeneration({text:''}) ``` ### Intent Classification Endpoint @@ -284,7 +284,7 @@ client.imageGeneration('') Call the `intentClassification()` method and pass the text you want to analyze in order to detect the intent. ```js -client.intentClassification('') +client.intentClassification({text:''}) ``` ### Keywords and Keyphrases Extraction Endpoint @@ -292,7 +292,7 @@ client.intentClassification('') Call the `kwKpExtraction()` method and pass the text you want to extract keywords and keyphrases from. ```js -client.kwKpExtraction('') +client.kwKpExtraction({text:''}) ``` ### Language Detection Endpoint @@ -300,7 +300,7 @@ client.kwKpExtraction('') Call the `langdetection()` method and pass the text you want to analyze in order to detect the languages. ```js -client.langdetection('') +client.langdetection({text:''}) ``` ### Library Versions Endpoint @@ -319,7 +319,7 @@ Call the `question()` method and pass the following: 1. A context that the model will use to try to answer your question ```js -client.question('','') +client.question({question:'', context:''}) ``` ### Semantic Search Endpoint @@ -337,7 +337,7 @@ The above command returns a JSON object. Call the `semanticSimilarity()` method and pass an array made up of 2 blocks of text that you want to compare. ```python -client.semanticSimilarity(['', '']) +client.semanticSimilarity({sentences:['', '']}) ``` The above command returns a JSON object. @@ -347,7 +347,7 @@ The above command returns a JSON object. Call the `sentenceDependencies()` method and pass a block of text made up of several sentences you want to perform POS + arcs on. ```js -client.sentenceDependencies('') +client.sentenceDependencies({text:''}) ``` ### Sentiment Analysis Endpoint @@ -355,7 +355,7 @@ client.sentenceDependencies('') Call the `sentiment()` method and pass the text you want to analyze the sentiment of: ```js -client.sentiment('') +client.sentiment({text:''}) ``` ### Speech Synthesis Endpoint @@ -363,7 +363,7 @@ client.sentiment('') Call the `speechSynthesis()` method and pass the text you want to convert to audio: ```js -client.speechSynthesis("") +client.speechSynthesis({text:""}) ``` The above command returns a JSON object. @@ -373,7 +373,7 @@ The above command returns a JSON object. Call the `summarization()` method and pass the text you want to summarize. ```js -client.summarization('') +client.summarization({text:''}) ``` ### Paraphrasing Endpoint @@ -381,7 +381,7 @@ client.summarization('') Call the `paraphrasing()` method and pass the text you want to paraphrase. ```js -client.paraphrasing('') +client.paraphrasing({text:''}) ``` ### Tokenization Endpoint @@ -389,7 +389,7 @@ client.paraphrasing('') Call the `tokens()` method and pass the text you want to tokenize. ```js -client.tokens('') +client.tokens({text:''}) ``` ### Translation Endpoint @@ -397,5 +397,5 @@ client.tokens('') Call the `translation()` method and pass the text you want to translate. ```js -client.translation('') +client.translation({text:''}) ``` diff --git a/dist/index.d.ts b/dist/index.d.ts index 33a6ca1..1d07029 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,20 +1,33 @@ export = Client; declare class Client { - constructor(model: string, token: string, gpu?: boolean, lang?: string, asynchronous?: boolean); + constructor(params: { + model: string; + token: string; + gpu?: boolean; + lang?: string; + async?: boolean; + }); headers: { Authorization: string; 'User-Agent': string; }; rootURL: string; - adGeneration(keywords: string[]): Promise<{ + adGeneration(params: { + keywords: string[] + }): Promise<{ status: number; statusText: string; data: { generated_text: string; }; }>; - asr(url?: string, encodedFile?: string, inputLanguage?: string): Promise<{ + + asr(params: { + url?: string, + encodedFile?: string, + inputLanguage?: string + }): Promise<{ status: number; statusText: string; data: { @@ -30,7 +43,10 @@ declare class Client { url: string; } }>; - asyncResult(url: string): Promise<{ + + asyncResult(params: { + url: string + }): Promise<{ status: number; statusText: string; data: { @@ -42,7 +58,12 @@ declare class Client { content: string; }; }>; - chatbot(input: string, context: string, history: { input: string, response: string }[]): Promise<{ + + chatbot(params: { + input: string, + context?: string, + history?: { input: string, response: string }[] + }): Promise<{ status: number; statusText: string; data: { @@ -50,7 +71,12 @@ declare class Client { history: { input: string, response: string }[]; }; }>; - classification(text: string, labels: string[], multiClass?: boolean): Promise<{ + + classification(params: { + text: string, + labels?: string[], + multiClass?: boolean + }): Promise<{ status: number; statusText: string; data: { @@ -58,35 +84,49 @@ declare class Client { scores: number[]; }; }>; - codeGeneration(instruction: string): Promise<{ + + codeGeneration(params: { + instruction: string + }): Promise<{ status: number; statusText: string; data: { generated_code: string; }; }>; - dependencies(text: string): Promise<{ + + dependencies(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { words: { text: string, tag: string }[]; }; }>; - embeddings(text: string[]): Promise<{ + + embeddings(params: { + sentences: string[] + }): Promise<{ status: number; statusText: string; data: { score: number[][]; }; }>; - entities(text: string, searchedEntity?: string): Promise<{ + + entities(params: { + text: string, + searchedEntity?: string + }): Promise<{ status: number; statusText: string; data: { entities: { start: number; end: number; type: string; text: string }[]; }; }>; - generation(text: string, + + generation(params: {text: string, maxLength?: number, lengthNoInput?: boolean, endSequence?: string, @@ -97,7 +137,8 @@ declare class Client { topP?: number, temperature?: number, repetitionPenalty?: number, - removeEndSequence?: boolean): Promise<{ + removeEndSequence?: boolean + }): Promise<{ status: number; statusText: string; data: { @@ -106,7 +147,10 @@ declare class Client { nb_generated_tokens: number; }; }>; - gsCorrection(text: string): Promise<{ + + gsCorrection(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { @@ -119,40 +163,56 @@ declare class Client { url: string; } }>; - imageGeneration(text: string): Promise<{ + + imageGeneration(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { url: string; }; }>; - intentClassification(text: string): Promise<{ + + intentClassification(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { intent: string; }; }>; - kwKpExtraction(text: string): Promise<{ + + kwKpExtraction(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { keywords_and_keyphrases: string[]; }; }>; - langdetection(text: string): Promise<{ + + langdetection(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { languages: any[]; }; }>; + libVersions(): Promise<{ status: number; statusText: string; data: any; }>; - paraphrasing(text: string): Promise<{ + + paraphrasing(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { @@ -165,7 +225,11 @@ declare class Client { url: string; } }>; - question(context: string, question: string): Promise<{ + + question(params: { + context?: string, + question: string + }): Promise<{ status: number; statusText: string; data: { @@ -175,42 +239,62 @@ declare class Client { end: number; }; }>; - semanticSearch(text: string, numResults: number): Promise<{ + + semanticSearch(params: { + text: string, + numResults?: number + }): Promise<{ status: number; statusText: string; data: { search_results: { score: number, text: string }[]; }; }>; - semanticSimilarity(sentences: string[]): Promise<{ + + semanticSimilarity(params: { + sentences: string[] + }): Promise<{ status: number; statusText: string; data: { score: number; }; }>; - sentenceDependencies(text: string): Promise<{ + + sentenceDependencies(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { sentence_dependencies: { sentence: string, dependencies: { words: { text: string, tag: string }[], arcs: { start: number, end: number, label: string, text: string, dir: string }[] } }[]; }; }>; - sentiment(text: string): Promise<{ + + sentiment(params: { + text: string + }): Promise<{ status: number; statusText: string; data: { scored_labels: { label: string, score: number }[]; }; }>; - speechSynthesis(text: string, voice?: string): Promise<{ + speechSynthesis(params: { + text: string, + voice?: string + }): Promise<{ status: number; statusText: string; data: { url: string; }; }>; - summarization(text: string, size?: string): Promise<{ + + summarization(params: { + text: string, + size?: string + }): Promise<{ status: number; statusText: string; data: { @@ -223,7 +307,12 @@ declare class Client { url: string; } }>; - translation(text: string, source: string, target: string): Promise<{ + + translation(params: { + text: string, + source?: string, + target: string + }): Promise<{ status: number; statusText: string; data: { diff --git a/index.js b/index.js index 186da8e..cee67f4 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,13 @@ const BASE_URL = 'https://api.nlpcloud.io' const API_VERSION = 'v1' class Client { - constructor(model, token, gpu = false, lang = '', asynchronous = false) { + constructor(params) { + var model = params.model + var token = params.token + var gpu = params.gpu ?? false + var lang = params.lang ?? '' + var async = params.async ?? false + this.headers = { 'Authorization': 'Token ' + token, 'User-Agent': 'nlpcloud-javascript-client' @@ -23,7 +29,7 @@ class Client { this.rootURL += 'gpu/' } - if (asynchronous) { + if (async) { this.rootURL += "async/" } @@ -35,7 +41,9 @@ class Client { } - adGeneration(keywords) { + adGeneration(params) { + var keywords = params.keywords + const payload = { 'keywords': keywords }; @@ -43,7 +51,11 @@ class Client { return axios.post(this.rootURL + '/' + 'ad-generation', payload, { headers: this.headers }) } - asr(url = null, encodedFile = null, inputLanguage = null) { + asr(params) { + var url = params.url ?? null + var encodedFile = params.encodedFile ?? null + var inputLanguage = params.inputLanguage ?? null + const payload = { 'url': url, 'encoded_file': encodedFile, @@ -53,11 +65,17 @@ class Client { return axios.post(this.rootURL + '/' + 'asr', payload, { headers: this.headers }) } - asyncResult(url) { + asyncResult(params) { + var url = params.url + return axios.get(url, { headers: this.headers }) } - chatbot(input, context = null, history = null) { + chatbot(params) { + var input = params.input + var context = params.context ?? null + var history = params.history ?? null + const payload = { 'input': input, 'context': context, @@ -67,7 +85,11 @@ class Client { return axios.post(this.rootURL + '/' + 'chatbot', payload, { headers: this.headers }) } - classification(text, labels = null, multiClass = null) { + classification(params) { + var text = params.text + var labels = params.labels ?? null + var multiClass = params.multiClass ?? null + const payload = { 'text': text, 'labels': labels, @@ -77,7 +99,9 @@ class Client { return axios.post(this.rootURL + '/' + 'classification', payload, { headers: this.headers }) } - codeGeneration(instruction) { + codeGeneration(params) { + var instruction = params.instruction + const payload = { 'instruction': instruction }; @@ -85,7 +109,9 @@ class Client { return axios.post(this.rootURL + '/' + 'code-generation', payload, { headers: this.headers }) } - dependencies(text) { + dependencies(params) { + var text = params.text + const payload = { 'text': text }; @@ -93,7 +119,9 @@ class Client { return axios.post(this.rootURL + '/' + 'dependencies', payload, { headers: this.headers }) } - embeddings(sentences) { + embeddings(params) { + var sentences = params.sentences + const payload = { 'sentences': sentences }; @@ -101,7 +129,10 @@ class Client { return axios.post(this.rootURL + '/' + 'embeddings', payload, { headers: this.headers }) } - entities(text, searchedEntity = null) { + entities(params) { + var text = params.text + var searchedEntity = params.searchedEntity ?? null + const payload = { 'text': text, 'searched_entity': searchedEntity @@ -110,11 +141,21 @@ class Client { return axios.post(this.rootURL + '/' + 'entities', payload, { headers: this.headers }) } - generation(text, maxLength = null, lengthNoInput = null, - endSequence = null, removeInput = null, numBeams = null, - numReturnSequences = null, topK = null, topP = null, - temperature = null, repetitionPenalty = null, badWords = null, - removeEndSequence = null) { + generation(params) { + var text = params.text + var maxLength = params.maxLength ?? null + var lengthNoInput = params.lengthNoInput ?? null + var endSequence = params.endSequence ?? null + var removeInput = params.removeInput ?? null + var numBeams = params.numBeams ?? null + var numReturnSequences = params.numReturnSequences ?? null + var topK = params.topK ?? null + var topP = params.topP ?? null + var temperature = params.temperature ?? null + var repetitionPenalty = params.repetitionPenalty ?? null + var badWords = params.badWords ?? null + var removeEndSequence = params.removeEndSequence ?? null + const payload = { 'text': text, 'max_length': maxLength, @@ -134,7 +175,9 @@ class Client { return axios.post(this.rootURL + '/' + 'generation', payload, { headers: this.headers }) } - gsCorrection(text) { + gsCorrection(params) { + var text = params.text + const payload = { 'text': text }; @@ -142,7 +185,9 @@ class Client { return axios.post(this.rootURL + '/' + 'gs-correction', payload, { headers: this.headers }) } - imageGeneration(text) { + imageGeneration(params) { + var text = params.text + const payload = { 'text': text }; @@ -150,7 +195,9 @@ class Client { return axios.post(this.rootURL + '/' + 'image-generation', payload, { headers: this.headers }) } - intentClassification(text) { + intentClassification(params) { + var text = params.text + const payload = { 'text': text }; @@ -158,7 +205,9 @@ class Client { return axios.post(this.rootURL + '/' + 'intent-classification', payload, { headers: this.headers }) } - kwKpExtraction(text) { + kwKpExtraction(params) { + var text = params.text + const payload = { 'text': text }; @@ -166,7 +215,9 @@ class Client { return axios.post(this.rootURL + '/' + 'kw-kp-extraction', payload, { headers: this.headers }) } - langdetection(text) { + langdetection(params) { + var text = params.text + const payload = { 'text': text }; @@ -179,7 +230,9 @@ class Client { return axios.get(this.rootURL + '/' + 'versions', { headers: this.headers }) } - paraphrasing(text) { + paraphrasing(params) { + var text = params.text + const payload = { 'text': text }; @@ -188,6 +241,9 @@ class Client { } question(question, context = null) { + var question = params.question + var context = params.context ?? null + const payload = { 'question': question, 'context': context @@ -196,7 +252,10 @@ class Client { return axios.post(this.rootURL + '/' + 'question', payload, { headers: this.headers }) } - semanticSearch(text, numResults = null) { + semanticSearch() { + var text = params.text + var numResults = params.numResults ?? null + const payload = { 'text': text, 'num_results': numResults @@ -205,7 +264,9 @@ class Client { return axios.post(this.rootURL + '/' + 'semantic-search', payload, { headers: this.headers }) } - semanticSimilarity(sentences) { + semanticSimilarity(params) { + var sentences = params.sentences + const payload = { 'sentences': sentences }; @@ -213,7 +274,9 @@ class Client { return axios.post(this.rootURL + '/' + 'semantic-similarity', payload, { headers: this.headers }) } - sentenceDependencies(text) { + sentenceDependencies(params) { + var text = params.text + const payload = { 'text': text }; @@ -221,7 +284,9 @@ class Client { return axios.post(this.rootURL + '/' + 'sentence-dependencies', payload, { headers: this.headers }) } - sentiment(text) { + sentiment(params) { + var text = params.text + const payload = { 'text': text }; @@ -230,6 +295,9 @@ class Client { } speechSynthesis(text, voice = null) { + var text = params.text + var voice = params.voice ?? null + const payload = { 'text': text, 'voice': voice @@ -238,7 +306,10 @@ class Client { return axios.post(this.rootURL + '/' + 'speech-synthesis', payload, { headers: this.headers }) } - summarization(text, size = null) { + summarization(params) { + var text = params.text + var size = params.size ?? null + const payload = { 'text': text, 'size': size @@ -247,7 +318,9 @@ class Client { return axios.post(this.rootURL + '/' + 'summarization', payload, { headers: this.headers }) } - tokens(text) { + tokens(params) { + var text = params.text + const payload = { 'text': text }; @@ -255,7 +328,11 @@ class Client { return axios.post(this.rootURL + '/' + 'tokens', payload, { headers: this.headers }) } - translation(text, source, target) { + translation(params) { + var text = params.text + var source = params.source ?? null + var target = params.target + const payload = { 'text': text, 'source': source, diff --git a/package-lock.json b/package-lock.json index 2fb0919..5080a9b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "nlpcloud", - "version": "1.0.13", + "version": "1.0.48", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "nlpcloud", - "version": "1.0.13", + "version": "1.0.48", "license": "MIT", "dependencies": { - "axios": "^0.21.1" + "axios": "^0.21.4" }, "devDependencies": { "typescript": "^4.4.3" diff --git a/package.json b/package.json index 98b0f06..d7db471 100644 --- a/package.json +++ b/package.json @@ -26,10 +26,10 @@ }, "homepage": "https://github.com/nlpcloud/nlpcloud-js#readme", "dependencies": { - "axios": "^0.21.1" + "axios": "^0.21.4" }, "devDependencies": { "typescript": "^4.4.3" }, "types": "./dist/index.d.ts" -} \ No newline at end of file +}