@mastra/rag 1.0.2 → 1.0.3-alpha.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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/rag@1.0.2-alpha.0 build /home/runner/work/mastra/mastra/packages/rag
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
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.5.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 17466ms
9
+ TSC ⚡️ Build success in 15492ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.8.3
13
13
  Writing package typings: /home/runner/work/mastra/mastra/packages/rag/dist/_tsup-dts-rollup.d.ts
14
14
  Analysis will use the bundled TypeScript version 5.8.3
15
15
  Writing package typings: /home/runner/work/mastra/mastra/packages/rag/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 13205ms
16
+ DTS ⚡️ Build success in 14880ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- ESM dist/index.js 245.51 KB
21
- ESM ⚡️ Build success in 4603ms
22
- CJS dist/index.cjs 247.58 KB
23
- CJS ⚡️ Build success in 4609ms
20
+ CJS dist/index.cjs 248.07 KB
21
+ CJS ⚡️ Build success in 4118ms
22
+ ESM dist/index.js 245.98 KB
23
+ ESM ⚡️ Build success in 4119ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @mastra/rag
2
2
 
3
+ ## 1.0.3-alpha.0
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 [db5cc15]
10
+ - Updated dependencies [5237998]
11
+ - Updated dependencies [37c1acd]
12
+ - Updated dependencies [1aa60b1]
13
+ - Updated dependencies [89ec9d4]
14
+ - Updated dependencies [626b0f4]
15
+ - Updated dependencies [c22a91f]
16
+ - Updated dependencies [f7403ab]
17
+ - Updated dependencies [6c89d7f]
18
+ - @mastra/core@0.10.15-alpha.0
19
+
3
20
  ## 1.0.2
4
21
 
5
22
  ### 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 fetch(url, {
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.client = new cohereAi.CohereClient({
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 this.client.rerank({
5990
- query,
5991
- documents: [text],
5992
- model: this.model,
5993
- topN: 1
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
- return response.results[0].relevanceScore;
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 fetch(url, {
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.client = new CohereClient({
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 this.client.rerank({
5984
- query,
5985
- documents: [text],
5986
- model: this.model,
5987
- topN: 1
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
- return response.results[0].relevanceScore;
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.2",
3
+ "version": "1.0.3-alpha.0",
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.29.0",
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.18",
50
- "@mastra/core": "0.10.11"
48
+ "@internal/lint": "0.0.19",
49
+ "@mastra/core": "0.10.15-alpha.0"
51
50
  },
52
51
  "keywords": [
53
52
  "rag",
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './document/document';
2
+ export * from './document/types';
2
3
  export * from './rerank';
3
4
  export * from './rerank/relevance';
4
5
  export { GraphRAG } from './graph-rag';
@@ -1,26 +1,56 @@
1
1
  import type { RelevanceScoreProvider } from '@mastra/core/relevance';
2
- import { CohereClient } from 'cohere-ai';
3
2
 
4
- // Cohere implementation
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.client = new CohereClient({
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 this.client.rerank({
18
- query,
19
- documents: [text],
20
- model: this.model,
21
- topN: 1,
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
- return response.results[0].relevanceScore;
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
  }