cognitive-decision-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/index.d.ts +12 -0
- package/index.js +59 -0
- package/package.json +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# cognitive-decision
|
|
2
|
+
|
|
3
|
+
Tiny SDK to call the `classify` method of the Cognitive Decision.
|
|
4
|
+
|
|
5
|
+
Installation
|
|
6
|
+
|
|
7
|
+
You can install from your repo or publish the package. For local testing, from the project root:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install ./sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Usage (CommonJS / Node)
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const { ClassifierClient } = require('cognitive-decision');
|
|
17
|
+
|
|
18
|
+
// No URL required — defaults to the public deployment
|
|
19
|
+
const client = new ClassifierClient();
|
|
20
|
+
|
|
21
|
+
(async () => {
|
|
22
|
+
const resp = await client.classify('I love transformers!', ['positive','negative']);
|
|
23
|
+
console.log(resp);
|
|
24
|
+
})();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Usage (TypeScript / ESM)
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { ClassifierClient } from 'cognitive-decision';
|
|
31
|
+
|
|
32
|
+
// No options required — defaults to https://cognitive-decision.vercel.app
|
|
33
|
+
const client = new ClassifierClient();
|
|
34
|
+
const resp = await client.classify('I love transformers!', ['positive','negative']);
|
|
35
|
+
console.log(resp);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Notes
|
|
39
|
+
|
|
40
|
+
- The SDK uses the global `fetch` API. In Node.js versions older than 18, install `node-fetch` in your project:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install node-fetch
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- The SDK defaults the API base URL to https://cognitive-decision.vercel.app.
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*
|
|
2
|
+
cognitive-decision
|
|
3
|
+
Simple SDK to call the /classify endpoint of your app.
|
|
4
|
+
|
|
5
|
+
Node compatibility: uses global fetch (Node 18+). For earlier Node versions,
|
|
6
|
+
install `node-fetch` and the SDK will require it automatically.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
let fetchFn = (typeof globalThis !== 'undefined' && globalThis.fetch) ? globalThis.fetch : null;
|
|
10
|
+
try {
|
|
11
|
+
if (!fetchFn) fetchFn = require('node-fetch');
|
|
12
|
+
} catch (e) {
|
|
13
|
+
// ignore; we'll throw below if no fetch available
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!fetchFn) {
|
|
17
|
+
throw new Error('Fetch API not found. Use Node 18+ or install node-fetch');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class ClassifierClient {
|
|
21
|
+
/**
|
|
22
|
+
* The SDK always uses the public deployment URL; any provided options are ignored.
|
|
23
|
+
* @param {any} [options] ignored
|
|
24
|
+
*/
|
|
25
|
+
constructor(options) {
|
|
26
|
+
if (options) {
|
|
27
|
+
try {
|
|
28
|
+
// Friendly warning when consumers pass options
|
|
29
|
+
console.warn('ClassifierClient: provided options/baseUrl are ignored; using https://cognitive-decision.vercel.app/api');
|
|
30
|
+
} catch (e) {}
|
|
31
|
+
}
|
|
32
|
+
this.baseUrl = 'https://cognitive-decision.vercel.app/api';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Call the classification API.
|
|
37
|
+
* @param {string} text
|
|
38
|
+
* @param {string[]} labels
|
|
39
|
+
*/
|
|
40
|
+
async classify(text, labels) {
|
|
41
|
+
if (!text) throw new Error('text is required');
|
|
42
|
+
if (!labels || !Array.isArray(labels)) throw new Error('labels array is required');
|
|
43
|
+
|
|
44
|
+
const res = await fetchFn(this.baseUrl + '/classify', {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { 'Content-Type': 'application/json' },
|
|
47
|
+
body: JSON.stringify({ text, labels })
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
const body = await res.text().catch(() => '');
|
|
52
|
+
throw new Error(`Classifier request failed: ${res.status} ${res.statusText} ${body}`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return res.json();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = { ClassifierClient };
|
package/package.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cognitive-decision-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tiny SDK to call the Cognitive Decision classification API",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"keywords": ["huggingface", "classification", "sdk"],
|
|
8
|
+
"license": "MIT"
|
|
9
|
+
}
|