@memberjunction/ai-cohere 0.0.1 → 3.3.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.
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @memberjunction/ai-cohere
3
+ *
4
+ * Cohere AI provider for MemberJunction.
5
+ * Currently provides reranking capabilities using Cohere's Rerank API.
6
+ *
7
+ * Supported models:
8
+ * - rerank-v3.5: Latest English reranker with best accuracy
9
+ * - rerank-multilingual-v3.0: Supports 100+ languages
10
+ *
11
+ * API Key:
12
+ * Set via environment variable: AI_VENDOR_API_KEY__COHERELLM
13
+ *
14
+ * Usage:
15
+ * ```typescript
16
+ * import { LoadCohereReranker } from '@memberjunction/ai-cohere';
17
+ *
18
+ * // Ensure class registration
19
+ * LoadCohereReranker();
20
+ *
21
+ * // Create instance via ClassFactory
22
+ * const reranker = ClassFactory.CreateInstance<BaseReranker>(
23
+ * BaseReranker,
24
+ * 'CohereLLM',
25
+ * apiKey,
26
+ * 'rerank-v3.5'
27
+ * );
28
+ *
29
+ * const response = await reranker.Rerank({
30
+ * query: 'What is the capital of France?',
31
+ * documents: [
32
+ * { id: '1', text: 'Paris is the capital of France.' },
33
+ * { id: '2', text: 'London is in England.' }
34
+ * ],
35
+ * topK: 5
36
+ * });
37
+ * ```
38
+ */
39
+ export { CohereReranker, createCohereReranker, LoadCohereReranker } from './models/CohereReranker';
40
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,EACH,cAAc,EACd,oBAAoB,EACpB,kBAAkB,EACrB,MAAM,yBAAyB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /**
3
+ * @memberjunction/ai-cohere
4
+ *
5
+ * Cohere AI provider for MemberJunction.
6
+ * Currently provides reranking capabilities using Cohere's Rerank API.
7
+ *
8
+ * Supported models:
9
+ * - rerank-v3.5: Latest English reranker with best accuracy
10
+ * - rerank-multilingual-v3.0: Supports 100+ languages
11
+ *
12
+ * API Key:
13
+ * Set via environment variable: AI_VENDOR_API_KEY__COHERELLM
14
+ *
15
+ * Usage:
16
+ * ```typescript
17
+ * import { LoadCohereReranker } from '@memberjunction/ai-cohere';
18
+ *
19
+ * // Ensure class registration
20
+ * LoadCohereReranker();
21
+ *
22
+ * // Create instance via ClassFactory
23
+ * const reranker = ClassFactory.CreateInstance<BaseReranker>(
24
+ * BaseReranker,
25
+ * 'CohereLLM',
26
+ * apiKey,
27
+ * 'rerank-v3.5'
28
+ * );
29
+ *
30
+ * const response = await reranker.Rerank({
31
+ * query: 'What is the capital of France?',
32
+ * documents: [
33
+ * { id: '1', text: 'Paris is the capital of France.' },
34
+ * { id: '2', text: 'London is in England.' }
35
+ * ],
36
+ * topK: 5
37
+ * });
38
+ * ```
39
+ */
40
+ Object.defineProperty(exports, "__esModule", { value: true });
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; } });
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;;;AAEH,0DAIiC;AAH7B,gHAAA,cAAc,OAAA;AACd,sHAAA,oBAAoB,OAAA;AACpB,oHAAA,kBAAkB,OAAA"}
@@ -0,0 +1,58 @@
1
+ import { BaseReranker, RerankParams, RerankResult } from '@memberjunction/ai';
2
+ /**
3
+ * Cohere implementation of the BaseReranker class.
4
+ * Uses Cohere's Rerank API for semantic document reranking.
5
+ *
6
+ * Supported models:
7
+ * - rerank-v3.5: Latest English reranker with best accuracy
8
+ * - rerank-multilingual-v3.0: Supports 100+ languages
9
+ *
10
+ * API Key:
11
+ * Set via environment variable: AI_VENDOR_API_KEY__COHERELLM
12
+ *
13
+ * Usage:
14
+ * ```typescript
15
+ * const reranker = ClassFactory.CreateInstance<BaseReranker>(
16
+ * BaseReranker,
17
+ * 'CohereReranker',
18
+ * apiKey,
19
+ * 'rerank-v3.5'
20
+ * );
21
+ *
22
+ * const response = await reranker.Rerank({
23
+ * query: 'What is the capital of France?',
24
+ * documents: [
25
+ * { id: '1', text: 'Paris is the capital of France.' },
26
+ * { id: '2', text: 'London is in England.' }
27
+ * ],
28
+ * topK: 5
29
+ * });
30
+ * ```
31
+ */
32
+ export declare class CohereReranker extends BaseReranker {
33
+ private _client;
34
+ /**
35
+ * Create a new CohereReranker instance.
36
+ * @param apiKey - Cohere API key
37
+ * @param modelName - Model to use (default: 'rerank-v3.5')
38
+ */
39
+ constructor(apiKey: string, modelName?: string);
40
+ /**
41
+ * Rerank documents using Cohere's Rerank API.
42
+ *
43
+ * The Cohere API returns results sorted by relevance with scores from 0-1.
44
+ * We map the response to our RerankResult format, preserving document metadata.
45
+ */
46
+ protected doRerank(params: RerankParams): Promise<RerankResult[]>;
47
+ }
48
+ /**
49
+ * Factory function to create a CohereReranker with default model.
50
+ * Convenience function for simple usage.
51
+ */
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
+ //# sourceMappingURL=CohereReranker.d.ts.map
@@ -0,0 +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;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAGzC"}
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.LoadCohereReranker = exports.createCohereReranker = exports.CohereReranker = void 0;
10
+ const global_1 = require("@memberjunction/global");
11
+ const ai_1 = require("@memberjunction/ai");
12
+ const cohere_ai_1 = require("cohere-ai");
13
+ /**
14
+ * Cohere implementation of the BaseReranker class.
15
+ * Uses Cohere's Rerank API for semantic document reranking.
16
+ *
17
+ * Supported models:
18
+ * - rerank-v3.5: Latest English reranker with best accuracy
19
+ * - rerank-multilingual-v3.0: Supports 100+ languages
20
+ *
21
+ * API Key:
22
+ * Set via environment variable: AI_VENDOR_API_KEY__COHERELLM
23
+ *
24
+ * Usage:
25
+ * ```typescript
26
+ * const reranker = ClassFactory.CreateInstance<BaseReranker>(
27
+ * BaseReranker,
28
+ * 'CohereReranker',
29
+ * apiKey,
30
+ * 'rerank-v3.5'
31
+ * );
32
+ *
33
+ * const response = await reranker.Rerank({
34
+ * query: 'What is the capital of France?',
35
+ * documents: [
36
+ * { id: '1', text: 'Paris is the capital of France.' },
37
+ * { id: '2', text: 'London is in England.' }
38
+ * ],
39
+ * topK: 5
40
+ * });
41
+ * ```
42
+ */
43
+ let CohereReranker = class CohereReranker extends ai_1.BaseReranker {
44
+ /**
45
+ * Create a new CohereReranker instance.
46
+ * @param apiKey - Cohere API key
47
+ * @param modelName - Model to use (default: 'rerank-v3.5')
48
+ */
49
+ constructor(apiKey, modelName) {
50
+ super(apiKey, modelName || 'rerank-v3.5');
51
+ // Use inherited protected apiKey getter from BaseModel
52
+ this._client = new cohere_ai_1.CohereClient({ token: this.apiKey });
53
+ }
54
+ /**
55
+ * Rerank documents using Cohere's Rerank API.
56
+ *
57
+ * The Cohere API returns results sorted by relevance with scores from 0-1.
58
+ * We map the response to our RerankResult format, preserving document metadata.
59
+ */
60
+ async doRerank(params) {
61
+ // Build enhanced query with context about what makes memory notes relevant
62
+ const enhancedQuery = `You are evaluating agent memory notes for relevance to a user message.
63
+
64
+ Key principles for scoring relevance:
65
+ - User identity notes (name, preferences) are almost ALWAYS relevant, especially for greetings
66
+ - Memory notes provide context that COULD be useful, not just direct matches
67
+ - Preferences should be applied whenever the topic might come up
68
+ - Context notes help maintain continuity across conversations
69
+
70
+ The user's current message is: "${params.query}"
71
+
72
+ Find memory notes that would help respond appropriately to this message.`;
73
+ // DEBUG: Log request with ALL documents
74
+ console.log('CohereReranker REQUEST:', JSON.stringify({
75
+ model: this._modelName,
76
+ originalQuery: params.query,
77
+ enhancedQuery: enhancedQuery.substring(0, 200) + '...',
78
+ documentCount: params.documents.length,
79
+ topK: params.topK,
80
+ allDocs: params.documents.map((d, i) => `[${i}] ${d.text.substring(0, 80)}...`)
81
+ }, null, 2));
82
+ // Call Cohere's rerank endpoint with enhanced query
83
+ const response = await this._client.rerank({
84
+ model: this._modelName,
85
+ query: enhancedQuery,
86
+ documents: params.documents.map(d => d.text),
87
+ topN: params.topK || params.documents.length,
88
+ returnDocuments: false // We track documents ourselves to preserve metadata
89
+ });
90
+ // DEBUG: Log response
91
+ console.log('CohereReranker RESPONSE:', JSON.stringify({
92
+ resultCount: response.results.length,
93
+ results: response.results.slice(0, 5).map(r => ({
94
+ index: r.index,
95
+ relevanceScore: r.relevanceScore
96
+ }))
97
+ }, null, 2));
98
+ // Map Cohere results to our format
99
+ // Cohere returns results already sorted by relevance
100
+ const results = response.results.map((result, idx) => {
101
+ const originalDoc = params.documents[result.index];
102
+ return {
103
+ id: originalDoc.id,
104
+ relevanceScore: result.relevanceScore,
105
+ document: originalDoc,
106
+ rank: idx
107
+ };
108
+ });
109
+ return results;
110
+ }
111
+ };
112
+ exports.CohereReranker = CohereReranker;
113
+ exports.CohereReranker = CohereReranker = __decorate([
114
+ (0, global_1.RegisterClass)(ai_1.BaseReranker, 'CohereLLM')
115
+ ], CohereReranker);
116
+ /**
117
+ * Factory function to create a CohereReranker with default model.
118
+ * Convenience function for simple usage.
119
+ */
120
+ function createCohereReranker(apiKey, modelName) {
121
+ return new CohereReranker(apiKey, modelName);
122
+ }
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
+ //# sourceMappingURL=CohereReranker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CohereReranker.js","sourceR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AAoB,CAAC,MAAc,EAAE,SAAkB;IACnE,OAAO,IAAI,cAAc,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AACjD,CAAC;AAFD,oDAEC;AAED;;;GAGG;AACH,SAAgB,kBAAkB;IAC9B,+DAA+D;IAC/D,wEAAwE;AAC5E,CAAC;AAHD,gDAGC"}
package/package.json CHANGED
@@ -1,10 +1,31 @@
1
1
  {
2
2
  "name": "@memberjunction/ai-cohere",
3
- "version": "0.0.1",
4
- "description": "OIDC trusted publishing setup package for @memberjunction/ai-cohere",
5
- "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
9
- ]
3
+ "version": "3.3.0",
4
+ "description": "MemberJunction: Cohere AI Provider - Semantic reranking using Cohere's Rerank API",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "/dist"
9
+ ],
10
+ "scripts": {
11
+ "start": "ts-node-dev src/index.ts",
12
+ "build": "tsc",
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "author": "MemberJunction.com",
16
+ "license": "ISC",
17
+ "dependencies": {
18
+ "@memberjunction/ai": "3.3.0",
19
+ "@memberjunction/global": "3.3.0",
20
+ "cohere-ai": "^7.14.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "20.14.2",
24
+ "ts-node-dev": "^2.0.0",
25
+ "typescript": "^5.4.5"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/MemberJunction/MJ"
30
+ }
10
31
  }
package/README.md DELETED
@@ -1,45 +0,0 @@
1
- # @memberjunction/ai-cohere
2
-
3
- ## ⚠️ IMPORTANT NOTICE ⚠️
4
-
5
- **This package is created solely for the purpose of setting up OIDC (OpenID Connect) trusted publishing with npm.**
6
-
7
- This is **NOT** a functional package and contains **NO** code or functionality beyond the OIDC setup configuration.
8
-
9
- ## Purpose
10
-
11
- This package exists to:
12
- 1. Configure OIDC trusted publishing for the package name `@memberjunction/ai-cohere`
13
- 2. Enable secure, token-less publishing from CI/CD workflows
14
- 3. Establish provenance for packages published under this name
15
-
16
- ## What is OIDC Trusted Publishing?
17
-
18
- OIDC trusted publishing allows package maintainers to publish packages directly from their CI/CD workflows without needing to manage npm access tokens. Instead, it uses OpenID Connect to establish trust between the CI/CD provider (like GitHub Actions) and npm.
19
-
20
- ## Setup Instructions
21
-
22
- To properly configure OIDC trusted publishing for this package:
23
-
24
- 1. Go to [npmjs.com](https://www.npmjs.com/) and navigate to your package settings
25
- 2. Configure the trusted publisher (e.g., GitHub Actions)
26
- 3. Specify the repository and workflow that should be allowed to publish
27
- 4. Use the configured workflow to publish your actual package
28
-
29
- ## DO NOT USE THIS PACKAGE
30
-
31
- This package is a placeholder for OIDC configuration only. It:
32
- - Contains no executable code
33
- - Provides no functionality
34
- - Should not be installed as a dependency
35
- - Exists only for administrative purposes
36
-
37
- ## More Information
38
-
39
- For more details about npm's trusted publishing feature, see:
40
- - [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
41
- - [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
42
-
43
- ---
44
-
45
- **Maintained for OIDC setup purposes only**