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:

    1. Inference Layer: The large language model running locally, responsible for actual thinking and generation.
    2. Service Layer: The model runtime framework exposes the inference capabilities as a local HTTP interface, typically listening on a local port.
    3. 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.
    4. 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:

    1. 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:

    1. Sub CallLocalLLM()
    2. Dim question As String, response As String
    3. Dim url As String, apiKey As String
    4. Dim http As Object
    5. ' Read the user's question from cell A2
    6. question = ThisWorkbook.Sheets(1).Range("A2").Value
    7. ' Chat API URL of the local knowledge base framework, workspace name must be lowercase
    8. url = "http://localhost:3001/api/v1/workspace/your_workspace_name/stream-chat"
    9. apiKey = "your_api_key"
    10. Set http = CreateObject("MSXML2.XMLHTTP")
    11. http.Open "POST", url, False
    12. http.setRequestHeader "accept", "text/event-stream"
    13. http.setRequestHeader "Content-Type", "application/json"
    14. http.setRequestHeader "Authorization", "Bearer " & apiKey
    15. ' Assemble the request body
    16. Dim body As String
    17. body = "{""message"":""" & question & """,""mode"":""chat""}"
    18. http.send body
    19. ' Handle the response
    20. If http.Status = 200 Then
    21. response = http.responseText
    22. ThisWorkbook.Sheets(1).Range("A4").Value = ParseNestedJSON(response)
    23. Else
    24. MsgBox "Request failed, status code: " & http.Status
    25. End If
    26. End 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:

    1. Function ParseNestedJSON(ByVal raw As String) As String
    2. Dim parts() As String, i As Long
    3. Dim pos As Long, jsonLine As String
    4. parts = Split(raw, vbLf)
    5. For i = LBound(parts) To UBound(parts)
    6. ' Skip empty and non-data lines, extract lines containing text fields
    7. If InStr(parts(i), """content""") > 0 Then
    8. jsonLine = Mid(parts(i), InStr(parts(i), "{"))
    9. ' Simple extraction of the content field value (for production, a JSON parsing library is recommended)
    10. Dim keyPos As Long, valStart As Long, valEnd As Long
    11. keyPos = InStr(jsonLine, """content"":""")
    12. If keyPos > 0 Then
    13. valStart = keyPos + Len("""content"":""")
    14. valEnd = InStr(valStart, jsonLine & """", """")
    15. ParseNestedJSON = ParseNestedJSON & Mid(jsonLine, valStart, valEnd - valStart)
    16. End If
    17. End If
    18. Next i
    19. End 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 CallLocalLLM macro;
    • 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

    1. 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.
    2. 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.
    3. 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.
    4. 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.