sending request: error sending request: Post "https://www.dmxapi.cn/v1/chat/comp...
🚨 错误信息
DMXAPI chat failed: failed to create chat completion: error sending request: error sending request: Post "https://www.dmxapi.cn/v1/chat/completions": context canceled
🤖 AI解决方案
DMXAPI chat failed: failed to create chat completion: error sending request: error sending request: Post "https://www.dmxapi.cn/v1/chat/completions": context canceledDMXAPI聊天失败:创建聊天补全失败:发送请求时出错:发送请求时出错:Post "https://www.dmxapi.cn/v1/chat/completions": 上下文已取消
原因分析
网络请求在发送过程中被取消了。这通常是由于网络不稳定、请求超时或者客户端主动取消了请求。
解决方案
context.WithTimeout来设置超时:
go
import (
"context"
"net/http"
"time"
) // 假设 client 是你的 *http.Client
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) // 设置30秒超时
defer cancel()
req, err := http.NewRequestWithContext(ctx, "POST", "https://www.dmxapi.cn/v1/chat/completions", nil)
// ... 构建你的请求 ...
resp, err := client.Do(req)
// ... 处理响应 ...