boweniac 5 days ago
parent
commit
2b3fca59fe
1 changed files with 90 additions and 89 deletions
  1. 90 89
      crontask/contact_form.go

+ 90 - 89
crontask/contact_form.go

@@ -1,12 +1,10 @@
 package crontask
 
 import (
-	"bytes"
 	"encoding/json"
 	"fmt"
 	"github.com/google/uuid"
 	"github.com/zeromicro/go-zero/core/logx"
-	"net/http"
 	"strconv"
 	"time"
 	"wechat-api/ent/contact"
@@ -15,6 +13,8 @@ import (
 	"wechat-api/ent/custom_types"
 	"wechat-api/ent/predicate"
 	"wechat-api/ent/usagedetail"
+	"wechat-api/internal/types"
+	"wechat-api/internal/utils/compapi"
 )
 
 type ResponseItem struct {
@@ -190,100 +190,101 @@ func (l *CronTask) analyze() {
 }
 
 func (l *CronTask) openaiRequest(messages string, template []custom_types.ContactFieldTemplate) ([]ResponseItem, error) {
-	//formData := ConvertFormData(template)
-	//jsonBytes, err := json.Marshal(formData)
-	//if err != nil {
-	//	return nil, err
-	//}
-	//jsonStr := string(jsonBytes)
-	//logx.Info("contactFieldTemplates: ", jsonStr)
-	//req := &types.CompApiReq{
-	//	types.CompCtlReq{
-	//		"form",
-	//		"",
-	//		false,
-	//		"",
-	//	},
-	//	types.StdCompApiReq{
-	//		"gpt-4o",
-	//		[]types.StdCompMessage{},
-	//		false,
-	//		nil,
-	//	},
-	//	types.FastGptSpecReq{
-	//		"",
-	//		"",
-	//		"",
-	//		false,
-	//		map[string]string{
-	//			"form_data":    jsonStr,
-	//			"chat_history": messages,
-	//			"external_id":  uuid.New().String(),
-	//		},
-	//	},
-	//}
-	//resp, err := compapi.NewClient(l.ctx, compapi.WithApiBase("http://new-api.gkscrm.com/v1/"),
-	//	compapi.WithApiKey("sk-wwttAtdLcTfeF7F2Eb9d3592Bd4c487f8e8fA544D6C4BbA9")).
-	//	Chat(req)
-	//if err == nil && resp != nil && len(resp.Choices) > 0 {
-	//	//logx.Info("resp.Choices: ", resp.Choices[0].Message.Content)
-	//	var items []ResponseItem
-	//	err = json.Unmarshal([]byte(resp.Choices[0].Message.Content), &items)
-	//	if err != nil {
-	//		return nil, err
-	//	}
-	//	return items, nil
-	//} else if resp != nil && len(resp.Choices) == 0 {
-	//	return nil, err
-	//}
-
-	url := "https://toolsapi-debug.gkscrm.com/call_center/form/extract"
-	bodyData := map[string]interface{}{
-		"form_data":    ConvertFormData(template),
-		"chat_history": messages,
-		"external_id":  uuid.New().String(),
-	}
-	logx.Info("bodyData: %+v", bodyData)
-	bodyBytes, err := json.Marshal(bodyData)
-	if err != nil {
-		return nil, err
-	}
-
-	req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyBytes))
-	if err != nil {
-		return nil, err
-	}
-	req.Header.Set("Content-Type", "application/json")
-	req.Header.Set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIn0.ZS9jnsLPCnmc8L_lu4yaQFp34vwWF1mHlHSBYrY5JVs")
-
-	client := &http.Client{}
-	resp, err := client.Do(req)
-	if err != nil || resp == nil || resp.Body == nil {
-		logx.Error("read body error: ", err)
-		return nil, err
-	}
-
-	logx.Info("err: ", err)
+	formData := ConvertFormData(template)
+	jsonBytes, err := json.Marshal(formData)
 	if err != nil {
 		return nil, err
 	}
-	defer resp.Body.Close()
-
-	if resp.StatusCode != http.StatusOK {
-		return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
-	}
-
-	//var result []ResponseItem
-	var fullResp struct {
-		Data []ResponseItem `json:"data"`
+	jsonStr := string(jsonBytes)
+	logx.Info("contactFieldTemplates: ", jsonStr)
+	req := &types.CompApiReq{
+		types.CompCtlReq{
+			"form",
+			"",
+			false,
+			"",
+		},
+		types.StdCompApiReq{
+			"gpt-4o",
+			[]types.StdCompMessage{},
+			false,
+			nil,
+		},
+		types.FastGptSpecReq{
+			"",
+			"",
+			"",
+			false,
+			map[string]string{
+				"form_data":    jsonStr,
+				"chat_history": messages,
+				"external_id":  uuid.New().String(),
+			},
+		},
 	}
-	err = json.NewDecoder(resp.Body).Decode(&fullResp)
-	if err != nil {
+	resp, err := compapi.NewClient(l.ctx, compapi.WithApiBase("http://new-api.gkscrm.com/v1/"),
+		compapi.WithApiKey("sk-wwttAtdLcTfeF7F2Eb9d3592Bd4c487f8e8fA544D6C4BbA9")).
+		Chat(req)
+	logx.Info("resp: ", resp)
+	if err == nil && resp != nil && len(resp.Choices) > 0 {
+		logx.Info("resp.Choices: ", resp.Choices[0].Message.Content)
+		var items []ResponseItem
+		err = json.Unmarshal([]byte(resp.Choices[0].Message.Content), &items)
+		if err != nil {
+			return nil, err
+		}
+		return items, nil
+	} else if resp != nil && len(resp.Choices) == 0 {
 		return nil, err
 	}
 
-	return fullResp.Data, nil
-	//return nil, err
+	//url := "https://toolsapi-debug.gkscrm.com/call_center/form/extract"
+	//bodyData := map[string]interface{}{
+	//	"form_data":    ConvertFormData(template),
+	//	"chat_history": messages,
+	//	"external_id":  uuid.New().String(),
+	//}
+	//logx.Info("bodyData: %+v", bodyData)
+	//bodyBytes, err := json.Marshal(bodyData)
+	//if err != nil {
+	//	return nil, err
+	//}
+	//
+	//req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyBytes))
+	//if err != nil {
+	//	return nil, err
+	//}
+	//req.Header.Set("Content-Type", "application/json")
+	//req.Header.Set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIn0.ZS9jnsLPCnmc8L_lu4yaQFp34vwWF1mHlHSBYrY5JVs")
+	//
+	//client := &http.Client{}
+	//resp, err := client.Do(req)
+	//if err != nil || resp == nil || resp.Body == nil {
+	//	logx.Error("read body error: ", err)
+	//	return nil, err
+	//}
+	//
+	//logx.Info("err: ", err)
+	//if err != nil {
+	//	return nil, err
+	//}
+	//defer resp.Body.Close()
+	//
+	//if resp.StatusCode != http.StatusOK {
+	//	return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+	//}
+	//
+	////var result []ResponseItem
+	//var fullResp struct {
+	//	Data []ResponseItem `json:"data"`
+	//}
+	//err = json.NewDecoder(resp.Body).Decode(&fullResp)
+	//if err != nil {
+	//	return nil, err
+	//}
+	//
+	//return fullResp.Data, nil
+	return nil, err
 }
 
 func (l *CronTask) UpdateContactFields(botID string, receiverID string, fields []ResponseItem) error {