ChatGPTNextChat项目重构计划(二):深入分析NextChat API交互
HeadersPayloadResponse都在 Network 面板的单条请求详情里。你可以对比官方文档,确认参数和返回格式是否符合预期。如果有异常,可以截图这些内容,便于排查问题。
🚀大模型落地开发实战指南!请关注微信公众号:「AGI启程号」 深入浅出,助你轻松入门!
📚 数据分析、深度学习、大模型与算法的综合进阶,尽在CSDN博客主页
目录
你现在已经能正常运行NextChat,并且想要分析和观察前端与后端(API)的数据交互,特别关注请求的 headers、payload 以及响应格式。你已经在浏览器开发者工具的 Network 面板下找到了相关内容。下面是详细的操作和说明:
1. 打开开发者工具
- 在浏览器中按 F12 或右键选择“检查”打开开发者工具。
- 切换到 Network(网络) 面板。
2. 过滤请求
- 在 Network 面板的过滤栏输入
completions或localhost,可以只显示相关的 API 请求。 - 你也可以点击左侧的某一条请求(如
/v1/chat/completions),查看详细信息。
3. 查看 Headers
- 选中一条请求后,点击右侧的 Headers 标签页。
- 这里可以看到:
- General:请求的URL、方法(POST/GET)、状态码等。
- Request Headers:如 Authorization(API Key)、Content-Type、User-Agent 等。
- Response Headers:如 Content-Type、服务器信息、CORS 相关等。
4. 查看 Payload(请求体)
- 选中一条 POST 请求,点击 Payload(有的浏览器叫 Request Payload)。
- 这里可以看到前端发送给后端的具体数据内容(如 model、messages、temperature 等参数)。
5. 查看响应(Response)
- 选中请求后,点击 Response 标签页。
- 这里可以看到后端返回的完整数据内容(通常是 JSON 格式),包括模型回复、token用量、错误信息等。
常见关注点
- Headers:是否带有
Authorization(API Key)、Content-Type: application/json等。 - Payload:messages、model、temperature、stream 等参数是否正确。
- Response:返回的 JSON 结构,是否有
choices、usage、error字段等。
具体内容
localhost:
Headers:
Request URL http://localhost:3000/
Request Method POST
Status Code 200 OK
Remote Address [::1]:3000
Referrer Policy strict-origin-when-cross-origin
completions:
Headers:
Request URL http://localhost:3000/api/openai/v1/chat/completions
Request Method POST
Status Code 200 OK
Remote Address [::1]:3000
Referrer Policy strict-origin-when-cross-origin
Request URL:说明你是在本地通过 NextChat 的后端接口转发请求。
Request Method:POST,符合 OpenAI API 的调用方式。
Status Code:200 OK,说明请求成功。
Payload
{
"messages": [
{
"role": "system",
"content": "\nYou are ChatGPT, a large language model trained by OpenAI.\nKnowledge cutoff: 2023-10\nCurrent model: gpt-4o-mini\nCurrent time: Wed May 14 2025 15:29:31 GMT+0800 (中国标准时间)\nLatex inline: \\(x^2\\) \nLatex block: $$e=mc^2$$\n\n"
},
{
"role": "user",
"content": "你好,你是谁?"
}
],
"stream": true,
"model": "gpt-4o-mini",
"temperature": 0.5,
"presence_penalty": 0,
"frequency_penalty": 0,
"top_p": 1,
"max_tokens": 4000
}
messages:对话历史,包含 system prompt 和用户输入。
stream:true,表示采用流式返回(SSE)。
model:gpt-4o-mini,指定了模型。
其他参数(temperature、penalty、top_p、max_tokens)都是控制生成内容的参数。
Response
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"你好"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"!"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"我是"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"Chat"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"GPT"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":",一个"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"由"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"Open"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"AI"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"开发"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"的"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"人工"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"智能"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"语言"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"模型"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"。我"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"可以"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"帮助"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"你"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"回答"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"问题"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"、"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"提供"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"信息"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"、"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"进行"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"对"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"话"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"等"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"。有"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"任何"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"想"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"了解"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"的"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"内容"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"吗"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{"content":"?"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-BX3RdjrKk37QZyiXERcVYdie0cjnX","object":"chat.completion.chunk","created":1747217765,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"fp_0392822090","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: [DONE]
delta.content:就是模型生成的内容片段,前端会把这些片段拼接起来,最终组成完整回复。
finish_reason:为 null 时表示还没结束,等到最后一块 finish_reason 为 “stop” 时,说明回复结束。
data: [DONE]:流式传输结束标志。
总结
- Headers、Payload、Response 都在 Network 面板的单条请求详情里。
- 你可以对比官方文档,确认参数和返回格式是否符合预期。
- 如果有异常,可以截图这些内容,便于排查问题。
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)