Skip to content

Instantly share code, notes, and snippets.

@ishan-marikar
Forked from hrishioa/genRegexFromJSON.ts
Created March 25, 2024 06:26
Show Gist options
  • Save ishan-marikar/67080431b1963e0b084a4ecc34b759e6 to your computer and use it in GitHub Desktop.
Save ishan-marikar/67080431b1963e0b084a4ecc34b759e6 to your computer and use it in GitHub Desktop.
Generate Regexes for ReLLM from Typescript types
// Meant to work with the code in https://github.com/hrishioa/socrate, specifically the functions in the `gpt/base.ts`.
// Created by Hrishi Olickel (@hrishioa) Sub 7 May
import { Messages, askChatGPT } from '../base';
async function generateRegexFromTypeSpec(typeSpec: string): Promise<string | null> {
//prettier-ignore
const prompts = {
systemPrompt: () => `You are a Typescript type to Regex converter that can only output valid Regexes.`,
specPrompt: (spec: string) => `Convert the following TYPESCRIPT_TYPE into a valid Regex that matches the type.
TYPESCRIPT_TYPE:
\`\`\`typescript
${spec}
\`\`\``
};
// prettier-ignore
const fewShotExamples = [
{
spec: `type FixedAcronym = string; // Expanded acronym for ReLLM`,
regex: `Re[a-z]+ L[a-z]+ L[a-z]+ M[a-z]+`
},
{
spec: `type AlphabetLetters = string[]; // First three letters of the alphabet`,
regex: `["[a-z]", "[a-z]", "[a-z]"]`
},
{
spec: `type ValidDateString = string; // Date in the format dd/mm/yyyy`,
regex: `[0-9]{2}/[0-9]{2}/[0-9]{4}`
},
{
spec: `type AnimalProperties = {
animalName: string; // Name of an animal
canKillYou: boolean; // Can this animal kill you?
numberOfLegs: number; // How many legs does this animal have? 1-20.
}`,
regex: `{
"animalName": "[.*]",
"canKillYou": (true|false),
"numberOfLegs": \d+
}`
}
]
const messages: Messages = [
{
role: 'system',
content: prompts.systemPrompt()
}
];
for(let i=0;i<fewShotExamples.length;i++) {
messages.push({
role: 'user',
content: prompts.specPrompt(fewShotExamples[i].spec)
});
messages.push({
role: 'assistant',
content: fewShotExamples[i].regex
})
}
messages.push({
role: 'user',
content: typeSpec
})
const res = await askChatGPT(messages, 'gpt-3.5-turbo');
console.log('Got response ',res);
if(res.response.type === 'completeMessage')
return res.response.completeMessage;
else
return null;
}
const sampleTypeSpec = `type ProcessedAnswers = {
commercialConditions: string; // What conditions must be followed for commercial use? "" if there are no conditions.
downstreamChanges: string; // Does using code licensed under this license require any changes to the licensing of the derivative work?
persistent: boolean; // Should all derivative work that uses code licensed under this license, also be distributed under the same license?
viral: boolean; // Viral effect means that combining copyleft licensed work with a work licensed under a different license leads to the resulting work (an aggregate work) falling under the copyleft license. Is this license viral?
requirePublish: boolean; // Does this license require that the source code be published?
}`;
// Just for testing!
(async function() {
const regex = await generateRegexFromTypeSpec(sampleTypeSpec);
console.log('Got regex ', regex);
})().then(() => `\n\nCompleted.`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment