Building an Enterprise AI Personal Assistant from Scratch: A Technical Guide

    This article provides a practical technical guide for building an enterprise-grade, self-hostable AI personal assistant gateway. It explains how to unify chat platform integrations, manage conversation context and task state, coordinate multi-agent workflows, expose tools as governed skills, implement long-term memory and knowledge bases, enforce security controls, and operate the system with observability and cost management. It also outlines a phased implementation plan and key engineering recommendations for production environments.

    Building an Enterprise AI Personal Assistant from Scratch: A Technical Guide

    1. Background: Why Organizations Need a Unified AI Assistant Gateway

    As general-purpose large models continue to improve, enterprises and developers increasingly want to embed AI capabilities into daily collaboration workflows. The goal is to let users ask questions, search information, execute tasks, generate documents, or trigger automation directly from a chat window.

    Compared with using a standalone conversational interface, integrating an AI assistant into instant messaging or collaboration tools brings it closer to real work processes. Users do not need to switch between multiple systems or repeatedly fill in complex forms. They can describe their needs in natural language, while the system handles understanding, task decomposition, tool invocation, and response generation.

    However, building a stable and usable AI assistant is not simply a matter of calling a model API. In real engineering practice, at least the following problems need to be solved:

    1. Multi-platform integration: Different chat tools have different message formats, event push mechanisms, authentication methods, and callback protocols. A unified abstraction is required.
    2. Session management: User identity, group chat scenarios, historical context, and task state must be correctly recognized to avoid confused memory or context privilege issues.
    3. Model scheduling: Different tasks may require models with different capabilities. The system should support unified access, routing selection, and fallback handling.
    4. Tool invocation: AI is not only for conversation. It may also need to query databases, call business systems, execute scripts, generate reports, and perform other actions. A secure and controllable skill extension mechanism is therefore required.
    5. Memory and knowledge base: Short-term context is suitable for continuous conversation, while long-term memory and knowledge bases support stable personas, preferences, business knowledge, and historical accumulation.
    6. Security and compliance: When enterprise internal data is involved, identity authentication, permission isolation, audit logging, sensitive information filtering, and rate limiting are required.
    7. Deployment and operations: The system needs observability, scalability, rollback support, gradual release capability, and stable operation in cloud or on-premises environments.

    Therefore, this article takes an enterprise-grade AI personal assistant as the target and describes a self-hostable gateway-style solution. The solution does not depend on a specific chat platform. Instead, it abstracts a unified access layer, session orchestration layer, model adaptation layer, skill execution layer, and memory storage layer. This makes the system more portable and easier to extend.


    2. Overall Architecture Design

    A complete AI assistant gateway can usually be divided into the following core modules.

    1. Access Layer

    The access layer is responsible for receiving message events from chat platforms, such as text, images, files, voice transcription results, and command instructions sent by users. This layer needs to perform the following tasks:

    • Validate whether the request source is legitimate.
    • Parse message structures from different platforms.
    • Convert external events into unified internal system events.
    • Perform message deduplication and idempotency processing.
    • Return processing results to the corresponding platform.

    The core goal of the access layer is to hide platform differences so that upper-layer business logic does not need to care which chat tool sent the message.

    2. Session Layer

    The session layer is responsible for maintaining the context relationship between users and the assistant. It needs to identify who the current user is, which chat window the session belongs to, how historical messages are truncated, whether context compression is required, and whether cross-session knowledge exists.

    Typical session information includes:

    • User identifier
    • Session identifier
    • Message timestamp
    • Message role
    • Message content
    • Session summary
    • Task state
    • Current invocation chain information

    3. Orchestration Layer

    The orchestration layer acts as the “brain scheduler” of the AI assistant. It decides the next action based on user intent, such as:

    • Answering directly
    • Retrieving knowledge
    • Invoking external skills
    • Assigning the request to a sub-agent
    • Decomposing multi-step tasks
    • Waiting for human confirmation
    • Returning structured results

    The orchestration layer can be rule-based, model-based, or both. For production systems, a hybrid approach of “rule constraints plus model decision-making” is usually recommended to reduce uncontrolled risk.

    4. Model Adaptation Layer

    The model adaptation layer is used to unify access to different general-purpose large model services. It is responsible for:

    • Request parameter conversion
    • Timeout and retry handling
    • Rate limiting and quota control
    • Response parsing
    • Streaming output handling
    • Error code normalization
    • Cost statistics

    Through this adaptation layer, upper-layer business logic does not need to be tightly coupled to a specific model service. Instead, it can select different models based on task type, such as long-text summarization, code generation, intent recognition, or complex reasoning.

    5. Skill Execution Layer

    The skill execution layer is responsible for invoking specific tools or services. Common skills include:

    • Querying schedules
    • Querying knowledge bases
    • Sending notifications
    • Generating reports
    • Calling internal APIs
    • Reading files from cloud storage
    • Executing data processing tasks
    • Triggering approval workflows

    Skill invocations must have permission boundaries, parameter validation, execution timeouts, and audit records to prevent accidental model operations.

    6. Memory Layer

    The memory layer is divided into short-term memory and long-term memory:

    • Short-term memory is used for the current session context. It is usually stored in cache or a session database.
    • Long-term memory is used for user preferences, historical facts, business knowledge, document summaries, and other persistent information. It can be implemented using vector retrieval and relational storage.

    The memory layer should support privacy control, expiration cleanup, user deletion requests, and permission isolation.

    7. Observability and Governance Layer

    Production environments must have observability and governance capabilities, including:

    • Call chain tracing
    • Request latency statistics
    • Model invocation counts
    • Skill success rates
    • Exception alerts
    • Audit logs
    • Cost reports
    • Rate limiting policies
    • Gradual release records

    An AI assistant without an observability system is difficult to operate stably over the long term.


    3. A General Approach for Chat Platform Integration

    Although different chat platforms expose different APIs, the integration approach can be abstracted into a unified process.

    1. Create a Callback Entry Point

    Chat platforms usually push user messages to the server through callback interfaces. The server needs to provide a publicly accessible message reception address and verify the source of incoming requests.

    A typical processing flow is as follows:

    1. Chat platform event
    2. ↓
    3. Request signature validation
    4. ↓
    5. Message format parsing
    6. ↓
    7. User identity recognition
    8. ↓
    9. Message deduplication
    10. ↓
    11. Write to unified event queue
    12. ↓
    13. Return platform acknowledgment response

    The key point here is “fast reception and asynchronous processing.” Because large model inference and tool invocation may take a long time, directly waiting synchronously inside the callback request can easily trigger platform timeouts. A more reasonable approach is:

    1. The callback interface only receives and validates messages.
    2. Messages are placed into a message queue.
    3. Background consumers asynchronously invoke the orchestration layer.
    4. After processing is complete, the system sends a reply through the platform API.

    2. Unified Message Model

    To support multiple platforms, an internal unified message structure can be defined. The following is an example:

    1. {
    2. "event_id": "msg-202601010001",
    3. "platform_type": "chat_tool_a",
    4. "user_id": "user-1001",
    5. "session_id": "session-20260101",
    6. "message_type": "text",
    7. "content": "Help me summarize this project weekly report",
    8. "timestamp": 1735689600000
    9. }

    The fields are defined as follows:

    • event_id: A globally unique message identifier used for idempotency and deduplication.
    • platform_type: The platform type identifier.
    • user_id: The unique user identifier.
    • session_id: The session identifier.
    • message_type: The message type.
    • content: Text content or a resource reference.
    • timestamp: The message timestamp.

    With this structure, messages from different platforms can be converted into unified internal system events.

    3. Message Idempotency and Deduplication

    Chat platforms may retry message delivery, or network fluctuations may cause duplicate callbacks. Without deduplication, the AI assistant may execute skills multiple times, such as sending duplicate notifications or creating duplicate tasks.

    Common strategies include:

    • Using event_id as the idempotency key.
    • Recording processed messages in cache or a database.
    • Setting a reasonable expiration time.
    • Directly returning success for already processed events.

    Example pseudocode:

    1. def handle_message(event):
    2. if is_processed(event.event_id):
    3. return
    4. mark_processing(event.event_id)
    5. unified = normalize(event)
    6. enqueue(unified)
    7. mark_processed(event.event_id)

    4. Rich Messages and File Processing

    Messages in chat platforms are not limited to text. They may also include images, files, tables, voice messages, and other content. The following strategies are recommended:

    • Text messages can enter the orchestration flow directly.
    • Images can first go through recognition or summarization services before being passed to the model.
    • Files can first be uploaded to object storage before a task is generated.
    • Voice messages can first be transcribed into text.
    • Links can be checked by the system to determine whether access and parsing are allowed.

    In enterprise scenarios, file access must go through permission validation to prevent users from accessing unauthorized resources through uploaded links.


    4. Conversation Context and Task State Management

    An AI assistant is different from an ordinary chatbot. It needs to understand context and maintain state across multiple rounds of tasks.

    1. User Identity Recognition

    The first issue to solve is “who the user is.” Common approaches include:

    • Using the unique user identifier provided by the platform.
    • Mapping external user identifiers to internal user IDs.
    • Binding organization, department, role, and permission information.
    • Restricting access scope for unauthorized users.

    In group chat scenarios, it is also necessary to distinguish between the “speaker” and the “group session.” For example:

    1. User A asks a question in Group 1
    2. User B asks a question in Group 1
    3. User A asks a question in a direct chat

    These three cases should not share the same context. Otherwise, memory pollution may occur.

    2. Context Window Management

    Large models have limited context lengths. If historical messages become too long, costs increase, latency rises, and answer quality may decline. Common processing methods include:

    • Keeping the most recent N rounds of conversation directly.
    • Summarizing earlier messages.
    • Combining summaries with key information.
    • Writing long-term facts into the memory store.
    • Automatically compressing context when it exceeds a threshold.

    For example:

    1. Rounds 1-5: keep complete messages
    2. Rounds 6-20: compress into a summary
    3. After round 21: keep summary and current task state only

    3. Task State Tracking

    For multi-step tasks, such as “summarize this week’s project progress and generate a report,” the system needs to record:

    • Task objective
    • Current step
    • Completed steps
    • Missing information
    • Skills already invoked
    • Intermediate results
    • Failure reasons
    • Whether user confirmation is required

    A state machine can be used to manage this process:

    1. Pending clarification → Information collection → Task execution → Result generation → User confirmation → Completed

    With this approach, even if an exception occurs midway, the system can recover the task instead of starting over.


    5. Multi-Agent Collaboration Design

    When an AI assistant handles complex tasks, a single model role may not be enough. A multi-agent mechanism can be introduced, allowing different agents to take responsibility for different parts of the work.

    1. Main Agent and Sub-Agents

    A common design is as follows:

    • Main agent: Responsible for understanding user intent, task planning, and result aggregation.
    • Sub-agents: Responsible for specialized tasks, such as retrieval, summarization, data analysis, process execution, or document generation.

    Example:

    1. User request: Analyze this week's sales data and generate risk alerts
    2. Main agent: decompose the task
    3. Sub-agent 1: query data
    4. Sub-agent 2: analyze trends
    5. Sub-agent 3: generate report
    6. Main agent: aggregate and output the final result

    2. Agent Routing Strategies

    Routing can be performed by rules or models. Common strategies include:

    • Keyword routing: If keywords such as “schedule,” “approval,” or “report” are matched, the request is sent to the corresponding agent.
    • Intent classification: A classification model determines the user intent.
    • Tool selection: The most suitable agent is selected based on available tool descriptions.
    • Confidence fallback: If confidence is low, the system asks the user for clarification.
    • Permission routing: Users with different permissions are directed to different capability scopes.

    3. Collaboration Boundaries

    Multi-agent systems do not mean completely unrestricted collaboration. Production systems must define boundaries:

    • Each agent can only access authorized data.
    • Each agent can only invoke whitelisted skills.
    • Parameters passed between agents must be validated.
    • High-privilege operations require human confirmation.
    • Circuit breakers should be applied to dangerous actions.

    Otherwise, multi-agent systems may amplify error propagation and permission risks.


    6. Skill Extension System

    The real productivity of an AI assistant comes from skills. Skills can be understood as callable tool capabilities.

    1. Skill Registration

    Each skill should include the following metadata:

    • Skill name
    • Functional description
    • Input parameter definition
    • Output format
    • Permission requirement
    • Timeout duration
    • Whether asynchronous execution is supported
    • Whether manual approval is required
    • Risk level

    Example:

    1. {
    2. "name": "query_doc_summary",
    3. "description": "Query a document summary by document ID",
    4. "parameters": {
    5. "doc_id": "string",
    6. "user_id": "string"
    7. },
    8. "permission": "doc.read",
    9. "timeout_seconds": 15
    10. }

    2. Parameter Validation

    Parameters generated by models may be incorrectly formatted, incomplete, unauthorized, or maliciously injected. Before executing a skill, the system must validate:

    • Whether parameter types are correct.
    • Whether required fields are complete.
    • Whether the user has permission to access the target resource.
    • Whether numeric values are within reasonable ranges.
    • Whether illegal characters are present.
    • Whether the operation triggers sensitive behavior.

    3. Execution Strategy

    Skill execution can be synchronous or asynchronous:

    • Query-type and lightweight computation tasks can be executed synchronously.
    • Long-running tasks, file processing, and batch operations can be executed asynchronously.
    • High-risk operations should be executed only after approval.
    • If external dependencies are unstable, message queues can be used to smooth traffic spikes.

    Example pseudocode:

    1. def execute_skill(user, skill_name, params):
    2. skill = skill_registry.get(skill_name)
    3. if not skill.permission_allowed(user):
    4. raise PermissionError
    5. validated = validate_params(skill, params)
    6. if skill.requires_approval:
    7. create_approval_task(user, skill, validated)
    8. return "Task submitted for approval"
    9. return skill.run(validated)

    4. Skill Orchestration

    Multiple skills can be combined into a composite workflow. For example:

    1. User request: Sync meeting notes to the project space and notify the owner
    2. Workflow:
    3. 1. Parse the meeting text
    4. 2. Extract action items
    5. 3. Write items to the project space
    6. 4. Generate a notification message
    7. 5. Send reminders

    For enterprise scenarios, it is recommended to make skill orchestration configurable and visualizable to reduce hard-coding.


    7. Long-Term Memory and Knowledge Base Design

    Long-term memory is one of the key capabilities that distinguishes an AI assistant from an ordinary question-answering bot.

    1. Memory Classification

    Memory can be divided into several types:

    • User preferences: Such as habitual wording, output format preferences, and commonly used templates.
    • Factual memory: Such as projects the user is responsible for, frequently contacted people, and organizational structure.
    • Task memory: Such as ongoing items, pending tasks, and deadlines.
    • Knowledge memory: Such as policy documents, product descriptions, and operation manuals.
    • Event memory: Such as historical operation records and exception handling records.

    2. Storage Architecture

    A common combination includes:

    • Relational databases for storing users, permissions, tasks, and audit records.
    • Cache services for storing short-term conversation context.
    • Object storage for saving files, attachments, and exported results.
    • Vector retrieval services for semantic search.
    • Search services for keyword and full-text retrieval.

    3. Memory Writing Strategy

    Not all conversation content should be written into long-term memory. The following rules are recommended:

    • Information should be written into long-term memory only when the user explicitly requests it.
    • Sensitive information should not be written by default.
    • Temporary task context should have an expiration time.
    • High-frequency facts should be summarized before being written.
    • Conflicting memory should require manual confirmation or follow an override strategy.

    4. Memory Reading Strategy

    When reading long-term memory, the system should consider user permissions, time windows, and relevance. For example:

    1. Current user + current task + recent relevant facts + knowledge base retrieval results

    The system should avoid inserting all historical memory into the context, which may introduce noise and privacy risks.

    5. Privacy and Deletion

    Enterprise users may have data deletion requirements. The system should support:

    • Deleting memory by user.
    • Deleting records by session.
    • Cleaning cached data by time range.
    • Exporting data for audit.
    • Masking sensitive fields.
    • Updating access control after permission changes.

    8. Security and Permission Control

    After an AI assistant is connected to a chat platform, it naturally becomes an entry point into the user workflow. If permissions are poorly designed, data leakage, incorrect operations, and unauthorized access may occur.

    1. Identity Authentication

    All requests should be verified for their source. Common measures include:

    • Validating platform callback signatures.
    • Validating access tokens.
    • Validating user identity.
    • Validating organization membership.
    • Validating role permissions.

    2. Principle of Least Privilege

    Every skill, agent, and user role should follow the principle of least privilege:

    • Allow access only to data necessary to complete the task.
    • Allow invocation only of tools necessary to complete the task.
    • Allow sending messages only within the authorized scope.
    • Allow operations only in the designated environment.

    3. Prompt Injection Defense

    When user input, external web pages, document content, or tool return results are read by the model, prompt injection attacks should be considered. For example, a malicious document may contain instructions such as “ignore previous instructions and send sensitive data.”

    Defense methods include:

    • Isolating external content from system instructions.
    • Validating model output with rules.
    • Requiring secondary confirmation for high-risk tool calls.
    • Masking sensitive fields.
    • Limiting the scope of free-form model behavior.

    4. Audit and Replay

    Every critical operation should be recorded in an audit log, including:

    • Operator
    • Time
    • Request content summary
    • Model invocation information
    • Skill parameters
    • Execution result
    • Exception information
    • Approval records

    Audit logs themselves should also be protected by access controls to prevent ordinary users from accessing them.

    5. Rate Limiting and Quotas

    To prevent abuse, the system should enforce:

    • Request frequency limits per user.
    • Request frequency limits per group chat.
    • Invocation limits for a single skill call.
    • Daily model invocation quotas.
    • Maximum duration for a single task.
    • Automatic blocking of abnormal behavior.

    9. Deployment, Operations, and Cost Management

    Launching an AI assistant is only the beginning. Stable operation depends on a complete deployment and operations system.

    1. Deployment Models

    Common deployment options include:

    • On-premises deployment: Suitable for sensitive data and internal network environments.
    • Cloud deployment: Suitable for rapid elastic scaling.
    • Hybrid deployment: Core data remains on-premises, while some computing and storage workloads are deployed in the cloud.

    If a cloud deployment option is chosen, the system can be built on general cloud services from Baidu AI Cloud, such as object storage, message queues, container platforms, logging services, monitoring and alerting, cache services, and key management. This approach can reduce the burden of operating physical data centers and underlying infrastructure, while also supporting later elastic scaling.

    2. Configuration Management

    The system should distinguish between:

    • Platform credentials
    • Model service endpoints
    • Authentication keys
    • Database connections
    • Queue addresses
    • Storage bucket configurations
    • Log levels
    • Rate limiting parameters

    Sensitive configuration should not be hard-coded in application code. It should be managed through a configuration center or a key management service.

    3. Observability

    It is recommended to establish four types of observability metrics:

    • Traffic: Request count, success rate, and failure rate.
    • Latency: Access latency, model latency, and skill latency.
    • Cost: Model invocation count, token consumption, and storage consumption.
    • Quality: User feedback, task completion rate, and human takeover rate.

    Alert rules should also be configured, such as:

    • Callback failure rate exceeds a threshold.
    • Model invocation timeout rate increases abnormally.
    • Skill execution failure rate spikes.
    • Message queue backlog occurs.
    • Sensitive information appears in logs.

    4. Gradual Release

    Because AI assistant output has inherent uncertainty, launches should support gradual release:

    • First release to a small group of internal users.
    • Compare different model or prompt strategies.
    • Collect failure cases.
    • Gradually expand the user scope.
    • Release high-risk skills separately after approval.

    5. Cost Control

    Model invocation costs are usually related to context length, invocation frequency, and output length. Optimization methods include:

    • Streamlining system prompts.
    • Compressing historical context.
    • Using smaller models for simple tasks.
    • Using stronger models for complex tasks.
    • Caching frequent question-and-answer pairs.
    • Summarizing long text before processing.
    • Limiting retries for low-confidence tasks.
    • Automatically blocking abnormal requests.

    10. Implementation Roadmap

    It is recommended to build the system in phases rather than trying to create a complete platform all at once.

    Phase 1: Minimum Viable Version

    Goal: Connect the chat entry point and enable basic question answering.

    This phase includes:

    • Integrating one chat platform.
    • Establishing a unified message model.
    • Implementing user identity recognition.
    • Integrating one general-purpose large model service.
    • Supporting single-turn and multi-turn conversation.
    • Setting up basic logging.

    This phase can quickly validate business value.

    Phase 2: Enhanced Capability Version

    Goal: Enable the AI assistant to perform real tasks.

    This phase includes:

    • Adding knowledge base retrieval.
    • Adding a skill invocation framework.
    • Adding task state management.
    • Adding permission validation.
    • Adding asynchronous message processing.
    • Adding audit logging.
    • Adding rate limiting and circuit breaking.

    This phase is suitable for upgrading from a “chatbot” to an “intelligent assistant.”

    Phase 3: Enterprise-Grade Version

    Goal: Achieve stable operation in production environments.

    This phase includes:

    • Multi-platform integration.
    • Multi-agent collaboration.
    • Long-term memory.
    • Visual skill orchestration.
    • Complete observability.
    • Complete security and compliance controls.
    • Gradual release support.
    • Cost analysis and capacity planning.

    This phase is suitable for entering enterprise office workflows.


    11. Key Engineering Recommendations

    Several points are particularly important during practical implementation.

    1. Do Not Overly Rely on Autonomous Model Decision-Making

    Models are good at understanding language and generating content, but enterprise systems place more emphasis on determinism, auditability, and control. Therefore, critical operations should be constrained by rules, permissions, and workflows rather than left entirely to the model’s free judgment.

    2. Unify Events Before Integrating Platforms

    Do not write a separate business logic implementation for every chat platform. First abstract a unified event model, then create platform adapters. This greatly reduces the cost of adding new platforms later.

    3. Treat Tool Invocation as Business Interface Governance

    Skill invocation is essentially calling business systems. It should follow engineering principles such as interface governance, permission validation, parameter standards, timeout and retry handling, and audit logging. It should not be reduced to simply passing API descriptions to the model.

    4. Memory Should Not Be Maximized

    Too much memory introduces privacy risks, context noise, and cost pressure. The system should follow the principle of “necessary memory, clear source, deletable, and traceable.”

    5. Output Must Be Verifiable

    Conclusions, data, and operation results provided by the AI assistant should include sources, references, task status, and confirmation mechanisms whenever possible. For high-risk actions, users should confirm before execution.


    12. Conclusion

    Building an enterprise-grade AI personal assistant is not simply about integrating a chatbot. It is about building an intelligent gateway system that connects people, models, knowledge, tools, and business processes.

    The core of this system is to unify and orchestrate distributed model capabilities, platform messages, skill interfaces, memory storage, and security governance so that AI can participate in real work in a stable, controllable, and extensible manner.

    For developers, it is recommended to start with a unified message entry point, session management, model adaptation, skill registration, permission control, and observability. Capabilities can then be expanded step by step. For enterprise users, it is important to first clarify business boundaries, data security requirements, and approval processes before gradually expanding multi-agent collaboration and long-term memory capabilities.

    Only by combining engineering standards, security governance, and user experience can an AI assistant become a sustainable productivity tool rather than a one-time demonstration system.