@mastra/rag 1.0.2 → 1.0.3
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +39 -0
- package/dist/_tsup-dts-rollup.d.cts +6 -1
- package/dist/_tsup-dts-rollup.d.ts +6 -1
- package/dist/index.cjs +32 -17
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +32 -18
- package/package.json +4 -5
- package/src/index.ts +1 -0
- package/src/rerank/relevance/cohere/index.ts +43 -13
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/rag@1.0.
|
|
2
|
+
> @mastra/rag@1.0.3-alpha.0 build /home/runner/work/mastra/mastra/packages/rag
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.5.0
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 14683ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/rag/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
14
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
15
15
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/rag/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 12799ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[32mESM[39m [1mdist/index.js [22m[32m245.
|
|
21
|
-
[32mESM[39m ⚡️ Build success in
|
|
22
|
-
[32mCJS[39m [1mdist/index.cjs [22m[
|
|
23
|
-
[32mCJS[39m ⚡️ Build success in
|
|
20
|
+
[32mESM[39m [1mdist/index.js [22m[32m245.98 KB[39m
|
|
21
|
+
[32mESM[39m ⚡️ Build success in 4369ms
|
|
22
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m248.07 KB[39m
|
|
23
|
+
[32mCJS[39m ⚡️ Build success in 4370ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
# @mastra/rag
|
|
2
2
|
|
|
3
|
+
## 1.0.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- edff568: Export types changeset
|
|
8
|
+
- 89ec9d4: remove cohere-ai client dependency and just make a fetch call
|
|
9
|
+
- Updated dependencies [0b56518]
|
|
10
|
+
- Updated dependencies [db5cc15]
|
|
11
|
+
- Updated dependencies [2ba5b76]
|
|
12
|
+
- Updated dependencies [5237998]
|
|
13
|
+
- Updated dependencies [c3a30de]
|
|
14
|
+
- Updated dependencies [37c1acd]
|
|
15
|
+
- Updated dependencies [1aa60b1]
|
|
16
|
+
- Updated dependencies [89ec9d4]
|
|
17
|
+
- Updated dependencies [cf3a184]
|
|
18
|
+
- Updated dependencies [d6bfd60]
|
|
19
|
+
- Updated dependencies [626b0f4]
|
|
20
|
+
- Updated dependencies [c22a91f]
|
|
21
|
+
- Updated dependencies [f7403ab]
|
|
22
|
+
- Updated dependencies [6c89d7f]
|
|
23
|
+
- @mastra/core@0.10.15
|
|
24
|
+
|
|
25
|
+
## 1.0.3-alpha.0
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- edff568: Export types changeset
|
|
30
|
+
- 89ec9d4: remove cohere-ai client dependency and just make a fetch call
|
|
31
|
+
- Updated dependencies [db5cc15]
|
|
32
|
+
- Updated dependencies [5237998]
|
|
33
|
+
- Updated dependencies [37c1acd]
|
|
34
|
+
- Updated dependencies [1aa60b1]
|
|
35
|
+
- Updated dependencies [89ec9d4]
|
|
36
|
+
- Updated dependencies [626b0f4]
|
|
37
|
+
- Updated dependencies [c22a91f]
|
|
38
|
+
- Updated dependencies [f7403ab]
|
|
39
|
+
- Updated dependencies [6c89d7f]
|
|
40
|
+
- @mastra/core@0.10.15-alpha.0
|
|
41
|
+
|
|
3
42
|
## 1.0.2
|
|
4
43
|
|
|
5
44
|
### Patch Changes
|
|
@@ -151,6 +151,7 @@ declare type ChunkOptions = {
|
|
|
151
151
|
};
|
|
152
152
|
export { ChunkOptions }
|
|
153
153
|
export { ChunkOptions as ChunkOptions_alias_1 }
|
|
154
|
+
export { ChunkOptions as ChunkOptions_alias_2 }
|
|
154
155
|
|
|
155
156
|
declare interface ChunkParams extends ChunkOptions {
|
|
156
157
|
strategy?: ChunkStrategy;
|
|
@@ -158,14 +159,16 @@ declare interface ChunkParams extends ChunkOptions {
|
|
|
158
159
|
}
|
|
159
160
|
export { ChunkParams }
|
|
160
161
|
export { ChunkParams as ChunkParams_alias_1 }
|
|
162
|
+
export { ChunkParams as ChunkParams_alias_2 }
|
|
161
163
|
|
|
162
164
|
declare type ChunkStrategy = 'recursive' | 'character' | 'token' | 'markdown' | 'html' | 'json' | 'latex';
|
|
163
165
|
export { ChunkStrategy }
|
|
164
166
|
export { ChunkStrategy as ChunkStrategy_alias_1 }
|
|
167
|
+
export { ChunkStrategy as ChunkStrategy_alias_2 }
|
|
165
168
|
|
|
166
169
|
declare class CohereRelevanceScorer implements RelevanceScoreProvider {
|
|
167
|
-
private client;
|
|
168
170
|
private model;
|
|
171
|
+
private apiKey?;
|
|
169
172
|
constructor(model: string, apiKey?: string);
|
|
170
173
|
getRelevanceScore(query: string, text: string): Promise<number>;
|
|
171
174
|
}
|
|
@@ -327,6 +330,7 @@ declare type ExtractParams = {
|
|
|
327
330
|
};
|
|
328
331
|
export { ExtractParams }
|
|
329
332
|
export { ExtractParams as ExtractParams_alias_1 }
|
|
333
|
+
export { ExtractParams as ExtractParams_alias_2 }
|
|
330
334
|
|
|
331
335
|
declare type ExtractQuestion = {
|
|
332
336
|
/**
|
|
@@ -551,6 +555,7 @@ declare enum Language {
|
|
|
551
555
|
}
|
|
552
556
|
export { Language }
|
|
553
557
|
export { Language as Language_alias_1 }
|
|
558
|
+
export { Language as Language_alias_2 }
|
|
554
559
|
|
|
555
560
|
export declare class LatexTransformer extends RecursiveCharacterTransformer {
|
|
556
561
|
constructor(options?: {
|
|
@@ -151,6 +151,7 @@ declare type ChunkOptions = {
|
|
|
151
151
|
};
|
|
152
152
|
export { ChunkOptions }
|
|
153
153
|
export { ChunkOptions as ChunkOptions_alias_1 }
|
|
154
|
+
export { ChunkOptions as ChunkOptions_alias_2 }
|
|
154
155
|
|
|
155
156
|
declare interface ChunkParams extends ChunkOptions {
|
|
156
157
|
strategy?: ChunkStrategy;
|
|
@@ -158,14 +159,16 @@ declare interface ChunkParams extends ChunkOptions {
|
|
|
158
159
|
}
|
|
159
160
|
export { ChunkParams }
|
|
160
161
|
export { ChunkParams as ChunkParams_alias_1 }
|
|
162
|
+
export { ChunkParams as ChunkParams_alias_2 }
|
|
161
163
|
|
|
162
164
|
declare type ChunkStrategy = 'recursive' | 'character' | 'token' | 'markdown' | 'html' | 'json' | 'latex';
|
|
163
165
|
export { ChunkStrategy }
|
|
164
166
|
export { ChunkStrategy as ChunkStrategy_alias_1 }
|
|
167
|
+
export { ChunkStrategy as ChunkStrategy_alias_2 }
|
|
165
168
|
|
|
166
169
|
declare class CohereRelevanceScorer implements RelevanceScoreProvider {
|
|
167
|
-
private client;
|
|
168
170
|
private model;
|
|
171
|
+
private apiKey?;
|
|
169
172
|
constructor(model: string, apiKey?: string);
|
|
170
173
|
getRelevanceScore(query: string, text: string): Promise<number>;
|
|
171
174
|
}
|
|
@@ -327,6 +330,7 @@ declare type ExtractParams = {
|
|
|
327
330
|
};
|
|
328
331
|
export { ExtractParams }
|
|
329
332
|
export { ExtractParams as ExtractParams_alias_1 }
|
|
333
|
+
export { ExtractParams as ExtractParams_alias_2 }
|
|
330
334
|
|
|
331
335
|
declare type ExtractQuestion = {
|
|
332
336
|
/**
|
|
@@ -551,6 +555,7 @@ declare enum Language {
|
|
|
551
555
|
}
|
|
552
556
|
export { Language }
|
|
553
557
|
export { Language as Language_alias_1 }
|
|
558
|
+
export { Language as Language_alias_2 }
|
|
554
559
|
|
|
555
560
|
export declare class LatexTransformer extends RecursiveCharacterTransformer {
|
|
556
561
|
constructor(options?: {
|
package/dist/index.cjs
CHANGED
|
@@ -5,7 +5,6 @@ var zod = require('zod');
|
|
|
5
5
|
var nodeHtmlBetterParser = require('node-html-better-parser');
|
|
6
6
|
var jsTiktoken = require('js-tiktoken');
|
|
7
7
|
var big_js = require('big.js');
|
|
8
|
-
var cohereAi = require('cohere-ai');
|
|
9
8
|
var agent = require('@mastra/core/agent');
|
|
10
9
|
var relevance = require('@mastra/core/relevance');
|
|
11
10
|
var ZeroEntropy = require('zeroentropy');
|
|
@@ -1025,7 +1024,7 @@ var postJsonToApi = async ({
|
|
|
1025
1024
|
failedResponseHandler,
|
|
1026
1025
|
successfulResponseHandler,
|
|
1027
1026
|
abortSignal,
|
|
1028
|
-
fetch
|
|
1027
|
+
fetch: fetch2
|
|
1029
1028
|
}) => postToApi({
|
|
1030
1029
|
url,
|
|
1031
1030
|
headers: {
|
|
@@ -1039,7 +1038,7 @@ var postJsonToApi = async ({
|
|
|
1039
1038
|
failedResponseHandler,
|
|
1040
1039
|
successfulResponseHandler,
|
|
1041
1040
|
abortSignal,
|
|
1042
|
-
fetch
|
|
1041
|
+
fetch: fetch2
|
|
1043
1042
|
});
|
|
1044
1043
|
var postFormDataToApi = async ({
|
|
1045
1044
|
url,
|
|
@@ -1048,7 +1047,7 @@ var postFormDataToApi = async ({
|
|
|
1048
1047
|
failedResponseHandler,
|
|
1049
1048
|
successfulResponseHandler,
|
|
1050
1049
|
abortSignal,
|
|
1051
|
-
fetch
|
|
1050
|
+
fetch: fetch2
|
|
1052
1051
|
}) => postToApi({
|
|
1053
1052
|
url,
|
|
1054
1053
|
headers,
|
|
@@ -1059,7 +1058,7 @@ var postFormDataToApi = async ({
|
|
|
1059
1058
|
failedResponseHandler,
|
|
1060
1059
|
successfulResponseHandler,
|
|
1061
1060
|
abortSignal,
|
|
1062
|
-
fetch
|
|
1061
|
+
fetch: fetch2
|
|
1063
1062
|
});
|
|
1064
1063
|
var postToApi = async ({
|
|
1065
1064
|
url,
|
|
@@ -1068,10 +1067,10 @@ var postToApi = async ({
|
|
|
1068
1067
|
successfulResponseHandler,
|
|
1069
1068
|
failedResponseHandler,
|
|
1070
1069
|
abortSignal,
|
|
1071
|
-
fetch = getOriginalFetch2()
|
|
1070
|
+
fetch: fetch2 = getOriginalFetch2()
|
|
1072
1071
|
}) => {
|
|
1073
1072
|
try {
|
|
1074
|
-
const response = await
|
|
1073
|
+
const response = await fetch2(url, {
|
|
1075
1074
|
method: "POST",
|
|
1076
1075
|
headers: removeUndefinedEntries(headers),
|
|
1077
1076
|
body: body.content,
|
|
@@ -5976,23 +5975,38 @@ var MDocument = class _MDocument {
|
|
|
5976
5975
|
return this.chunks.map((doc) => doc.metadata);
|
|
5977
5976
|
}
|
|
5978
5977
|
};
|
|
5978
|
+
|
|
5979
|
+
// src/rerank/relevance/cohere/index.ts
|
|
5979
5980
|
var CohereRelevanceScorer = class {
|
|
5980
|
-
client;
|
|
5981
5981
|
model;
|
|
5982
|
+
apiKey;
|
|
5982
5983
|
constructor(model, apiKey) {
|
|
5983
|
-
this.
|
|
5984
|
-
token: apiKey || process.env.COHERE_API_KEY || ""
|
|
5985
|
-
});
|
|
5984
|
+
this.apiKey = apiKey;
|
|
5986
5985
|
this.model = model;
|
|
5987
5986
|
}
|
|
5988
5987
|
async getRelevanceScore(query, text) {
|
|
5989
|
-
const response = await
|
|
5990
|
-
|
|
5991
|
-
|
|
5992
|
-
|
|
5993
|
-
|
|
5988
|
+
const response = await fetch(`https://api.cohere.com/v2/rerank`, {
|
|
5989
|
+
method: "POST",
|
|
5990
|
+
headers: {
|
|
5991
|
+
"Content-Type": "application/json",
|
|
5992
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
5993
|
+
},
|
|
5994
|
+
body: JSON.stringify({
|
|
5995
|
+
query,
|
|
5996
|
+
documents: [text],
|
|
5997
|
+
model: this.model,
|
|
5998
|
+
top_n: 1
|
|
5999
|
+
})
|
|
5994
6000
|
});
|
|
5995
|
-
|
|
6001
|
+
if (!response.ok) {
|
|
6002
|
+
throw new Error(`Cohere API error: ${response.status} ${await response.text()}`);
|
|
6003
|
+
}
|
|
6004
|
+
const data = await response.json();
|
|
6005
|
+
const relevanceScore = data.results[0]?.relevance_score;
|
|
6006
|
+
if (!relevanceScore) {
|
|
6007
|
+
throw new Error("No relevance score found on Cohere response");
|
|
6008
|
+
}
|
|
6009
|
+
return relevanceScore;
|
|
5996
6010
|
}
|
|
5997
6011
|
};
|
|
5998
6012
|
var MastraAgentRelevanceScorer = class {
|
|
@@ -7590,6 +7604,7 @@ exports.CHROMA_PROMPT = CHROMA_PROMPT;
|
|
|
7590
7604
|
exports.CohereRelevanceScorer = CohereRelevanceScorer;
|
|
7591
7605
|
exports.GraphRAG = GraphRAG;
|
|
7592
7606
|
exports.LIBSQL_PROMPT = LIBSQL_PROMPT;
|
|
7607
|
+
exports.Language = Language;
|
|
7593
7608
|
exports.MDocument = MDocument;
|
|
7594
7609
|
exports.MONGODB_PROMPT = MONGODB_PROMPT;
|
|
7595
7610
|
exports.MastraAgentRelevanceScorer = MastraAgentRelevanceScorer;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export { GraphRAG } from './_tsup-dts-rollup.cjs';
|
|
2
2
|
export { MDocument } from './_tsup-dts-rollup.cjs';
|
|
3
|
+
export { Language } from './_tsup-dts-rollup.cjs';
|
|
4
|
+
export { ExtractParams } from './_tsup-dts-rollup.cjs';
|
|
5
|
+
export { ChunkOptions } from './_tsup-dts-rollup.cjs';
|
|
6
|
+
export { ChunkStrategy } from './_tsup-dts-rollup.cjs';
|
|
7
|
+
export { ChunkParams } from './_tsup-dts-rollup.cjs';
|
|
3
8
|
export { rerankWithScorer } from './_tsup-dts-rollup.cjs';
|
|
4
9
|
export { rerank } from './_tsup-dts-rollup.cjs';
|
|
5
10
|
export { RerankResult } from './_tsup-dts-rollup.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export { GraphRAG } from './_tsup-dts-rollup.js';
|
|
2
2
|
export { MDocument } from './_tsup-dts-rollup.js';
|
|
3
|
+
export { Language } from './_tsup-dts-rollup.js';
|
|
4
|
+
export { ExtractParams } from './_tsup-dts-rollup.js';
|
|
5
|
+
export { ChunkOptions } from './_tsup-dts-rollup.js';
|
|
6
|
+
export { ChunkStrategy } from './_tsup-dts-rollup.js';
|
|
7
|
+
export { ChunkParams } from './_tsup-dts-rollup.js';
|
|
3
8
|
export { rerankWithScorer } from './_tsup-dts-rollup.js';
|
|
4
9
|
export { rerank } from './_tsup-dts-rollup.js';
|
|
5
10
|
export { RerankResult } from './_tsup-dts-rollup.js';
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,6 @@ import { z } from 'zod';
|
|
|
3
3
|
import { parse } from 'node-html-better-parser';
|
|
4
4
|
import { encodingForModel, getEncoding } from 'js-tiktoken';
|
|
5
5
|
import { Big } from 'big.js';
|
|
6
|
-
import { CohereClient } from 'cohere-ai';
|
|
7
6
|
import { Agent } from '@mastra/core/agent';
|
|
8
7
|
import { createSimilarityPrompt } from '@mastra/core/relevance';
|
|
9
8
|
import ZeroEntropy from 'zeroentropy';
|
|
@@ -1019,7 +1018,7 @@ var postJsonToApi = async ({
|
|
|
1019
1018
|
failedResponseHandler,
|
|
1020
1019
|
successfulResponseHandler,
|
|
1021
1020
|
abortSignal,
|
|
1022
|
-
fetch
|
|
1021
|
+
fetch: fetch2
|
|
1023
1022
|
}) => postToApi({
|
|
1024
1023
|
url,
|
|
1025
1024
|
headers: {
|
|
@@ -1033,7 +1032,7 @@ var postJsonToApi = async ({
|
|
|
1033
1032
|
failedResponseHandler,
|
|
1034
1033
|
successfulResponseHandler,
|
|
1035
1034
|
abortSignal,
|
|
1036
|
-
fetch
|
|
1035
|
+
fetch: fetch2
|
|
1037
1036
|
});
|
|
1038
1037
|
var postFormDataToApi = async ({
|
|
1039
1038
|
url,
|
|
@@ -1042,7 +1041,7 @@ var postFormDataToApi = async ({
|
|
|
1042
1041
|
failedResponseHandler,
|
|
1043
1042
|
successfulResponseHandler,
|
|
1044
1043
|
abortSignal,
|
|
1045
|
-
fetch
|
|
1044
|
+
fetch: fetch2
|
|
1046
1045
|
}) => postToApi({
|
|
1047
1046
|
url,
|
|
1048
1047
|
headers,
|
|
@@ -1053,7 +1052,7 @@ var postFormDataToApi = async ({
|
|
|
1053
1052
|
failedResponseHandler,
|
|
1054
1053
|
successfulResponseHandler,
|
|
1055
1054
|
abortSignal,
|
|
1056
|
-
fetch
|
|
1055
|
+
fetch: fetch2
|
|
1057
1056
|
});
|
|
1058
1057
|
var postToApi = async ({
|
|
1059
1058
|
url,
|
|
@@ -1062,10 +1061,10 @@ var postToApi = async ({
|
|
|
1062
1061
|
successfulResponseHandler,
|
|
1063
1062
|
failedResponseHandler,
|
|
1064
1063
|
abortSignal,
|
|
1065
|
-
fetch = getOriginalFetch2()
|
|
1064
|
+
fetch: fetch2 = getOriginalFetch2()
|
|
1066
1065
|
}) => {
|
|
1067
1066
|
try {
|
|
1068
|
-
const response = await
|
|
1067
|
+
const response = await fetch2(url, {
|
|
1069
1068
|
method: "POST",
|
|
1070
1069
|
headers: removeUndefinedEntries(headers),
|
|
1071
1070
|
body: body.content,
|
|
@@ -5970,23 +5969,38 @@ var MDocument = class _MDocument {
|
|
|
5970
5969
|
return this.chunks.map((doc) => doc.metadata);
|
|
5971
5970
|
}
|
|
5972
5971
|
};
|
|
5972
|
+
|
|
5973
|
+
// src/rerank/relevance/cohere/index.ts
|
|
5973
5974
|
var CohereRelevanceScorer = class {
|
|
5974
|
-
client;
|
|
5975
5975
|
model;
|
|
5976
|
+
apiKey;
|
|
5976
5977
|
constructor(model, apiKey) {
|
|
5977
|
-
this.
|
|
5978
|
-
token: apiKey || process.env.COHERE_API_KEY || ""
|
|
5979
|
-
});
|
|
5978
|
+
this.apiKey = apiKey;
|
|
5980
5979
|
this.model = model;
|
|
5981
5980
|
}
|
|
5982
5981
|
async getRelevanceScore(query, text) {
|
|
5983
|
-
const response = await
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5982
|
+
const response = await fetch(`https://api.cohere.com/v2/rerank`, {
|
|
5983
|
+
method: "POST",
|
|
5984
|
+
headers: {
|
|
5985
|
+
"Content-Type": "application/json",
|
|
5986
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
5987
|
+
},
|
|
5988
|
+
body: JSON.stringify({
|
|
5989
|
+
query,
|
|
5990
|
+
documents: [text],
|
|
5991
|
+
model: this.model,
|
|
5992
|
+
top_n: 1
|
|
5993
|
+
})
|
|
5988
5994
|
});
|
|
5989
|
-
|
|
5995
|
+
if (!response.ok) {
|
|
5996
|
+
throw new Error(`Cohere API error: ${response.status} ${await response.text()}`);
|
|
5997
|
+
}
|
|
5998
|
+
const data = await response.json();
|
|
5999
|
+
const relevanceScore = data.results[0]?.relevance_score;
|
|
6000
|
+
if (!relevanceScore) {
|
|
6001
|
+
throw new Error("No relevance score found on Cohere response");
|
|
6002
|
+
}
|
|
6003
|
+
return relevanceScore;
|
|
5990
6004
|
}
|
|
5991
6005
|
};
|
|
5992
6006
|
var MastraAgentRelevanceScorer = class {
|
|
@@ -7579,4 +7593,4 @@ Example Complex Query:
|
|
|
7579
7593
|
}
|
|
7580
7594
|
`;
|
|
7581
7595
|
|
|
7582
|
-
export { ASTRA_PROMPT, CHROMA_PROMPT, CohereRelevanceScorer, GraphRAG, LIBSQL_PROMPT, MDocument, MONGODB_PROMPT, MastraAgentRelevanceScorer, PGVECTOR_PROMPT, PINECONE_PROMPT, QDRANT_PROMPT, UPSTASH_PROMPT, VECTORIZE_PROMPT, ZeroEntropyRelevanceScorer, createDocumentChunkerTool, createGraphRAGTool, createVectorQueryTool, defaultGraphRagDescription, defaultVectorQueryDescription, filterDescription, queryTextDescription, rerank, rerankWithScorer, topKDescription };
|
|
7596
|
+
export { ASTRA_PROMPT, CHROMA_PROMPT, CohereRelevanceScorer, GraphRAG, LIBSQL_PROMPT, Language, MDocument, MONGODB_PROMPT, MastraAgentRelevanceScorer, PGVECTOR_PROMPT, PINECONE_PROMPT, QDRANT_PROMPT, UPSTASH_PROMPT, VECTORIZE_PROMPT, ZeroEntropyRelevanceScorer, createDocumentChunkerTool, createGraphRAGTool, createVectorQueryTool, defaultGraphRagDescription, defaultVectorQueryDescription, filterDescription, queryTextDescription, rerank, rerankWithScorer, topKDescription };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/rag",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@paralleldrive/cuid2": "^2.2.2",
|
|
25
25
|
"big.js": "^7.0.1",
|
|
26
|
-
"cohere-ai": "^7.17.1",
|
|
27
26
|
"js-tiktoken": "^1.0.20",
|
|
28
27
|
"node-html-better-parser": "^1.4.11",
|
|
29
28
|
"pathe": "^2.0.3",
|
|
@@ -42,12 +41,12 @@
|
|
|
42
41
|
"@types/node": "^20.19.0",
|
|
43
42
|
"ai": "^4.3.16",
|
|
44
43
|
"dotenv": "^17.0.0",
|
|
45
|
-
"eslint": "^9.
|
|
44
|
+
"eslint": "^9.30.1",
|
|
46
45
|
"tsup": "^8.5.0",
|
|
47
46
|
"typescript": "^5.8.3",
|
|
48
47
|
"vitest": "^3.2.4",
|
|
49
|
-
"@internal/lint": "0.0.
|
|
50
|
-
"@mastra/core": "0.10.
|
|
48
|
+
"@internal/lint": "0.0.20",
|
|
49
|
+
"@mastra/core": "0.10.15"
|
|
51
50
|
},
|
|
52
51
|
"keywords": [
|
|
53
52
|
"rag",
|
package/src/index.ts
CHANGED
|
@@ -1,26 +1,56 @@
|
|
|
1
1
|
import type { RelevanceScoreProvider } from '@mastra/core/relevance';
|
|
2
|
-
import { CohereClient } from 'cohere-ai';
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
interface CohereRerankingResponse {
|
|
4
|
+
results: Array<{
|
|
5
|
+
index: number;
|
|
6
|
+
relevance_score: number;
|
|
7
|
+
}>;
|
|
8
|
+
id: string;
|
|
9
|
+
meta: {
|
|
10
|
+
api_version: {
|
|
11
|
+
version: string;
|
|
12
|
+
is_experimental: boolean;
|
|
13
|
+
};
|
|
14
|
+
billed_units: {
|
|
15
|
+
search_units: number;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
5
20
|
export class CohereRelevanceScorer implements RelevanceScoreProvider {
|
|
6
|
-
private client: any;
|
|
7
21
|
private model: string;
|
|
22
|
+
private apiKey?: string;
|
|
8
23
|
constructor(model: string, apiKey?: string) {
|
|
9
|
-
this.
|
|
10
|
-
token: apiKey || process.env.COHERE_API_KEY || '',
|
|
11
|
-
});
|
|
12
|
-
|
|
24
|
+
this.apiKey = apiKey;
|
|
13
25
|
this.model = model;
|
|
14
26
|
}
|
|
15
27
|
|
|
16
28
|
async getRelevanceScore(query: string, text: string): Promise<number> {
|
|
17
|
-
const response = await
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
29
|
+
const response = await fetch(`https://api.cohere.com/v2/rerank`, {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: {
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
34
|
+
},
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
query,
|
|
37
|
+
documents: [text],
|
|
38
|
+
model: this.model,
|
|
39
|
+
top_n: 1,
|
|
40
|
+
}),
|
|
22
41
|
});
|
|
23
42
|
|
|
24
|
-
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
throw new Error(`Cohere API error: ${response.status} ${await response.text()}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const data = (await response.json()) as CohereRerankingResponse;
|
|
48
|
+
const relevanceScore = data.results[0]?.relevance_score;
|
|
49
|
+
|
|
50
|
+
if (!relevanceScore) {
|
|
51
|
+
throw new Error('No relevance score found on Cohere response');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return relevanceScore;
|
|
25
55
|
}
|
|
26
56
|
}
|