Guide to Deploying a Local Private AI Assistant: From API Keys to Message Integration
Deploying a local AI assistant requires several critical components to work simultaneously: ensuring the LLM API functions correctly, integrating with a collaborative office platform for message delivery, maintaining local service stability, and securing API keys and ports. This guide walks through the practical deployment sequence, detailing operation points, configuration methods, and troubleshooting strategies for each phase.
Deploying a local AI assistant requires several components to work simultaneously: the large language model (LLM) API must be accessible, the collaborative office platform must be able to push messages to the local service, the local service must run stably, and keys and ports must not be leaked. This article explains the operation points, configuration methods, and troubleshooting ideas for each step according to the actual deployment sequence.
1. Pre-Deployment Checklist
Before starting, confirm the following conditions:
- Python 3.10 or higher is installed, and you can check the version number in the terminal using
python --version. - Git is installed for pulling open-source code.
- You have an account on a collaborative office platform with permissions to create enterprise organizations and applications.
- The local machine can access the internet and the API domain of your chosen LLM provider.
- An idle port is prepared, such as 8080, and confirmed not to be occupied by other programs.
2. Setting Up an Isolated Runtime Environment
Dependencies between Python projects can easily conflict, so each project should use an independent virtual environment.
git clone <open-source-project-url>cd <project-directory>python -m venv .venvsource .venv/bin/activate # On Windows, use .venv\Scripts\activatepip install -r requirements.txt
If dependency installation fails, first check if the Python version meets the framework requirements, then review the package names and version numbers in the error message. Most conflicts can be resolved by upgrading pip or deleting and recreating the virtual environment.
3. Planning Network and Ports
The local service needs to handle two types of communication. The first is outbound requests, i.e., accessing the external LLM API, which usually requires no additional configuration. The second is inbound requests, i.e., receiving events pushed by the collaborative office platform, which requires opening a port for the local service.
It is recommended to bind the service to 127.0.0.1 to allow local access only, and then use an intranet penetration (tunneling) tool to forward requests to the public network. If you bind directly to 0.0.0.0, other devices on the same local network can also access the port, which expands the attack surface.
4. Obtaining the LLM API Key
4.1 Dimensions to Verify During Selection
When choosing a provider, it is recommended to check the items in the table below one by one, and use the same test conversation to compare two or three platforms.
| Dimension | Content to Verify | Why It Matters |
|---|---|---|
| API Compatibility | Whether it provides an OpenAI-compatible Endpoint, or only a native SDK | Determines if the framework can connect directly, or if adaptation code is needed |
| Context Length | Maximum number of tokens supported per request | Affects the usability of long conversations and document Q&A |
| Billing Metrics | Unit price for input and output tokens, and whether there are cache billing rules | Directly determines monthly cost estimates |
| Rate Limits | Requests Per Minute (RPM) and Tokens Per Minute (TPM) limits | When multiple people use it simultaneously, rate limits determine if 429 errors will occur frequently |
| Data Policy | Whether call data is used for model training, and how long data is stored | Relates to compliance risks for internal materials |
You can prioritize comparing two types of solutions. One is platforms that provide OpenAI-compatible interfaces, where the framework usually only needs to modify the Base URL and model name to switch. The other is platforms that only provide native SDKs, which require modifying or adding an adaptation layer. If your team already has procurement contracts or compliance requirements, follow the compliance requirements first.
4.2 Creating an App and Saving the Key
Create an app in the provider’s developer center and copy the generated API Key. Some platforms only display the full key once at creation, requiring you to save it immediately to a secure location. If a key is leaked, it should be deleted immediately in the platform’s backend and regenerated.
4.3 Quota and Cost Control
LLM APIs are usually billed per token. For initial configuration, you can top up a small amount for testing, such as a few dollars. Set up usage alerts or spending limits in the platform’s backend to avoid unexpected bills due to loop calls or abuse.
4.4 Connectivity Test
Set the key and address as environment variables instead of writing them into code files:
export LLM_API_KEY="your_api_key_here"export LLM_BASE_URL="https://api.example.com/v1"export LLM_MODEL="your-model-name"
Then run the following script:
import osimport requestsapi_key = os.environ["LLM_API_KEY"]base_url = os.environ["LLM_BASE_URL"].rstrip("/")model = os.environ["LLM_MODEL"]resp = requests.post(f"{base_url}/chat/completions",headers={"Authorization": f"Bearer {api_key}"},json={"model": model,"messages": [{"role": "user", "content": "Hello, please reply in one sentence."}],},timeout=30,)print(resp.status_code)print(resp.text[:500])
If the returned status code is 200 and the response contains the choices field, the configuration is correct. Common errors are as follows:
- 401: The key is incorrect, empty, or the
Bearerprefix is missing in the request header. - 403: The account lacks permission for the model, or the region does not match the terms of service.
- 429: Requests are too frequent, or the balance is insufficient. Some platforms return 402 when the balance is insufficient.
- Timeout: There is an issue with the local network or proxy configuration. Check proxy settings and DNS resolution.
5. Creating an App on the Collaboration Platform
5.1 Creating an Organization and Custom App
Log in to the collaborative office platform, create an enterprise organization, and then create a “custom app” in the developer backend. After creation, an App ID and App Secret will be generated, which are used for API authentication. The storage requirements for the App Secret are the same as for the API Key; it must not be written into code repositories, chat logs, or logs.
5.2 Applying for Minimum Necessary Permissions
Apply for permissions based on actual functionality. Receiving and sending messages are basic permissions. Only apply for user information permissions if you need to identify the sender’s identity. The fewer permissions granted, the lower the impact if a key is leaked.
After the permission application is submitted, the review duration varies depending on the platform and enterprise settings, so allow for waiting time. Do not assume it can be completed immediately. During the review period, you can proceed with the development and testing of the local service.
5.3 Configuring Event Subscriptions
In the app’s “Event Subscription” or “Webhook” module, fill in the callback URL and record the Verification Token and Encrypt Key provided by the platform. The callback URL should use HTTPS. When the platform pushes a message to the callback URL, the local service must verify the request signature and timestamp, rejecting requests that fail verification.
6. Configuring the Local Service
Create a .env file in the project root directory with the following example fields:
LLM_API_KEY=your_api_key_hereLLM_BASE_URL=https://api.example.com/v1LLM_MODEL=your-model-nameAPP_ID=your_app_idAPP_SECRET=your_app_secretVERIFY_TOKEN=your_verify_tokenENCRYPT_KEY=your_encrypt_keyLISTEN_HOST=127.0.0.1LISTEN_PORT=8080
The sources of each field are: the LLM-related fields come from Part 4, the collaboration platform fields come from Part 5, and LISTEN_HOST and LISTEN_PORT are determined by local network planning.
Please note the following before use:
- Add
.envto.gitignoreto prevent keys from being committed to the code repository. - On Linux or macOS, you can run
chmod 600 .envto restrict other users from reading the file. - If a key appears in chat logs, screenshots, or logs, treat it as leaked and rotate it immediately on the corresponding platform.
After configuration, start the service using the command in the project README, commonly python main.py. If the console shows that the service is listening on 127.0.0.1:8080, the local process has started successfully.
7. Secure Practices for Intranet Penetration
Since home or office networks typically do not have public IPs, you need to use an intranet penetration (tunneling) tool to forward the platform’s callback requests to the local port. Tunneling solutions are only recommended for testing and development phases. Follow these requirements when using them:
- Choose a tool with clear security documentation and use an HTTPS domain.
- Forward only the callback path; do not expose the entire service or admin console to the public network.
- Verify the platform signature and timestamp in the code to reject replayed old requests.
- If the tool supports source IP restrictions, only allow the collaborative platform’s push addresses to access it.
- Close the tunneling session immediately after testing and rotate the Verification Token used during testing in the platform backend.
8. Effect Verification
Find the newly created bot in the collaborative office platform and send a test message. The complete link should appear in the following order:
- The platform pushes the event, and the local console receives it.
