@memberjunction/ai-cohere 3.4.0 → 4.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 +75 -0
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -11
- package/dist/index.js.map +1 -1
- package/dist/models/CohereReranker.d.ts +0 -5
- package/dist/models/CohereReranker.d.ts.map +1 -1
- package/dist/models/CohereReranker.js +13 -22
- package/dist/models/CohereReranker.js.map +1 -1
- package/package.json +8 -7
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# @memberjunction/ai-cohere
|
|
2
|
+
|
|
3
|
+
MemberJunction AI provider for Cohere's reranking capabilities. This package implements the `BaseReranker` interface to provide semantic document reranking using Cohere's Rerank API, useful for improving search result relevance in RAG (Retrieval-Augmented Generation) pipelines.
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph TD
|
|
9
|
+
A["CohereReranker<br/>(Provider)"] -->|extends| B["BaseReranker<br/>(@memberjunction/ai)"]
|
|
10
|
+
A -->|wraps| C["CohereClient<br/>(cohere-ai SDK)"]
|
|
11
|
+
C -->|calls| D["Cohere Rerank API"]
|
|
12
|
+
D -->|returns| E["Ranked Documents<br/>with Relevance Scores"]
|
|
13
|
+
B -->|registered via| F["@RegisterClass"]
|
|
14
|
+
|
|
15
|
+
style A fill:#7c5295,stroke:#563a6b,color:#fff
|
|
16
|
+
style B fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
17
|
+
style C fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
18
|
+
style D fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
19
|
+
style E fill:#b8762f,stroke:#8a5722,color:#fff
|
|
20
|
+
style F fill:#b8762f,stroke:#8a5722,color:#fff
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Semantic Reranking**: Reorder documents by relevance to a query using neural models
|
|
26
|
+
- **Multiple Models**: Support for `rerank-v3.5` (English) and `rerank-multilingual-v3.0` (100+ languages)
|
|
27
|
+
- **Relevance Scoring**: Documents scored 0-1 with fine-grained relevance ranking
|
|
28
|
+
- **RAG Pipeline Integration**: Designed for use in retrieval-augmented generation workflows
|
|
29
|
+
- **Context-Aware**: Enhanced query processing for better relevance evaluation
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @memberjunction/ai-cohere
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { CohereReranker } from '@memberjunction/ai-cohere';
|
|
41
|
+
|
|
42
|
+
const reranker = new CohereReranker('your-cohere-api-key', 'rerank-v3.5');
|
|
43
|
+
|
|
44
|
+
const results = await reranker.Rerank({
|
|
45
|
+
query: 'What is the capital of France?',
|
|
46
|
+
documents: [
|
|
47
|
+
{ id: '1', text: 'Paris is the capital of France.' },
|
|
48
|
+
{ id: '2', text: 'London is the capital of England.' },
|
|
49
|
+
{ id: '3', text: 'France is a country in Europe.' }
|
|
50
|
+
],
|
|
51
|
+
topK: 5
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Results sorted by relevance score (0-1)
|
|
55
|
+
for (const result of results) {
|
|
56
|
+
console.log(`${result.documentId}: ${result.relevanceScore}`);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Supported Models
|
|
61
|
+
|
|
62
|
+
| Model | Description |
|
|
63
|
+
|-------|-------------|
|
|
64
|
+
| `rerank-v3.5` | Latest English reranker with best accuracy (default) |
|
|
65
|
+
| `rerank-multilingual-v3.0` | Supports 100+ languages |
|
|
66
|
+
|
|
67
|
+
## Class Registration
|
|
68
|
+
|
|
69
|
+
Registered as `CohereLLM` via `@RegisterClass(BaseReranker, 'CohereLLM')`.
|
|
70
|
+
|
|
71
|
+
## Dependencies
|
|
72
|
+
|
|
73
|
+
- `@memberjunction/ai` - Core AI abstractions (BaseReranker)
|
|
74
|
+
- `@memberjunction/global` - Class registration
|
|
75
|
+
- `cohere-ai` - Official Cohere SDK
|
package/dist/index.d.ts
CHANGED
|
@@ -13,10 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Usage:
|
|
15
15
|
* ```typescript
|
|
16
|
-
* import {
|
|
17
|
-
*
|
|
18
|
-
* // Ensure class registration
|
|
19
|
-
* LoadCohereReranker();
|
|
16
|
+
* import { CohereReranker } from '@memberjunction/ai-cohere';
|
|
20
17
|
*
|
|
21
18
|
* // Create instance via ClassFactory
|
|
22
19
|
* const reranker = ClassFactory.CreateInstance<BaseReranker>(
|
|
@@ -36,5 +33,5 @@
|
|
|
36
33
|
* });
|
|
37
34
|
* ```
|
|
38
35
|
*/
|
|
39
|
-
export { CohereReranker, createCohereReranker
|
|
36
|
+
export { CohereReranker, createCohereReranker } from './models/CohereReranker.js';
|
|
40
37
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EACH,cAAc,EACd,oBAAoB,EACvB,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @memberjunction/ai-cohere
|
|
4
3
|
*
|
|
@@ -14,10 +13,7 @@
|
|
|
14
13
|
*
|
|
15
14
|
* Usage:
|
|
16
15
|
* ```typescript
|
|
17
|
-
* import {
|
|
18
|
-
*
|
|
19
|
-
* // Ensure class registration
|
|
20
|
-
* LoadCohereReranker();
|
|
16
|
+
* import { CohereReranker } from '@memberjunction/ai-cohere';
|
|
21
17
|
*
|
|
22
18
|
* // Create instance via ClassFactory
|
|
23
19
|
* const reranker = ClassFactory.CreateInstance<BaseReranker>(
|
|
@@ -37,10 +33,5 @@
|
|
|
37
33
|
* });
|
|
38
34
|
* ```
|
|
39
35
|
*/
|
|
40
|
-
|
|
41
|
-
exports.LoadCohereReranker = exports.createCohereReranker = exports.CohereReranker = void 0;
|
|
42
|
-
var CohereReranker_1 = require("./models/CohereReranker");
|
|
43
|
-
Object.defineProperty(exports, "CohereReranker", { enumerable: true, get: function () { return CohereReranker_1.CohereReranker; } });
|
|
44
|
-
Object.defineProperty(exports, "createCohereReranker", { enumerable: true, get: function () { return CohereReranker_1.createCohereReranker; } });
|
|
45
|
-
Object.defineProperty(exports, "LoadCohereReranker", { enumerable: true, get: function () { return CohereReranker_1.LoadCohereReranker; } });
|
|
36
|
+
export { CohereReranker, createCohereReranker } from './models/CohereReranker.js';
|
|
46
37
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EACH,cAAc,EACd,oBAAoB,EACvB,MAAM,yBAAyB,CAAC"}
|
|
@@ -50,9 +50,4 @@ export declare class CohereReranker extends BaseReranker {
|
|
|
50
50
|
* Convenience function for simple usage.
|
|
51
51
|
*/
|
|
52
52
|
export declare function createCohereReranker(apiKey: string, modelName?: string): CohereReranker;
|
|
53
|
-
/**
|
|
54
|
-
* Ensure the CohereReranker class is loaded for tree-shaking prevention.
|
|
55
|
-
* Import this function in your module's entry point.
|
|
56
|
-
*/
|
|
57
|
-
export declare function LoadCohereReranker(): void;
|
|
58
53
|
//# sourceMappingURL=CohereReranker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CohereReranker.d.ts","sourceRoot":"","sources":["../../src/models/CohereReranker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAG9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBACa,cAAe,SAAQ,YAAY;IAC5C,OAAO,CAAC,OAAO,CAAe;IAE9B;;;;OAIG;gBACS,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;IAM9C;;;;;OAKG;cACa,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;CAwD1E;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,cAAc,CAEvF
|
|
1
|
+
{"version":3,"file":"CohereReranker.d.ts","sourceRoot":"","sources":["../../src/models/CohereReranker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAG9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBACa,cAAe,SAAQ,YAAY;IAC5C,OAAO,CAAC,OAAO,CAAe;IAE9B;;;;OAIG;gBACS,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;IAM9C;;;;;OAKG;cACa,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;CAwD1E;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,cAAc,CAEvF"}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
6
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
11
|
+
import { BaseReranker } from '@memberjunction/ai';
|
|
12
|
+
import { CohereClient } from 'cohere-ai';
|
|
13
13
|
/**
|
|
14
14
|
* Cohere implementation of the BaseReranker class.
|
|
15
15
|
* Uses Cohere's Rerank API for semantic document reranking.
|
|
@@ -40,7 +40,7 @@ const cohere_ai_1 = require("cohere-ai");
|
|
|
40
40
|
* });
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
|
-
let CohereReranker = class CohereReranker extends
|
|
43
|
+
let CohereReranker = class CohereReranker extends BaseReranker {
|
|
44
44
|
/**
|
|
45
45
|
* Create a new CohereReranker instance.
|
|
46
46
|
* @param apiKey - Cohere API key
|
|
@@ -49,7 +49,7 @@ let CohereReranker = class CohereReranker extends ai_1.BaseReranker {
|
|
|
49
49
|
constructor(apiKey, modelName) {
|
|
50
50
|
super(apiKey, modelName || 'rerank-v3.5');
|
|
51
51
|
// Use inherited protected apiKey getter from BaseModel
|
|
52
|
-
this._client = new
|
|
52
|
+
this._client = new CohereClient({ token: this.apiKey });
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* Rerank documents using Cohere's Rerank API.
|
|
@@ -109,25 +109,16 @@ Find memory notes that would help respond appropriately to this message.`;
|
|
|
109
109
|
return results;
|
|
110
110
|
}
|
|
111
111
|
};
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
(
|
|
112
|
+
CohereReranker = __decorate([
|
|
113
|
+
RegisterClass(BaseReranker, 'CohereLLM'),
|
|
114
|
+
__metadata("design:paramtypes", [String, String])
|
|
115
115
|
], CohereReranker);
|
|
116
|
+
export { CohereReranker };
|
|
116
117
|
/**
|
|
117
118
|
* Factory function to create a CohereReranker with default model.
|
|
118
119
|
* Convenience function for simple usage.
|
|
119
120
|
*/
|
|
120
|
-
function createCohereReranker(apiKey, modelName) {
|
|
121
|
+
export function createCohereReranker(apiKey, modelName) {
|
|
121
122
|
return new CohereReranker(apiKey, modelName);
|
|
122
123
|
}
|
|
123
|
-
exports.createCohereReranker = createCohereReranker;
|
|
124
|
-
/**
|
|
125
|
-
* Ensure the CohereReranker class is loaded for tree-shaking prevention.
|
|
126
|
-
* Import this function in your module's entry point.
|
|
127
|
-
*/
|
|
128
|
-
function LoadCohereReranker() {
|
|
129
|
-
// This function exists to ensure the class registration occurs
|
|
130
|
-
// when the module is imported, preventing tree-shaking from removing it
|
|
131
|
-
}
|
|
132
|
-
exports.LoadCohereReranker = LoadCohereReranker;
|
|
133
124
|
//# sourceMappingURL=CohereReranker.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CohereReranker.js","sourceRoot":"","sources":["../../src/models/CohereReranker.ts"],"names":[],"mappings":";;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"CohereReranker.js","sourceRoot":"","sources":["../../src/models/CohereReranker.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,YAAY,EAA8B,MAAM,oBAAoB,CAAC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,EAAE,SAAS,CAAC,CAAC;AACjD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-cohere",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "4.1.0",
|
|
4
5
|
"description": "MemberJunction: Cohere AI Provider - Semantic reranking using Cohere's Rerank API",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
@@ -9,20 +10,20 @@
|
|
|
9
10
|
],
|
|
10
11
|
"scripts": {
|
|
11
12
|
"start": "ts-node-dev src/index.ts",
|
|
12
|
-
"build": "tsc",
|
|
13
|
+
"build": "tsc && tsc-alias -f",
|
|
13
14
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
15
|
},
|
|
15
16
|
"author": "MemberJunction.com",
|
|
16
17
|
"license": "ISC",
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@memberjunction/ai": "
|
|
19
|
-
"@memberjunction/global": "
|
|
20
|
-
"cohere-ai": "^7.
|
|
19
|
+
"@memberjunction/ai": "4.1.0",
|
|
20
|
+
"@memberjunction/global": "4.1.0",
|
|
21
|
+
"cohere-ai": "^7.20.0"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
|
-
"@types/node": "
|
|
24
|
+
"@types/node": "24.10.11",
|
|
24
25
|
"ts-node-dev": "^2.0.0",
|
|
25
|
-
"typescript": "^5.
|
|
26
|
+
"typescript": "^5.9.3"
|
|
26
27
|
},
|
|
27
28
|
"repository": {
|
|
28
29
|
"type": "git",
|