-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathServiceContext.ts
64 lines (59 loc) · 1.86 KB
/
ServiceContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { PromptHelper } from "./PromptHelper.js";
import { OpenAIEmbedding } from "./embeddings/OpenAIEmbedding.js";
import type { BaseEmbedding } from "./embeddings/types.js";
import { OpenAI } from "./llm/openai.js";
import type { LLM } from "./llm/types.js";
import { SimpleNodeParser } from "./nodeParsers/SimpleNodeParser.js";
import type { NodeParser } from "./nodeParsers/types.js";
/**
* The ServiceContext is a collection of components that are used in different parts of the application.
*/
export interface ServiceContext {
llm: LLM;
promptHelper: PromptHelper;
embedModel: BaseEmbedding;
nodeParser: NodeParser;
// llamaLogger: any;
}
export interface ServiceContextOptions {
llm?: LLM;
promptHelper?: PromptHelper;
embedModel?: BaseEmbedding;
nodeParser?: NodeParser;
// NodeParser arguments
chunkSize?: number;
chunkOverlap?: number;
}
export function serviceContextFromDefaults(options?: ServiceContextOptions) {
const serviceContext: ServiceContext = {
llm: options?.llm ?? new OpenAI(),
embedModel: options?.embedModel ?? new OpenAIEmbedding(),
nodeParser:
options?.nodeParser ??
new SimpleNodeParser({
chunkSize: options?.chunkSize,
chunkOverlap: options?.chunkOverlap,
}),
promptHelper: options?.promptHelper ?? new PromptHelper(),
};
return serviceContext;
}
export function serviceContextFromServiceContext(
serviceContext: ServiceContext,
options: ServiceContextOptions,
) {
const newServiceContext = { ...serviceContext };
if (options.llm) {
newServiceContext.llm = options.llm;
}
if (options.promptHelper) {
newServiceContext.promptHelper = options.promptHelper;
}
if (options.embedModel) {
newServiceContext.embedModel = options.embedModel;
}
if (options.nodeParser) {
newServiceContext.nodeParser = options.nodeParser;
}
return newServiceContext;
}