Generating unit tests from source code using large language models, vector databases, and prompt engineering
Annamaa, Jouni (2024-03-15)
Annamaa, Jouni
J. Annamaa
15.03.2024
© 2024, Jouni Annamaa. Tämä Kohde on tekijänoikeuden ja/tai lähioikeuksien suojaama. Voit käyttää Kohdetta käyttöösi sovellettavan tekijänoikeutta ja lähioikeuksia koskevan lainsäädännön sallimilla tavoilla. Muunlaista käyttöä varten tarvitset oikeudenhaltijoiden luvan.
Julkaisun pysyvä osoite on
https://urn.fi/URN:NBN:fi:oulu-202403152254
https://urn.fi/URN:NBN:fi:oulu-202403152254
Tiivistelmä
In a fast-evolving artificial intelligence (AI) space, new use cases for AI models are constantly researched. One use case is implementing a model that could help programmers to make unit tests for their code.
This thesis analyzes five open-source large language models (LLMs) and their capability to generate unit tests from source code. For analyzing these LLMs, a prototype was made. The implemented prototype takes a prompt from the user and data from vector database as input and generates an answer with an LLM. This thesis also analyzes prompt engineering techniques and their effectiveness in generating the output, as well as the use of vector database as means of providing additional context for the LLMs and how it affects the results. Further analysis shows the output of the best performing open-source LLM in comparison with ChatGPT 3.5, with the analysis focusing on how many iterations the desired output took to make and how much of the code the unit tests cover. Finally, the best performing model and generative pre-trained transformers 3.5 (GPT-3.5) are compared to a programmer at a general level in terms of relative time used for making the unit tests.
The resulting prototype and the analysis of the five open-source LLMs show how the additional vector database helps with the generation of unit tests, especially with a model that was trained with a coding focused dataset. The vector database gave enough information for the model to make unit tests covering 84.9% in 44 iterations in the C program and 86.9% in 33 iterations with the C++ program. To achieve 100% coverage, the model would have been required to make mocks for some methods. With both programs, the unit tests followed the example unit test in the vector database in terms of quality.
Comparing the results of the best performing model to ChatGPT 3.5, the latter achieved 100% coverage for both programs. The tests made by GPT-3.5 were simple, with useful comments in the tests. The biggest differences between GPT-3.5 and Falcoder were the mocks GPT-3.5 managed to make and the ability to debug and fix the mistakes it made.
Overall, the additional vector database enhances the ability of an LLM to make unit tests. The resulting prototype shows promising results on using LLMs for making basic unit tests. The prototype with the open-source model does not achieve the same quality as the OpenAI's GPT-3.5. Viime aikoina tekoälyn kehitys on ollut nopeaa. Uusia käyttötarkoituksia sekä käyttökohteita etsitään ja tutkitaan jatkuvasti. Yksi tällainen käyttötarkoitus on yksikkötestien luominen lähdekoodista tekoälymallin avulla.
Tässä työssä tutkitaan viittä avoimen lähdekoodin suurta kielimallia ja analysoidaan niiden luomia yksikkötestejä lähdekoodin pohjalta. Analyysiä varten luotiin prototyyppi, joka hyödyntää käyttäjän syötettä sekä vektoritietokannasta saatuja tietoja yksikkötestien luomisessa. Työssä myös tutkitaan erilaisia kehotesuunnittelun tekniikoita ja analysoidaan niiden tehokkuutta kielimallin vastauksissa. Vektoritietokantaa hyödynnetään kontekstin laajentamisessa ja analyysi kohdistuu vektoritietokannan vaikutukseen kielimallien luomissa vastauksissa. Lisäksi työssä vertaillaan parhaiten suoriutuvan kielimallin vastauksia kaupalliseen ChatGPT 3.5-malliin tutkimalla, kuinka monta iteraatiota vaadittiin haluttuihin yksikkötesteihin sekä kuinka laaja testikattavuus saavutettiin. Työssä myös vertaillaan tutkittuja malleja ohjelmoijiin yleisellä tasolla yksikkötestien luonnissa.
Valmis prototyppi osoittaa, miten vektoritietokannoista saatu tieto auttaa kielimalleja yksikkötestien luonnissa. Vektoritietokantoja hyödynsi erityisesti kielimalli, joka oli koulutettu ohjelmointia varten. Vektoritietokanta antoi tarpeeksi tietoa mallille ja malli pystyi luomaan yksikkötestejä C-ohjelmalle, jotka kattavat 84,9 % lähdekoodista 44 iteraatiolla. C++-ohjelmalle avoimen lähdekoodin malli pystyi luomaan testejä, jotka kattavat 86,9 % lähdekoodista 33 iteraatiolla. Luodut yksikkötestit mukailivat vektoritietokannassa olevan esimerkkiyksikkötestin laatua. Jotta avoimen lähdekoodin malli olisi saanut luotua 100 % testikattavuuden, sen olisi pitänyt luoda koodia, joka jäljittelee lähdekoodissa käytettyjä metodeja (englanniksi "mock").
Kun generatiivinen esikoulutettu muuntaja 3.5 (englanniksi "generative pre-trained transformers") (GPT-3.5) loi yksikkötestejä, se saavutti 100 % testikattavuuden molemmissa ohjelmissa. GPT-3.5:n luomat testit erosivat avoimen lähdekoodin mallin testeistä siten, että GPT-3.5:n testeissä oli selventäviä kommentteja, joiden avulla se pystyi korjaamaan virheitään ja luomaan testeihin metodeja, jotka mukailevat oikeaa koodia.
Kaiken kaikkiaan osoittautui, että vektoritietokannat auttavat kielimalleja yksikkötestien luonnissa. Työssä tehty prototyyppi avoimen lähdekoodin malleilla toimii yksinkertaisten yksikkötestien luonnissa, mutta ei saavuta samaa tasoa kuin GPT-3.5.
This thesis analyzes five open-source large language models (LLMs) and their capability to generate unit tests from source code. For analyzing these LLMs, a prototype was made. The implemented prototype takes a prompt from the user and data from vector database as input and generates an answer with an LLM. This thesis also analyzes prompt engineering techniques and their effectiveness in generating the output, as well as the use of vector database as means of providing additional context for the LLMs and how it affects the results. Further analysis shows the output of the best performing open-source LLM in comparison with ChatGPT 3.5, with the analysis focusing on how many iterations the desired output took to make and how much of the code the unit tests cover. Finally, the best performing model and generative pre-trained transformers 3.5 (GPT-3.5) are compared to a programmer at a general level in terms of relative time used for making the unit tests.
The resulting prototype and the analysis of the five open-source LLMs show how the additional vector database helps with the generation of unit tests, especially with a model that was trained with a coding focused dataset. The vector database gave enough information for the model to make unit tests covering 84.9% in 44 iterations in the C program and 86.9% in 33 iterations with the C++ program. To achieve 100% coverage, the model would have been required to make mocks for some methods. With both programs, the unit tests followed the example unit test in the vector database in terms of quality.
Comparing the results of the best performing model to ChatGPT 3.5, the latter achieved 100% coverage for both programs. The tests made by GPT-3.5 were simple, with useful comments in the tests. The biggest differences between GPT-3.5 and Falcoder were the mocks GPT-3.5 managed to make and the ability to debug and fix the mistakes it made.
Overall, the additional vector database enhances the ability of an LLM to make unit tests. The resulting prototype shows promising results on using LLMs for making basic unit tests. The prototype with the open-source model does not achieve the same quality as the OpenAI's GPT-3.5.
Tässä työssä tutkitaan viittä avoimen lähdekoodin suurta kielimallia ja analysoidaan niiden luomia yksikkötestejä lähdekoodin pohjalta. Analyysiä varten luotiin prototyyppi, joka hyödyntää käyttäjän syötettä sekä vektoritietokannasta saatuja tietoja yksikkötestien luomisessa. Työssä myös tutkitaan erilaisia kehotesuunnittelun tekniikoita ja analysoidaan niiden tehokkuutta kielimallin vastauksissa. Vektoritietokantaa hyödynnetään kontekstin laajentamisessa ja analyysi kohdistuu vektoritietokannan vaikutukseen kielimallien luomissa vastauksissa. Lisäksi työssä vertaillaan parhaiten suoriutuvan kielimallin vastauksia kaupalliseen ChatGPT 3.5-malliin tutkimalla, kuinka monta iteraatiota vaadittiin haluttuihin yksikkötesteihin sekä kuinka laaja testikattavuus saavutettiin. Työssä myös vertaillaan tutkittuja malleja ohjelmoijiin yleisellä tasolla yksikkötestien luonnissa.
Valmis prototyppi osoittaa, miten vektoritietokannoista saatu tieto auttaa kielimalleja yksikkötestien luonnissa. Vektoritietokantoja hyödynsi erityisesti kielimalli, joka oli koulutettu ohjelmointia varten. Vektoritietokanta antoi tarpeeksi tietoa mallille ja malli pystyi luomaan yksikkötestejä C-ohjelmalle, jotka kattavat 84,9 % lähdekoodista 44 iteraatiolla. C++-ohjelmalle avoimen lähdekoodin malli pystyi luomaan testejä, jotka kattavat 86,9 % lähdekoodista 33 iteraatiolla. Luodut yksikkötestit mukailivat vektoritietokannassa olevan esimerkkiyksikkötestin laatua. Jotta avoimen lähdekoodin malli olisi saanut luotua 100 % testikattavuuden, sen olisi pitänyt luoda koodia, joka jäljittelee lähdekoodissa käytettyjä metodeja (englanniksi "mock").
Kun generatiivinen esikoulutettu muuntaja 3.5 (englanniksi "generative pre-trained transformers") (GPT-3.5) loi yksikkötestejä, se saavutti 100 % testikattavuuden molemmissa ohjelmissa. GPT-3.5:n luomat testit erosivat avoimen lähdekoodin mallin testeistä siten, että GPT-3.5:n testeissä oli selventäviä kommentteja, joiden avulla se pystyi korjaamaan virheitään ja luomaan testeihin metodeja, jotka mukailevat oikeaa koodia.
Kaiken kaikkiaan osoittautui, että vektoritietokannat auttavat kielimalleja yksikkötestien luonnissa. Työssä tehty prototyyppi avoimen lähdekoodin malleilla toimii yksinkertaisten yksikkötestien luonnissa, mutta ei saavuta samaa tasoa kuin GPT-3.5.
Kokoelmat
- Avoin saatavuus [37887]