Skip to content

Commit

Permalink
feat(prompt): add formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk authored and BroKun committed Dec 20, 2023
1 parent eec5fa0 commit ad314da
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

import type { DefaultEncodedFormatter } from '@difizen/libro-jupyter';
import {
concatMultilineString,
DefaultDecodedFormatter,
FormatterContribution,
} from '@difizen/libro-jupyter';
import { singleton } from '@difizen/mana-app';

export interface PromptDecodedFormatter extends DefaultDecodedFormatter {
modelType?: string;
}

@singleton({ contrib: FormatterContribution })
export class FormatterPromptMagicContribution
implements FormatterContribution<PromptDecodedFormatter>
{
formatter = 'formatter-prompt-magic';
formatterOptions?: object;
canHandle = (libroFormatter: string) => {
if (libroFormatter === this.formatter) {
return 100;
}
return 1;
};

encode = (source: PromptDecodedFormatter) => {
const promptObj = {
model_name: source.modelType || 'CodeGPT',
prompt: source.value,
};
const encodeValue = `%%prompt \n${JSON.stringify(promptObj)}`;
return {
source: encodeValue,
metadata: {
libroFormatter: this.formatter,
},
};
};

decode = (formatterValue: DefaultEncodedFormatter) => {
const value = concatMultilineString(formatterValue.source);

if (value.startsWith('%%prompt \n')) {
const run = value.split('%%prompt \n')[1];
const runValue = JSON.parse(run);
const codeValue = runValue.prompt;
const modelType = runValue.model_name;
return {
value: codeValue,
modelType,
};
}
return {
value: '',
};
};

validate = (source: PromptDecodedFormatter): source is PromptDecodedFormatter => {
return DefaultDecodedFormatter.is(source) && 'modelType' in source;
};
}
2 changes: 2 additions & 0 deletions packages/libro-prompt-cell/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CellOptions, LibroModule, OutputModule } from '@difizen/libro-core';
import { LibroRenderMimeModule } from '@difizen/libro-rendermime';
import { ManaModule } from '@difizen/mana-app';

import { FormatterPromptMagicContribution } from './libro-formatter-prompt-magic-contribution.js';
import { LibroPromptCellCommandContribution } from './prompt-cell-command-contribution.js';
import { PromptCellContribution } from './prompt-cell-contribution.js';
import { LibroPromptCellModel } from './prompt-cell-model.js';
Expand All @@ -18,6 +19,7 @@ export const LibroPromptCellModule = ManaModule.create()
LibroPromptOutputArea,
LibroPromptOutputMimeTypeContribution,
LibroPromptCellCommandContribution,
FormatterPromptMagicContribution,
{
token: LibroPromptCellModelFactory,
useFactory: (ctx) => {
Expand Down
32 changes: 7 additions & 25 deletions packages/libro-prompt-cell/src/prompt-cell-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { concatMultilineString } from '@difizen/libro-common';
import type {
ICellMetadata,
ExecutionCount,
Expand Down Expand Up @@ -65,36 +64,19 @@ export class LibroPromptCellModel
this.executeCount = (options.cell as ICodeCell).execution_count || null;
this.hasOutputHidden = false;
this.hasOutputsScrolled = false;
this.mimeType = 'text/x-python';
this.metadata = options?.cell?.metadata || {};
this.fromSource(concatMultilineString(options?.cell?.source));
}

fromSource(source: string) {
try {
const run = source.split('%%prompt \n')[1];
const runValue = JSON.parse(run);
this.value = runValue.prompt;
this.modelType = runValue.model_name;
} catch {
() => {
//
};
}
this.libroFormatType = 'formatter-prompt-magic';
this.mimeType = 'application/vnd.libro.prompt+json';
this.metadata = {
...options?.cell?.metadata,
libroFormatter: this.libroFormatType,
};
}

override toJSON(): Omit<ICodeCell, 'outputs'> {
// const outputs = this.outputArea?.toJSON() ?? this.outputs;
const promptObj = {
model_name: this.modelType || 'CodeGPT',
prompt: this.value,
};
const encodeValue = `%%prompt \n${JSON.stringify(promptObj)}`;

return {
id: this.id,
cell_type: this.type,
source: encodeValue,
source: this.source,
metadata: this.metadata,
execution_count: this.executeCount,
// outputs: this.outputs,
Expand Down
23 changes: 12 additions & 11 deletions packages/libro-prompt-cell/src/prompt-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
IRange,
CodeEditorView,
} from '@difizen/libro-code-editor';

import type { ICodeCell, IOutput } from '@difizen/libro-common';
import { isOutput } from '@difizen/libro-common';
import type {
Expand Down Expand Up @@ -74,8 +73,12 @@ const PropmtEditorViewComponent = React.forwardRef<HTMLDivElement>(
.then(() => {
const len = instance.modelSelection.length;
if (len > 0) {
setSelectedModel(instance.modelSelection[len - 1].label);
instance.model.modelType = instance.modelSelection[len - 1].label;
instance.model.decodeObject = {
modelType: instance.modelSelection[len - 1].label,
...instance.model.decodeObject,
};
setSelectedModel(instance.model.decodeObject['modelType']);
instance.model.modelType = instance.model.decodeObject['modelType'];
return;
}
return;
Expand All @@ -88,6 +91,10 @@ const PropmtEditorViewComponent = React.forwardRef<HTMLDivElement>(

const handleChange = (value: string) => {
instance.model.modelType = value;
instance.model.decodeObject = {
...instance.model.decodeObject,
modelType: value,
};
setSelectedModel(value);
};

Expand Down Expand Up @@ -338,13 +345,7 @@ export class LibroPromptCellView extends LibroExecutableCellView {

const kernelConnection = getOrigin(libroModel.kernelConnection);

// const cellContent = '%prompt ' + toBase64(this.model.value) + ',model:';
const promptObj = {
model_name: this.model.modelType || 'CodeGPT',
prompt: this.model.value,
};

const cellContent = `%%prompt \n${JSON.stringify(promptObj)}`;
const cellContent = this.model.source;

try {
// Promise.resolve().then(() => {
Expand Down Expand Up @@ -410,7 +411,7 @@ export class LibroPromptCellView extends LibroExecutableCellView {
content: KernelMessage.IExecuteRequestMsg['content'],
ioCallback: (msg: KernelMessage.IIOPubMessage) => any,
) => {
const model = this.parent!.model! as LibroJupyterModel;
const model = this.parent?.model as LibroJupyterModel;
await model.kcReady;
const connection = model.kernelConnection!;
const future = connection.requestExecute(content);
Expand Down

0 comments on commit ad314da

Please sign in to comment.