### Certbot Installation and SSL Configuration Source: https://github.com/songquanpeng/one-api/blob/main/README.md Commands for installing Certbot on Ubuntu and using it to obtain and configure SSL certificates for Nginx, enabling HTTPS for the One API service. This process involves installing Certbot, linking it to the PATH, and running the configuration tool. ```bash # Ubuntu 安装 certbot: sudo snap install --classic certbot sudo ln -s /snap/bin/certbot /usr/bin/certbot # 生成证书 & 修改 Nginx 配置 sudo certbot --nginx # 根据指示进行操作 # 重启 Nginx sudo service nginx restart ``` -------------------------------- ### GET /api/user/self Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md Retrieves information about the currently logged-in user. ```APIDOC ## GET /api/user/self ### Description Retrieves information about the currently logged-in user. ### Method GET ### Endpoint /api/user/self ### Parameters No specific parameters are required for this endpoint. ### Request Example (No request body for GET requests) ### Response #### Success Response (200) - **user_info** (object) - Contains the user's profile information. #### Response Example ```json { "message": "User information retrieved successfully", "success": true, "data": { "user_info": { "id": 1, "username": "root", "email": "root@example.com" } } } ``` ``` -------------------------------- ### Display Help Information Source: https://github.com/songquanpeng/one-api/blob/main/README.md Use the --help command-line argument to view detailed usage instructions and a description of all available parameters for the One API application. ```bash ./oneapi --help ``` -------------------------------- ### Display System Version Source: https://github.com/songquanpeng/one-api/blob/main/README.md Execute the --version command-line argument to print the current version of the One API system and exit the application. ```bash ./oneapi --version ``` -------------------------------- ### Docker Deployment for One API Source: https://github.com/songquanpeng/one-api/blob/main/README.md Provides Docker commands for deploying One API with SQLite or MySQL databases. It includes instructions for port mapping, volume mounting for data persistence, and considerations for high concurrency and image pulling. ```shell # 使用 SQLite 的部署命令: docker run --name one-api -d --restart always -p 3000:3000 -e TZ=Asia/Shanghai -v /home/ubuntu/data/one-api:/data justsong/one-api # 使用 MySQL 的部署命令,在上面的基础上添加 -e SQL_DSN="root:123456@tcp(localhost:3306)/oneapi",请自行修改数据库连接参数,不清楚如何修改请参见下面环境变量一节。 # 例如: docker run --name one-api -d --restart always -p 3000:3000 -e SQL_DSN="root:123456@tcp(localhost:3306)/oneapi" -e TZ=Asia/Shanghai -v /home/ubuntu/data/one-api:/data justsong/one-api ``` -------------------------------- ### Create Initial Root System Management Token Source: https://github.com/songquanpeng/one-api/blob/main/README.md Setting INITIAL_ROOT_ACCESS_TOKEN will create a system management token for the root user during the initial system startup, using the provided token value. ```bash INITIAL_ROOT_ACCESS_TOKEN=your_management_access_token ``` -------------------------------- ### Create Initial Root User Token Source: https://github.com/songquanpeng/one-api/blob/main/README.md If INITIAL_ROOT_TOKEN is set, a root user token with the specified value will be automatically created upon the system's first startup. ```bash INITIAL_ROOT_TOKEN=your_secret_root_token ``` -------------------------------- ### Set System Theme Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure the THEME environment variable to customize the system's appearance. Refer to the documentation for available theme options. The default theme is 'default'. ```bash THEME=dark ``` -------------------------------- ### Configure SQL Database Connection Source: https://github.com/songquanpeng/one-api/blob/main/README.md Use the SQL_DSN environment variable to specify a database for One API, supporting MySQL and PostgreSQL. The program will automatically create tables if the 'oneapi' database exists. Connection parameters can be adjusted for local or cloud databases, including TLS settings. ```bash # MySQL Example SQL_DSN=root:123456@tcp(localhost:3306)/oneapi # PostgreSQL Example SQL_DSN=postgres://postgres:123456@localhost:5432/oneapi?tls=skip-verify ``` -------------------------------- ### Configure Proxy for User Content Requests Source: https://github.com/songquanpeng/one-api/blob/main/README.md Use USER_CONTENT_REQUEST_PROXY to set a proxy for requesting user-uploaded content. ```bash USER_CONTENT_REQUEST_PROXY=http://content-proxy:8080 ``` -------------------------------- ### 为用户充值额度 API Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md 提供了一个 POST 请求接口 `/api/topup`,用于向指定用户充值额度。需要提供用户 ID、要充值的额度以及一个可选的备注信息。 ```json { "user_id": 1, "quota": 100000, "remark": "充值 100000 额度" } ``` -------------------------------- ### POST /api/topup Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md Credits a specified amount of quota to a given user. ```APIDOC ## POST /api/topup ### Description Credits a specified amount of quota to a given user. ### Method POST ### Endpoint /api/topup ### Parameters #### Request Body - **user_id** (integer) - Required - The ID of the user to top up. - **quota** (integer) - Required - The amount of quota to add. - **remark** (string) - Optional - A remark for the transaction. ### Request Example ```json { "user_id": 1, "quota": 100000, "remark": "Recharge 100000 quota" } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message of the top-up. #### Response Example ```json { "message": "User quota topped up successfully", "success": true, "data": {} } ``` ``` -------------------------------- ### Nginx Configuration for One API Source: https://github.com/songquanpeng/one-api/blob/main/README.md A sample Nginx server block configuration for proxying requests to the One API service. It includes settings for client body size, proxy headers, and read timeouts, crucial for handling large requests and GPT-4 interactions. ```nginx server{ server_name openai.justsong.cn; # 请根据实际情况修改你的域名 location / { client_max_body_size 64m; proxy_http_version 1.1; proxy_pass http://localhost:3000; # 请根据实际情况修改你的端口 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_cache_bypass $http_upgrade; proxy_set_header Accept-Encoding gzip; proxy_read_timeout 300s; # GPT-4 需要较长的超时时间,请自行调整 } } ``` -------------------------------- ### Register new theme in Go configuration Source: https://github.com/songquanpeng/one-api/blob/main/web/README.md This Go code snippet demonstrates how to register a new theme by adding its name to the `ValidThemes` slice. This ensures that the new theme is recognized and can be used by the One API application. ```go package config var ValidThemes = []string{ "default", "berry", "air", // Add your theme name here } ``` -------------------------------- ### 调用 API 获取 Token Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md 展示了如何使用 Token 作为请求头的 Authorization 字段来调用 One API 的测试渠道接口。需要将获取到的 Token 放置在 Authorization 字段中。 ```shell curl -X GET "YOUR_ONE_API_ADDRESS/api/channel/test?" \ -H "Authorization: Bearer YOUR_TOKEN" ``` -------------------------------- ### Set Test Prompt for Model Testing Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure TEST_PROMPT with the user prompt to be used when testing models. The default prompt is 'Print your model name exactly and do not output without any other text.'. ```bash TEST_PROMPT=Tell me about yourself. ``` -------------------------------- ### Configure Global API and Web Rate Limits Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set GLOBAL_API_RATE_LIMIT and GLOBAL_WEB_RATE_LIMIT to control the maximum number of requests per IP address within a three-minute window for API and web traffic, respectively. Defaults are 180 for API and 60 for Web. ```bash GLOBAL_API_RATE_LIMIT=180 GLOBAL_WEB_RATE_LIMIT=60 ``` -------------------------------- ### Configure New Channel Settings in React (JavaScript) Source: https://github.com/songquanpeng/one-api/blob/main/web/berry/README.md This snippet demonstrates how to define configuration settings for a new channel in `Config.js`. The `typeConfig` object maps channel IDs to their specific input labels, prompts, and model group identifiers. This allows for customized UI elements and backend interactions for each channel. ```javascript const typeConfig = { // key 为渠道ID 3: { inputLabel: { // 输入框名称 配置 // 对应的字段名称 base_url: "AZURE_OPENAI_ENDPOINT", other: "默认 API 版本", }, prompt: { // 输入框提示 配置 // 对应的字段名称 base_url: "请填写AZURE_OPENAI_ENDPOINT", // 注意:通过判断 `other` 是否有值来判断是否需要显示 `other` 输入框, 默认是没有值的 other: "请输入默认API版本,例如:2024-03-01-preview", }, modelGroup: "openai", // 模型组名称,这个值是给 填入渠道支持模型 按钮使用的。 填入渠道支持模型 按钮会根据这个值来获取模型组,如果填写默认是 openai }, }; ``` -------------------------------- ### Specify Gemini API Version Source: https://github.com/songquanpeng/one-api/blob/main/README.md Use GEMINI_VERSION to specify the version of the Gemini API that One API will use. The default version is 'v1'. ```bash GEMINI_VERSION=v1.5 ``` -------------------------------- ### Configure Relay Proxy for API Requests Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set the RELAY_PROXY environment variable to use a specific proxy for making API requests. ```bash RELAY_PROXY=http://your-proxy-address:port ``` -------------------------------- ### Configure Independent Logging Database Source: https://github.com/songquanpeng/one-api/blob/main/README.md The LOG_SQL_DSN environment variable allows you to set up a separate database specifically for storing logs in the 'logs' table. This configuration supports both MySQL and PostgreSQL, similar to the main SQL_DSN. ```bash LOG_SQL_DSN=postgres://user:password@host:port/logdb ``` -------------------------------- ### Specify Server Port with Command-Line Argument Source: https://github.com/songquanpeng/one-api/blob/main/README.md Use the --port command-line argument to set the port number on which the server will listen. The default port is 3000. ```bash ./oneapi --port 8080 ``` -------------------------------- ### Specify Log Directory with Command-Line Argument Source: https://github.com/songquanpeng/one-api/blob/main/README.md The --log-dir argument allows you to specify a directory for storing log files. If not provided, logs are saved in the 'logs' folder within the working directory. ```bash ./oneapi --log-dir /var/log/oneapi ``` -------------------------------- ### Set Frontend Base URL for Redirects Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure the FRONTEND_BASE_URL environment variable to redirect frontend requests to a specified address. This is particularly useful for server-side configuration and ensuring that frontend assets are served from the correct location. ```bash FRONTEND_BASE_URL=https://openai.justsong.cn ``` -------------------------------- ### Authentication Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md One API supports two authentication methods: Cookie and Token. For Token authentication, the token should be passed in the Authorization header. ```APIDOC ## Authentication One API supports two authentication methods: Cookie and Token. For Token authentication, obtain the token as shown in the image and include it in the `Authorization` header of your requests. ### Example Token Usage ``` Authorization: Bearer YOUR_TOKEN_HERE ``` ``` -------------------------------- ### One API 请求响应格式 Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md 定义了 One API 的标准请求和响应格式,主要使用 JSON。响应体包含 message、success 和 data 字段,用于传递请求信息、状态和具体数据。 ```json { "message": "请求信息", "success": true, "data": {} } ``` -------------------------------- ### Specify Node Type (Master/Slave) Source: https://github.com/songquanpeng/one-api/blob/main/README.md Use the NODE_TYPE environment variable to designate the role of the current node. Options are 'master' or 'slave'. The default setting is 'master'. ```bash NODE_TYPE=slave ``` -------------------------------- ### Configure Gemini Safety Settings Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set GEMINI_SAFETY_SETTING to configure safety thresholds for Gemini models. The default value is 'BLOCK_NONE'. ```bash GEMINI_SAFETY_SETTING=BLOCK_MEDIUM_AND_ABOVE ``` -------------------------------- ### Set Metric Success Rate Threshold Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure METRIC_SUCCESS_RATE_THRESHOLD to define the minimum acceptable success rate for a channel. Channels below this threshold may be disabled if ENABLE_METRIC is true. The default is 0.8. ```bash METRIC_SUCCESS_RATE_THRESHOLD=0.9 ``` -------------------------------- ### Modify build command in package.json Source: https://github.com/songquanpeng/one-api/blob/main/web/README.md This command modifies the build process in `package.json` to output the built files to a specific theme directory. It assumes the build is done using `react-scripts` and then moves the `build` folder to a parent directory named after the theme. ```json { "scripts": { "build": "react-scripts build && mv -f build ../build/default" } } ``` -------------------------------- ### Set Timeout for User Content Downloads Source: https://github.com/songquanpeng/one-api/blob/main/README.md USER_CONTENT_REQUEST_TIMEOUT specifies the timeout in seconds for downloading user-uploaded content, such as images. ```bash USER_CONTENT_REQUEST_TIMEOUT=60 ``` -------------------------------- ### Enforce Usage Return in Stream Mode Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set ENFORCE_INCLUDE_USAGE to 'true' to mandate the return of usage information even in stream mode. The default setting is 'false'. ```bash ENFORCE_INCLUDE_USAGE=true ``` -------------------------------- ### Set Data Gym Cache Directory Source: https://github.com/songquanpeng/one-api/blob/main/README.md DATA_GYM_CACHE_DIR serves a similar purpose to TIKTOKEN_CACHE_DIR by caching data, though TIKTOKEN_CACHE_DIR has higher priority. ```bash DATA_GYM_CACHE_DIR=/path/to/data/gym/cache ``` -------------------------------- ### Recharge Link Parameters Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md Information about additional parameters appended to recharge links for user and transaction identification. ```APIDOC ## Recharge Link Parameters When a user clicks the recharge button, One API appends user and transaction information to the recharge URL. ### Example URL `https://example.com?username=root&user_id=1&transaction_id=4b3eed80-55d5-443f-bd44-fb18c648c837` ### Available Parameters - **username** (string) - The username of the user. - **user_id** (integer) - The ID of the user. - **transaction_id** (string) - A unique identifier for the transaction. These parameters can be parsed to retrieve user and transaction details, which can then be used to call the `/api/topup` endpoint. ``` -------------------------------- ### Enable Channel Disabling Based on Success Rate Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set ENABLE_METRIC to 'true' to allow channels to be automatically disabled if their request success rate falls below a threshold. The default is 'false'. ```bash ENABLE_METRIC=true ``` -------------------------------- ### Configure Cache Synchronization Frequency Source: https://github.com/songquanpeng/one-api/blob/main/README.md The SYNC_FREQUENCY environment variable determines how often the system synchronizes configuration with the database when caching is enabled. The value is in seconds, with a default of 600 seconds (10 minutes). ```bash SYNC_FREQUENCY=60 ``` -------------------------------- ### Request and Response Formats Source: https://github.com/songquanpeng/one-api/blob/main/docs/API.md One API uses JSON for both requests and responses. The standard response format includes a message, success status, and data payload. ```APIDOC ## Request and Response Formats One API uses JSON format for requests and responses. ### Standard Response Format ```json { "message": "Request information", "success": true, "data": {} } ``` ``` -------------------------------- ### Set Tokenizer Cache Directory Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure TIKTOKEN_CACHE_DIR to specify a directory for caching tokenizer data. This is useful for unstable network environments or offline deployments, preventing issues during startup when common tokenizers are downloaded. ```bash TIKTOKEN_CACHE_DIR=/path/to/tokenizer/cache ``` -------------------------------- ### Add New Channel Option in React (JavaScript) Source: https://github.com/songquanpeng/one-api/blob/main/web/berry/README.md This snippet shows how to add a new channel option to the `CHANNEL_OPTIONS` object in `ChannelConstants.js`. Each option includes a unique ID, display text, value, and color for UI representation. This is essential for integrating new service providers. ```javascript export const CHANNEL_OPTIONS = { //key 为渠道ID 1: { key: 1, // 渠道ID text: "OpenAI", // 渠道名称 value: 1, // 渠道ID color: "primary", // 渠道列表显示的颜色 }, }; ``` -------------------------------- ### Enable In-Memory Cache with Latency Considerations Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set MEMORY_CACHE_ENABLED to 'true' to activate an in-memory cache. Be aware that this can introduce a delay in user quota updates. The default value is 'false'. ```bash MEMORY_CACHE_ENABLED=true ``` -------------------------------- ### Configure Redis Cache with Environment Variables Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set the REDIS_CONN_STRING environment variable to enable Redis caching. This variable accepts a connection string and can be configured for sentinel or cluster modes by providing a list of nodes and additional variables like REDIS_PASSWORD and REDIS_MASTER_NAME. Note that Redis caching may introduce data lag if database access latency is already low. ```bash REDIS_CONN_STRING=redis://default:redispw@localhost:49153 REDIS_PASSWORD=your_redis_password REDIS_MASTER_NAME=mymaster ``` -------------------------------- ### Enable Batch Database Updates Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set BATCH_UPDATE_ENABLED to 'true' to enable aggregated batch updates to the database. This can help mitigate 'Too many connections' errors but may cause slight delays in user quota updates. The default value is 'false'. ```bash BATCH_UPDATE_ENABLED=true ``` -------------------------------- ### Set Channel Balance Update Frequency Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure CHANNEL_UPDATE_FREQUENCY to periodically update channel balances. The value is specified in minutes. If not set, balances will not be updated automatically. ```bash CHANNEL_UPDATE_FREQUENCY=1440 ``` -------------------------------- ### Configure Metric Success Rate Queue Size Source: https://github.com/songquanpeng/one-api/blob/main/README.md Set METRIC_QUEUE_SIZE to adjust the size of the queue used for collecting request success rate statistics. The default value is 10. ```bash METRIC_QUEUE_SIZE=20 ``` -------------------------------- ### Configure Polling Interval for Batch Updates Source: https://github.com/songquanpeng/one-api/blob/main/README.md The POLLING_INTERVAL environment variable controls the request interval in seconds when performing batch updates for channel balances and testing availability. By default, there is no specific interval. ```bash POLLING_INTERVAL=5 ``` -------------------------------- ### Set Channel Test Frequency Source: https://github.com/songquanpeng/one-api/blob/main/README.md Use CHANNEL_TEST_FREQUENCY to define how often channels are tested for availability. The interval is set in minutes. No automatic checking occurs if this variable is not configured. ```bash CHANNEL_TEST_FREQUENCY=1440 ``` -------------------------------- ### Set Relay Request Timeout Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure RELAY_TIMEOUT in seconds to set a timeout for relay requests. By default, there is no timeout specified. ```bash RELAY_TIMEOUT=30 ``` -------------------------------- ### Set Session Secret for Persistent User Sessions Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure the SESSION_SECRET environment variable with a fixed secret key. This ensures that user session cookies remain valid across system restarts, allowing logged-in users to maintain their sessions without re-authentication. ```bash SESSION_SECRET=random_string_here ``` -------------------------------- ### Set Batch Update Aggregation Interval Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure BATCH_UPDATE_INTERVAL to specify the time interval in seconds for batch update aggregation. The default value is 5 seconds. ```bash BATCH_UPDATE_INTERVAL=5 ``` -------------------------------- ### Set SQLite Lock Wait Timeout Source: https://github.com/songquanpeng/one-api/blob/main/README.md Configure SQLITE_BUSY_TIMEOUT in milliseconds to set the lock wait timeout for SQLite databases. The default value is 3000 milliseconds. ```bash SQLITE_BUSY_TIMEOUT=5000 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.