@mastra/rag 0.1.19 → 0.1.20-alpha.1
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 +23 -0
- package/dist/_tsup-dts-rollup.d.cts +225 -86
- package/dist/_tsup-dts-rollup.d.ts +225 -86
- package/dist/index.cjs +417 -131
- package/dist/index.js +385 -99
- package/package.json +2 -3
- package/src/document/document.ts +6 -9
- package/src/document/extractors/base.ts +30 -0
- package/src/document/extractors/index.ts +1 -1
- package/src/document/extractors/keywords.test.ts +1 -1
- package/src/document/extractors/keywords.ts +7 -19
- package/src/document/extractors/questions.test.ts +1 -1
- package/src/document/extractors/questions.ts +7 -25
- package/src/document/extractors/summary.test.ts +1 -1
- package/src/document/extractors/summary.ts +7 -19
- package/src/document/extractors/title.test.ts +1 -1
- package/src/document/extractors/title.ts +7 -44
- package/src/document/extractors/types.ts +1 -1
- package/src/document/prompts/base.ts +77 -0
- package/src/document/prompts/format.ts +9 -0
- package/src/document/prompts/index.ts +15 -0
- package/src/document/prompts/prompt.ts +60 -0
- package/src/document/prompts/types.ts +29 -0
- package/src/document/schema/index.ts +3 -0
- package/src/document/schema/node.ts +187 -0
- package/src/document/schema/types.ts +40 -0
- package/src/document/transformers/html.ts +1 -1
- package/src/document/transformers/json.ts +1 -1
- package/src/document/transformers/markdown.ts +1 -1
- package/src/document/transformers/text.ts +1 -1
- package/src/document/transformers/transformer.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/rag",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.20-alpha.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,11 +23,10 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@paralleldrive/cuid2": "^2.2.2",
|
|
25
25
|
"js-tiktoken": "^1.0.19",
|
|
26
|
-
"llamaindex": "^0.9.17",
|
|
27
26
|
"node-html-better-parser": "^1.4.7",
|
|
28
27
|
"pathe": "^2.0.3",
|
|
29
28
|
"zod": "^3.24.2",
|
|
30
|
-
"@mastra/core": "^0.9.
|
|
29
|
+
"@mastra/core": "^0.9.1-alpha.1"
|
|
31
30
|
},
|
|
32
31
|
"peerDependencies": {
|
|
33
32
|
"ai": "^4.0.0"
|
package/src/document/document.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Document as Chunk, IngestionPipeline, NodeRelationship, ObjectType } from 'llamaindex';
|
|
2
|
-
|
|
3
1
|
import { TitleExtractor, SummaryExtractor, QuestionsAnsweredExtractor, KeywordExtractor } from './extractors';
|
|
2
|
+
import type { BaseNode } from './schema';
|
|
3
|
+
import { Document as Chunk, NodeRelationship, ObjectType } from './schema';
|
|
4
4
|
|
|
5
5
|
import { CharacterTransformer, RecursiveCharacterTransformer } from './transformers/character';
|
|
6
6
|
import { HTMLHeaderTransformer, HTMLSectionTransformer } from './transformers/html';
|
|
@@ -54,13 +54,10 @@ export class MDocument {
|
|
|
54
54
|
);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const nodes = await pipeline.run({
|
|
62
|
-
documents: this.chunks,
|
|
63
|
-
});
|
|
57
|
+
let nodes: BaseNode[] = this.chunks;
|
|
58
|
+
for (const extractor of transformations) {
|
|
59
|
+
nodes = await extractor.processNodes(nodes);
|
|
60
|
+
}
|
|
64
61
|
|
|
65
62
|
this.chunks = this.chunks.map((doc, i) => {
|
|
66
63
|
return new Chunk({
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { BaseNode } from '../schema';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Abstract class for all extractors.
|
|
5
|
+
*/
|
|
6
|
+
export abstract class BaseExtractor {
|
|
7
|
+
isTextNodeOnly: boolean = true;
|
|
8
|
+
|
|
9
|
+
abstract extract(nodes: BaseNode[]): Promise<Record<string, any>[]>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param nodes Nodes to extract metadata from.
|
|
14
|
+
* @returns Metadata extracted from the nodes.
|
|
15
|
+
*/
|
|
16
|
+
async processNodes(nodes: BaseNode[]): Promise<BaseNode[]> {
|
|
17
|
+
let newNodes: BaseNode[] = nodes;
|
|
18
|
+
|
|
19
|
+
const curMetadataList = await this.extract(newNodes);
|
|
20
|
+
|
|
21
|
+
for (const idx in newNodes) {
|
|
22
|
+
newNodes[idx]!.metadata = {
|
|
23
|
+
...newNodes[idx]!.metadata,
|
|
24
|
+
...curMetadataList[idx],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return newNodes;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -2,4 +2,4 @@ export { TitleExtractor } from './title';
|
|
|
2
2
|
export { SummaryExtractor } from './summary';
|
|
3
3
|
export { QuestionsAnsweredExtractor } from './questions';
|
|
4
4
|
export { KeywordExtractor } from './keywords';
|
|
5
|
-
export
|
|
5
|
+
export type { KeywordExtractArgs, QuestionAnswerExtractArgs, SummaryExtractArgs, TitleExtractorsArgs } from './types';
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
2
|
-
import {
|
|
3
|
-
import type { KeywordExtractPrompt
|
|
2
|
+
import { defaultKeywordExtractPrompt, PromptTemplate } from '../prompts';
|
|
3
|
+
import type { KeywordExtractPrompt } from '../prompts';
|
|
4
|
+
import type { BaseNode } from '../schema';
|
|
5
|
+
import { TextNode } from '../schema';
|
|
6
|
+
import { BaseExtractor } from './base';
|
|
4
7
|
import { baseLLM } from './types';
|
|
5
8
|
import type { KeywordExtractArgs } from './types';
|
|
6
9
|
|
|
@@ -15,23 +18,8 @@ type ExtractKeyword = {
|
|
|
15
18
|
* Extract keywords from a list of nodes.
|
|
16
19
|
*/
|
|
17
20
|
export class KeywordExtractor extends BaseExtractor {
|
|
18
|
-
/**
|
|
19
|
-
* MastraLanguageModel instance.
|
|
20
|
-
* @type {MastraLanguageModel}
|
|
21
|
-
*/
|
|
22
21
|
llm: MastraLanguageModel;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Number of keywords to extract.
|
|
26
|
-
* @type {number}
|
|
27
|
-
* @default 5
|
|
28
|
-
*/
|
|
29
22
|
keywords: number = 5;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The prompt template to use for the question extractor.
|
|
33
|
-
* @type {string}
|
|
34
|
-
*/
|
|
35
23
|
promptTemplate: KeywordExtractPrompt;
|
|
36
24
|
|
|
37
25
|
/**
|
|
@@ -66,7 +54,7 @@ export class KeywordExtractor extends BaseExtractor {
|
|
|
66
54
|
* Adds error handling for malformed/empty LLM output.
|
|
67
55
|
*/
|
|
68
56
|
async extractKeywordsFromNodes(node: BaseNode): Promise<ExtractKeyword> {
|
|
69
|
-
const text = node.getContent(
|
|
57
|
+
const text = node.getContent();
|
|
70
58
|
if (!text || text.trim() === '') {
|
|
71
59
|
return { excerptKeywords: '' };
|
|
72
60
|
}
|
|
@@ -86,7 +74,7 @@ export class KeywordExtractor extends BaseExtractor {
|
|
|
86
74
|
{
|
|
87
75
|
type: 'text',
|
|
88
76
|
text: this.promptTemplate.format({
|
|
89
|
-
context: node.getContent(
|
|
77
|
+
context: node.getContent(),
|
|
90
78
|
maxKeywords: this.keywords.toString(),
|
|
91
79
|
}),
|
|
92
80
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createOpenAI } from '@ai-sdk/openai';
|
|
2
|
-
import { TextNode } from 'llamaindex';
|
|
3
2
|
import { describe, it, expect, vi } from 'vitest';
|
|
3
|
+
import { TextNode } from '../schema';
|
|
4
4
|
import { QuestionsAnsweredExtractor } from './questions';
|
|
5
5
|
|
|
6
6
|
const openai = createOpenAI({
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
2
|
-
import { PromptTemplate, defaultQuestionExtractPrompt
|
|
3
|
-
import type { QuestionExtractPrompt
|
|
2
|
+
import { PromptTemplate, defaultQuestionExtractPrompt } from '../prompts';
|
|
3
|
+
import type { QuestionExtractPrompt } from '../prompts';
|
|
4
|
+
import type { BaseNode } from '../schema';
|
|
5
|
+
import { TextNode } from '../schema';
|
|
6
|
+
import { BaseExtractor } from './base';
|
|
4
7
|
import { baseLLM, STRIP_REGEX } from './types';
|
|
5
8
|
import type { QuestionAnswerExtractArgs } from './types';
|
|
6
9
|
|
|
@@ -15,30 +18,9 @@ type ExtractQuestion = {
|
|
|
15
18
|
* Extract questions from a list of nodes.
|
|
16
19
|
*/
|
|
17
20
|
export class QuestionsAnsweredExtractor extends BaseExtractor {
|
|
18
|
-
/**
|
|
19
|
-
* MastraLanguageModel instance.
|
|
20
|
-
* @type {MastraLanguageModel}
|
|
21
|
-
*/
|
|
22
21
|
llm: MastraLanguageModel;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Number of questions to generate.
|
|
26
|
-
* @type {number}
|
|
27
|
-
* @default 5
|
|
28
|
-
*/
|
|
29
22
|
questions: number = 5;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The prompt template to use for the question extractor.
|
|
33
|
-
* @type {string}
|
|
34
|
-
*/
|
|
35
23
|
promptTemplate: QuestionExtractPrompt;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Wheter to use metadata for embeddings only
|
|
39
|
-
* @type {boolean}
|
|
40
|
-
* @default false
|
|
41
|
-
*/
|
|
42
24
|
embeddingOnly: boolean = false;
|
|
43
25
|
|
|
44
26
|
/**
|
|
@@ -72,7 +54,7 @@ export class QuestionsAnsweredExtractor extends BaseExtractor {
|
|
|
72
54
|
* @returns {Promise<Array<ExtractQuestion> | Array<{}>>} Questions extracted from the node.
|
|
73
55
|
*/
|
|
74
56
|
async extractQuestionsFromNode(node: BaseNode): Promise<ExtractQuestion> {
|
|
75
|
-
const text = node.getContent(
|
|
57
|
+
const text = node.getContent();
|
|
76
58
|
if (!text || text.trim() === '') {
|
|
77
59
|
return { questionsThisExcerptCanAnswer: '' };
|
|
78
60
|
}
|
|
@@ -80,7 +62,7 @@ export class QuestionsAnsweredExtractor extends BaseExtractor {
|
|
|
80
62
|
return { questionsThisExcerptCanAnswer: '' };
|
|
81
63
|
}
|
|
82
64
|
|
|
83
|
-
const contextStr = node.getContent(
|
|
65
|
+
const contextStr = node.getContent();
|
|
84
66
|
|
|
85
67
|
const prompt = this.promptTemplate.format({
|
|
86
68
|
context: contextStr,
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
2
|
-
import { PromptTemplate, defaultSummaryPrompt
|
|
3
|
-
import type { SummaryPrompt
|
|
2
|
+
import { PromptTemplate, defaultSummaryPrompt } from '../prompts';
|
|
3
|
+
import type { SummaryPrompt } from '../prompts';
|
|
4
|
+
import type { BaseNode } from '../schema';
|
|
5
|
+
import { TextNode } from '../schema';
|
|
6
|
+
import { BaseExtractor } from './base';
|
|
4
7
|
import { baseLLM, STRIP_REGEX } from './types';
|
|
5
8
|
import type { SummaryExtractArgs } from './types';
|
|
6
9
|
|
|
@@ -18,27 +21,12 @@ type ExtractSummary = {
|
|
|
18
21
|
* @returns Array of summary results
|
|
19
22
|
*/
|
|
20
23
|
export class SummaryExtractor extends BaseExtractor {
|
|
21
|
-
/**
|
|
22
|
-
* MastraLanguageModel instance.
|
|
23
|
-
* @type {MastraLanguageModel}
|
|
24
|
-
*/
|
|
25
24
|
private llm: MastraLanguageModel;
|
|
26
|
-
/**
|
|
27
|
-
* List of summaries to extract: 'self', 'prev', 'next'
|
|
28
|
-
* @type {string[]}
|
|
29
|
-
*/
|
|
30
25
|
summaries: string[];
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The prompt template to use for the summary extractor.
|
|
34
|
-
* @type {string}
|
|
35
|
-
*/
|
|
36
26
|
promptTemplate: SummaryPrompt;
|
|
37
|
-
|
|
38
27
|
private selfSummary: boolean;
|
|
39
28
|
private prevSummary: boolean;
|
|
40
29
|
private nextSummary: boolean;
|
|
41
|
-
|
|
42
30
|
constructor(options?: SummaryExtractArgs) {
|
|
43
31
|
const summaries = options?.summaries ?? ['self'];
|
|
44
32
|
|
|
@@ -67,14 +55,14 @@ export class SummaryExtractor extends BaseExtractor {
|
|
|
67
55
|
* @returns {Promise<string>} Summary extracted from the node.
|
|
68
56
|
*/
|
|
69
57
|
async generateNodeSummary(node: BaseNode): Promise<string> {
|
|
70
|
-
const text = node.getContent(
|
|
58
|
+
const text = node.getContent();
|
|
71
59
|
if (!text || text.trim() === '') {
|
|
72
60
|
return '';
|
|
73
61
|
}
|
|
74
62
|
if (this.isTextNodeOnly && !(node instanceof TextNode)) {
|
|
75
63
|
return '';
|
|
76
64
|
}
|
|
77
|
-
const context = node.getContent(
|
|
65
|
+
const context = node.getContent();
|
|
78
66
|
|
|
79
67
|
const prompt = this.promptTemplate.format({
|
|
80
68
|
context,
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import type { MastraLanguageModel } from '@mastra/core/agent';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
TextNode,
|
|
8
|
-
BaseExtractor,
|
|
9
|
-
} from 'llamaindex';
|
|
10
|
-
import type { TitleCombinePrompt, TitleExtractorPrompt, BaseNode } from 'llamaindex';
|
|
2
|
+
import { defaultTitleCombinePromptTemplate, defaultTitleExtractorPromptTemplate, PromptTemplate } from '../prompts';
|
|
3
|
+
import type { TitleCombinePrompt, TitleExtractorPrompt } from '../prompts';
|
|
4
|
+
import { TextNode } from '../schema';
|
|
5
|
+
import type { BaseNode } from '../schema';
|
|
6
|
+
import { BaseExtractor } from './base';
|
|
11
7
|
import { baseLLM } from './types';
|
|
12
8
|
import type { TitleExtractorsArgs } from './types';
|
|
13
9
|
|
|
@@ -19,45 +15,12 @@ type ExtractTitle = {
|
|
|
19
15
|
* Extract title from a list of nodes.
|
|
20
16
|
*/
|
|
21
17
|
export class TitleExtractor extends BaseExtractor {
|
|
22
|
-
/**
|
|
23
|
-
* MastraLanguageModel instance.
|
|
24
|
-
* @type {MastraLanguageModel}
|
|
25
|
-
*/
|
|
26
18
|
llm: MastraLanguageModel;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Can work for mixture of text and non-text nodes
|
|
30
|
-
* @type {boolean}
|
|
31
|
-
* @default false
|
|
32
|
-
*/
|
|
33
19
|
isTextNodeOnly: boolean = false;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Number of nodes to extrct titles from.
|
|
37
|
-
* @type {number}
|
|
38
|
-
* @default 5
|
|
39
|
-
*/
|
|
40
20
|
nodes: number = 5;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The prompt template to use for the title extractor.
|
|
44
|
-
* @type {string}
|
|
45
|
-
*/
|
|
46
21
|
nodeTemplate: TitleExtractorPrompt;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* The prompt template to merge title with..
|
|
50
|
-
* @type {string}
|
|
51
|
-
*/
|
|
52
22
|
combineTemplate: TitleCombinePrompt;
|
|
53
23
|
|
|
54
|
-
/**
|
|
55
|
-
* Constructor for the TitleExtractor class.
|
|
56
|
-
* @param {MastraLanguageModel} llm MastraLanguageModel instance.
|
|
57
|
-
* @param {number} nodes Number of nodes to extract titles from.
|
|
58
|
-
* @param {TitleExtractorPrompt} nodeTemplate The prompt template to use for the title extractor.
|
|
59
|
-
* @param {string} combineTemplate The prompt template to merge title with..
|
|
60
|
-
*/
|
|
61
24
|
constructor(options?: TitleExtractorsArgs) {
|
|
62
25
|
super();
|
|
63
26
|
|
|
@@ -92,7 +55,7 @@ export class TitleExtractor extends BaseExtractor {
|
|
|
92
55
|
const nodeIndexes: number[] = [];
|
|
93
56
|
|
|
94
57
|
nodes.forEach((node, idx) => {
|
|
95
|
-
const text = node.getContent(
|
|
58
|
+
const text = node.getContent();
|
|
96
59
|
if (!text || text.trim() === '') {
|
|
97
60
|
results[idx] = { documentTitle: '' };
|
|
98
61
|
} else {
|
|
@@ -189,7 +152,7 @@ export class TitleExtractor extends BaseExtractor {
|
|
|
189
152
|
{
|
|
190
153
|
type: 'text',
|
|
191
154
|
text: this.nodeTemplate.format({
|
|
192
|
-
context: node.getContent(
|
|
155
|
+
context: node.getContent(),
|
|
193
156
|
}),
|
|
194
157
|
},
|
|
195
158
|
],
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { format } from './format';
|
|
2
|
+
import type { BasePromptTemplateOptions, ChatMessage, PromptTemplateOptions } from './types';
|
|
3
|
+
|
|
4
|
+
export abstract class BasePromptTemplate<const TemplatesVar extends readonly string[] = string[]> {
|
|
5
|
+
templateVars: Set<string> = new Set();
|
|
6
|
+
options: Partial<Record<TemplatesVar[number] | (string & {}), string>> = {};
|
|
7
|
+
|
|
8
|
+
protected constructor(options: BasePromptTemplateOptions<TemplatesVar>) {
|
|
9
|
+
const { templateVars } = options;
|
|
10
|
+
if (templateVars) {
|
|
11
|
+
this.templateVars = new Set(templateVars);
|
|
12
|
+
}
|
|
13
|
+
if (options.options) {
|
|
14
|
+
this.options = options.options;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
abstract partialFormat(
|
|
19
|
+
options: Partial<Record<TemplatesVar[number] | (string & {}), string>>,
|
|
20
|
+
): BasePromptTemplate<TemplatesVar>;
|
|
21
|
+
|
|
22
|
+
abstract format(options?: Partial<Record<TemplatesVar[number] | (string & {}), string>>): string;
|
|
23
|
+
|
|
24
|
+
abstract formatMessages(options?: Partial<Record<TemplatesVar[number] | (string & {}), string>>): ChatMessage[];
|
|
25
|
+
|
|
26
|
+
abstract get template(): string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class PromptTemplate<
|
|
30
|
+
const TemplatesVar extends readonly string[] = string[],
|
|
31
|
+
> extends BasePromptTemplate<TemplatesVar> {
|
|
32
|
+
#template: string;
|
|
33
|
+
|
|
34
|
+
constructor(options: PromptTemplateOptions<TemplatesVar>) {
|
|
35
|
+
const { template, ...rest } = options;
|
|
36
|
+
super(rest);
|
|
37
|
+
this.#template = template;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
partialFormat(options: Partial<Record<TemplatesVar[number] | (string & {}), string>>): PromptTemplate<TemplatesVar> {
|
|
41
|
+
const prompt = new PromptTemplate({
|
|
42
|
+
template: this.template,
|
|
43
|
+
templateVars: [...this.templateVars],
|
|
44
|
+
options: this.options,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
prompt.options = {
|
|
48
|
+
...prompt.options,
|
|
49
|
+
...options,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return prompt;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
format(options?: Partial<Record<TemplatesVar[number] | (string & {}), string>>): string {
|
|
56
|
+
const allOptions = {
|
|
57
|
+
...this.options,
|
|
58
|
+
...options,
|
|
59
|
+
} as Record<TemplatesVar[number], string>;
|
|
60
|
+
|
|
61
|
+
return format(this.template, allOptions);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
formatMessages(options?: Partial<Record<TemplatesVar[number] | (string & {}), string>>): ChatMessage[] {
|
|
65
|
+
const prompt = this.format(options);
|
|
66
|
+
return [
|
|
67
|
+
{
|
|
68
|
+
role: 'user',
|
|
69
|
+
content: prompt,
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get template(): string {
|
|
75
|
+
return this.#template;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a string by replacing placeholders with values from the provided parameters.
|
|
3
|
+
* @param str The string to format.
|
|
4
|
+
* @param params A record of placeholder names to their corresponding values.
|
|
5
|
+
* @returns The formatted string.
|
|
6
|
+
*/
|
|
7
|
+
export function format(str: string, params: Record<string, string>) {
|
|
8
|
+
return str.replace(/{(\w+)}/g, (_, k) => params[k] ?? '');
|
|
9
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { BasePromptTemplate, PromptTemplate } from './base';
|
|
2
|
+
export {
|
|
3
|
+
defaultKeywordExtractPrompt,
|
|
4
|
+
defaultQuestionExtractPrompt,
|
|
5
|
+
defaultSummaryPrompt,
|
|
6
|
+
defaultTitleCombinePromptTemplate,
|
|
7
|
+
defaultTitleExtractorPromptTemplate,
|
|
8
|
+
} from './prompt';
|
|
9
|
+
export type {
|
|
10
|
+
KeywordExtractPrompt,
|
|
11
|
+
QuestionExtractPrompt,
|
|
12
|
+
SummaryPrompt,
|
|
13
|
+
TitleCombinePrompt,
|
|
14
|
+
TitleExtractorPrompt,
|
|
15
|
+
} from './prompt';
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { PromptTemplate } from './base';
|
|
2
|
+
|
|
3
|
+
export type SummaryPrompt = PromptTemplate<['context']>;
|
|
4
|
+
export type KeywordExtractPrompt = PromptTemplate<['context', 'maxKeywords']>;
|
|
5
|
+
export type QuestionExtractPrompt = PromptTemplate<['context', 'numQuestions']>;
|
|
6
|
+
export type TitleExtractorPrompt = PromptTemplate<['context']>;
|
|
7
|
+
export type TitleCombinePrompt = PromptTemplate<['context']>;
|
|
8
|
+
|
|
9
|
+
export const defaultSummaryPrompt: SummaryPrompt = new PromptTemplate({
|
|
10
|
+
templateVars: ['context'],
|
|
11
|
+
template: `Write a summary of the following. Try to use only the information provided. Try to include as many key details as possible.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
{context}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
SUMMARY:"""
|
|
18
|
+
`,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const defaultKeywordExtractPrompt: KeywordExtractPrompt = new PromptTemplate({
|
|
22
|
+
templateVars: ['maxKeywords', 'context'],
|
|
23
|
+
template: `
|
|
24
|
+
Some text is provided below. Given the text, extract up to {maxKeywords} keywords from the text. Avoid stopwords.
|
|
25
|
+
---------------------
|
|
26
|
+
{context}
|
|
27
|
+
---------------------
|
|
28
|
+
Provide keywords in the following comma-separated format: 'KEYWORDS: <keywords>'
|
|
29
|
+
`,
|
|
30
|
+
}).partialFormat({
|
|
31
|
+
maxKeywords: '10',
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const defaultQuestionExtractPrompt = new PromptTemplate({
|
|
35
|
+
templateVars: ['numQuestions', 'context'],
|
|
36
|
+
template: `(
|
|
37
|
+
"Given the contextual informations below, generate {numQuestions} questions this context can provides specific answers to which are unlikely to be found else where. Higher-level summaries of surrounding context may be provided as well. "
|
|
38
|
+
"Try using these summaries to generate better questions that this context can answer."
|
|
39
|
+
"---------------------"
|
|
40
|
+
"{context}"
|
|
41
|
+
"---------------------"
|
|
42
|
+
"Provide questions in the following format: 'QUESTIONS: <questions>'"
|
|
43
|
+
)`,
|
|
44
|
+
}).partialFormat({
|
|
45
|
+
numQuestions: '5',
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export const defaultTitleExtractorPromptTemplate = new PromptTemplate({
|
|
49
|
+
templateVars: ['context'],
|
|
50
|
+
template: `{context}
|
|
51
|
+
Give a title that summarizes all of the unique entities, titles or themes found in the context.
|
|
52
|
+
Title: `,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export const defaultTitleCombinePromptTemplate = new PromptTemplate({
|
|
56
|
+
templateVars: ['context'],
|
|
57
|
+
template: `{context}
|
|
58
|
+
Based on the above candidate titles and contents, what is the comprehensive title for this document?
|
|
59
|
+
Title: `,
|
|
60
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type MessageType = 'user' | 'assistant' | 'system' | 'memory' | 'developer';
|
|
2
|
+
|
|
3
|
+
export type MessageContentTextDetail = {
|
|
4
|
+
type: 'text';
|
|
5
|
+
text: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Extended type for the content of a message that allows for multi-modal messages.
|
|
10
|
+
*/
|
|
11
|
+
export type MessageContent = string | MessageContentTextDetail[];
|
|
12
|
+
|
|
13
|
+
export type ChatMessage<AdditionalMessageOptions extends object = object> = {
|
|
14
|
+
content: MessageContent;
|
|
15
|
+
role: MessageType;
|
|
16
|
+
options?: undefined | AdditionalMessageOptions;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type BasePromptTemplateOptions<TemplatesVar extends readonly string[]> = {
|
|
20
|
+
templateVars?:
|
|
21
|
+
| TemplatesVar
|
|
22
|
+
// loose type for better type inference
|
|
23
|
+
| readonly string[];
|
|
24
|
+
options?: Partial<Record<TemplatesVar[number] | (string & {}), string>>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type PromptTemplateOptions<TemplatesVar extends readonly string[]> = BasePromptTemplateOptions<TemplatesVar> & {
|
|
28
|
+
template: string;
|
|
29
|
+
};
|