The advent of large language models (LLMs) has significantly advanced the field of code translation, enabling automated translation between programming languages. However, these models often struggle with complex translation tasks due to inadequate contextual understanding. This paper introduces a novel approach that enhances code translation through Few-Shot Learning, augmented with retrieval-based techniques. By leveraging a repository of existing code translations, we dynamically retrieve the most relevant examples to guide the model in translating new code segments. Our method, based on Retrieval-Augmented Generation (RAG), substantially improves translation quality by providing contextual examples from which the model can learn in real-time. We selected RAG over traditional fine-tuning methods due to its ability to utilize existing codebases or a locally stored corpus of code, which allows for dynamic adaptation to diverse translation tasks without extensive retraining. Extensive experiments on diverse datasets with open LLM models such as Starcoder, Llama3-70B Instruct, CodeLlama-34B Instruct, Granite-34B Code Instruct, and Mixtral-8x22B, as well as commercial LLM models like GPT-3.5 Turbo and GPT-4o, demonstrate our approach's superiority over traditional zero-shot methods, especially in translating between Fortran and CPP. We also explored varying numbers of shots i.e. examples provided during inference, specifically 1, 2, and 3 shots and different embedding models for RAG, including Nomic-Embed, Starencoder, and CodeBERT, to assess the robustness and effectiveness of our approach.
翻译:大型语言模型(LLM)的出现显著推动了代码翻译领域的发展,实现了编程语言间的自动翻译。然而,由于上下文理解不足,这些模型在处理复杂翻译任务时常常面临困难。本文提出一种新颖方法,通过少样本学习并结合基于检索的技术来增强代码翻译。该方法利用现有代码翻译库,动态检索最相关的示例来指导模型翻译新的代码片段。我们基于检索增强生成(RAG)的方法,通过提供上下文示例使模型能够实时学习,从而显著提升了翻译质量。我们选择RAG而非传统的微调方法,是因为其能够利用现有代码库或本地存储的代码语料库,从而无需大量重新训练即可动态适应多样化的翻译任务。我们在多样化数据集上进行了大量实验,使用的开源LLM模型包括Starcoder、Llama3-70B Instruct、CodeLlama-34B Instruct、Granite-34B Code Instruct和Mixtral-8x22B,以及商用LLM模型如GPT-3.5 Turbo和GPT-4o。实验结果表明,我们的方法优于传统的零样本方法,尤其是在Fortran与CPP之间的翻译任务中。我们还探究了推理过程中提供的不同示例数量(即1、2和3样本)以及用于RAG的不同嵌入模型(包括Nomic-Embed、Starencoder和CodeBERT),以评估我们方法的鲁棒性和有效性。