### Install MoonPalace Debugging Tool Source: https://context7.com/moonshotai/moonpalace/llms.txt Install the latest version of the MoonPalace debugging tool using the Go toolchain. Verify the installation and view help information. ```bash # 使用 go install 安装最新版本 go install github.com/MoonshotAI/moonpalace@latest # 验证安装是否成功 moonpalace --version # 输出: moonpalace version v0.12.0 # 查看帮助信息 moonpalace --help ``` -------------------------------- ### Start MoonPalace Proxy Server Source: https://context7.com/moonshotai/moonpalace/llms.txt Start the MoonPalace local HTTP proxy server to intercept Kimi API requests. Options include specifying a port, default API key, and enabling features like auto-caching, repeat detection, and forced streaming. ```bash # 基础启动(默认端口 9988) moonpalace start # 指定自定义端口启动 moonpalace start --port 8080 # 使用默认 API Key 启动(无需在请求中手动设置) moonpalace start --key sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # 启用自动缓存功能 # --cache-min-bytes: 触发缓存的最小请求大小(字节) # --cache-ttl: 缓存有效时间(秒) # --cache-cleanup: 清理过期缓存的时间间隔(秒) moonpalace start --auto-cache --cache-min-bytes 4096 --cache-ttl 90 --cache-cleanup 86400 # 启用重复内容输出检测(防止模型重复输出消耗额外 Tokens) # --repeat-threshold: 容忍度阈值 [0,1],越高越敏感 # --repeat-min-length: 开始检测的最小字符数 moonpalace start --detect-repeat --repeat-threshold 0.3 --repeat-min-length 20 # 启用强制流式输出(减少网络超时问题) moonpalace start --force-stream # 完整配置启动示例 moonpalace start \ --port 9988 \ --key sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ --auto-cache \ --cache-min-bytes 4096 \ --cache-ttl 90 \ --detect-repeat \ --repeat-threshold 0.5 \ --repeat-min-length 100 \ --force-stream # 启动成功输出: # [MoonPalace] 2024/07/29 17:00:29 MoonPalace Starts => change base_url to "http://127.0.0.1:9988/v1" ``` -------------------------------- ### Example Exported Request JSON Source: https://github.com/moonshotai/moonpalace/blob/main/README.md This is an example of a successfully exported request in JSON format, including metadata, request, response details, category, and tags. ```json { "metadata": { "chatcmpl": "chatcmpl-2e1aa823e2c94ebdad66450a0e6df088", "content_type": "application/json", "group_id": "enterprise-tier-5", "moonpalace_id": "13", "request_id": "c07c118e-4dae-11ef-b423-62db244b9277", "requested_at": "2024-07-29 21:30:43", "server_timing": "1033", "status": "200 OK", "user_id": "cn0psmmcp7fclnphkcpg" }, "request": { "url": "https://api.moonshot.cn/v1/chat/completions", "header": "Accept: application/json\r\nAccept-Encoding: gzip\r\nConnection: keep-alive\r\nContent-Length: 2450\r\nContent-Type: application/json\r\nUser-Agent: OpenAI/Python 1.36.1\r\nX-Stainless-Arch: arm64\r\nX-Stainless-Async: false\r\nX-Stainless-Lang: python\r\nX-Stainless-Os: MacOS\r\nX-Stainless-Package-Version: 1.36.1\r\nX-Stainless-Runtime: CPython\r\nX-Stainless-Runtime-Version: 3.11.6\r\n", "body": {} }, "response": { "status": "200 OK", "header": "Content-Encoding: gzip\r\nContent-Type: application/json; charset=utf-8\r\nDate: Mon, 29 Jul 2024 13:30:43 GMT\r\nMsh-Cache: updated\r\nMsh-Gid: enterprise-tier-5\r\nMsh-Request-Id: c07c118e-4dae-11ef-b423-62db244b9277\r\nMsh-Trace-Mode: on\r\nMsh-Uid: cn0psmmcp7fclnphkcpg\r\nServer: nginx\r\nServer-Timing: inner; dur=1033\r\nStrict-Transport-Security: max-age=15724800; includeSubDomains\r\nVary: Accept-Encoding\r\nVary: Origin\r\n", "body": {} }, "category": "goodcase", "tags": [ "code", "python" ] } ``` -------------------------------- ### Query Moonshot Requests Table Source: https://context7.com/moonshotai/moonpalace/llms.txt An example of directly querying the moonshot_requests table using the sqlite3 command-line tool. This demonstrates how to retrieve recent request IDs, Moonshot IDs, status codes, and creation times. ```bash sqlite3 ~/.moonpalace/moonpalace.sqlite "SELECT id, moonshot_id, response_status_code, created_at FROM moonshot_requests ORDER BY id DESC LIMIT 5;" ``` -------------------------------- ### Enable Automatic Caching Source: https://context7.com/moonshotai/moonpalace/llms.txt Activate automatic context caching with `moonpalace start --auto-cache`. This feature reduces token consumption by caching and reusing previous requests. You can configure parameters like minimum request size for caching, cache time-to-live (TTL), and cache cleanup intervals. ```bash moonpalace start --auto-cache ``` ```bash moonpalace start \ --auto-cache \ --cache-min-bytes 4096 \ --cache-ttl 90 \ --cache-cleanup 86400 ``` -------------------------------- ### Force Streaming Output Source: https://context7.com/moonshotai/moonpalace/llms.txt Enable forced streaming output with `moonpalace start --force-stream`. This command automatically converts non-streaming requests to streaming requests, which helps prevent network timeout errors by continuously receiving data chunks. ```bash moonpalace start --force-stream ``` -------------------------------- ### Enable Repeat Output Detection Source: https://context7.com/moonshotai/moonpalace/llms.txt Activate repeat output detection using `moonpalace start --detect-repeat`. This feature uses a suffix automaton algorithm to identify and interrupt repetitive content, saving token usage. You can customize the tolerance threshold and minimum detection length. ```bash moonpalace start --detect-repeat ``` ```bash moonpalace start \ --detect-repeat \ --repeat-threshold 0.3 \ --repeat-min-length 20 ``` -------------------------------- ### Content Truncation Detection Source: https://context7.com/moonshotai/moonpalace/llms.txt MoonPalace automatically detects if model output is truncated due to a small `max_tokens` value. This feature is enabled by default when running `moonpalace start`. It logs warnings and suggests appropriate `max_tokens` values, especially in non-streaming mode. ```bash moonpalace start ``` -------------------------------- ### MoonPalace Configuration File Source: https://context7.com/moonshotai/moonpalace/llms.txt Configure MoonPalace startup parameters using a YAML file located at `$HOME/.moonpalace/config.yaml`. This allows for persistent settings without needing to specify them on the command line. ```yaml # $HOME/.moonpalace/config.yaml start: port: 8080 # 对应 --port 命令行参数 key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # 对应 --key 命令行参数 detect-repeat: # 对应 --detect-repeat 命令行选项 threshold: 0.5 # 对应 --repeat-threshold 命令行参数 min-length: 100 # 对应 --repeat-min-length 命令行参数 force-stream: true # 对应 --force-stream 命令行选项 auto-cache: min-bytes: 4096 # 对应 --cache-min-bytes 命令行选项 ttl: 90 # 对应 --cache-ttl 命令行选项 cleanup: 86400 # 对应 --cache-cleanup 命令行选项 # 自定义 API 端点(可选) endpoint: "https://api.moonshot.cn" ``` -------------------------------- ### Enable Auto-Caching with Parameters Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Enable automatic caching by using the --auto-cache parameter. Configure cache behavior with --cache-min-bytes, --cache-ttl, and --cache-cleanup. ```shell moonpalace start --port --auto-cache --cache-min-bytes 4096 --cache-ttl 90 --cache-cleanup 86400 ``` -------------------------------- ### Call Kimi API with Python OpenAI SDK via MoonPalace Source: https://context7.com/moonshotai/moonpalace/llms.txt Use the Python OpenAI SDK to call the Kimi API through the MoonPalace proxy. Modify the `base_url` to point to the MoonPalace proxy address. Supports both non-streaming and streaming calls. ```python from openai import OpenAI # 创建客户端,将 base_url 指向 MoonPalace 代理服务器 client = OpenAI( api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", base_url="http://127.0.0.1:9988/v1" # MoonPalace 代理地址 ) # 非流式调用 response = client.chat.completions.create( model="moonshot-v1-8k", messages=[ {"role": "system", "content": "你是一个乐于助人的助手。"}, {"role": "user", "content": "你好,请介绍一下自己。"} ], max_tokens=1024 ) print(response.choices[0].message.content) # 流式调用 stream = client.chat.completions.create( model="moonshot-v1-8k", messages=[ {"role": "system", "content": "你是一个乐于助人的助手。"}, {"role": "user", "content": "写一首关于春天的诗。"} ], stream=True, max_tokens=1024 ) for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) # MoonPalace 控制台将显示: # [MoonPalace] 2024/07/29 21:30:53 POST /v1/chat/completions 200 OK # [MoonPalace] 2024/07/29 21:30:53 - Request Headers: # [MoonPalace] 2024/07/29 21:30:53 - Content-Type: application/json # [MoonPalace] 2024/07/29 21:30:53 - Response Headers: # [MoonPalace] 2024/07/29 21:30:53 - Msh-Request-Id: c34f3421-4dae-11ef-b237-9620e33511ee # [MoonPalace] 2024/07/29 21:30:53 - Server-Timing: 7134 # [MoonPalace] 2024/07/29 21:30:53 - Response: # [MoonPalace] 2024/07/29 21:30:53 - id: cmpl-12be8428ebe74a9e8466a37bee7a9b11 # [MoonPalace] 2024/07/29 21:30:53 - prompt_tokens: 1449 # [MoonPalace] 2024/07/29 21:30:53 - completion_tokens: 158 # [MoonPalace] 2024/07/29 21:30:53 - total_tokens: 1607 # [MoonPalace] 2024/07/29 21:30:53 New Row Inserted: last_insert_id=15 ``` -------------------------------- ### Enable Repeat Output Detection Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Enable detection of repetitive output from Kimi models using --detect-repeat. This helps prevent unnecessary token costs, especially with expensive models. ```shell moonpalace start --port --detect-repeat --repeat-threshold 0.3 --repeat-min-length 20 ``` -------------------------------- ### List All Requests Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Use the `list` command to view recent requests. Displays ID, status, chat completion ID, request ID, server timing, and requested time. ```shell $ moonpalace list +----+--------+-------------------------------------------+--------------------------------------+---------------+---------------------+ | id | status | chatcmpl | request_id | server_timing | requested_at | +----+--------+-------------------------------------------+--------------------------------------+---------------+---------------------+ | 15 | 200 | cmpl-12be8428ebe74a9e8466a37bee7a9b11 | c34f3421-4dae-11ef-b237-9620e33511ee | 7134 | 2024-07-29 21:30:53 | | 14 | 200 | cmpl-1bf43a688a2b48eda80042583ff6fe7f | c13280e0-4dae-11ef-9c01-debcfc72949d | 3479 | 2024-07-29 21:30:46 | | 13 | 200 | chatcmpl-2e1aa823e2c94ebdad66450a0e6df088 | c07c118e-4dae-11ef-b423-62db244b9277 | 1033 | 2024-07-29 21:30:43 | | 12 | 200 | cmpl-e7f984b5f80149c3adae46096a6f15c2 | 50d5686c-4d98-11ef-ba65-3613954e2587 | 774 | 2024-07-29 18:50:06 | | 11 | 200 | chatcmpl-08f7d482b8434a869b001821cf0ee0d9 | 4c20f0a4-4d98-11ef-999a-928b67d58fa8 | 593 | 2024-07-29 18:49:58 | | 10 | 200 | chatcmpl-6f3cf14db8e044c6bfd19689f6f66eb4 | 49f30295-4d98-11ef-95d0-7a2774525b85 | 738 | 2024-07-29 18:49:55 | | 9 | 200 | cmpl-2a70a8c9c40e4bcc9564a5296a520431 | 7bd58976-4d8a-11ef-999a-928b67d58fa8 | 40488 | 2024-07-29 17:11:45 | | 8 | 200 | chatcmpl-59887f868fc247a9a8da13cfbb15d04f | ceb375ea-4d7d-11ef-bd64-3aeb95b9dfac | 867 | 2024-07-29 15:40:21 | | 7 | 200 | cmpl-36e5e21b1f544a80bf9ce3f8fc1fce57 | cd7f48d6-4d7d-11ef-999a-928b67d58fa8 | 794 | 2024-07-29 15:40:19 | | 6 | 200 | cmpl-737d27673327465fb4827e3797abb1b3 | cc6613ac-4d7d-11ef-95d0-7a2774525b85 | 670 | 2024-07-29 15:40:17 | +----+--------+-------------------------------------------+--------------------------------------+---------------+---------------------+ ``` -------------------------------- ### Inspect Request with Body Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Use the `--print request_body,response_body` flags with the `inspect` command to display the request and response body content. Note that body content can be very large. ```shell $ moonpalace inspect --chatcmpl chatcmpl-2e1aa823e2c94ebdad66450a0e6df088 --print request_body,response_body # 由于 body 信息过于冗长,这里不再完整展示 body 详细内容 +--------------------------------------------------+--------------------------------------------------+ | request_body | response_body | +--------------------------------------------------+--------------------------------------------------+ | ... | ... | +--------------------------------------------------+--------------------------------------------------+ ``` -------------------------------- ### Force Stream Output Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Force all /v1/chat/completions requests to use streaming output with the --force-stream option. This can help mitigate connection errors and timeouts. ```shell moonpalace start --port --force-stream ``` -------------------------------- ### Detect Truncated Content Source: https://github.com/moonshotai/moonpalace/blob/main/README.md MoonPalace detects if Kimi model output is truncated or incomplete. This feature is enabled by default and logs warnings when max_tokens is too small. ```shell [MoonPalace] 2024/08/05 19:06:19 it seems that your max_tokens value is too small, please set a larger value ``` -------------------------------- ### Inspect Specific Request Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Use the `inspect` command with `--id`, `--chatcmpl`, or `--requestid` to retrieve details of a specific request. By default, request and response bodies are not shown. ```shell # Inspect by ID $ moonpalace inspect --id 13 # Inspect by chatcmpl ID $ moonpalace inspect --chatcmpl chatcmpl-2e1aa823e2c94ebdad66450a0e6df088 # Inspect by request ID $ moonpalace inspect --requestid c07c118e-4dae-11ef-b423-62db244b9277 +--------------------------------------------------------------+ | metadata | +--------------------------------------------------------------+ | { | "chatcmpl": "chatcmpl-2e1aa823e2c94ebdad66450a0e6df088", | "content_type": "application/json", | "group_id": "enterprise-tier-5", | "moonpalace_id": "13", | "request_id": "c07c118e-4dae-11ef-b423-62db244b9277", | "requested_at": "2024-07-29 21:30:43", | "server_timing": "1033", | "status": "200 OK", | "user_id": "cn0psmmcp7fclnphkcpg" | } +--------------------------------------------------------------+ ``` -------------------------------- ### Export Request Data with MoonPalace Source: https://context7.com/moonshotai/moonpalace/llms.txt Use the `moonpalace export` command to save request data as JSON or curl commands. This is useful for debugging, reporting issues, or reproducing tests. You can specify the request by ID or chat completion ID and choose to export to standard output, a directory, or a specific file. ```bash moonpalace inspect --id 13 --print-response --merge-event-stream ``` ```bash moonpalace export --id 13 ``` ```bash moonpalace export --chatcmpl chatcmpl-xxx --directory ~/Downloads/ ``` ```bash moonpalace export --id 13 --bad --tag "code" --tag "python" --directory ~/Downloads/ ``` ```bash moonpalace export --id 13 --good --tag "translation" --directory ~/Downloads/ ``` ```bash moonpalace export --id 13 --curl ``` ```bash moonpalace export --id 13 --output ~/Downloads/request.json ``` -------------------------------- ### Repeat Output Detection Log Source: https://github.com/moonshotai/moonpalace/blob/main/README.md When repeat output is detected with --detect-repeat enabled, MoonPalace logs a message indicating a potential issue with content repetition. ```shell [MoonPalace] 2024/08/05 18:20:37 it appears that there is an issue with content repeating in the current response ``` -------------------------------- ### Create Moonshot Requests Table Source: https://context7.com/moonshotai/moonpalace/llms.txt Defines the schema for the moonshot_requests table, which stores details of API requests and responses. Includes fields for request method, path, query, headers, body, response status, and timing metrics. ```sql -- 请求记录表 create table moonshot_requests ( id integer not null primary key autoincrement, request_method text not null, -- HTTP 方法 (GET/POST/etc.) request_path text not null, -- 请求路径 request_query text not null, -- 查询参数 request_content_type text, -- 请求 Content-Type request_id text, -- 客户端请求 ID moonshot_id text, -- Kimi API 返回的完成 ID (chatcmpl) moonshot_gid text, -- 用户组 ID moonshot_uid text, -- 用户 ID moonshot_request_id text, -- Kimi API 请求 ID moonshot_server_timing integer, -- 服务器处理时间 (ms) response_status_code integer, -- HTTP 状态码 response_content_type text, -- 响应 Content-Type request_header text, -- 请求头 request_body text, -- 请求体 response_header text, -- 响应头 response_body text, -- 响应体 error text, -- 错误信息 response_ttft integer, -- Time To First Token (ms) response_tpot integer, -- Time Per Output Token (ms) response_otps real, -- Output Tokens Per Second latency integer, -- 总延迟 (ns) endpoint text, -- API 端点 created_at text default (datetime('now', 'localtime')) not null ); ``` -------------------------------- ### Export Requests with Curl Command Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Export specific requests using the `export` command with options like `--id`, `--chatcmpl`, or `--requestid` to identify the request. Tag requests and specify a directory for the exported JSON file. ```shell # id/chatcmpl/requestid 选项只需要任选其一即可检索出对应的请求 $ moonpalace export \ --id 13 \ --chatcmpl chatcmpl-2e1aa823e2c94ebdad66450a0e6df088 \ --requestid c07c118e-4dae-11ef-b423-62db244b9277 \ --good/--bad \ --tag "code" --tag "python" \ --directory $HOME/Downloads/ ``` -------------------------------- ### Create Moonshot Caches Table Source: https://context7.com/moonshotai/moonpalace/llms.txt Defines the schema for the moonshot_caches table, used to store caching information for API responses. Includes fields for cache ID, content hash, size, API key identifier, and timestamps. ```sql -- 缓存记录表 create table moonshot_caches ( id integer not null primary key autoincrement, cache_id text not null, -- Kimi API 缓存 ID hash text not null, -- 内容哈希值 n_bytes integer not null, -- 内容大小 k_ident text not null, -- API Key 标识 created_at text default (datetime('now', 'localtime')) not null, updated_at text -- 最后使用时间 ); ``` -------------------------------- ### Filter Requests with Predicates Source: https://github.com/moonshotai/moonpalace/blob/main/README.md Use the `--predicate` argument to filter captured requests based on expressions. Expressions follow the 'Field Operator Literal' format and can be combined with '&&' and '||'. JSON fields can be accessed using dot notation. ```shell $ moonpalace list \ --predicate "request_body.model == 'moonshot-v1-128k' || request_body.model == 'moonshot-v1-8k'" \ --predicate "response_body.choices.0.finish_reason == 'length'" ``` -------------------------------- ### Cleanup Historical Requests with MoonPalace Source: https://context7.com/moonshotai/moonpalace/llms.txt The `moonpalace cleanup` command removes historical request records from the database to free up storage. You can specify a date or datetime to delete records created before that point. By default, it cleans records older than 7 days. ```bash moonpalace cleanup ``` ```bash moonpalace cleanup --before "2024-07-01" ``` ```bash moonpalace cleanup --before "2024-07-29 12:00:00" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.