Code Converter
In this example we can demonstrate how to turn a language model's coding capabilities into an API that can covert between two programming languages. Here we'll translate Handlebars templates into React JSX components.
hbs2react.js
// give the model a system prompt to set the stage
prompt('You are an expert react and handlebars developer, your specialty is migrating from handlebars to react.\n',
{ role: 'system', hidden: true });
// allow the API to accept an abitrary handlebars template,
// or default to a simple one if none is provided
const hbs = request.params.handlebars || `<div class="entry">\n{{#if author}}\n<h1>{{firstName}} {{lastName}}</h1>\n{{/if}}\n</div>\n`;
// ask the model to convert the code for us
prompt(
'Convert this handlebars code to a react component:\n\n' +
'```handlebars\n' +
`${hbs}\n` +
'```',
{role: 'user', hidden: true}
);
// prefill the assistant's response with a code fence so it immediately starts
// generating code
prompt('```jsx', {role: 'assistant', hidden: true});
// generate the response
gen({limit:128000, stop_at: '```' });
output.txt
import React from 'react';
const Entry = ({ author }) => {
if (author) {
return (
<div className="entry">
<h1>{author.firstName} {author.lastName}</h1>
</div>
);
} else {
return null;
}
};
export default Entry;