
Share
This article explores how large language models can be integrated as dynamic functions to generate tailored code, offering a glimpse into the future of programming and automation.
Language models (LLMs) have become incredibly versatile, extending beyond their traditional role in text generation. One fascinating application is using LLMs to implement functions within a program-what I call "LLM As A Function." This approach leverages the power of language models to generate code dynamically based on specific inputs and constraints.
Let's consider a practical example where you want to build a website builder that uses only components from your design system. Here’s how you can achieve this using an LLM:
const prompt = 'Build a chat app UI';
// Step 1: Determine the required components
const components = llm<Array<string>>(`
You only have the following components: ${designSystem.getAllExistingComponents().join(', ')}\n
What components do you need to build the following:\n
${prompt}
`);
// Output: ['List', 'Card', 'ProfilePicture', 'TextInput']
// Step 2: Generate the code for these components
const result = llm<{javascript: string, css: string}>(`
You only have the following components: ${components.join(',')}\n
Here are examples of how to use them:\n
${components.map(component => designSystem.getExamplesForComponent(component).join('\n')).join('\n')}\n
Write code for making the following:\n
${prompt}
`);
// Output: { javascript: '...', css: '...' }
The llm<Type>(prompt: string): Type function is key here. It takes a type definition and a prompt, then returns a value of that type. Here’s a breakdown of the process:
{javascript: string, css: string}.{javascript: string, css: string}, the augmented prompt might look like:
You need to respond using JSON that looks like {"javascript": "...", "css": "..."}

To implement this approach, you need:
Here’s a more detailed look at how the llm function might be implemented:
function llm<Type>(prompt: string): Type {
// Convert type to JSON example
const typeExample = convertTypeToJsonExample(Type);
// Augment prompt with type information
const augmentedPrompt = `${typeExample}\n${prompt}`;
// Call the LLM API
const response = callLlmApi(augmentedPrompt);
// Parse the response and return it as the specified type
return JSON.parse(response) as Type;
}
function convertTypeToJsonExample(Type: any): string {
// Example implementation for simple types
if (Type === Array<string>) {
return 'You need to respond using a JSON array of strings like ["item1", "item2"]';
} else if (Type === {javascript: string, css: string}) {
return 'You need to respond using JSON that looks like {"javascript": "...", "css": "..."}';
}
// Add more type handling as
Tags
Original Sources
About the author
Kai built ML infrastructure at a Bay Area startup before developing an obsession with transformer architectures and inference optimisation that eventually pulled him out of product work entirely. A stint at a compute research lab sharpened his instinct for what actually matters in a model release versus what is marketing. He writes from the inside — from the perspective of someone who has debugged the systems he is describing at three in the morning. He is allergic to hype and instinctively drawn to the unglamorous plumbing questions that everyone else skips over.
More from The Engineer →This Week's Edition
5 December 2023
133 articles
Related Articles
Related Articles
More Stories