Skip to content

Commit f2eafba

Browse files
committed
Refactor litlytics class to always return new pipeline after updates
1 parent 42656dc commit f2eafba

File tree

2 files changed

+51
-79
lines changed

2 files changed

+51
-79
lines changed

packages/litlytics/litlytics.ts

+45-78
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { runLLMStep, type RunLLMStepArgs } from './engine/step/runLLMStep';
1212
import { testPipelineStep } from './engine/testStep';
1313
import type { LLMModel, LLMProvider } from './llm/types';
1414
import { OUTPUT_ID } from './output/Output';
15-
import { pipelineFromText } from './pipeline/fromText';
15+
import {
16+
pipelineFromText,
17+
type PipelineFromTextStatus,
18+
} from './pipeline/fromText';
1619
import { generatePipeline } from './pipeline/generate';
1720
import {
1821
emptyPipeline,
@@ -59,15 +62,15 @@ export interface LitLyticsConfig {
5962

6063
export class LitLytics {
6164
// model config
62-
private _provider?: LLMProviders;
63-
private _model?: LLMModel;
65+
provider?: LLMProviders;
66+
model?: LLMModel;
6467
#llmKey?: string;
6568
// local LLM engine
66-
private _engine?: MLCEngine;
69+
engine?: MLCEngine;
6770

6871
// pipeline
69-
private _pipeline: Pipeline = emptyPipeline;
70-
private _pipelineStatus: PipelineStatus = {
72+
pipeline: Pipeline = emptyPipeline;
73+
pipelineStatus: PipelineStatus = {
7174
status: 'init',
7275
};
7376

@@ -82,89 +85,66 @@ export class LitLytics {
8285
key: string;
8386
engine?: MLCEngine;
8487
}) {
85-
this._provider = provider;
86-
this._model = model;
88+
this.provider = provider;
89+
this.model = model;
8790
this.#llmKey = key;
88-
this._engine = engine;
91+
this.engine = engine;
8992
}
9093

9194
/**
9295
* Config management
9396
*/
94-
public get config(): LitLyticsConfig {
95-
return {
96-
// model config
97-
provider: this._provider,
98-
model: this._model,
99-
// pipeline
100-
pipeline: this._pipeline,
101-
};
102-
}
103-
10497
exportConfig = (): LitLyticsConfig => {
10598
return {
10699
// model config
107-
provider: this._provider,
108-
model: this._model,
100+
provider: this.provider,
101+
model: this.model,
109102
llmKey: this.#llmKey,
110103
// pipeline
111-
pipeline: this._pipeline,
104+
pipeline: this.pipeline,
112105
};
113106
};
114107

115108
importConfig = (config: LitLyticsConfig) => {
116-
this._provider = config.provider;
117-
this._model = config.model;
109+
this.provider = config.provider;
110+
this.model = config.model;
118111
this.#llmKey = config.llmKey;
119-
this._pipeline = config.pipeline ?? this._pipeline ?? emptyPipeline;
120-
};
121-
122-
getEngine = (): MLCEngine | undefined => {
123-
return this._engine;
124-
};
125-
126-
setWebEngine = (engine?: MLCEngine) => {
127-
this._engine = engine;
112+
this.pipeline = config.pipeline ?? this.pipeline ?? emptyPipeline;
128113
};
129114

130115
/**
131116
* Pipeline management
132117
*/
133-
public get pipeline(): Pipeline {
134-
return this._pipeline;
135-
}
136-
137118
setPipeline = (newPipeline: Partial<Pipeline>) => {
138-
this._pipeline = {
139-
...this._pipeline,
119+
this.pipeline = {
120+
...this.pipeline,
140121
...newPipeline,
141122
};
123+
return this.pipeline;
142124
};
143125

144126
resetPipeline = () => {
145-
this._pipeline = emptyPipeline;
146-
this._pipelineStatus = { status: 'init' };
127+
this.pipeline = structuredClone(emptyPipeline);
128+
this.pipelineStatus = { status: 'init' };
129+
return this.pipeline;
147130
};
148131

149132
/**
150133
* Pipeline status
151134
*/
152-
public get pipelineStatus(): PipelineStatus {
153-
return this._pipelineStatus;
154-
}
155-
156135
setPipelineStatus = (status: PipelineStatus) => {
157-
this._pipelineStatus = {
158-
...this._pipelineStatus,
136+
this.pipelineStatus = {
137+
...this.pipelineStatus,
159138
...status,
160139
};
140+
return this.pipelineStatus;
161141
};
162142

163143
/**
164144
* Document management
165145
*/
166146
public get docs(): Doc[] {
167-
return this._pipeline.source.docs;
147+
return this.pipeline.source.docs;
168148
}
169149

170150
setDocs = (docs: Doc[]) => {
@@ -186,18 +166,18 @@ export class LitLytics {
186166
args,
187167
}: Pick<RunPromptFromMessagesArgs, 'messages' | 'args'>) => {
188168
if (
189-
!this._provider?.length ||
190-
!this._model?.length ||
191-
(!this.#llmKey?.length && this._provider !== 'local')
169+
!this.provider?.length ||
170+
!this.model?.length ||
171+
(!this.#llmKey?.length && this.provider !== 'local')
192172
) {
193173
throw new Error('No provider, model or key set!');
194174
}
195175

196176
return await runPromptFromMessages({
197-
provider: this._provider,
177+
provider: this.provider,
198178
key: this.#llmKey ?? 'local',
199-
model: this._model,
200-
engine: this._engine,
179+
model: this.model,
180+
engine: this.engine,
201181
messages,
202182
args,
203183
});
@@ -209,18 +189,18 @@ export class LitLytics {
209189
args,
210190
}: Pick<RunPromptArgs, 'system' | 'user' | 'args'>) => {
211191
if (
212-
!this._provider?.length ||
213-
!this._model?.length ||
214-
(!this.#llmKey?.length && this._provider !== 'local')
192+
!this.provider?.length ||
193+
!this.model?.length ||
194+
(!this.#llmKey?.length && this.provider !== 'local')
215195
) {
216196
throw new Error('No provider, model or key set!');
217197
}
218198

219199
return await runPrompt({
220-
provider: this._provider,
200+
provider: this.provider,
221201
key: this.#llmKey ?? 'local',
222-
model: this._model,
223-
engine: this._engine,
202+
model: this.model,
203+
engine: this.engine,
224204
system,
225205
user,
226206
args,
@@ -231,19 +211,12 @@ export class LitLytics {
231211
* Pipeline
232212
*/
233213
pipelineFromText = async (
234-
onStatus: ({
235-
step,
236-
totalSteps,
237-
}: {
238-
step: number;
239-
totalSteps: number;
240-
}) => void
214+
onStatus: ({ step, totalSteps }: PipelineFromTextStatus) => void
241215
) => {
242216
if (!this.pipeline.pipelinePlan) {
243217
return;
244218
}
245219

246-
this.setPipelineStatus({ status: 'sourcing' });
247220
const newSteps = await pipelineFromText(
248221
this,
249222
this.pipeline.pipelinePlan,
@@ -254,7 +227,7 @@ export class LitLytics {
254227
newSteps.at(-1)!.connectsTo = [OUTPUT_ID];
255228

256229
// save
257-
this.setPipeline({
230+
return this.setPipeline({
258231
// assign input to first step
259232
source: {
260233
...this.pipeline.source,
@@ -263,8 +236,6 @@ export class LitLytics {
263236
// assign steps
264237
steps: newSteps,
265238
});
266-
267-
this.setPipelineStatus({ status: 'done' });
268239
};
269240

270241
generatePipeline = async () => {
@@ -277,11 +248,9 @@ export class LitLytics {
277248
description: this.pipeline.pipelineDescription,
278249
});
279250

280-
this.setPipeline({
251+
return this.setPipeline({
281252
pipelinePlan: plan ?? '',
282253
});
283-
284-
return this.pipeline;
285254
};
286255

287256
refinePipeline = async ({ refineRequest }: { refineRequest: string }) => {
@@ -290,8 +259,7 @@ export class LitLytics {
290259
refineRequest,
291260
pipeline: this.pipeline,
292261
});
293-
this.setPipeline({
294-
...this.pipeline,
262+
return this.setPipeline({
295263
pipelinePlan: plan ?? '',
296264
});
297265
};
@@ -308,8 +276,7 @@ export class LitLytics {
308276
try {
309277
setStatus({ status: 'init' });
310278
const newPipeline = await runPipeline(this, setStatus);
311-
this.setPipeline(newPipeline);
312-
return newPipeline;
279+
return this.setPipeline(newPipeline);
313280
} catch (err) {
314281
setStatus({ status: 'error', error: err as Error });
315282
}

packages/litlytics/pipeline/fromText.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import type { ProcessingStep, StepInput } from '../step/Step';
33
import { stepToJSONPrompt } from './prompts/stepToJSON';
44
import { parseLLMJSON } from './util';
55

6+
export interface PipelineFromTextStatus {
7+
step: number;
8+
totalSteps: number;
9+
}
10+
611
export async function pipelineFromText(
712
litlytics: LitLytics,
813
description: string,
9-
onStatus: ({ step, totalSteps }: { step: number; totalSteps: number }) => void
14+
onStatus: ({ step, totalSteps }: PipelineFromTextStatus) => void
1015
) {
1116
let steps = description
1217
.split('---')

0 commit comments

Comments
 (0)