Abstract
Regarding software engineering (SE) tasks, Large language models (LLMs) have the capability of zero-shot learning, which does not require training or fine-tuning, unlike pre-trained models (PTMs). However, LLMs are primarily designed for natural language output, and cannot directly produce intermediate embeddings from source code. They also face some challenges, for example, the restricted context length may prevent them from handling larger inputs, limiting their applicability to many SE tasks; while hallucinations may occur when LLMs are applied to complex downstream tasks. Motivated by the above facts, we propose zsLLMCode, a novel approach that generates functional code embeddings using LLMs. Our approach utilizes LLMs to convert source code into concise summaries through zero-shot learning, which is then transformed into functional code embeddings using specialized embedding models. This unsupervised approach eliminates the need for training and addresses the issue of hallucinations encountered with LLMs. To the best of our knowledge, this is the first approach that combines LLMs and embedding models to generate code embeddings. We conducted experiments to evaluate the performance of our approach. The results demonstrate the effectiveness and superiority of our approach over state-of-the-art unsupervised methods.
Introduction
Representing source code as mathematical vectors—embeddings—is a fundamental challenge in software engineering (SE). High-quality embeddings must capture the functional semantics of code, allowing for tasks like code clone detection and clustering. Traditionally, this has required a trade-off between lightweight unsupervised models that lack deep semantic understanding and expensive, fine-tuned Large Language Models (LLMs).
While LLMs excel at zero-shot reasoning, they face significant hurdles in SE tasks: restricted context windows, high computational costs for pairwise comparisons, and potential hallucinations in complex logic. We propose zsLLMCode, a novel framework that bridges this gap by decoupling semantic extraction from vectorization.
Methodology: The zsLLMCode Pipeline
The core innovation of zsLLMCode is a "late interaction" approach that uses LLMs as semantic filters. The process consists of four distinct stages:
- Uniform Prompt Design: A standardized prompt instructs the LLM to summarize a code fragment into a single, concise sentence focusing on its functional purpose rather than its syntax.
- Summarization and Storage: The LLM generates a natural language summary, effectively "denoising" the code by removing syntactic variations while preserving intent.
- Functional Code Embedding Generation: These summaries are passed through a specialized, lightweight sentence embedding model (e.g., MiniLM or SBERT) to produce the final vectors.
- Downstream Tasks: The resulting embeddings are stored in a vector database for efficient similarity analysis, enabling tasks like clone detection and clustering via standard distance metrics.
Efficiency and Scalability
A major advantage of zsLLMCode is its computational efficiency. Standard LLM-based clone detection requires comparing every possible pair in a dataset, leading to $O(n^2)$ LLM calls. In contrast, zsLLMCode only requires $n$ LLM calls—one for each code fragment—to generate the initial summaries. Once summarized and embedded, comparisons are performed using fast vector mathematics, making the system highly scalable for large repositories.
Experimental Evaluation
We evaluated zsLLMCode on three major benchmarks: POJ-104 (C code), OJClone C (pairwise clones), and BigCloneBench (Java).
Key Results
- Clone Detection: On the OJClone C dataset, a configuration using GPT-3.5 and MiniLM achieved an F1 score of 91.82%, significantly outperforming the unsupervised state-of-the-art TransformCode (67.10%).
- Code Clustering: The approach reached an Adjusted Rand Index (ARI) of 0.97 on clustering tasks, indicating near-perfect alignment with ground-truth categories.
- Cross-Language Generalization: The framework proved effective across both C and Java, demonstrating that the "Summarize-then-Embed" logic is language-agnostic.
Conclusion and Future Work
zsLLMCode demonstrates that combining the zero-shot reasoning of LLMs with specialized sentence encoders can surpass the performance of models specifically trained for code tasks. Future research will focus on incorporating structural information, such as Function Call Graphs, to further enhance the LLM's understanding of complex program interactions and exploring the fine-tuning of sentence models on technical code summaries.
Summary updated