Wednesday, March 6, 2024

Large Language Model Code Generation with DeepSeek 6.7B

There are two main input methods that I use from the Continue plugin (Installation Instructions from Part 1).

  • Getting a response back in the Continue plugin and maintaining context.
  • Use /edit and having the results displayed as a diff directly in the code.

 Let's take a look at a few examples. The final output for these are available on github. All examples were run from an M1 MacBook Pro with 32GB RAM running ollama and the DeepSeek 6.7B model.

Creating Code in the Continue Console

Simple Example

Command+L to start a new session and open the Continue prompt

 prompt:  create a pom file for a spring boot application with spring jdbc, spring mvc, and flyway support

This has some older version numbers, so I tried again with a new session

prompt: create a pom file for for java 21 for a spring boot 3.2.3 application with spring jdbc, spring mvc, and flyway support

Here is part of the result. The full file is available on github.

<dependency>
  <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
     <version>${spring-boot.version}</version>
     <dependency>
       <groupId>org.flywaydb</groupId>
        ⋮
     </dependency>
</dependency>

A bit odd that there is a dependency in a dependency. I'm going to try to find better prompts in the next blog post.

Creating more code from an existing context

 The prompt can be aware of previous prompts. Here is an example using SQL. Command+L to start a new prompt. (Until you start a new session, Continue will keep the current context.)

Step 1

prompt: Create flyway migration for postgreSQL to create an information table with a generated UUID primary key, a required 2000 character description, and required json data

CREATE TABLE information (
  id uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
  description VARCHAR(2000) NOT NULL,
  json_data JSONB NOT NULL
);

Step 2

prompt: Create a postgreSQL user table with a user name primary key and a required 2000 character full name

CREATE TABLE users (
  user_name VARCHAR(50) PRIMARY KEY NOT NULL, -- Username must be unique and not null
  full_name VARCHAR(2000) NOT NULL -- Full name is required
);

Step 3

prompt: Create a postgreSQL rating table with references to the previous two tables and a required integer rating

CREATE TABLE rating (
 id SERIAL PRIMARY KEY, -- Auto-incrementing integer ID
 user_name VARCHAR(50) NOT NULL REFERENCES users(user_name), -- References the 'users' table
 info_id uuid NOT NULL REFERENCES information(id), -- References the 'information' table
 rating INTEGER NOT NULL, -- The required integer rating
);

The prompt was still in the same context, so I could reference the previous two commands.

It did create the SQL with a surrogate primary key. Usually a good idea, but maybe not necessary here.

Step 4

prompt:  Same again, but use the two foreign keys as a compound primary key

This removed “id SERIAL PRIMARY KEY, -- Auto-incrementing integer ID

CREATE TABLE rating (
    user_name VARCHAR(50) NOT NULL REFERENCES users(user_name), -- References the 'users' table
    info_id uuid NOT NULL REFERENCES information(id), -- References the 'information' table
    rating INTEGER NOT NULL,  -- The required integer rating
    PRIMARY KEY (user_name, info_id) -- Makes user_name and info_id together a primary key
);

Creating code from existing code

Highlight the generated information table ddl from the previous section.

Command+Shift+L for a new prompt
prompt: Create a class using spring jdbc to insert and update

prompt:  And add delete

Adding delete works because the prompt has the original ddl in context as well as the previous prompt. 

public void deleteInformation(UUID id) {
    jdbcTemplate.update("DELETE FROM information WHERE id = ?", id.toString());
}

Creating code directly in the source

Adding code to a specific file

Create a file called UserService.java and highlight a blank line

Command+L (The first file selected is the default destination for the generated output)

Highlight the create users ddl (generated earlier)

Command+L

prompt: /edit Add method using namedJdbcTemplate to insert a user (the /edit has to be typed, the rest of the prompt can be pasted)

Command+Shift+Enter to save

Adding code to existing code

When adding code to directly to a file there is not as much context as creating code in Continue.

Highlight the addUser method

 Command+Shift+L (trying to keep the same context, but does not quite work)

prompt:  /edit add update method below addUser method

Command+Shift+Enter to save (Without the below clause, the update method will overwrite the add method) 

public void updateUser(String oldUserName, String newUserName, String newFullName) {
    String sql = "UPDATE users SET user_name=:newUsername, full_name=:newFullName WHERE user_name=:oldUsername";

    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("newUsername", newUserName);
    parameters.addValue("newFullName", newFullName);
    parameters.addValue("oldUsername", oldUserName);

    namedParameterJdbcTemplate.update(sql, parameters);

}

 

Hopefully this is enough to get started. Next I'm going to try to get better outputs from different prompts.

 A few more

I tried a few more prompts, listed below, so there are a few more files in github.

  • Highlight the rating table sql (Command+L). Create a class using namedjdbctemplate to insert and update.
  • What is the command to create a vite project for React and typescript?


 Conclusion

 These are two useful ways to generate code. Next I'm going to try to get better outputs from different prompts.