Files
OpenPipe-llm/src/state/sharedVariantEditor.slice.ts
Kyle Corbitt ded6678e97 Prep for more model providers
Adds a `modelProvider` field to `promptVariants`, currently just set to "openai/ChatCompletion" for all variants for now.

Adds a `modelProviders/` directory where we can define and store pluggable model providers. Currently just OpenAI. Not everything is pluggable yet -- notably the code to actually generate completions hasn't been migrated to this setup yet.

Does a lot of work to get the types working. Prompts are now defined with a function `definePrompt(modelProvider, config)` instead of `prompt = config`. Added a script to migrate old prompt definitions.

This is still partial work, but the diff is large enough that I want to get it in. I don't think anything is broken but I haven't tested thoroughly.
2023-07-20 14:49:22 -07:00

100 lines
2.8 KiB
TypeScript

import { type RouterOutputs } from "~/utils/api";
import { type SliceCreator } from "./store";
import loader from "@monaco-editor/loader";
import formatPromptConstructor from "~/utils/formatPromptConstructor";
export const editorBackground = "#fafafa";
export type SharedVariantEditorSlice = {
monaco: null | ReturnType<typeof loader.__getMonacoInstance>;
loadMonaco: () => Promise<void>;
scenarios: RouterOutputs["scenarios"]["list"];
updateScenariosModel: () => void;
setScenarios: (scenarios: RouterOutputs["scenarios"]["list"]) => void;
};
export const createVariantEditorSlice: SliceCreator<SharedVariantEditorSlice> = (set, get) => ({
monaco: loader.__getMonacoInstance(),
loadMonaco: async () => {
// We only want to run this client-side
if (typeof window === "undefined") return;
const [monaco, promptTypes] = await Promise.all([
loader.init(),
get().api?.client.experiments.promptTypes.query(),
]);
monaco.editor.defineTheme("customTheme", {
base: "vs",
inherit: true,
rules: [],
colors: {
"editor.background": editorBackground,
},
});
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
allowNonTsExtensions: true,
strictNullChecks: true,
lib: ["esnext"],
});
monaco.languages.typescript.typescriptDefaults.addExtraLib(
promptTypes ?? "",
"file:///PromptTypes.d.ts",
);
monaco.languages.registerDocumentFormattingEditProvider("typescript", {
provideDocumentFormattingEdits: async (model) => {
return [
{
range: model.getFullModelRange(),
text: await formatPromptConstructor(model.getValue()),
},
];
},
});
set((state) => {
state.sharedVariantEditor.monaco = monaco;
});
get().sharedVariantEditor.updateScenariosModel();
},
scenarios: [],
setScenarios: (scenarios) => {
set((state) => {
state.sharedVariantEditor.scenarios = scenarios;
});
get().sharedVariantEditor.updateScenariosModel();
},
updateScenariosModel: () => {
const monaco = get().sharedVariantEditor.monaco;
if (!monaco) return;
const modelContents = `
const scenarios = ${JSON.stringify(
get().sharedVariantEditor.scenarios.map((s) => s.variableValues),
null,
2,
)} as const;
type Scenario = typeof scenarios[number];
declare var scenario: Scenario | { [key: string]: string };
`;
const scenariosModel = monaco.editor.getModel(monaco.Uri.parse("file:///scenarios.ts"));
if (scenariosModel) {
scenariosModel.setValue(modelContents);
} else {
monaco.editor.createModel(
modelContents,
"typescript",
monaco.Uri.parse("file:///scenarios.ts"),
);
}
},
});