Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Code Prompts, a Prompt Format for AI-Powered Code Auto-Completion #58024

Open
DanielRosenwasser opened this issue Apr 1, 2024 · 7 comments
Labels
Experiment A fork with an experimental idea which might not make it into master In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

A Prompt Format For AI-Powered Code Auto-Completion

A few years ago, I invented GitHub Copilot Chat and ChatGPT. While this was really hard to think of, I think it turned out really really good. On the other hand, using a chat bot to write code is kind of annoying sometimes - so maybe sometimes it's bad. That's why a lot of the time, I prefer Copilot's inline completions more. I know, nobody else does, but I do. I think.

The last paragraph being completed

The problem is that language models don't always know what code is valid. As a result, developers often need to review the code and find all the issues. While pointing out the mistakes of an AI helps bolster our self-esteem, we developers could be doing so much more. We need to improve the success rate of these language models to make them effective.

One key insight is that developers want to stop being software engineers, and instead become prompt engineers. Developers are infamously sick of learning new libraries, techniques and frameworks. They want only to be concerned with planning, processes, and the business bottom line. They want deeply to find the best way to tell an AI how to do everything so they can achieve this.

That's why today I'm proposing a new format to aid auto-completion tools: code prompts.

"Code Prompts?"

Yes, is there an echo in here?

Code prompts are a succinct declarative format to guide AI language models to write valid code. Rather than guessing based on surrounding code context, code prompts add rich information to prime a language model for inline completions.

For example, consider a variable named person. Imagine it has a name and an age property, along with a method called syncToDatabase(). As a developer, I want the AI to increment the age property and sync that to a database. Note that syncToDatabase is an asynchronous operation, and we want any subsequent code to be aware of this.

Traditionally, describing this to a language model was verbose and cumbersome. In fact, I basically just did to you. Here's what it would look like in code.

// consider a variable named `person`. Imagine it has a `name` and an `age` property
// along with a method called `syncToDatabase()`. As a developer, I want the AI to
// increment the age property and sync that to a database. Note that `syncToDatabase` is an
// asynchronous operation, and we want any subsequent code to be aware of this.
// Also, name is a string and age is a number.

This is super duper annoying to read, and at least twice as annoying to write (especially since I just wrote it twice).

Code prompts help dramatically here. Before we start off, I want you to be aware of how easy it is to add code prompts to an existing prompt.

First, let's start small.

// Imagine `person` has a `name` and an `age` property
// along with a method called `syncToDatabase()`. As a developer, I want the AI to
// increment the age property and sync that to a database. Note that `syncToDatabase` is an
// asynchronous operation, and we want any subsequent code to be aware of this.
// Also, name is a string and age is a number.

let person;

There's a big change there. Did you catch it?

- // consider a variable named `person`. Imagine it has a `name` and an `age` property
+ // Imagine `person` has a `name` and an `age` property
  // along with a method called `syncToDatabase()`. As a developer, I want the AI to
  // increment the age property and sync that to a database. Note that `syncToDatabase` is an
  // asynchronous operation, and we want any subsequent code to be aware of this.
  // Also, name is a string and age is a number.
  
+ let person;

Notice that we were able to incrementally add code prompts to our existing prompt. They can live side-by-side. It's not a completely different language, it's just a superset.

Plus, we really shaved off a lot of characters from our prompt. This is extra good for language models with small token windows. So let's keep going! Can we shorten the description of the properties and methods on person?

// As a developer, I want the AI to
// increment the age property and sync that to a database. Note that `syncToDatabase` is an
// asynchronous operation, and we want any subsequent code to be aware of this.
// Also, name is a string and age is a number.

let person: {
    name;
    age;
    syncToDatabase();
};

At this point, you might be satisfied, but we can go even further. You see, even our code prompt has information about the member names of person, we had to use lengthy comment prompts to describe those members. We can go deeper!

// As a developer, I want the AI to
// increment the age property and sync that to a database.

let person: {
    name: string;
    age: number;
    syncToDatabase(): Promise<void>;
};

This prompt is practically perfection. Who knows what the AI will do with the code prompts we've provided it? I don't, but I'm excited to find out!

person.age++;
person.syncToDatabase().then(() => {
    console.log('Synced to database');
});

Single quotes? Gross.

Fine-Grained Completions

Occasionally, inline completions might be a bit too aggressive. Occasionally, as you program, you might find it valuable to explicitly request completions on the next token. This is where fine-grained completions come in.

Rather than completing the entire line, you can request completions for just the next token. This is especially useful when you want to guide the AI to complete your code in a particular way. The UI for this is extremely lightweight.

A completion list for person.

This feature brings us the Intelligence of AI, but allows us to complete using our own discretion and common Sense.

Obviously, it should be named "SensiGence".

Checking for Validity

One of the key benefits of code prompts is that they can be used to check the validity of code completions. By providing a code prompt, you can ensure that the AI-generated code adheres to the constraints you've set. This is especially useful when you want to ensure that the AI doesn't generate code that violates your business logic or architectural patterns.

In short: what if something could check what the AI typed?

Error that person is used before being assigned.

This would be a game changer. The biggest question is how we'll be able to train a model of this complexity. If I can raise $10 trillion to procure enough GPU power, then I think it's within reach. So feel free to sponsor me on GitHub!

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus Experiment A fork with an experimental idea which might not make it into master labels Apr 1, 2024
@DanielRosenwasser DanielRosenwasser changed the title RFC: Code Prompts, a Prompt Format for AI-Powered Code Auto-Completion [RFC] Code Prompts, a Prompt Format for AI-Powered Code Auto-Completion Apr 1, 2024
@pgodman
Copy link

pgodman commented Apr 1, 2024

Approved. Begin implementation immediately, mortal.

@jits
Copy link

jits commented Apr 1, 2024

Yes, but, is it powered by The Blockchain??

@rubiesonthesky
Copy link

rubiesonthesky commented Apr 1, 2024

Just add tea.yml file to the repo and you will get the money in no time.

@DanielRosenwasser
Copy link
Member Author

@jits see #48513

@jits
Copy link

jits commented Apr 1, 2024

@DanielRosenwasser — ahh yes, that was a classic!

@mrT23
Copy link

mrT23 commented Apr 2, 2024

It does sound very reasonable that if you want to generate code, than your prompt will be more ... code-like 💪

just want to mention that someone really smart (not sure who) suggested using Pydantic class and its equivalence to YAML format to guide LLMs in code generation.
https://arxiv.org/pdf/2401.08500.pdf

This method has most of the advantages you mentioned above, and maybe some extra (or not ? ), since YAML format is very suitable for code generation.
This is not just an academic work, but actually used in practice, for example
https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/pr_code_suggestions_prompts.toml

image

image

@GorvGoyl
Copy link

GorvGoyl commented Apr 4, 2024

petition to rename typescript to aiscript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experiment A fork with an experimental idea which might not make it into master In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants