Store vector embeddings in JSON file to eliminate runtime regeneration #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements persistent storage of vector embeddings to eliminate the need to regenerate them on every server startup. Previously, the server would connect to Azure OpenAI and generate embeddings for all code samples each time it started, resulting in slow startup times (30-60 seconds) and unnecessary API costs.
Solution
The implementation moves embedding generation to build time using a new CLI tool, with embeddings stored in a JSON file that's included in the Docker container. The server no longer generates embeddings for example files at runtime - it only loads pre-generated embeddings.
New CLI Tool:
csla-embeddings-generatorA standalone console application that:
csla-examples/directory for.csand.mdfilesembeddings.jsonwith file content, vectors, and version metadataUsage:
Enhanced VectorStoreService
Added two new methods to support JSON persistence:
LoadEmbeddingsFromJsonAsync(string jsonFilePath)- Loads pre-generated embeddings from JSON fileExportEmbeddingsToJsonAsync(string jsonFilePath)- Exports current embeddings to JSON fileThe service maintains the same in-memory
Dictionary<string, DocumentEmbedding>structure, ensuring no changes to search performance or functionality.Updated Server Startup
The server now only loads embeddings from
embeddings.jsonon startup:If the embeddings file doesn't exist, the server displays a warning and semantic search is disabled (keyword search continues to work). The server does not generate embeddings for example files at runtime.
Integrated Build Process
The
build.shscript now:embeddings.jsonThe Dockerfile copies
embeddings.jsoninto the container at/app/embeddings.json.Benefits
Performance Impact
Before
After
Documentation
readme.mdwith vector embeddings sectioncsla-embeddings-generator/README.md- Usage and command-line optionsIMPLEMENTATION.md- Technical details and design overviewTESTING.md- Step-by-step testing and verification proceduresARCHITECTURE.md- Design decisions, workflows, and diagramsTesting
The implementation has been validated for code structure, JSON serialization/deserialization, build logic, and Dockerfile configuration. Full end-to-end testing requires .NET 10.0 SDK and Azure OpenAI credentials. See
TESTING.mdfor complete testing procedures.Server Behavior
Fixes #6
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.