Refine prompt (#63)
* Remove unused ScenarioVariantCell fields * Refine deriveNewConstructFn * Fix prettier * Remove migration script * Add refine modal * Fix prettier * Fix diff checker overflow * Decrease diff height
This commit is contained in:
@@ -5,18 +5,20 @@ import dedent from "dedent";
|
||||
import { openai } from "./openai";
|
||||
import { getApiShapeForModel } from "./getTypesForModel";
|
||||
import { isObject } from "lodash-es";
|
||||
import { type CompletionCreateParams } from "openai/resources/chat/completions";
|
||||
|
||||
const isolate = new ivm.Isolate({ memoryLimit: 128 });
|
||||
|
||||
export async function deriveNewConstructFn(
|
||||
originalVariant: PromptVariant | null,
|
||||
newModel?: SupportedModel,
|
||||
instructions?: string,
|
||||
) {
|
||||
if (originalVariant && !newModel) {
|
||||
if (originalVariant && !newModel && !instructions) {
|
||||
return originalVariant.constructFn;
|
||||
}
|
||||
if (originalVariant && newModel) {
|
||||
return await getPromptFunctionForNewModel(originalVariant, newModel);
|
||||
if (originalVariant && (newModel || instructions)) {
|
||||
return await requestUpdatedPromptFunction(originalVariant, newModel, instructions);
|
||||
}
|
||||
return dedent`
|
||||
prompt = {
|
||||
@@ -31,38 +33,48 @@ export async function deriveNewConstructFn(
|
||||
}
|
||||
|
||||
const NUM_RETRIES = 5;
|
||||
const getPromptFunctionForNewModel = async (
|
||||
const requestUpdatedPromptFunction = async (
|
||||
originalVariant: PromptVariant,
|
||||
newModel: SupportedModel,
|
||||
newModel?: SupportedModel,
|
||||
instructions?: string,
|
||||
) => {
|
||||
const originalModel = originalVariant.model as SupportedModel;
|
||||
let newContructionFn = "";
|
||||
for (let i = 0; i < NUM_RETRIES; i++) {
|
||||
try {
|
||||
// TODO: Add api shape info to prompt
|
||||
const messages: CompletionCreateParams.CreateChatCompletionRequestNonStreaming.Message[] = [
|
||||
{
|
||||
role: "system",
|
||||
content: `Your job is to update prompt constructor functions. Here is the api shape for the current model:\n---\n${JSON.stringify(
|
||||
getApiShapeForModel(originalModel),
|
||||
null,
|
||||
2,
|
||||
)}`,
|
||||
},
|
||||
];
|
||||
if (newModel) {
|
||||
messages.push({
|
||||
role: "user",
|
||||
content: `Return the prompt constructor function for ${newModel} given the following prompt constructor function for ${originalModel}:\n---\n${originalVariant.constructFn}`,
|
||||
});
|
||||
}
|
||||
if (instructions) {
|
||||
messages.push({
|
||||
role: "user",
|
||||
content: `Follow these instructions: ${instructions}`,
|
||||
});
|
||||
}
|
||||
messages.push({
|
||||
role: "user",
|
||||
content:
|
||||
"The prompt variable has already been declared, so do not declare it again. Rewrite the entire prompt constructor function.",
|
||||
});
|
||||
const completion = await openai.chat.completions.create({
|
||||
model: "gpt-4",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: `Your job is to translate prompt constructor functions from ${originalModel} to ${newModel}. Here are is the api shape for the original model:\n---\n${JSON.stringify(
|
||||
getApiShapeForModel(originalModel),
|
||||
null,
|
||||
2,
|
||||
)}`,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: `Return the prompt constructor function for ${newModel} given the following prompt constructor function for ${originalModel}:\n---\n${originalVariant.constructFn}`,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: "The prompt variable has already been declared, so do not declare it again.",
|
||||
},
|
||||
],
|
||||
messages,
|
||||
functions: [
|
||||
{
|
||||
name: "translate_prompt_constructor_function",
|
||||
name: "update_prompt_constructor_function",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -75,7 +87,7 @@ const getPromptFunctionForNewModel = async (
|
||||
},
|
||||
],
|
||||
function_call: {
|
||||
name: "translate_prompt_constructor_function",
|
||||
name: "update_prompt_constructor_function",
|
||||
},
|
||||
});
|
||||
const argString = completion.choices[0]?.message?.function_call?.arguments || "{}";
|
||||
|
||||
Reference in New Issue
Block a user