4.7 KiB
4.7 KiB
Chat Assistant Module
Overview
The Chat Assistant module provides a conversational AI interface for answering policy and compliance questions using Ollama.
Architecture
Backend (be0/src/chat_assistant.py)
The ChatAssistant class provides:
- Chat functionality: Conversational AI for policy questions
- Content verification: Verify content against compliance requirements
- Policy Q&A: Answer questions about policies and compliance
Frontend (fe0/src/features/chat/)
The frontend chat feature includes:
- Service layer: API communication with backend
- React hooks: Easy-to-use hooks for chat functionality
- Type definitions: TypeScript types for type safety
API Endpoints
1. Chat Endpoint
POST /api/v1/chat
Request Body:
{
"message": "What are ISO 27001 requirements?",
"conversation_history": [
{
"role": "user",
"content": "Previous message"
},
{
"role": "assistant",
"content": "Previous response"
}
],
"context": "Optional context about policies"
}
Response:
{
"message": "ISO 27001 is an information security management system...",
"model": "gemma3:27b",
"tokens_used": 150
}
2. Verify Content Endpoint
POST /api/v1/chat/verify
Form Data:
field_name: Name of the field being verifiedcontent: Content to verifyverification_criteria: (Optional) Specific criteria to check
Response:
{
"message": "The content meets compliance requirements...",
"model": "gemma3:27b",
"tokens_used": 200
}
3. Policy Question Endpoint
POST /api/v1/chat/question
Form Data:
question: The user's questionpolicy_context: (Optional) Context about specific policies
Response:
{
"message": "Answer to the policy question...",
"model": "gemma3:27b",
"tokens_used": 180
}
Features
1. Conversational Context
- Maintains conversation history for context-aware responses
- Keeps last 10 messages for context
- System prompt guides the assistant's behavior
2. Policy Expertise
- Specialized in IT governance and compliance
- Knowledgeable about ISO 27001, NIST, GDPR, etc.
- Provides accurate, actionable advice
3. Content Verification
- Analyzes content against compliance requirements
- Provides detailed feedback
- Suggests improvements
Usage
Backend
from src.chat_assistant import get_chat_assistant
# Get chat assistant instance
assistant = get_chat_assistant()
# Chat
request = ChatRequest(
message="What is ISO 27001?",
context="IT governance"
)
response = await assistant.chat(request)
# Verify content
response = await assistant.verify_content(
field_name="Project Description",
content="Our project implements security controls..."
)
Frontend
import { useChat } from '@/features/chat/hooks/useChat';
const { sendMessage, verifyContent, isLoading } = useChat();
// Send a message
const response = await sendMessage(
"What are compliance requirements?",
conversationHistory, // Optional
"ISO 27001 context" // Optional
);
// Verify content
const verification = await verifyContent(
"Project Name",
"Project content to verify"
);
Configuration
Model Selection
The default model is gemma3:27b. To change it:
# In chat_assistant.py
assistant = ChatAssistant(model_name="your-model-name")
System Prompt
The system prompt can be customized in the ChatAssistant.__init__ method to change the assistant's behavior and expertise.
Logging
All chat interactions are logged to:
be0/logs/ChatAssistant.log
This helps with debugging and monitoring.
Error Handling
The module includes comprehensive error handling:
- Catches and logs all exceptions
- Returns user-friendly error messages
- Raises HTTPException for API errors
Testing
To test the chat assistant:
-
Start the backend:
cd be0 docker-compose up be0 -
Test via API:
curl -X POST http://localhost:4402/api/v1/chat \ -H "Content-Type: application/json" \ -d '{"message": "What is ISO 27001?"}' -
Test via Frontend:
- Open the Dashboard
- Use the ChatAssistant component
- Ask questions or verify content
Integration
The ChatAssistant is integrated with:
- ChatAssistant.tsx: React component in the Dashboard
- useChat hook: React hook for chat functionality
- chatService: API service layer
Future Enhancements
Potential improvements:
- Streaming responses for real-time text generation
- Multi-turn conversation management
- Document context injection
- Voice input/output
- Response rating and feedback
- Conversation export
- Custom model fine-tuning