Language Server Context Retrieval for Accurate LLM Code Generation
Find Innovative SolutionsGenerate Solutions
Solution Overview
Problem
Large language models (LLMs) face limitations in generating correct and syntactically formatted compilable computer code due to limited context provision, especially when dealing with proprietary or difficult-to-change code types, and providing relevant context without overwhelming inference costs.
Innovation Solution
A language server connected to an integrated Development Environment (IDE) identifies code artifacts within a file being edited, searches a code repository for relevant declarations, definitions, and references, and passes this context to the LLM for improved code generation.
Engineering Contradictions & Design Principles
Engineering Contradiction Analysis
1Reliability
If comprehensive contextual information from code repositories is provided to the LLM, then code generation accuracy and relevance are improved, but inference costs and processing time increase
Solution Approach 1:
The patent segments the codebase into modular units organized as a directed acyclic graph (DAG), where code artifacts are divided into discrete nodes representing individual functions, classes, or modules. This segmentation allows the system to retrieve only relevant code segments rather than processing entire codebases, reducing inference time while maintaining generation accuracy.
Solution Approach 2:
The system performs preliminary indexing and organization of code artifacts into a DAG structure before LLM inference occurs. Dependencies, imports, and relationships are pre-computed and stored in the graph structure, enabling rapid retrieval during code generation without requiring real-time analysis of the entire codebase.
2Reliability
If comprehensive contextual information from code repositories is provided to the LLM, then code generation accuracy and relevance are improved, but inference costs increase
Solution Approach 1:
The patent extracts only the essential contextual information needed for code generation by querying the DAG for specific ancestors and descendants of the target code artifact. This extraction approach retrieves minimal necessary context (e.g., parent classes, imported modules, dependent functions) rather than providing comprehensive codebase information, reducing inference costs while maintaining generation accuracy.
3Reliability
If the LLM is provided with more context about the codebase, then the reliability of code generation improves, but the complexity of the system increases
Solution Approach 1:
The patent introduces a language server as an intermediary component that mediates between the LLM and the codebase. The language server handles the complex tasks of parsing code, building the DAG structure, and querying dependencies, while the LLM receives simplified contextual information. This intermediary approach improves code generation reliability by ensuring accurate context retrieval while containing system complexity in a dedicated component.
4Adaptability or versatility
If proprietary or difficult-to-change code types are handled by the LLM, then code generation capability is improved, but error rates increase
Solution Approach 1:
The patent applies local quality by providing targeted contextual information specific to the proprietary or difficult-to-change code type being generated. The DAG structure enables retrieval of context locally relevant to the target artifact (e.g., specific framework conventions, project-specific patterns) rather than generic codebase information, improving output correctness for specialized code types while maintaining versatility.
Data Source
Figure 1
Figure 2
Figure 3
AI summary
In an example embodiment, a language server connected to an integrated Development Environment (IDE) is used to identify, from a given input code, various code artifacts, such as functions, variables, etc., and then to search a repository of code files for declarations, definitions, and references related to those identified code artifacts. The declarations, definitions, and references can then be passed as context into an LLM.