-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eec5fa0
commit ad314da
Showing
4 changed files
with
84 additions
and
36 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/libro-prompt-cell/src/libro-formatter-prompt-magic-contribution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters