|
@@ -1,6 +1,10 @@
|
|
|
package chat
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
"net/http"
|
|
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
@@ -8,6 +12,8 @@ import (
|
|
|
"wechat-api/internal/logic/chat"
|
|
|
"wechat-api/internal/svc"
|
|
|
"wechat-api/internal/types"
|
|
|
+
|
|
|
+ "github.com/zeromicro/go-zero/core/logx"
|
|
|
)
|
|
|
|
|
|
// swagger:route post /v1/chat/completions chat ChatCompletions
|
|
@@ -27,12 +33,40 @@ import (
|
|
|
|
|
|
func ChatCompletionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
- var req types.CompApiReq
|
|
|
- if err := httpx.Parse(r, &req, true); err != nil {
|
|
|
+ /*
|
|
|
+ var req types.CompApiReq
|
|
|
+ if err := httpx.Parse(r, &req, true); err != nil {
|
|
|
+ httpx.ErrorCtx(r.Context(), w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ l := chat.NewChatCompletionsLogic(r.Context(), svcCtx)
|
|
|
+ resp, err := l.ChatCompletions(&req)
|
|
|
+ if err != nil {
|
|
|
+ httpx.ErrorCtx(r.Context(), w, err)
|
|
|
+ } else {
|
|
|
+ httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
+ }
|
|
|
+ */
|
|
|
+ // 读取请求体
|
|
|
+ body, err := io.ReadAll(r.Body)
|
|
|
+ if err != nil {
|
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
|
return
|
|
|
}
|
|
|
+ // 将请求体还原,以便后续处理
|
|
|
+ r.Body = io.NopCloser(bytes.NewBuffer(body))
|
|
|
|
|
|
+ // 打印请求体
|
|
|
+ logx.Info(string(body))
|
|
|
+ var req types.CompApiReq
|
|
|
+ err = json.Unmarshal([]byte(string(body)), &req)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 打印请求体
|
|
|
+ logx.Info(req)
|
|
|
l := chat.NewChatCompletionsLogic(r.Context(), svcCtx)
|
|
|
resp, err := l.ChatCompletions(&req)
|
|
|
if err != nil {
|