A comprehensive AI assistant for analyzing Deadlock game data using advanced language models and database querying capabilities. Optimized for Discord bot integration with session-based conversation management and concurrent user support.
Before beginning the installation process, ensure your development environment meets the following requirements:
This project requires Python 3.11 or higher. Verify your Python installation by executing the following command in your terminal:
python --versionIf Python is not installed or the version is insufficient, download the latest version from the official Python website at https://python.org. During installation on Windows systems, ensure you select the option to "Add Python to PATH" to enable command-line access.
Install UV, the modern Python package manager, from https://docs.astral.sh/uv/getting-started/installation/. UV provides faster dependency resolution and improved project management compared to traditional pip installations.
Clone the repository and navigate to the project root directory. The project structure should appear as follows:
ai-assistant/
├── ai_assistant/ # Main package directory
│ ├── __main__.py # Application entry point
│ ├── tools.py # Custom tools and functions
│ └── utils.py # Utility functions
├── pyproject.toml # Project configuration
└── README.md # This file
From the project root directory, install all required dependencies using UV:
uv syncThis command creates a virtual environment and installs all packages specified in the project configuration.
Install the pre-commit hook to ensure code quality standards:
pre-commit installThe application supports multiple language model providers with runtime selection capabilities. Configuration requires selecting an appropriate model and establishing authentication credentials.
The application includes support for the following model providers:
- Hugging Face Inference API (Default - Recommended for new users)
- Google Gemini Models (Requires Google Cloud credentials)
- Ollama Local Models (Requires local Ollama installation)
Hugging Face provides the most straightforward configuration process and is set as the default model provider.
Navigate to your Hugging Face account settings at https://huggingface.co/settings/tokens and create a new User Access Token with Read permissions. Provide a descriptive name such as "Deadlock AI Assistant Development" for future reference.
Configure your Hugging Face token using one of the following methods:
Windows System Environment Variables (Permanent)
- Open System Properties through Control Panel
- Navigate to Advanced system settings
- Select Environment Variables
- Add a new user variable named
HF_TOKENwith your token value
Project-Specific Configuration
Create a .env file in the project root directory with the following content:
HF_TOKEN=your_token_here
Session-Based Configuration (Temporary) Execute the following command in PowerShell before running the application:
$env:HF_TOKEN="your_token_here"Execute the application in interactive console mode from the project root directory:
uv run python -m ai_assistantImportant: Always run this command from the project root directory, not from within the ai_assistant subdirectory. The application requires proper Python module path resolution to locate package imports correctly.
Launch the application as a web service using Uvicorn:
uv run uvicorn ai_assistant.__main__:app --reloadThe web service provides API endpoints accessible at http://localhost:8000, with interactive documentation available at http://localhost:8000/scalar.
The web service provides session-based conversation management with support for multiple concurrent users, making it ideal for Discord bot integration.
Submit prompts to the AI assistant with automatic session management.
Parameters:
prompt(required): The question or command to send to the AI assistant (1-10000 characters)user_id(optional): Discord user ID or custom identifier for automatic session isolationsession_id(optional): Override session ID for development and testing purposesmodel(optional): Model to use for inference (default: "hf")
Usage Examples:
Discord Bot Integration:
POST /invoke?user_id=discord_123456789&prompt=analyze my steam profile&model=gemini-proDevelopment Testing:
POST /invoke?session_id=test-session&prompt=what deadlock heroes are available?Anonymous Usage:
POST /invoke?prompt=query the database for match statisticsList all available language models and the current default.
Response:
{
"models": ["hf", "gemini-flash", "gemini-pro", "ollama"],
"default": "hf"
}Pre-create a session with a specific model configuration.
Parameters:
user_id(optional): Discord user ID or custom identifiermodel(optional): Model to use for this session (default: "hf")
Response:
{
"session_id": "uuid-or-user-id",
"model": "hf"
}View all currently active conversation sessions.
Response:
{
"sessions": {
"discord_123456789": {
"model": "gemini-pro",
"active": true
},
"test-session": {
"model": "hf",
"active": true
}
}
}Remove a specific conversation session and its context.
Response:
{
"message": "Session {session_id} cleared"
}- GET /health - Service health verification
- GET /scalar - Interactive API documentation
- GET / - Redirect to documentation
All streaming responses follow a structured format for reliable parsing:
{
"type": "session_info|action|planning|delta|final_answer|error",
"data": {}
}Response Types:
session_info: Contains the session ID being usedaction: Tool execution and function callsplanning: AI reasoning and decision-making stepsdelta: Incremental response contentfinal_answer: Complete response dataerror: Error messages and troubleshooting information
The API is specifically optimized for Discord bot usage with automatic user isolation:
import asyncio
import aiohttp
async def handle_discord_message(user_id: str, message: str):
async with aiohttp.ClientSession() as session:
async with session.post(
'http://localhost:8000/invoke',
params={
'user_id': user_id, # Automatic session per Discord user
'prompt': message,
'model': 'gemini-pro' # Optional model selection
}
) as response:
async for line in response.content:
if line.startswith(b'data: '):
data = json.loads(line[6:])
if data['type'] == 'final_answer':
return data['data']Key Benefits for Discord Bots:
- Each Discord user maintains separate conversation context
- Multiple users can chat simultaneously without interference
- Automatic session management eliminates manual session handling
- Graceful error handling prevents bot crashes
Windows systems typically use python rather than python3 as the executable name. If you encounter "Python was not found" errors, ensure you are using the correct command syntax for your operating system.
Import errors typically indicate execution from an incorrect directory. Ensure all commands are executed from the project root directory containing pyproject.toml, not from within the ai_assistant subdirectory.
Connection errors usually result from missing or incorrect API credentials. Verify that your environment variables are properly configured and that your chosen model provider credentials are valid. Check available models using the /models endpoint.
If sessions behave unexpectedly, use the /sessions endpoint to view active sessions and /sessions/{session_id} DELETE endpoint to clear problematic sessions.
Windows may redirect Python commands to the Microsoft Store. Disable these redirects by navigating to Settings → Apps → Advanced app settings → App execution aliases and disabling the toggles for "python.exe" and "python3.exe".
The application uses a session-based architecture that provides:
- Concurrency Safety: Multiple users can interact simultaneously without context pollution
- Memory Management: Sessions can be individually created and destroyed
- Model Flexibility: Runtime model selection per session
- Discord Optimization: Automatic user isolation using Discord user IDs
- Development Support: Manual session override for testing and debugging
Maintain code quality by ensuring pre-commit hooks execute successfully before committing changes. The hooks perform automated formatting, linting, and testing to maintain consistency across the codebase.
For additional development resources and advanced configuration options, consult the project documentation and dependency specifications within pyproject.toml.