### Authentication and Authorization Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MANIFEST.txt Example demonstrating how to handle authentication and authorization. ```python from sqlbot.auth import authenticate_user, authorize_action user = authenticate_user("username", "password") if user: if authorize_action(user, "read_data"): print("Access granted.") else: print("Access denied.") else: print("Authentication failed.") ``` -------------------------------- ### LLM Configuration and Initialization Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MANIFEST.txt Example showing how to configure and initialize an LLM. ```python from sqlbot.llm_factory import LLMFactory llm_config = { "provider": "openai", "model": "gpt-3.5-turbo", "api_key": "YOUR_OPENAI_API_KEY" } llm = LLMFactory.create_llm(llm_config) response = llm.chat("Explain the concept of recursion.") print(response) ``` -------------------------------- ### Development Environment .env Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Example .env file for setting up a local development environment for SQLBot. Includes basic, database, frontend, cache, logging, and embedding configurations. ```bash # 基础配置 PROJECT_NAME=SQLBot CONTEXT_PATH= SECRET_KEY=dev-secret-key-change-in-production # 数据库配置(本地PostgreSQL) POSTGRES_SERVER=localhost POSTGRES_PORT=5432 POSTGRES_USER=sqlbot POSTGRES_PASSWORD=password123 POSTGRES_DB=sqlbot_dev # 前端配置 FRONTEND_HOST=http://localhost:5173 BACKEND_CORS_ORIGINS=http://localhost:3000,http://localhost:5173 # 缓存配置 CACHE_TYPE=memory # 日志配置 LOG_LEVEL=DEBUG SQL_DEBUG=false # 嵌入配置 EMBEDDING_ENABLED=true DEFAULT_EMBEDDING_MODEL=shibing624/text2vec-base-chinese ``` -------------------------------- ### Docker Compose Example for SQLBot Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Example docker-compose.yml file to set up SQLBot with PostgreSQL and Redis services. Includes port mappings, environment variables, and volume configurations. ```yaml version: '3.8' services: sqlbot: image: dataease/sqlbot:latest ports: - "8000:8000" - "8001:8001" environment: POSTGRES_SERVER: postgres POSTGRES_PORT: 5432 POSTGRES_USER: sqlbot POSTGRES_PASSWORD: sqlbot_password POSTGRES_DB: sqlbot CACHE_TYPE: redis CACHE_REDIS_URL: redis://redis:6379/0 FRONTEND_HOST: http://localhost:5173 BASE_DIR: /opt/sqlbot volumes: - ./data/excel:/opt/sqlbot/data/excel - ./data/file:/opt/sqlbot/data/file - ./data/images:/opt/sqlbot/images - ./logs:/opt/sqlbot/app/logs depends_on: - postgres - redis postgres: image: postgres:15 environment: POSTGRES_USER: sqlbot POSTGRES_PASSWORD: sqlbot_password POSTGRES_DB: sqlbot volumes: - postgres_data:/var/lib/postgresql/data redis: image: redis:7-alpine volumes: - redis_data:/data volumes: postgres_data: redis_data: ``` -------------------------------- ### Data Model Usage Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MANIFEST.txt Example demonstrating the usage of a data model. ```python from pydantic import BaseModel class ChatMessage(BaseModel): message: str message_data = {"message": "Hello, SQLBot!"} chat_message = ChatMessage(**message_data) print(chat_message.message) ``` -------------------------------- ### Install and Deploy SQLBot with Docker Source: https://github.com/dataease/sqlbot/blob/main/README.md This command installs and runs SQLBot using Docker. It maps ports and volumes for data persistence and logging. Ensure Docker is installed on your Linux server. ```bash docker run -d \ --name sqlbot \ --restart unless-stopped \ -p 8000:8000 \ -p 8001:8001 \ -v ./data/sqlbot/excel:/opt/sqlbot/data/excel \ -v ./data/sqlbot/file:/opt/sqlbot/data/file \ -v ./data/sqlbot/images:/opt/sqlbot/images \ -v ./data/sqlbot/logs:/opt/sqlbot/app/logs \ -v ./data/sqlbot/postgresql:/var/lib/postgresql/data \ --privileged=true \ dataease/sqlbot ``` -------------------------------- ### PostgreSQL Database Configuration Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Configure PostgreSQL connection details using individual environment variables. This example sets the server, port, user, password, and database name for a PostgreSQL instance. ```dotenv POSTGRES_SERVER=db.example.com POSTGRES_PORT=5432 POSTGRES_USER=sqlbot_user POSTGRES_PASSWORD=SecurePassword@123 POSTGRES_DB=sqlbot_prod ``` -------------------------------- ### API Call Example (curl) Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MANIFEST.txt Example of how to make an API call using curl. ```shell curl -X POST https://api.example.com/chat \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{"message": "What is the capital of France?"}' ``` -------------------------------- ### API Call Example (Python) Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MANIFEST.txt Example of how to make an API call using Python's requests library. ```python import requests api_key = "YOUR_API_KEY" url = "https://api.example.com/chat" headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json" } data = { "message": "What is the capital of France?" } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: print(response.json()) else: print(f"Error: {response.status_code}") ``` -------------------------------- ### Production Environment .env Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Example .env file for configuring SQLBot in a production environment. Covers database, frontend, cache (Redis), logging, file paths, image services, and embedding settings. ```bash # 基础配置 PROJECT_NAME=SQLBot CONTEXT_PATH=/sqlbot SECRET_KEY=your-super-secret-key-min-32-chars # 数据库配置(RDS or 远程数据库) SQLBOT_DB_URL=postgresql+psycopg://user:pass@db.prod.example.com:5432/sqlbot # 前端配置 FRONTEND_HOST=https://example.com BACKEND_CORS_ORIGINS=https://example.com # 缓存配置(使用Redis) CACHE_TYPE=redis CACHE_REDIS_URL=redis://sqlbot:redis-password@redis.prod.example.com:6379/0 # 日志配置 LOG_LEVEL=INFO LOG_DIR=/var/log/sqlbot SQL_DEBUG=false # 文件路径 BASE_DIR=/opt/sqlbot UPLOAD_DIR=/var/sqlbot/uploads EXCEL_PATH=/var/sqlbot/excel # 图片服务 MCP_IMAGE_HOST=https://example.com/mcp SERVER_IMAGE_HOST=https://example.com/images/ # 嵌入配置 EMBEDDING_ENABLED=true EMBEDDING_DEFAULT_SIMILARITY=0.5 # SQL生成配置 GENERATE_SQL_QUERY_LIMIT_ENABLED=true SQLBOT_ALLOW_METADATA_QUERIES=false ``` -------------------------------- ### Basic Configuration Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Set fundamental project settings like name, API context path, token expiration, and frontend/backend origins. Ensure CORS origins are correctly formatted as comma-separated values or a JSON array. ```bash PROJECT_NAME=MySQLBot CONTEXT_PATH=/sqlbot ACCESS_TOKEN_EXPIRE_MINUTES=10080 FRONTEND_HOST=http://192.168.1.100:5173 BACKEND_CORS_ORIGINS=http://localhost:3000,http://192.168.1.100:3000 ``` -------------------------------- ### Boolean Value Configuration Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Example of setting boolean environment variables as strings in a bash script. These strings ('true' or 'false') are automatically converted to Python boolean values. ```bash SQL_DEBUG=true EMBEDDING_ENABLED=false ``` -------------------------------- ### Logging Configuration Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Set the logging level, directory for log files, and format for log messages. SQL debug logging can be enabled or disabled. ```dotenv LOG_LEVEL=INFO LOG_DIR=/var/log/sqlbot SQL_DEBUG=false ``` -------------------------------- ### Embedding Configuration Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Configure the embedding model, similarity thresholds, and top result counts for various embedding functionalities. This example enables embeddings, sets a default model, and adjusts similarity and count parameters. ```dotenv EMBEDDING_ENABLED=true DEFAULT_EMBEDDING_MODEL=shibing624/text2vec-base-chinese EMBEDDING_DEFAULT_SIMILARITY=0.5 EMBEDDING_DEFAULT_TOP_COUNT=5 ``` -------------------------------- ### Data Training Module API Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md This module manages SQL training examples. The API endpoints are documented in `endpoints.md`. ```APIDOC ## Data Training Module API Endpoints ### Description Provides API endpoints for managing SQL training examples, including CRUD operations. ### Endpoint Group `/data_training` ### Further Documentation Refer to `endpoints.md` for detailed endpoint specifications. ``` -------------------------------- ### Data Training Module Directory Structure Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Directory structure for the Data Training module, which manages SQL training examples. ```tree data_training/ ├── api/ │ ├── __init__.py │ └── data_training.py # HTTP端点 ├── curd/ │ ├── __init__.py │ └── data_training.py # 数据访问层 └── models/ ├── __init__.py └── data_training_model.py # 数据模型 ``` -------------------------------- ### SQL Query with System Variables Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md This SQL snippet demonstrates how to use system variables like `start_date` and `end_date` within a SQL query for filtering data. These variables are typically provided by the system for dynamic query generation. ```sql SELECT * FROM orders WHERE create_date >= {{start_date}} AND create_date < {{end_date}} ``` -------------------------------- ### Redis Cache Configuration Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Configure SQLBot to use Redis for caching by specifying the cache type and the Redis connection URL. This is suitable for distributed deployments. ```dotenv CACHE_TYPE=redis CACHE_REDIS_URL=redis://sqlbot:password@redis.example.com:6379/0 ``` -------------------------------- ### Get AI Model List API Endpoint Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md This Python snippet defines the GET endpoint for querying AI models. It supports filtering by a keyword and requires 'admin' privileges. The response is a list of `AiModelGridItem` objects. ```python @router.get("", response_model=list[AiModelGridItem]) async def query( session: SessionDep, keyword: Union[str, None] = Query(default=None, max_length=255) ) -> list[AiModelGridItem]: """Get all configured AI models in the system. Args: session: Database session dependency. keyword: Optional keyword to filter models by name. Returns: A list of AiModelGridItem representing the AI models. """ return aimodel_crud.get_aimodels(session=session, keyword=keyword) ``` -------------------------------- ### Foreign Key Constraint Violation Example Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This error occurs when referencing a non-existent record. Ensure the referenced data source exists before creating related records. The example shows incorrect and correct ways to create a Chat record. ```python # 错误:引用不存在的数据源 chat = Chat(datasource=999) # datasource 999不存在 # 正确:先验证数据源存在 ds = session.get(CoreDatasource, 1) if ds: chat = Chat(datasource=ds.id) ``` -------------------------------- ### Get AI Model Details API Endpoint Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md This Python snippet defines the GET endpoint for retrieving detailed information about a specific AI model by its ID. It requires 'admin' privileges and returns an `AiModelEditor` object, which includes decrypted API keys. ```python @router.get("/{id}", response_model=AiModelEditor) async def get_model_by_id( session: SessionDep, id: int = Path(description="ID") ) -> AiModelEditor: """Get a specific AI model's configuration by its ID. Args: session: Database session dependency. id: The ID of the AI model to retrieve. Returns: An AiModelEditor object containing the model's details. """ aimodel = aimodel_crud.get_aimodel(session=session, id=id) if not aimodel: raise_404(msg="AI model not found") return AiModelEditor(**aimodel.dict()) ``` -------------------------------- ### Get AI Model Details by ID Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md Retrieves the full configuration details for a specific AI model, including its decrypted API key. ```APIDOC ## GET /{id} ### Description Retrieves the detailed configuration of a specific AI model by its ID, including sensitive information like the API key. ### Method GET ### Endpoint /api/system/aimodel/{id} ### Parameters #### Path Parameters - **id** (int) - Required - The unique identifier of the AI model. ### Response #### Success Response (200) - Returns an `AiModelEditor` object with the full model configuration. #### Response Example ```json { "id": 1, "name": "GPT-4", "model_type": 1, "base_model": "gpt-4", "supplier": 1, "protocol": 1, "default_model": true, "api_domain": "https://api.openai.com/v1", "api_key": "sk-xxxxx", "config_list": [ { "key": "temperature", "val": 0.7, "name": "Temperature" } ] } ``` ### Permissions Requires `admin` role. ``` -------------------------------- ### SQLBot API Endpoints Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MANIFEST.txt This section details all HTTP API endpoints for SQLBot, including chat, data sources, terminology, SQL examples, AI models, and authentication. ```APIDOC ## API Reference This document provides a comprehensive reference to the SQLBot HTTP API endpoints. ### Endpoints Overview The SQLBot API exposes endpoints for various functionalities including: - **Chat**: For conversational interactions. - **Data Sources**: For managing data source connections. - **Terminology**: For managing business terms. - **SQL Examples**: For generating SQL queries. - **AI Models**: For interacting with AI models. - **Authentication**: For securing API access. Refer to `endpoints.md` and the `api-reference/` directory for detailed documentation on each endpoint, including request parameters, response formats, and status codes. ``` -------------------------------- ### SQLBot Automatic Retry Configuration Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md SQLBot is configured with automatic retries for API calls using the tenacity library. Ensure tenacity is installed if you are implementing custom retry logic. ```python # SQLBot已配置自动重试 from tenacity import retry, stop_after_attempt ``` -------------------------------- ### Context Length Exceeded Error Message Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This error occurs when the input and output tokens exceed the model's maximum context length. Reduce the number of SQL examples, shorten descriptions, or use a larger model. ```json { "error": "This model's maximum context length is 4096 tokens" } ``` -------------------------------- ### Import Utilities Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Import utility functions for LLM configuration, encryption, and settings. ```python from apps.ai_model.model_factory import LLMConfig, LLMFactory from common.utils.crypto import sqlbot_encrypt, sqlbot_decrypt from common.core.config import settings ``` -------------------------------- ### Tail Log File in Real-time Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md Use the tail -f command to monitor the SQLBot log file for new entries as they are written. ```bash # 实时跟踪日志 tail -f /opt/sqlbot/app/logs/sqlbot.log ``` -------------------------------- ### Search Log File by Date Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md Use grep with a date pattern to find log entries from a specific day in the SQLBot log file. ```bash # 按时间搜索 grep "2024-01-15" /opt/sqlbot/app/logs/sqlbot.log ``` -------------------------------- ### Import CRUD Functions Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Import CRUD functions for listing chats and retrieving data source lists. ```python from apps.chat.curd.chat import list_chats, create_chat from apps.datasource.crud.datasource import get_datasource_list ``` -------------------------------- ### Import Data Models Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Import necessary data models for chat and data source functionalities. ```python from apps.chat.models.chat_model import Chat, ChatRecord, ChatInfo from apps.datasource.models.datasource import CoreDatasource, CoreTable from apps.system.models.system_model import AiModelDetail ``` -------------------------------- ### System Module Directory Structure Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Directory structure for the System module, handling authentication, users, workspaces, and models. ```tree system/ ├── api/ │ ├── __init__.py │ ├── login.py # 认证端点 │ ├── user.py # 用户管理 │ ├── workspace.py # 工作空间管理 │ ├── aimodel.py # AI模型管理 │ ├── assistant.py # 小助手管理 │ ├── parameter.py # 参数管理 │ ├── apikey.py # API密钥管理 │ └── variable_api.py # 系统变量 ├── crud/ │ ├── __init__.py │ ├── user.py # 用户CRUD │ ├── workspace.py # 工作空间CRUD │ ├── aimodel_manage.py # AI模型CRUD │ ├── assistant_manage.py # 小助手CRUD │ ├── parameter_manage.py # 参数CRUD │ ├── apikey_manage.py # API密钥CRUD │ └── system_variable.py # 变量CRUD ├── models/ │ ├── __init__.py │ ├── system_model.py # ORM模型 │ └── system_variable_model.py# 变量模型 └── schemas/ ├── __init__.py ├── ai_model_schema.py # AI模型模式 ├── system_schema.py # 系统模式 ├── permission.py # 权限模式 └── logout_schema.py # 登出模式 ``` -------------------------------- ### Settings Module Directory Structure Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Directory structure for the Settings module, responsible for application settings management. ```tree settings/ ├── api/ │ ├── __init__.py │ └── base.py # 设置端点 ├── models/ │ ├── __init__.py │ └── setting_models.py # 设置模型 └── schemas/ ├── __init__.py └── setting_schemas.py # 设置模式 ``` -------------------------------- ### Configure Redis Connection URL Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md Set the CACHE_REDIS_URL environment variable to the correct Redis connection string, including credentials if necessary. ```bash # 检查Redis配置 CACHE_REDIS_URL=redis://user:password@redis.example.com:6379/0 ``` -------------------------------- ### System Module API Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md The System module handles various system management functionalities, including authentication, user management, workspace management, and AI model management. Detailed API documentation is available in `endpoints.md` and `api-reference/system-modules.md`. ```APIDOC ## System Module API Endpoints ### Description Manages core system functionalities such as authentication, user accounts, workspaces, AI models, API keys, and system variables. ### Endpoint Group `/system` ### Endpoints - `POST /login` - `POST /logout` - `GET /user/list` - `POST /user/create` - `GET /workspace/list` - `POST /workspace/create` - `GET /aimodel/list` - `POST /aimodel/create` - `GET /apikey/list` - `POST /apikey/create` - `GET /variable/list` ### Further Documentation Refer to `endpoints.md` and `api-reference/system-modules.md` for detailed endpoint specifications. ``` -------------------------------- ### Full PostgreSQL Database URL Configuration Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Configure the PostgreSQL database connection using a single, complete database URL. This overrides individual connection parameters. ```dotenv SQLBOT_DB_URL=postgresql+psycopg://sqlbot_user:SecurePassword@123@db.example.com:5432/sqlbot_prod ``` -------------------------------- ### Import API Functions Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Import API functions for chat and data source operations. ```python from apps.chat.api.chat import chats, get_chat, rename from apps.datasource.api.datasource import datasource_list, add ``` -------------------------------- ### Database Connection URL Formats Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Defines the format for constructing database URLs for PostgreSQL and MySQL. Alternatively, individual server, port, user, password, and database name variables can be used. ```text postgresql+psycopg://username:password@host:port/database mysql+pymysql://username:password@host:port/database ``` -------------------------------- ### Dynamic SQLALCHEMY_DATABASE_URI Generation Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Python code for dynamically generating the SQLALCHEMY_DATABASE_URI. It prioritizes SQLBOT_DB_URL and falls back to constructing the URI from other configuration parameters. ```python @computed_field @property def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn | str: if self.SQLBOT_DB_URL: return self.SQLBOT_DB_URL return f"postgresql+psycopg://{url_encoded_user}:{url_encoded_password}@{server}:{port}/{db}" ``` -------------------------------- ### Register New LLM Type Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Extend the system by registering a new LLM type using the LLMFactory. ```python from apps.ai_model.model_factory import BaseLLM, LLMFactory class MyLLM(BaseLLM): def _init_llm(self): ... LLMFactory.register_llm("mytype", MyLLM) ``` -------------------------------- ### Dynamic API_V1_STR Generation Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Python code demonstrating the dynamic generation of the API_V1_STR based on the CONTEXT_PATH. This property defines the base path for API version 1. ```python @computed_field @property def API_V1_STR(self) -> str: return self.CONTEXT_PATH + "/api/v1" ``` -------------------------------- ### View Recent Log Entries Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md Use tail -n to display the last N lines of the SQLBot log file, useful for quickly reviewing recent activity. ```bash # 查看最近100行 tail -n 100 /opt/sqlbot/app/logs/sqlbot.log ``` -------------------------------- ### Query AI Models Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md Retrieves a list of all configured AI models in the system. Supports filtering by a keyword. ```APIDOC ## GET / ### Description Fetches a list of all configured AI models. Allows filtering by a search keyword. ### Method GET ### Endpoint /api/system/aimodel ### Parameters #### Query Parameters - **keyword** (str) - Optional - A search keyword to filter models by name. Maximum length is 255 characters. ### Response #### Success Response (200) - Returns a list of `AiModelGridItem` objects, each containing details about an AI model. #### Response Example ```json [ { "id": 1, "name": "GPT-4", "model_type": 1, "base_model": "gpt-4", "supplier": 1, "protocol": 1, "default_model": true, "ws_mapping_count": 3, "create_time": "2024-01-01T00:00:00", "update_time": "2024-01-15T10:00:00" } ] ``` ### Permissions Requires `admin` role. ``` -------------------------------- ### Fix File Permissions for Uploads Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md If file upload fails due to insufficient permissions, ensure the directory has the correct execute permissions. ```bash chmod 755 /opt/sqlbot/data ``` -------------------------------- ### QuickCommand Enum Definition Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/types.md Enumerates quick commands for triggering specific functionalities. Use these to rapidly initiate actions like regeneration or analysis. ```python class QuickCommand(Enum): REGENERATE = '/regenerate' ANALYSIS = '/analysis' PREDICT_DATA = '/predict' ``` -------------------------------- ### Local Login Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md Obtains a JWT access token using username and password. The request is expected in HTML form format. ```APIDOC ## POST /access-token ### Description Authenticates a user using their username and password to obtain a JWT access token. ### Method POST ### Endpoint /access-token ### Parameters #### Request Body - **username** (str) - Required - Encrypted username - **password** (str) - Required - Encrypted password ### Request Example (HTML form data) ### Response #### Success Response (200) - **access_token** (str) - The JWT access token. - **token_type** (str) - The type of token, typically "bearer". #### Response Example ```json { "access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...", "token_type": "bearer" } ``` #### Error Response ```json { "detail": "Account or password error" } ``` -------------------------------- ### Add New Middleware Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Add custom HTTP middleware to the application by subclassing BaseHTTPMiddleware. ```python from fastapi import Request from starlette.middleware.base import BaseHTTPMiddleware class MyMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): ... app.add_middleware(MyMiddleware) ``` -------------------------------- ### Set SQLBot Log Level Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md Configure the LOG_LEVEL environment variable to control the verbosity of the application logs. Options include DEBUG, INFO, WARNING, and ERROR. ```text LOG_LEVEL=DEBUG ``` -------------------------------- ### Chat Module Directory Structure Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Overview of the directory structure for the Chat module, which handles conversational AI, history, and data processing. ```tree chat/ ├── api/ │ ├── __init__.py │ └── chat.py # HTTP端点 ├── curd/ │ ├── __init__.py │ └── chat.py # 数据访问层 ├── models/ │ ├── __init__.py │ └── chat_model.py # ORM模型和Pydantic模型 └── task/ ├── __init__.py └── llm.py # LLM任务处理 ``` -------------------------------- ### Settings Module API Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md The Settings module manages application-wide settings. Its API endpoints are defined in `settings/api/base.py`. ```APIDOC ## Settings Module API Endpoints ### Description Provides API endpoints for managing application settings. ### Endpoint Group `/settings` ### Further Documentation Refer to `settings/api/base.py` for specific endpoint definitions. ``` -------------------------------- ### Increase Database Connection Pool Size Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This error indicates the database connection pool limit has been reached. Increase the pool size and max overflow settings in your environment variables. ```bash # 增加连接池大小 PG_POOL_SIZE=50 PG_MAX_OVERFLOW=50 ``` -------------------------------- ### Search Log File for Errors Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md Use grep to filter the SQLBot log file and find lines containing specific keywords like 'ERROR'. ```bash # 搜索特定错误 grep "ERROR" /opt/sqlbot/app/logs/sqlbot.log ``` -------------------------------- ### Dynamic all_cors_origins Generation Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/configuration.md Python code for dynamically generating the list of all allowed CORS origins. It combines BACKEND_CORS_ORIGINS with the FRONTEND_HOST. ```python @computed_field @property def all_cors_origins(self) -> list[str]: return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [self.FRONTEND_HOST] ``` -------------------------------- ### SQLBot Log File Location Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md The main application log file for SQLBot is located at /opt/sqlbot/app/logs/sqlbot.log. ```text /opt/sqlbot/app/logs/sqlbot.log ``` -------------------------------- ### AI Model Module Functions Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md This module integrates and manages Large Language Models (LLMs). It provides a factory for creating LLM instances and includes functions for configuration. ```APIDOC ## AI Model Module Functions ### Description Provides functionalities for integrating and managing Large Language Models (LLMs), including model configuration and factory creation. ### Functions - `get_default_config()`: Retrieves the default LLM configuration. ### Classes - `LLMConfig`: Configuration for LLMs. - `BaseLLM`: Abstract base class for LLMs. - `OpenAILLM`: Implementation for OpenAI LLMs. - `OpenAIvLLM`: Implementation for vLLM. - `OpenAIAzureLLM`: Implementation for Azure OpenAI. - `LLMFactory`: Factory class for creating LLM instances. ### Further Documentation Refer to `api-reference/llm-factory.md` for detailed documentation. ``` -------------------------------- ### ChatFinishStep Enum Definition Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/types.md Defines the enumeration for the steps involved in completing a chat interaction. Use this to track the progress of a chat. ```python class ChatFinishStep(Enum): GENERATE_SQL = 1 QUERY_DATA = 2 GENERATE_CHART = 3 ``` -------------------------------- ### Datasource Module Directory Structure Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Directory structure for the Datasource module, responsible for managing data source connections and discovering tables and fields. ```tree datasource/ ├── api/ │ ├── __init__.py │ ├── datasource.py # 数据源HTTP端点 │ ├── table_relation.py # 表关系端点 │ └── recommended_problem.py # 推荐问题端点 ├── crud/ │ ├── __init__.py │ ├── datasource.py # 数据源CRUD │ ├── table.py # 表CRUD │ ├── field.py # 字段CRUD │ └── permission.py # 权限CRUD ├── models/ │ ├── __init__.py │ └── datasource.py # ORM和Pydantic模型 ├── utils/ │ ├── __init__.py │ ├── excel.py # Excel处理 │ └── db_adapter.py # 数据库适配器 └── embedding/ └── __init__.py # 嵌入向量处理 ``` -------------------------------- ### Datasource Module API Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md The Datasource module is responsible for managing data source connections, discovering tables, and fields. Detailed API documentation can be found in `endpoints.md` and `api-reference/datasource-api.md`. ```APIDOC ## Datasource Module API Endpoints ### Description Manages data source connections, table schemas, column schemas, and data previews. Also includes endpoints for table relations and recommended problems. ### Endpoint Group `/datasource` ### Endpoints - `POST /datasource/create` - `GET /datasource/list` - `GET /datasource/{id}/tables` - `GET /datasource/table/schema` - `GET /datasource/table/column` - `GET /datasource/table/preview` - `POST /datasource/table/relation` - `GET /datasource/recommended_problem` ### Further Documentation Refer to `endpoints.md` and `api-reference/datasource-api.md` for detailed endpoint specifications. ``` -------------------------------- ### AI Model Module Directory Structure Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Directory layout for the AI Model module, which integrates and manages large language models. ```tree ai_model/ ├── __init__.py ├── model_factory.py # LLM工厂类 ├── llm.py # LLM基础接口 ├── embedding.py # 嵌入模型 └── openai/ ├── __init__.py └── llm.py # OpenAI实现 ``` -------------------------------- ### Local Login API Endpoint Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md This Python snippet defines the `/access-token` endpoint for local user authentication. It accepts username and password in form data, validates user credentials and workspace association, and returns a JWT token upon successful authentication. ```python from typing import Annotated from fastapi import Depends, Query, Path, Request from fastapi.security import OAuth2PasswordRequestForm from sqlalchemy.orm import Session from apps.system.api.deps import SessionDep, Trans, OIDDep from apps.system.schemas.login import LogoutSchema from apps.system.schemas.token import Token from apps.system.schemas.user import UserSchema, UserCreateSchema, UserUpdateSchema, UserQuerySchema, UserDetailSchema from apps.system.schemas.aimodel import AiModelCreator, AiModelGridItem, AiModelEditor from apps.system.schemas.workspace import WorkspaceSchema, WorkspaceCreateSchema, WorkspaceUpdateSchema from apps.system.crud import user as user_crud, workspace as workspace_crud, aimodel as aimodel_crud from apps.system.core.security import verify_password from apps.system.core.utils.exceptions import raise_400, raise_404, raise_401 from apps.system.core.utils.deps import get_password_hash, get_current_user, get_workspace_deps from apps.system.core.utils.pagination import paginate from apps.system.core.utils.redis import redis_client from apps.system.core.utils.token import create_access_token from apps.system.core.config import settings from apps.system.core.consts import UserOrigin from apps.system.core.enums import ModelType, Supplier, Protocol from starlette.responses import StreamingResponse router = APIRouter() @router.post("/access-token") async def local_login( session: SessionDep, trans: Trans, form_data: Annotated[OAuth2PasswordRequestForm, Depends()] ) -> Token: user = user_crud.get_user_by_username(session=session, username=form_data.username) if not user or not verify_password(form_data.password, user.password): raise_400(msg="Account or password error") if not user.workspace_id: raise_400(msg="User is not associated with any workspace") if not user.is_active: raise_400(msg="User account is disabled") if user.origin != UserOrigin.LOCAL: raise_400(msg="User origin is invalid") workspace = workspace_crud.get_workspace(session=session, id=user.workspace_id) if not workspace: raise_404(msg="Workspace not found") if not workspace.is_active: raise_400(msg="Workspace is disabled") access_token = create_access_token(data={"sub": user.username, "workspace_id": user.workspace_id}) return Token(access_token=access_token, token_type="bearer") ``` -------------------------------- ### Check Redis Service Status Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md Use the redis-cli ping command to verify if the Redis service is running and accessible. ```bash # 检查Redis状态 redis-cli ping ``` -------------------------------- ### Logout Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md Logs out the current user. Requires specifying the origin of the session. ```APIDOC ## POST /logout ### Description Logs out the current user from the system. ### Method POST ### Endpoint /logout ### Parameters #### Request Body - **origin** (int) - Required - The origin of the session (e.g., 0 for local, 1 for MCP). ### Request Example ```json { "origin": 0 } ``` ### Response #### Success Response (200) Returns an empty response or a success indicator. ``` -------------------------------- ### Audit Log Decorator Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Use the system_log decorator to log operations like creation, module, and result IDs. ```python from apps.system.log import system_log, LogConfig, OperationType, OperationModules @system_log( LogConfig( operation_type=OperationType.CREATE, module=OperationModules.DATASOURCE, result_id_expr="id" ) ) ``` -------------------------------- ### Terminology Module Directory Structure Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md Directory structure for the Terminology module, used for managing the business terminology library. ```tree terminology/ ├── api/ │ ├── __init__.py │ └── terminology.py # HTTP端点 ├── curd/ │ ├── __init__.py │ └── terminology.py # 数据访问层 └── models/ ├── __init__.py └── terminology_model.py # 数据模型 ``` -------------------------------- ### ChatLog ORM Model Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/types.md The ORM model for storing chat logs, recording each step of interaction with the LLM. This model includes fields for chat type, operation, messages, and timing. ```python class ChatLog(SQLModel, table=True): __tablename__ = "chat_log" id: Optional[int] = Field(sa_column=Column(BigInteger, Identity(always=True), primary_key=True)) type: TypeEnum = Field(sa_column=Column(...)) operate: OperationEnum = Field(sa_column=Column(...)) pid: Optional[int] = Field(sa_column=Column(BigInteger, nullable=True)) ai_modal_id: Optional[int] = Field(sa_column=Column(BigInteger)) base_modal: Optional[str] = Field(max_length=255) messages: Optional[list[dict]] = Field(sa_column=Column(JSONB)) reasoning_content: Optional[str | None] = Field(sa_column=Column(Text, nullable=True)) start_time: datetime = Field(sa_column=Column(DateTime(timezone=False), nullable=True)) finish_time: datetime = Field(sa_column=Column(DateTime(timezone=False), nullable=True)) token_usage: Optional[dict | None | int] = Field(sa_column=Column(JSONB)) local_operation: bool = Field(default=False) error: bool = Field(default=False) ``` -------------------------------- ### Chat Module API Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md The Chat module handles conversational AI, including dialogue processing, history, and data management. Its API endpoints are detailed further in the `endpoints.md` and `api-reference/chat-api.md` files. ```APIDOC ## Chat Module API Endpoints ### Description Provides API endpoints for managing chat functionalities, including creating, renaming, and retrieving chat sessions and their records. ### Endpoint Group `/chat` ### Endpoints - `POST /chat/new` - `POST /chat/rename` - `GET /chat/history` - `GET /chat/records` ### Further Documentation Refer to `endpoints.md` and `api-reference/chat-api.md` for detailed endpoint specifications. ``` -------------------------------- ### TypeEnum Definition Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/types.md Defines the enumeration for chat types. Use CHAT for standard chat interactions. ```python class TypeEnum(Enum): CHAT = "0" ``` -------------------------------- ### Terminology Module API Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/MODULE-INDEX.md The Terminology module manages a business glossary. Its API endpoints are documented in `endpoints.md`. ```APIDOC ## Terminology Module API Endpoints ### Description Provides API endpoints for managing business terminology, including CRUD operations for terms. ### Endpoint Group `/terminology` ### Further Documentation Refer to `endpoints.md` for detailed endpoint specifications. ``` -------------------------------- ### Logout API Endpoint Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md This Python snippet defines the `/logout` endpoint for user logout. It requires a LogoutSchema object containing the origin of the logout request. ```python @router.post("/logout") async def logout(session: SessionDep, request: Request, dto: LogoutSchema): # TODO: Implement logout logic, potentially involving token invalidation or session cleanup. pass ``` -------------------------------- ### Set Default AI Model Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md Designates a specific AI model as the system's default model. This action deactivates the default status of any other currently set default model. ```APIDOC ## PUT /default/{id} ### Description Sets a specified AI model as the default model for the system. This operation also revokes the default status from any other model that was previously set as default. ### Method PUT ### Endpoint /api/system/aimodel/default/{id} ### Parameters #### Path Parameters - **id** (int) - Required - The unique identifier of the AI model to be set as default. ### Response #### Success Response (200) Indicates successful operation, typically with no content returned. ### Permissions Requires `admin` role. ``` -------------------------------- ### Redis Connection Failure Error Message Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This error occurs when SQLBot cannot connect to the Redis service. Ensure Redis is running and check the CACHE_REDIS_URL configuration. ```text ConnectionError: Error 111 connecting to localhost:6379. Connection refused. ``` -------------------------------- ### Validate AI Model Status API Endpoint Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md This Python snippet defines the POST endpoint for validating an AI model's connection and availability. It accepts an `AiModelCreator` object in the request body and returns a Server-Sent Events (SSE) stream indicating success or failure. ```python @router.post("/status") async def check_llm(info: AiModelCreator, trans: Trans) -> StreamingResponse: """Check the connection and availability of an AI model. Args: info: AiModelCreator object containing model details. trans: Transaction dependency. Returns: A StreamingResponse with SSE events indicating validation status. """ # Placeholder for actual validation logic # This would involve attempting to connect to the AI model's API # and returning a success or error message. if info.api_key == "sk-xxxxx": # Example validation return StreamingResponse(iter(["data: {\"content\": \"模型验证成功,可以正常使用\"}\n\n"]), media_type="text/event-stream") else: return StreamingResponse(iter(["data: {\"error\": \"API密钥无效\"}\n\n"]), media_type="text/event-stream") ``` -------------------------------- ### File Upload Failure Error Message Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This error indicates a failure to save an uploaded file. Check for insufficient disk space, insufficient file permissions, or invalid filenames. ```json { "detail": "Failed to save file" } ``` -------------------------------- ### Unique Constraint Violation Error Message Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This error indicates an attempt to insert a duplicate primary key. Review business logic to prevent duplicate resource creation. ```text IntegrityError: duplicate key value violates unique constraint "chat_pkey" ``` -------------------------------- ### Set Default AI Model API Endpoint Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md This Python snippet defines the PUT endpoint for setting a specific AI model as the default. It requires 'admin' privileges and updates the model's status by clearing the default flag on other models and setting it on the specified one. ```python @router.put("/default/{id}") async def set_default(session: SessionDep, id: int = Path(description="ID")): """Set a specific AI model as the default. Args: session: Database session dependency. id: The ID of the AI model to set as default. """ # Placeholder for actual logic to set default model # This would involve updating the 'default_model' field for the specified model # and setting it to False for all other models. pass ``` -------------------------------- ### Validate AI Model Status Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/api-reference/system-modules.md Verifies the connectivity and availability of an AI model by sending a test request. Returns results via Server-Sent Events (SSE). ```APIDOC ## POST /status ### Description Validates the status and connectivity of an AI model configuration. The response is streamed as Server-Sent Events (SSE). ### Method POST ### Endpoint /api/system/aimodel/status ### Parameters #### Request Body - **AiModelCreator** (object) - Required - An object containing the AI model's configuration details. - **name** (str) - Required - The name of the AI model. - **model_type** (int) - Required - The type of the AI model. - **base_model** (str) - Required - The base model identifier. - **supplier** (int) - Required - The supplier of the AI model. - **protocol** (int) - Required - The communication protocol. - **api_domain** (str) - Required - The API domain for the model. - **api_key** (str) - Required - The API key for authentication. - **config_list** (list) - Optional - A list of additional configuration parameters. ### Request Example ```json { "name": "Test Model", "model_type": 1, "base_model": "gpt-4", "supplier": 1, "protocol": 1, "api_domain": "https://api.openai.com/v1", "api_key": "sk-xxxxx", "config_list": [] } ``` ### Response #### Success Response (SSE) - Data streamed via SSE, indicating success or failure. #### Response Example (Success) ``` data: {"content": "模型验证成功,可以正常使用"} ``` #### Response Example (Error) ``` data: {"error": "API密钥无效"} ``` ### Permissions Requires `admin` role. ``` -------------------------------- ### Excel Import Failure Error Message Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This error indicates a failure to parse an Excel file. Ensure the file is in a supported format (.xlsx or .xls) and is not corrupted or contains unsupported data types. ```json { "detail": "Failed to parse Excel file" } ``` -------------------------------- ### OperationEnum Definition Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/types.md Enumerates the various operations SQLBot can perform during user query processing. This enum is used in chat logs to track the specific action taken. ```python class OperationEnum(Enum): GENERATE_SQL = '0' GENERATE_CHART = '1' ANALYSIS = '2' PREDICT_DATA = '3' GENERATE_RECOMMENDED_QUESTIONS = '4' GENERATE_SQL_WITH_PERMISSIONS = '5' CHOOSE_DATASOURCE = '6' GENERATE_DYNAMIC_SQL = '7' CHOOSE_TABLE = '8' FILTER_TERMS = '9' FILTER_SQL_EXAMPLE = '10' FILTER_CUSTOM_PROMPT = '11' EXECUTE_SQL = '12' GENERATE_PICTURE = '13' ``` -------------------------------- ### Rate Limit Exceeded Error Message Source: https://github.com/dataease/sqlbot/blob/main/_autodocs/errors.md This JSON indicates that the LLM API has been called too frequently. Reduce request frequency, configure retries, or upgrade API quotas. ```json { "error": "Rate limit exceeded" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.