AI智能摘要
Grok2API是基于FastAPI构建的Grok token管理工具,可将Grok.com的Web服务转换为OpenAI兼容的API格式。该项目支持流式对话、图像生成与编辑、视频生成、语音聊天等多样化功能,并提供后台管理和WebUI界面。系统采用容器化部署,支持多种存储方式,具备账号池并发和自动负载均衡能力。当前Basic账号提供80次/20小时调用配额,Super账号提供140次/2小时配额,支持从grok-3到grok-4.2系列模型的调用。
— 此摘要由AI分析文章内容生成,仅供参考。
项目介绍
基于 FastAPI 构建的 Grok2API,支持将 Grok.com 的 Web 端服务一键转换为 OpenAI API 兼容的调用格式。项目支持原生流式对话、非流式对话、图像生成、图像编辑、视频生成、工具调用、语音聊天、一键NSFW、号池并发与自动负载均衡一体化,且提供后台管理、功能玩法等 WebUI,方便小白快速上手。
快速开始
环境变量
| 变量名 | 说明 | 默认值 | 示例 |
|---|---|---|---|
LOG_LEVEL | 日志级别 | INFO | DEBUG |
LOG_FILE_ENABLED | 启用日志文件 | true | false |
DATA_DIR | 数据存放路径 | ./data | /data |
SERVER_HOST | 服务监听地址 | 0.0.0.0 | 0.0.0.0 |
SERVER_PORT | 服务端口 | 8000 | 8000 |
HOST_PORT | Docker Compose 宿主机映射端口 | 8000 | 9000 |
SERVER_WORKERS | Uvicorn worker 数量 | 1 | 2 |
SERVER_STORAGE_TYPE | 存储类型,支持local/redis/mysql/pgsql | local | pgsql |
SERVER_STORAGE_URL | 存储连接串,存储类型为 local 时可为空 | "" | postgresql+asyncpg://user:password@host:5432/db |
Docker Compose
services:
grok2api:
container_name: grok2api
image: ghcr.io/chenyme/grok2api:latest
ports:
- "${HOST_PORT:-8000}:${SERVER_PORT:-8000}"
environment:
TZ: Asia/Shanghai
LOG_LEVEL: ${LOG_LEVEL:-INFO}
SERVER_HOST: ${SERVER_HOST:-0.0.0.0}
SERVER_PORT: ${SERVER_PORT:-8000}
SERVER_WORKERS: ${SERVER_WORKERS:-1}
SERVER_STORAGE_TYPE: ${SERVER_STORAGE_TYPE:-local}
SERVER_STORAGE_URL: ${SERVER_STORAGE_URL:-}
# 启用 CF 自动刷新: 取消以下三行注释,并取消底部 flaresolverr 服务的注释
# FLARESOLVERR_URL: http://flaresolverr:8191
# CF_REFRESH_INTERVAL: "600"
# CF_TIMEOUT: "60"
# SERVER_STORAGE_TYPE: (local, redis, mysql, pgsql) default: local
# SERVER_STORAGE_URL: (local mode is empty) default: empty
# Redis: redis://localhost:6379/0 or redis://:password@localhost:6379/0
# MySQL: mysql+aiomysql://user:pass@localhost/db
# PgSQL: postgresql+asyncpg://user:pass@localhost/db
command:
- sh
- -c
- >
granian --interface asgi --host "$${SERVER_HOST:-0.0.0.0}"
--port "$${SERVER_PORT:-8000}" --workers "$${SERVER_WORKERS:-1}" main:app
volumes:
- ./data:/app/data
- ./logs:/app/logs
restart: unless-stopped
# 如果出口 IP 不干净,可取消以下注释使用 Warp 作为落地代理
# 启用后将 proxy.base_proxy_url 设为 socks5://warp:1080
# warp:
# container_name: warp
# image: caomingjun/warp:latest
# restart: unless-stopped
# ports:
# - "127.0.0.1:1080:1080"
# environment:
# - WARP_SLEEP=2
# cap_add:
# - NET_ADMIN
# 启用 CF 自动刷新时取消以下注释
# flaresolverr:
# container_name: flaresolverr
# image: ghcr.io/flaresolverr/flaresolverr:latest
# ports:
# - "127.0.0.1:8191:8191"
# environment:
# TZ: Asia/Shanghai
# LOG_LEVEL: info
# restart: unless-stopped
git clone https://github.com/chenyme/grok2api cd grok2api docker compose up -d
修改 .env 或 docker-compose.yml 文件生效,环境变量见上。
其他部署方式
详见文档:传送门
使用教程
1. 项目地址
- 管理后台:
http://<host>:8000/admin - 功能玩法:
http://<host>:8000/(管理后台开启后生效) - 默认密码:
grok2api
2. 账号配额
GROK 官方网页更新后并未真实暴露 rate-limits 接口,导致无法准确计算 Token 剩余,请耐心等待官方接口上线,目前自动刷新后会统一更新为剩余 8 次
- Basic 账号:80 次 / 20h
- Super 账号:140 次 / 2h
3. 模型支持
| 模型名称 | 调用计次 | 可用账号 | 对话功能 | 图像功能 | 视频功能 |
|---|---|---|---|---|---|
grok-3 | 1 | Basic/Super | 支持 | 支持 | – |
grok-3-mini | 1 | Basic/Super | 支持 | 支持 | – |
grok-3-thinking | 1 | Basic/Super | 支持 | 支持 | – |
grok-4 | 1 | Basic/Super | 支持 | 支持 | – |
grok-4-mini | 1 | Basic/Super | 支持 | 支持 | – |
grok-4-thinking | 1 | Basic/Super | 支持 | 支持 | – |
grok-4-heavy | 4 | Super | 支持 | 支持 | – |
grok-4.1-mini | 1 | Basic/Super | 支持 | 支持 | – |
grok-4.1-fast | 1 | Basic/Super | 支持 | 支持 | – |
grok-4.1-expert | 4 | Basic/Super | 支持 | 支持 | – |
grok-4.1-thinking | 4 | Basic/Super | 支持 | 支持 | – |
grok-4.20-beta | 1 | Basic/Super | 支持 | 支持 | – |
grok-imagine-1.0 | – | Basic/Super | – | 支持 | – |
grok-imagine-1.0-fast | – | Basic/Super | – | 支持 | – |
grok-imagine-1.0-edit | – | Basic/Super | – | 支持 | – |
grok-imagine-1.0-video | – | Basic/Super | – | – | 支持 |
接口信息
1. POST/v1/chat/completions
OpenAI Chat Completions API 通用接口,支持聊天对话、图像生成、图像编辑、视频生成
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-4",
"messages": [{"role":"user","content":"你好"}]
}'
2. POST /v1/responses
OpenAI Responses API 兼容接口,支持聊天对话、图像生成、图像编辑、视频生成
curl http://localhost:8000/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-4",
"input": "解释一下量子隧穿",
"stream": true
}'
3. POST /v1/images/generations
OpenAI Image Generations API 兼容接口,支持图像生成
curl http://localhost:8000/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-imagine-1.0",
"prompt": "一只在太空漂浮的猫",
"n": 1
}'
4. POST /v1/images/edits
OpenAI Image Edits API 兼容接口,支持图像编辑(multipart/form-data)
curl http://localhost:8000/v1/images/edits \ -H "Authorization: Bearer $GROK2API_API_KEY" \ -F "model=grok-imagine-1.0-edit" \ -F "prompt=把图片变清晰" \ -F "image=@/path/to/image.png" \ -F "n=1"
5. POST /v1/videos
OpenAI Videos API 兼容接口,支持视频生成(multipart/form-data)
curl http://localhost:8000/v1/videos \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-imagine-1.0-video",
"prompt": "霓虹雨夜街头,慢镜头追拍",
"size": "1792x1024",
"seconds": 18,
"quality": "standard"
}'
发布者:木木,转载请注明出处:https://blog.huzz.cn/10798.html
微信
支付宝