Creation of a code generation tool using Rust and local LLMs from Ollama

This is a reaction to the release of ChatGPT o-preview. An attempt to add logic to open-source LLMs that can be run at home on a modest GPU or even on a CPU

`

This is a reaction to the release of ChatGPT o1-preview. An attempt to add logic to open-source LLMs that can be run at home on a modest GPU or even on a CPU

Currently, I am working on a Rust-based tool that automates the generation, compilation, and testing of code using large language models (LLM). The idea is to interact with the LLM to generate code based on user-provided explanations, compile the code, resolve dependencies, and run tests to ensure everything works as expected.

I would like to optimize the process of coding functions based on natural language descriptions. I wanted to create a system where I could input an explanation of what the function should do, and the tool would handle everything else from code generation to testing.

The tool's work starts by asking the user for an explanation of the function they want to create. Then it interacts with the LLM to generate the function's code, compiles it, and checks for compilation errors. If errors are found, the tool attempts to fix them, possibly by adding dependencies or rewriting the code. After successfully compiling the code, it generates tests for the function, runs them, and handles any errors again, iteratively improving the code or tests.

The first step is to get an explanation from the user:

println!("Explain what the function should do:");
let mut explanation = String::new();
std::io::stdin().read_line(&mut explanation).unwrap(

Using this explanation, the tool creates a request to send to the LLM:

let generate_code_prompt = construct_prompt(
    generate_code_prompt_template,
    vec![&explanation],
);

Here is `generate_code_prompt_template`:

let generate_code_prompt_template = r#"
{{{0}}}

Write on Rust language code of this function (without example of usage like main function):
``rust
fn solution(
"#

								    

							    
Comments