Local LLM Deployment in Practice: Integrating AI into Spreadsheets
Deploying large language models locally offers data privacy, cost efficiency, and control, but integrating them into daily workflows remains a challenge for many users. This article explores a practical implementation of local LLMs by integrating them into spreadsheet software as an on-demand Q&A assistant. It covers the complete workflow from architecture design and environment preparation to API configuration and VBA scripting, providing a replicable pattern for automating office tasks with private AI capabilities.
1. Why Deploy LLMs Locally
Deploying large language models locally provides three core benefits: data sovereignty, zero marginal cost, and a controllable response chain. For office scenarios involving sensitive information such as internal documents and business data, local inference avoids the compliance risks associated with uploading data to external services. Additionally, once deployed, model calls do not incur per-use fees, making them ideal for high-frequency, lightweight daily tasks.
However, many users stop after completing the deployment—the model is running, but they do not know how to integrate it with actual work. This article uses a representative scenario as an example: integrating a local model into spreadsheet software to create an on-demand office assistant. This scenario covers the complete workflow of “environment preparation—service exposure—application integration—result parsing.” Once mastered, this approach can be adapted and migrated to other scenarios such as document processing and email assistants.
2. Overall Architecture Design
The entire solution consists of four layers:
- Inference Layer: The large language model running locally, responsible for actual thinking and generation.
- Service Layer: The model runtime framework exposes the inference capabilities as a local HTTP interface, typically listening on a local port.
- Enhancement Layer (Optional): Attaches private documents to the model via a knowledge base Q&A framework to implement Retrieval-Augmented Generation (RAG), allowing answers to be based on your own materials.
- Application Layer: The spreadsheet software initiates HTTP requests using its built-in programming capabilities, sending user questions to the interface and writing the returned content back to cells.
The benefit of this layering is decoupling: changing the model only affects the inference layer, changing the frontend only affects the application layer, and the intermediate interface protocol remains unchanged.
3. Environment Preparation Checklist
Before starting, please confirm the following conditions:
- Hardware: Model inference is sensitive to VRAM/RAM. Small-parameter models (e.g., 7B-level quantized versions) can run smoothly on consumer-grade GPUs; the larger the parameters, the more VRAM is required. It is recommended to choose an appropriate quantized version based on your local hardware configuration, rather than blindly pursuing large-parameter models.
- Inference Runtime: Install a runtime framework that supports local model hosting. It handles model download, loading, and provides inference services via a unified local API. These tools usually offer a single command to start the model.
- Model Files: Download the required open-source model weights from the official model repository, ensuring you select the parameter scale and quantization level that match your local hardware.
- Knowledge Base Framework (Optional): If you want the model to answer questions based on private documents, you can deploy an open-source local knowledge base Q&A framework and register the aforementioned inference service as its backend.
- Spreadsheet Software: A desktop spreadsheet software that supports macro (VBA) programming is required for writing the call scripts.
4. Key Configuration Steps
4.1 Deploy and Verify the Model Service
After installing the inference framework, pull and run the model via the command line. After startup, it is recommended to perform a quick verification:
curl http://localhost:11434/api/generate -d '{"model":"your_model_name","prompt":"hello"}'
If it returns the generated content normally, the inference layer is ready. Record the service address and port, as they will be needed for subsequent configurations.
4.2 Configure the Knowledge Base Framework (Optional)
In the model provider settings of the knowledge base framework, enter the interface address of the local inference service (e.g., http://host.docker.internal:11434 or http://localhost:11434), select the deployed model, and create a workspace. It is recommended to use lowercase letters for the workspace name to avoid case ambiguity issues in URLs.
Next, create an API key in the framework—the application layer will use it for authentication.
4.3 Write the Call Script in Spreadsheet Software
Open the VBA editor in your spreadsheet software (shortcut Alt + F11), insert a module, and write the following code:
Sub CallLocalLLM()Dim question As String, response As StringDim url As String, apiKey As StringDim http As Object' Read the user's question from cell A2question = ThisWorkbook.Sheets(1).Range("A2").Value' Chat API URL of the local knowledge base framework, workspace name must be lowercaseurl = "http://localhost:3001/api/v1/workspace/your_workspace_name/stream-chat"apiKey = "your_api_key"Set http = CreateObject("MSXML2.XMLHTTP")http.Open "POST", url, Falsehttp.setRequestHeader "accept", "text/event-stream"http.setRequestHeader "Content-Type", "application/json"http.setRequestHeader "Authorization", "Bearer " & apiKey' Assemble the request bodyDim body As Stringbody = "{""message"":""" & question & """,""mode"":""chat""}"http.send body' Handle the responseIf http.Status = 200 Thenresponse = http.responseTextThisWorkbook.Sheets(1).Range("A4").Value = ParseNestedJSON(response)ElseMsgBox "Request failed, status code: " & http.StatusEnd IfEnd Sub
4.4 Write the JSON Parsing Function
Since the interface returns streaming text (SSE format) with nested JSON segments, you need to extract the final answer:
Function ParseNestedJSON(ByVal raw As String) As StringDim parts() As String, i As LongDim pos As Long, jsonLine As Stringparts = Split(raw, vbLf)For i = LBound(parts) To UBound(parts)' Skip empty and non-data lines, extract lines containing text fieldsIf InStr(parts(i), """content""") > 0 ThenjsonLine = Mid(parts(i), InStr(parts(i), "{"))' Simple extraction of the content field value (for production, a JSON parsing library is recommended)Dim keyPos As Long, valStart As Long, valEnd As LongkeyPos = InStr(jsonLine, """content"":""")If keyPos > 0 ThenvalStart = keyPos + Len("""content"":""")valEnd = InStr(valStart, jsonLine & """", """")ParseNestedJSON = ParseNestedJSON & Mid(jsonLine, valStart, valEnd - valStart)End IfEnd IfNext iEnd Function
Note: The parsing in the example is a simplified implementation. Actual SSE responses contain both thought process fragments and final answer fragments, which can be distinguished as needed. If high robustness is required, it is recommended to introduce a standard JSON parsing library in VBA to handle escape characters.
5. UI Design and User Experience
Make a simple layout in the worksheet:
- Cell A2: Enter the question;
- Button: Insert a shape or form button and bind it to the
CallLocalLLMmacro; - Cell A4: Display the model’s thought process and final answer.
When in use, simply enter a question in A2 (e.g., “Help me summarize the sales data in column B by region and write a brief analysis”), click the button, and the result will appear in A4 after a few seconds. With the knowledge base framework mounting internal documents, you can also directly ask questions like “What are the acceptance criteria in the contract template?” to achieve Q&A based on private materials.
6. Common Issues and Optimization Suggestions
- Request Timeout: During the first call, the model requires a cold load, which takes longer. You can adjust the asynchronous parameters and timeout strategy of
http.Open, or send a warm-up request first. - Special Character Errors: If the question text contains English double quotes, it will break the JSON structure. You should escape or replace the input accordingly.
- Model Selection: Small models are sufficient for daily Q&A; switch to large-parameter models for complex reasoning. The higher the quantization level (higher value), the lower the footprint but slightly reduced precision—this requires a trade-off.
- Security Tip: Although the local interface only listens to the localhost, do not hardcode API keys in shared files. Instead, read them from environment variables or configuration files.
7. Conclusion
The true value of deploying large language models locally lies in “welding” them into daily workflows. The spreadsheet integration solution demonstrated in this article is essentially a universal integration paradigm: local inference service → API exposure → office software macro call → result writeback. Once you master this chain, you can further extend it to scenarios such as automated weekly report generation, data cleaning, and batch document summarization, allowing privately deployed model capabilities to continuously translate into actual productivity.
