Generating C4 Diagrams with LLMs: An Overview and Future Directions
C4 diagrams provide a structured method for visualising complex software architectures by breaking down systems into containers, components, and contexts. Manually creating these diagrams can be time-consuming, but advancements in natural language processing (NLP) offer a path to automate this task.
Building on the concepts in the recent paper DiagrammerGPT, this article discusses an LLM-based solution that uses PlantUML and PlantUML C4 to generate C4 diagrams from textual descriptions.
System Overview
My solution leverages Google Gemma7B Large Language Model (LLM) for the tasks of designing and auditing.
The design and audit task are executed in a loop creating and iterative process that allows to refine entities and their relationships, and ultimately providing a diagram layout that accurately represents of the received caption — the description of the diagram to generate.
The generated design is then automatically converted to its PlantUML representation.
Designer
The designer is responsible for creating the diagram layout describing entities and relationships in a form that is tractable by the LLM. For this reason during the generation process entities and relationships are separated into clearly defined groups.
Inside each group, entities and relationships are defined using PlantUML and PlantUML C4 syntax.
The LLM is instructed using a few-shot prompt, which includes:
- Generation rules: Guidelines for creating C4 diagrams.
- Examples: A set of examples illustrating how the diagrams should be generated.
- Caption and feedback: The provided caption, and if applicable, feedback from the auditor on previously generated diagrams.
Auditor
The auditor is responsible for evaluating the generated list of entities and relationships for correctness.
Same as per the designer the diagram layout describing entities and relationships is provided in a form that is tractable by the LLM. For this reason during the audit process entities and relationships are separated into clearly defined groups.
During the audit process the LLM is instructed using few-shot prompt that includes:
- Generation rules: The same guidelines used by the generator.
- Examples: Both correct and incorrect diagram generations.
- Generated diagram and caption: The diagram produced by the generator and the original caption.
The auditor outputs a statement indicating whether the generated diagram is correct. If the diagram is lacking, feedback is given to the generator for further iterations.
Generated diagrams
Below are examples of the generated C4 diagrams.
Caption: generate a container diagram with a the following typescript services: a game rule service responsible for validating game actions, an account service responsible for managing user accounts which uses DynamoDB and connects to it via https, a game service responsible for maintaining the game status and that uses DynamoDB as a storage and a notification service responsible for sending notification via SNS software system. All services apart from notification service are exposed via AWS API Gateway. Account service and Game Service push notification to a queue based on AWS SNS while notification service reads from the same queue
Caption: generate a diagram composed by an api gateway responsible for the api management which exposes via https two services: payment service and cart service. the cart service is responsible for managing customers shopping cart is built using springboot and is connected to a postgresql db responsible for storing cart information, the payment service is responsible for orchestrating payments is implemented using typescript and is connected to stripe software system using https
Integration with RAG
As evolution for the system I am planning to integrate with Retrieval-Augmented Generation (RAG) to enhance the generator with specialised context.
By using RAG, the generator will access domain-specific knowledge base, enabling for more accurate and tailored diagrams.
Furthermore, RAG allows the system to extend its capabilities by offering textual assessments of how the generated diagrams align with accepted or standard patterns.
This feature enhances the auditor’s feedback, helping users understand the strengths and weaknesses of the generated diagrams in relation to best practices.
Benefits and Challenges
My system offers several benefits, including:
- Efficiency: Generating diagrams in a single step reduces the time needed to create visual representations of software architectures.
- Consistency: The use of LLMs ensures consistent application of diagram generation rules across different diagrams.
- Scalability: Automated diagram generation can handle large volumes of textual descriptions, making it scalable for complex projects.
- Specialised Knowledge: Integration with RAG enables access to specialised context for improved diagram accuracy.
However, there are challenges:
- Model Accuracy: Ensuring LLMs’ accuracy is essential to avoid errors in diagram generation and auditing.
- Iterative Process: Although the system can generate diagrams in a single step, multiple iterations may be needed to perfect results based on auditor feedback.
- Knowledge Base Management: Building and maintaining a robust knowledge base for RAG requires careful curation to ensure high-quality, up-to-date information.
Conclusion
My LLM-based system using PlantUML and Google Gemma7B demonstrates how modern NLP techniques can revolutionise the creation of C4 diagrams from textual descriptions.
By integrating my system with RAG, the accuracy and specialisation of diagram generation can be enhanced, as well as provide valuable textual assessments.
Despite the challenges, the benefits in terms of efficiency, consistency, and scalability make this approach a promising path for automating software architecture visualisation.
If you have any questions or would like to discuss my work further, please feel free to reach out!