Comparing version 4.0.0 to 4.1.0
@@ -1,7 +0,9 @@ | ||
import * as models from './models'; | ||
import * as models from "./models"; | ||
interface CohereService { | ||
init(key: string, version?: string): void; | ||
generate(config: models.generate): Promise<models.cohereResponse<models.text>>; | ||
embed(config: models.embed): Promise<models.cohereResponse<models.embeddings>>; | ||
extract(config: models.extract): Promise<models.cohereResponse<models.extraction[]>>; | ||
generate(config: models.generateRequest): Promise<models.cohereResponse<models.generateResponse>>; | ||
classify(config: models.classifyRequest): Promise<models.cohereResponse<models.classifyResponse>>; | ||
tokenize(config: models.tokenizeRequest): Promise<models.cohereResponse<models.tokenizeResponse>>; | ||
embed(config: models.embedRequest): Promise<models.cohereResponse<models.embedResponse>>; | ||
extract(config: models.extractRequest): Promise<models.cohereResponse<models.extractResponse>>; | ||
} | ||
@@ -14,3 +16,7 @@ declare class Cohere implements CohereService { | ||
*/ | ||
generate(config: models.generate): Promise<models.cohereResponse<models.text>>; | ||
generate(config: models.generateRequest): Promise<models.cohereResponse<models.generateResponse>>; | ||
/** Returns a list of tokens for the specified text. | ||
* See: https://docs.cohere.ai/tokenize-reference | ||
*/ | ||
tokenize({ text, }: models.tokenizeRequest): Promise<models.cohereResponse<models.tokenizeResponse>>; | ||
/** Returns text embeddings. An embedding is a list of floating point numbers that captures semantic | ||
@@ -20,13 +26,14 @@ * information about the text that it represents. | ||
*/ | ||
embed(config: models.embed): Promise<models.cohereResponse<models.embeddings>>; | ||
embed(config: models.embedRequest): Promise<models.cohereResponse<models.embedResponse>>; | ||
/** | ||
* Classifies text as one of the given labels. Returns a confidence score for each label. | ||
* See: https://docs.cohere.ai/classify-reference | ||
*/ | ||
classify(config: models.classify): Promise<models.cohereResponse<models.classifications>>; | ||
classify(config: models.classifyRequest): Promise<models.cohereResponse<models.classifyResponse>>; | ||
/** | ||
* Extract text from texts, with examples | ||
* (Beta) Extract entities from text, by providing examples | ||
*/ | ||
extract(config: models.extract): Promise<models.cohereResponse<models.extraction[]>>; | ||
extract(config: models.extractRequest): Promise<models.cohereResponse<models.extractResponse>>; | ||
} | ||
declare const cohere: Cohere; | ||
export = cohere; |
@@ -40,2 +40,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
ENDPOINT["EXTRACT"] = "/extract"; | ||
ENDPOINT["TOKENIZE"] = "/tokenize"; | ||
})(ENDPOINT || (ENDPOINT = {})); | ||
@@ -58,2 +59,11 @@ var COHERE_EMBED_BATCH_SIZE = 5; | ||
}; | ||
/** Returns a list of tokens for the specified text. | ||
* See: https://docs.cohere.ai/tokenize-reference | ||
*/ | ||
Cohere.prototype.tokenize = function (_a) { | ||
var text = _a.text; | ||
return this.makeRequest(ENDPOINT.TOKENIZE, { | ||
text: text, | ||
}); | ||
}; | ||
/** Returns text embeddings. An embedding is a list of floating point numbers that captures semantic | ||
@@ -95,2 +105,3 @@ * information about the text that it represents. | ||
* Classifies text as one of the given labels. Returns a confidence score for each label. | ||
* See: https://docs.cohere.ai/classify-reference | ||
*/ | ||
@@ -101,3 +112,3 @@ Cohere.prototype.classify = function (config) { | ||
/** | ||
* Extract text from texts, with examples | ||
* (Beta) Extract entities from text, by providing examples | ||
*/ | ||
@@ -166,4 +177,4 @@ Cohere.prototype.extract = function (config) { | ||
function APIImpl() { | ||
this.COHERE_API_KEY = ''; | ||
this.COHERE_VERSION = ''; | ||
this.COHERE_API_KEY = ""; | ||
this.COHERE_VERSION = ""; | ||
} | ||
@@ -173,3 +184,3 @@ APIImpl.prototype.init = function (key, version) { | ||
if (version === undefined) { | ||
this.COHERE_VERSION = '2021-11-08'; // currently latest, update when we version better | ||
this.COHERE_VERSION = "2021-11-08"; // currently latest, update when we version better | ||
} | ||
@@ -194,14 +205,14 @@ else { | ||
path: endpoint, | ||
method: 'POST', | ||
method: "POST", | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
'Content-Length': Buffer.byteLength(reqData, 'utf8'), | ||
'Cohere-Version': _this.COHERE_VERSION, | ||
"Content-Type": "application/json; charset=utf-8", | ||
"Content-Length": Buffer.byteLength(reqData, "utf8"), | ||
"Cohere-Version": _this.COHERE_VERSION, | ||
Authorization: "Bearer " + _this.COHERE_API_KEY, | ||
'Request-Source': 'node-sdk', | ||
"Request-Source": "node-sdk", | ||
}, | ||
}, function (res) { | ||
var data = []; | ||
res.on('data', function (chunk) { return data.push(chunk); }); | ||
res.on('end', function () { | ||
res.on("data", function (chunk) { return data.push(chunk); }); | ||
res.on("end", function () { | ||
resolve({ | ||
@@ -213,6 +224,6 @@ statusCode: res.statusCode, | ||
}); | ||
req.on('error', function (error) { | ||
req.on("error", function (error) { | ||
return reject(error_service_1.default.handleError(error)); | ||
}); | ||
req.write(reqData, 'utf8'); | ||
req.write(reqData, "utf8"); | ||
req.end(); | ||
@@ -246,3 +257,3 @@ })]; | ||
message: message, | ||
} | ||
}, | ||
}; | ||
@@ -249,0 +260,0 @@ }; |
@@ -6,4 +6,21 @@ export interface cohereResponse<T> { | ||
/*-- function parameters --*/ | ||
export interface generate { | ||
/*-- types --*/ | ||
export interface extractEntity { | ||
type: string; | ||
value: string; | ||
} | ||
export interface extractExample { | ||
text: string; | ||
entities: extractEntity[]; | ||
} | ||
export interface extraction { | ||
id: string; | ||
text: string; | ||
entities: extractEntity[]; | ||
} | ||
/*-- requests --*/ | ||
export interface generateRequest { | ||
/** Denotes the model to be used. Defaults to the best performing model */ | ||
@@ -46,6 +63,6 @@ model?: string; | ||
*/ | ||
return_likelihoods?: 'GENERATION' | 'ALL' | 'NONE'; | ||
return_likelihoods?: "GENERATION" | "ALL" | "NONE"; | ||
} | ||
export interface embed { | ||
export interface embedRequest { | ||
/** Denotes the model to be used. Defaults to the best performing model */ | ||
@@ -56,6 +73,6 @@ model?: string; | ||
/** Specifies how the API will handle inputs longer than the maximum token length. */ | ||
truncate?: 'NONE' | 'LEFT' | 'RIGHT'; | ||
truncate?: "NONE" | "LEFT" | "RIGHT"; | ||
} | ||
export interface classify { | ||
export interface classifyRequest { | ||
/** Denotes the model to be used. Defaults to the best performing model */ | ||
@@ -73,23 +90,39 @@ model?: string; | ||
export type cohereParameters = generate | embed | classify | extract; | ||
export interface tokenizeRequest { | ||
/** The text to be tokenized */ | ||
text: string; | ||
} | ||
export interface extractRequest { | ||
examples: extractExample[]; | ||
texts: string[]; | ||
} | ||
export type cohereParameters = | ||
| generateRequest | ||
| embedRequest | ||
| classifyRequest | ||
| extractRequest | ||
| tokenizeRequest; | ||
/* -- responses -- */ | ||
export interface text { | ||
/** Contains the generated text. */ | ||
text: string; | ||
/** The sum of the log-likehoods of each token in the string. */ | ||
likelihood: number; | ||
/** Only returned if `return_likelihoods` is not set to NONE. The likelihood. */ | ||
token_likelihoods?: { | ||
/** The token. */ | ||
token: string; | ||
/** Refers to the log-likelihood of the token. The first token of a context will not | ||
* have a likelihood. | ||
* */ | ||
export interface generateResponse { | ||
generations: { | ||
text: string; | ||
likelihood?: number; | ||
}; | ||
[key: string]: any; | ||
token_likelihoods?: [ | ||
{ | ||
token: string; | ||
likelihood: number; | ||
} | ||
]; | ||
}[]; | ||
} | ||
export interface embeddings { | ||
export interface tokenizeResponse { | ||
/** An array of integers, representing the token ids for the specified text. */ | ||
tokens: number[]; | ||
} | ||
export interface embedResponse { | ||
/** An array of embeddings, where each embedding is an array of floats. The length of the embeddings | ||
@@ -99,32 +132,5 @@ * array will be the same as the length of the original texts array. | ||
embeddings: number[][]; | ||
[key: string]: any; | ||
} | ||
export interface scores { | ||
/** An array of floats corresponding to a score for each of the options, where a higher score | ||
* represents a more likely query-option pair. | ||
*/ | ||
scores: number[]; | ||
/** An array of tokens corresponding to the tokens for each of the options. */ | ||
tokens: string[][]; | ||
/** An array of log likelihoods corresponding to the tokens of each of the options. */ | ||
token_log_likelihoods: number[][]; | ||
[key: string]: any; | ||
} | ||
export interface token_likelihoods { | ||
/** The sum of the log-likehoods of each token in the string. */ | ||
likelihood: number; | ||
token_likelihoods: { | ||
/** The token. */ | ||
token: string; | ||
/** Refers to the log-likelihood of the token. The first token of a context will not | ||
* have a likelihood. | ||
* */ | ||
likelihood?: number; | ||
}; | ||
[key: string]: any; | ||
} | ||
export interface classifications { | ||
export interface classifyResponse { | ||
classifications: { | ||
@@ -140,34 +146,16 @@ /** The input that is being classified. */ | ||
export interface extraction { | ||
id: string; | ||
text: string; | ||
entities: extractEntity[]; | ||
export interface extractResponse { | ||
results: extraction[]; | ||
} | ||
export interface extractEntity { | ||
type: string; | ||
value: string; | ||
} | ||
export interface extractExample { | ||
text: string; | ||
entities: extractEntity[]; | ||
} | ||
export interface extract { | ||
examples: extractExample[]; | ||
texts: string[]; | ||
} | ||
export interface error { | ||
/** Text explaining what went wrong. */ | ||
message?: string; | ||
[key: string]: any; | ||
} | ||
export type responseBody = | ||
| text | ||
| embeddings | ||
| scores | ||
| token_likelihoods | ||
| classifications | ||
| generateResponse | ||
| embedResponse | ||
| classifyResponse | ||
| tokenizeResponse | ||
| error; |
@@ -1,1 +0,1 @@ | ||
module.exports = require('./dist/cohere'); | ||
module.exports = require("./dist/cohere"); |
{ | ||
"name": "cohere-ai", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "A Node.js SDK with TypeScript support for the Cohere API.", | ||
@@ -12,4 +12,6 @@ "homepage": "https://docs.cohere.ai", | ||
"dev": "webpack --progress --env development --env nodemon", | ||
"test": "npm run build && mocha -r ts-node/register test/test.ts", | ||
"publish": "npm run build && npm publish" | ||
"test": "mocha -r ts-node/register test/test.ts", | ||
"publish": "npm run build && npm publish", | ||
"lint": "eslint . --ext .ts && prettier --check .", | ||
"lint:fix": "prettier --write ." | ||
}, | ||
@@ -48,7 +50,8 @@ "files": [ | ||
"copy-webpack-plugin": "^8.1.1", | ||
"dotenv": "^9.0.2", | ||
"eslint": "^7.27.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-webpack-plugin": "^2.5.4", | ||
"mocha": "^8.4.0", | ||
"mocha": "^10.0.0", | ||
"nodemon-webpack-plugin": "^4.5.2", | ||
"prettier": "^2.7.1", | ||
"ts-loader": "^9.1.2", | ||
@@ -55,0 +58,0 @@ "ts-node": "^9.1.1", |
@@ -16,13 +16,17 @@ # Welcome to the Cohere AI Node.js SDK. | ||
### Import the library to your node.js project. | ||
```js | ||
const cohere = require('cohere-ai'); | ||
const cohere = require("cohere-ai"); | ||
``` | ||
### Initialize the library using the latest version of the API. | ||
```js | ||
cohere.init('YOUR_API_KEY'); | ||
cohere.init("YOUR_API_KEY"); | ||
``` | ||
### Or optionally initialize with a specific version. (Learn about versions [here](https://docs.cohere.ai/versions-reference).) | ||
```js | ||
cohere.init('YOUR_API_KEY', '2021-11-08'); | ||
cohere.init("YOUR_API_KEY", "2021-11-08"); | ||
``` | ||
@@ -33,16 +37,22 @@ | ||
```js | ||
cohere.generate('MODEL_NAME', config); | ||
cohere.generate("MODEL_NAME", config); | ||
``` | ||
## Endpoints | ||
For a full breakdown of endpoints and their config objects please consult the [Cohere Docs](https://docs.cohere.ai/). | ||
Cohere Endpoint | Function | ||
----- | ----- | ||
/generate | cohere.generate() | ||
/embed | cohere.embed() | ||
| Cohere Endpoint | Function | | ||
| --------------- | ----------------- | | ||
| /generate | cohere.generate() | | ||
| /embed | cohere.embed() | | ||
| /classify | cohere.classify() | | ||
| /tokenize | cohere.tokenize() | | ||
## Models | ||
To view an up to date list of available models please consult the [Cohere CLI](https://docs.cohere.ai/command/). To get started try out `large`. | ||
## Responses | ||
All of the endpoint functions will return a response structure. For a detailed breakdown of the response body visit the [Cohere Docs](https://docs.cohere.ai/). | ||
@@ -57,12 +67,14 @@ | ||
## *Code Examples:* | ||
## _Code Examples:_ | ||
```js | ||
(async () => { | ||
cohere.init(process.env.API_KEY); | ||
cohere.init(process.env.COHERE_API_KEY); | ||
// Hit the `generate` endpoint on the `large` model | ||
const generateResponse = await cohere.generate("large", { | ||
const generateResponse = await cohere.generate({ | ||
model: "large", | ||
prompt: "Once upon a time in a magical land called", | ||
max_tokens: 50, | ||
temperature: 1 | ||
temperature: 1, | ||
}); | ||
@@ -95,9 +107,13 @@ | ||
## TypeScript support | ||
Import the package as a class. | ||
```ts | ||
import cohere = require('cohere-ai') | ||
import cohere = require("cohere-ai"); | ||
``` | ||
Require the `cohere` package as usual, and the `./index.d.ts` file will be imported by typescript automatically. | ||
## cohere-node package readme | ||
If you'd like to help contribute to the package library itself or modify it locally, please check the development instructions [readme](https://github.com/cohere-ai/cohere-node/blob/main/DEV.md). | ||
If you'd like to help contribute to the package library itself or modify it locally, please check the development instructions [readme](https://github.com/cohere-ai/cohere-node/blob/main/DEV.md). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
37437
461
116
18