🚀大模型落地开发实战指南!请关注微信公众号:「AGI启程号」 深入浅出,助你轻松入门!
📚 数据分析、深度学习、大模型与算法的综合进阶,尽在CSDN博客主页
本篇文章是上一篇文章基础上补充总结,可以跳过直接到下一篇。
目录
NextChat API 文档测试总结
1. 主要API端点
1.1 聊天接口 - /api/openai/v1/chat/completions
- 请求方法: POST
- 请求格式:
{ "messages": [ {"role": "system", "content": "你是一个助手"}, {"role": "user", "content": "你好"} ], "model": "gpt-4o-mini", "stream": true, "temperature": 0.5, "presence_penalty": 0, "frequency_penalty": 0, "top_p": 1, "max_tokens": 4000 }
- 响应格式:
- 流式响应 (stream=true):
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"你好"},"index":0}]} data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"!"},"index":0}]} ... data: [DONE]
- 非流式响应 (stream=false):
{ "id": "chatcmpl-xxx", "object": "chat.completion", "choices": [{ "message": {"role": "assistant", "content": "你好!有什么可以帮助你的吗?"}, "index": 0 }] }
- 流式响应 (stream=true):
1.2 获取模型列表 - /api/openai/v1/models
- 请求方法: GET
- 响应格式:
{ "object": "list", "data": [ {"id": "gpt-4o-mini", "object": "model", "created": 1721172741, "owned_by": "system"}, {"id": "gpt-3.5-turbo", "object": "model", "created": 1677610602, "owned_by": "openai"} ] }
1.3 获取系统配置 - /api/config
- 请求方法: GET
- 响应格式:
{ "needCode": false, "hideUserApiKey": false, "disableGPT4": false, "hideBalanceQuery": true, "disableFastLink": false, "customModels": "", "defaultModel": "", "visionModels": "" }
2. 测试命令
2.1 测试模型列表端点
Invoke-WebRequest -Uri "http://localhost:3000/api/openai/v1/models" -Method GET |
Select-Object -ExpandProperty Content |
ConvertFrom-Json |
Format-List
2.2 测试配置端点
Invoke-WebRequest -Uri "http://localhost:3000/api/config" -Method GET |
Select-Object -ExpandProperty Content |
ConvertFrom-Json |
Format-List
2.3 测试聊天接口 (非流式)
$body = @{
messages = @(
@{role = "user"; content = "你好"}
)
model = "gpt-4o-mini"
stream = $false
} | ConvertTo-Json
Invoke-WebRequest -Uri "http://localhost:3000/api/openai/v1/chat/completions" -Method POST -Body $body -ContentType "application/json" |
Select-Object -ExpandProperty Content
2.4 浏览器Console测试
// 测试配置
fetch('/api/config').then(res => res.json()).then(console.log)
// 测试模型列表
fetch('/api/openai/v1/models').then(res => res.json()).then(console.log)
// 测试聊天接口
fetch('/api/openai/v1/chat/completions', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
messages: [{role: 'user', content: '你好'}],
model: 'gpt-4o-mini',
stream: false
})
}).then(res => res.json()).then(console.log)
3. 参数说明
- temperature: 控制生成文本的随机性,值越高结果越随机 (0-1)
- presence_penalty: 避免主题重复的程度 (-2.0 到 2.0)
- frequency_penalty: 避免词语重复的程度 (-2.0 到 2.0)
- top_p: 控制模型考虑的概率质量 (0-1)
- max_tokens: 生成文本的最大长度
4. 验证方式
- 使用环境变量中的服务器端API密钥
- 或在请求头中使用
Authorization: Bearer <api_key>
5. 支持的模型
NextChat支持多种模型,包括:
- gpt-4o
- gpt-4o-mini
- gpt-3.5-turbo
- gpt-4-turbo
- gpt-4.1
- o1 (Claude Opus替代)
- o1-mini (Claude Sonnet替代)
6. 执行位置
- PowerShell终端: 确保NextChat项目正在本地运行(通过
yarn dev
启动) - 浏览器开发者工具: 打开F12,在Console选项卡中使用fetch API
- Postman/Insomnia: 专业API测试工具
7. 注意事项
- 所有API请求都通过NextChat服务器代理转发到实际的LLM服务提供商
- 流式请求(stream=true)会减少首次响应时间,但总传输时间可能略长
- 请求端口(默认3000)可能因项目配置不同而变化