|
@@ -16,6 +16,7 @@ import (
|
|
"wechat-api/internal/types"
|
|
"wechat-api/internal/types"
|
|
"wechat-api/internal/utils/compapi"
|
|
"wechat-api/internal/utils/compapi"
|
|
"wechat-api/internal/utils/contextkey"
|
|
"wechat-api/internal/utils/contextkey"
|
|
|
|
+ "wechat-api/internal/utils/typekit"
|
|
|
|
|
|
"wechat-api/ent/custom_types"
|
|
"wechat-api/ent/custom_types"
|
|
"wechat-api/ent/predicate"
|
|
"wechat-api/ent/predicate"
|
|
@@ -25,6 +26,13 @@ import (
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+type baseLogicWorkflow interface {
|
|
|
|
+ AppendAsyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) error
|
|
|
|
+ DoSyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (*types.CompOpenApiResp, error)
|
|
|
|
+ AppendUsageDetailLog(authToken string, req *types.CompApiReq, resp *types.CompOpenApiResp) error
|
|
|
|
+ AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey)
|
|
|
|
+}
|
|
|
|
+
|
|
type ChatCompletionsLogic struct {
|
|
type ChatCompletionsLogic struct {
|
|
logx.Logger
|
|
logx.Logger
|
|
ctx context.Context
|
|
ctx context.Context
|
|
@@ -39,15 +47,32 @@ type MismatchChatLogic struct {
|
|
ChatCompletionsLogic
|
|
ChatCompletionsLogic
|
|
}
|
|
}
|
|
|
|
|
|
-type FormChatLogic struct {
|
|
|
|
|
|
+type IntentChatLogic struct {
|
|
ChatCompletionsLogic
|
|
ChatCompletionsLogic
|
|
}
|
|
}
|
|
|
|
|
|
-type baseLogicWorkflow interface {
|
|
|
|
- AppendAsyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) error
|
|
|
|
- DoSyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (*types.CompOpenApiResp, error)
|
|
|
|
- AppendUsageDetailLog(authToken string, req *types.CompApiReq, resp *types.CompOpenApiResp) error
|
|
|
|
- AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey)
|
|
|
|
|
|
+/*
|
|
|
|
+扩展LogicChat工厂方法
|
|
|
|
+返回根据不同EventType相关的扩展LogicChat的baseLogicWorkflow接口形式
|
|
|
|
+
|
|
|
|
+每增加一个新的扩展LogicChat结构,需要在此函数中增加相应的创建语句
|
|
|
|
+*/
|
|
|
|
+func (l *ChatCompletionsLogic) getLogicWorkflow(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (baseLogicWorkflow, error) {
|
|
|
|
+ var (
|
|
|
|
+ err error
|
|
|
|
+ wf baseLogicWorkflow
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ if apiKeyObj.Edges.Agent.Type != 2 {
|
|
|
|
+ err = fmt.Errorf("api agent type not support(%d)", apiKeyObj.Edges.Agent.Type)
|
|
|
|
+ } else if req.EventType == "mismatch" {
|
|
|
|
+ wf = &MismatchChatLogic{ChatCompletionsLogic: *l}
|
|
|
|
+ } else if req.EventType == "intent" {
|
|
|
|
+ wf = &IntentChatLogic{ChatCompletionsLogic: *l}
|
|
|
|
+ } else {
|
|
|
|
+ wf = &FastgptChatLogic{ChatCompletionsLogic: *l}
|
|
|
|
+ }
|
|
|
|
+ return wf, err
|
|
}
|
|
}
|
|
|
|
|
|
func NewChatCompletionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatCompletionsLogic {
|
|
func NewChatCompletionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatCompletionsLogic {
|
|
@@ -57,27 +82,6 @@ func NewChatCompletionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *C
|
|
svcCtx: svcCtx}
|
|
svcCtx: svcCtx}
|
|
}
|
|
}
|
|
|
|
|
|
-func (l *FastgptChatLogic) AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey) {
|
|
|
|
-
|
|
|
|
- l.ChatCompletionsLogic.AdjustRequest(req, apiKeyObj) //先父类的参数调整
|
|
|
|
-
|
|
|
|
- if req.EventType != "fastgpt" {
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- if len(req.Model) > 0 {
|
|
|
|
- if req.Variables == nil {
|
|
|
|
- req.Variables = make(map[string]string)
|
|
|
|
- }
|
|
|
|
- req.Variables["model"] = req.Model
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if len(req.ChatId) > 0 && len(req.FastgptChatId) == 0 {
|
|
|
|
- req.FastgptChatId = req.ChatId
|
|
|
|
- } else if len(req.ChatId) == 0 && len(req.FastgptChatId) > 0 {
|
|
|
|
- req.ChatId = req.FastgptChatId
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func (l *ChatCompletionsLogic) ChatCompletions(req *types.CompApiReq) (asyncMode bool, resp *types.CompOpenApiResp, err error) {
|
|
func (l *ChatCompletionsLogic) ChatCompletions(req *types.CompApiReq) (asyncMode bool, resp *types.CompOpenApiResp, err error) {
|
|
// todo: add your logic here and delete this line
|
|
// todo: add your logic here and delete this line
|
|
|
|
|
|
@@ -92,39 +96,15 @@ func (l *ChatCompletionsLogic) ChatCompletions(req *types.CompApiReq) (asyncMode
|
|
if !ok {
|
|
if !ok {
|
|
return asyncMode, nil, errors.New("content get auth info err")
|
|
return asyncMode, nil, errors.New("content get auth info err")
|
|
}
|
|
}
|
|
-
|
|
|
|
- //微调apiKeyObj的openaikey
|
|
|
|
- //apiKeyObjAdjust(req.EventType, req.WorkId, apiKeyObj)
|
|
|
|
-
|
|
|
|
- /*
|
|
|
|
- fmt.Println("=========================================")
|
|
|
|
- fmt.Printf("In ChatCompletion Get Token Info:\nKey:'%s'\n", apiKeyObj.Key)
|
|
|
|
- fmt.Printf("Auth Token:'%s'\n", apiKeyObj.Key)
|
|
|
|
- fmt.Printf("ApiKey AgentID:%d\n", apiKeyObj.AgentID)
|
|
|
|
- fmt.Printf("ApiKey APIBase:'%s'\n", apiKeyObj.Edges.Agent.APIBase)
|
|
|
|
- fmt.Printf("ApiKey APIKey:'%s'\n", apiKeyObj.Edges.Agent.APIKey)
|
|
|
|
- fmt.Printf("ApiKey Type:%d\n", apiKeyObj.Edges.Agent.Type)
|
|
|
|
- fmt.Printf("ApiKey Model:'%s'\n", apiKeyObj.Edges.Agent.Model)
|
|
|
|
- fmt.Printf("EventType:'%s'\n", req.EventType)
|
|
|
|
- fmt.Printf("req.ChatId:'%s VS req.FastgptChatId:'%s'\n", req.ChatId, req.FastgptChatId)
|
|
|
|
- fmt.Println("=========================================")
|
|
|
|
- */
|
|
|
|
|
|
|
|
//根据请求产生相关的工作流接口集
|
|
//根据请求产生相关的工作流接口集
|
|
wf, err := l.getLogicWorkflow(apiKeyObj, req)
|
|
wf, err := l.getLogicWorkflow(apiKeyObj, req)
|
|
if err != nil {
|
|
if err != nil {
|
|
return false, nil, err
|
|
return false, nil, err
|
|
}
|
|
}
|
|
- /*
|
|
|
|
- switch wf.(type) {
|
|
|
|
- case *MismatchChatLogic:
|
|
|
|
- fmt.Println("MismatchChatLogic Flow.....")
|
|
|
|
- case *FastgptChatLogic:
|
|
|
|
- fmt.Println("FastgptChatLogic Flow.....")
|
|
|
|
- default:
|
|
|
|
- fmt.Println("Other Flow.....")
|
|
|
|
- }
|
|
|
|
- */
|
|
|
|
|
|
+
|
|
|
|
+ //请求前临时观察相关参数
|
|
|
|
+ //PreChatVars(req, apiKeyObj, wf)
|
|
|
|
|
|
//微调部分请求参数
|
|
//微调部分请求参数
|
|
wf.AdjustRequest(req, apiKeyObj)
|
|
wf.AdjustRequest(req, apiKeyObj)
|
|
@@ -143,24 +123,6 @@ func (l *ChatCompletionsLogic) ChatCompletions(req *types.CompApiReq) (asyncMode
|
|
return asyncMode, resp, err
|
|
return asyncMode, resp, err
|
|
}
|
|
}
|
|
|
|
|
|
-func (l *ChatCompletionsLogic) getLogicWorkflow(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (baseLogicWorkflow, error) {
|
|
|
|
- var (
|
|
|
|
- err error
|
|
|
|
- wf baseLogicWorkflow
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
- if apiKeyObj.Edges.Agent.Type != 2 {
|
|
|
|
- err = fmt.Errorf("api agent type not support(%d)", apiKeyObj.Edges.Agent.Type)
|
|
|
|
- } else if req.EventType == "mismatch" {
|
|
|
|
- wf = &MismatchChatLogic{ChatCompletionsLogic: *l}
|
|
|
|
- } else if req.EventType == "form" {
|
|
|
|
- wf = &FormChatLogic{ChatCompletionsLogic: *l}
|
|
|
|
- } else {
|
|
|
|
- wf = &FastgptChatLogic{ChatCompletionsLogic: *l}
|
|
|
|
- }
|
|
|
|
- return wf, err
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func (l *ChatCompletionsLogic) AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey) {
|
|
func (l *ChatCompletionsLogic) AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey) {
|
|
|
|
|
|
if len(req.EventType) == 0 {
|
|
if len(req.EventType) == 0 {
|
|
@@ -182,50 +144,16 @@ func (l *ChatCompletionsLogic) AdjustRequest(req *types.CompApiReq, apiKeyObj *e
|
|
}
|
|
}
|
|
|
|
|
|
func (l *ChatCompletionsLogic) DoSyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (*types.CompOpenApiResp, error) {
|
|
func (l *ChatCompletionsLogic) DoSyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (*types.CompOpenApiResp, error) {
|
|
- //return compapi.NewFastgptChatCompletions(l.ctx, apiKeyObj.Edges.Agent.APIKey, apiKeyObj.Edges.Agent.APIBase, req)
|
|
|
|
resp, err := compapi.NewClient(l.ctx, compapi.WithApiBase(apiKeyObj.Edges.Agent.APIBase),
|
|
resp, err := compapi.NewClient(l.ctx, compapi.WithApiBase(apiKeyObj.Edges.Agent.APIBase),
|
|
compapi.WithApiKey(apiKeyObj.Edges.Agent.APIKey)).
|
|
compapi.WithApiKey(apiKeyObj.Edges.Agent.APIKey)).
|
|
Chat(req)
|
|
Chat(req)
|
|
|
|
|
|
- /*
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if req.EventType == "mismatch" {
|
|
|
|
-
|
|
|
|
- client := compapi.MismatchClient{}
|
|
|
|
- taskData := ent.CompapiAsynctask{}
|
|
|
|
- taskData.ID = 1234
|
|
|
|
- taskData.ResponseChatItemID = req.ResponseChatItemId
|
|
|
|
- taskData.EventType = req.EventType
|
|
|
|
- taskData.ChatID = req.ChatId
|
|
|
|
- var err error
|
|
|
|
- taskData.ResponseRaw, err = resp.ToString()
|
|
|
|
- if err != nil {
|
|
|
|
- fmt.Println(err)
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- var bs []byte
|
|
|
|
- bs, err = client.CallbackPrepare(&taskData)
|
|
|
|
- if err != nil {
|
|
|
|
- fmt.Println(err)
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- fmt.Println(string(bs))
|
|
|
|
- nres := map[string]string{}
|
|
|
|
- err = json.Unmarshal(bs, &nres)
|
|
|
|
- if err != nil {
|
|
|
|
- fmt.Println(err)
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- fmt.Println(typekit.PrettyPrint(nres))
|
|
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ //以下临时测试case
|
|
|
|
+ //humanSeeValidResult(l.ctx, req, resp)
|
|
|
|
|
|
- res := compapi.MismatchResponse{}
|
|
|
|
- err = compapi.NewChatResult(resp).ParseContentAs(&res)
|
|
|
|
- fmt.Println(err)
|
|
|
|
- fmt.Println(typekit.PrettyPrint(res))
|
|
|
|
- }
|
|
|
|
- */
|
|
|
|
return resp, err
|
|
return resp, err
|
|
}
|
|
}
|
|
|
|
|
|
@@ -359,6 +287,106 @@ func (l *ChatCompletionsLogic) sumTotalTokensByAuthToken(authToken string) (uint
|
|
return totalTokens, err
|
|
return totalTokens, err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (l *FastgptChatLogic) AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey) {
|
|
|
|
+
|
|
|
|
+ l.ChatCompletionsLogic.AdjustRequest(req, apiKeyObj) //先父类的参数调整
|
|
|
|
+
|
|
|
|
+ if req.EventType != "fastgpt" {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if len(req.Model) > 0 {
|
|
|
|
+ if req.Variables == nil {
|
|
|
|
+ req.Variables = make(map[string]string)
|
|
|
|
+ }
|
|
|
|
+ req.Variables["model"] = req.Model
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(req.ChatId) > 0 && len(req.FastgptChatId) == 0 {
|
|
|
|
+ req.FastgptChatId = req.ChatId
|
|
|
|
+ } else if len(req.ChatId) == 0 && len(req.FastgptChatId) > 0 {
|
|
|
|
+ req.ChatId = req.FastgptChatId
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func humanSeePreChatVars(req *types.CompApiReq, apiKeyObj *ent.ApiKey, wf baseLogicWorkflow) {
|
|
|
|
+
|
|
|
|
+ fmt.Println("=========================================")
|
|
|
|
+ fmt.Printf("In ChatCompletion Get Token Info:\nKey:'%s'\n", apiKeyObj.Key)
|
|
|
|
+ fmt.Printf("Auth Token:'%s'\n", apiKeyObj.Key)
|
|
|
|
+ fmt.Printf("ApiKey AgentID:%d\n", apiKeyObj.AgentID)
|
|
|
|
+ fmt.Printf("ApiKey APIBase:'%s'\n", apiKeyObj.Edges.Agent.APIBase)
|
|
|
|
+ fmt.Printf("ApiKey APIKey:'%s'\n", apiKeyObj.Edges.Agent.APIKey)
|
|
|
|
+ fmt.Printf("ApiKey Type:%d\n", apiKeyObj.Edges.Agent.Type)
|
|
|
|
+ fmt.Printf("ApiKey Model:'%s'\n", apiKeyObj.Edges.Agent.Model)
|
|
|
|
+ fmt.Printf("EventType:'%s'\n", req.EventType)
|
|
|
|
+ fmt.Printf("req.ChatId:'%s VS req.FastgptChatId:'%s'\n", req.ChatId, req.FastgptChatId)
|
|
|
|
+ fmt.Println("=========================================")
|
|
|
|
+
|
|
|
|
+ switch wf.(type) {
|
|
|
|
+ case *MismatchChatLogic:
|
|
|
|
+ fmt.Println("MismatchChatLogic Flow.....")
|
|
|
|
+ case *IntentChatLogic:
|
|
|
|
+ fmt.Println("IntentChatLogic Flow.....")
|
|
|
|
+ case *FastgptChatLogic:
|
|
|
|
+ fmt.Println("FastgptChatLogic Flow.....")
|
|
|
|
+ default:
|
|
|
|
+ fmt.Println("Other Flow.....")
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func humanSeeValidResult(ctx context.Context, req *types.CompApiReq, resp *types.CompOpenApiResp) {
|
|
|
|
+ clientFace, err := compapi.NewClient(ctx).GetClientActFace(req.EventType)
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ taskData := ent.CompapiAsynctask{}
|
|
|
|
+ taskData.ID = 1234
|
|
|
|
+ taskData.ResponseChatItemID = req.ResponseChatItemId
|
|
|
|
+ taskData.EventType = req.EventType
|
|
|
|
+ taskData.ChatID = req.ChatId
|
|
|
|
+ taskData.ResponseRaw, err = resp.ToString()
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var bs []byte
|
|
|
|
+ bs, err = clientFace.CallbackPrepare(&taskData)
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ }
|
|
|
|
+ fmt.Printf("当前请求EventType:%s\n", req.EventType)
|
|
|
|
+ fmt.Printf("当前请求MODEL:%s\n", req.Model)
|
|
|
|
+ fmt.Println("client.CallbackPrepare结果[]byte版.........")
|
|
|
|
+ fmt.Println(string(bs))
|
|
|
|
+ nres := map[string]any{}
|
|
|
|
+ err = json.Unmarshal(bs, &nres)
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ }
|
|
|
|
+ fmt.Println("client.CallbackPrepare结果map[string]any版.........")
|
|
|
|
+ fmt.Println(typekit.PrettyPrint(nres))
|
|
|
|
+
|
|
|
|
+ config := compapi.ResponseFormatConfig{}
|
|
|
|
+ if req.EventType == "mismatch" {
|
|
|
|
+ clientInst := clientFace.(*compapi.MismatchClient)
|
|
|
|
+ config = clientInst.ResponseFormatSetting(req)
|
|
|
|
+ } else if req.EventType == "intent" {
|
|
|
|
+ clientInst := clientFace.(*compapi.IntentClient)
|
|
|
|
+ config = clientInst.ResponseFormatSetting(req)
|
|
|
|
+ } else {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ err = compapi.NewChatResult(resp).ParseContentAs(&config.ResformatStruct)
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println(err)
|
|
|
|
+ }
|
|
|
|
+ nres["content"] = config.ResformatStruct
|
|
|
|
+ fmt.Println("client.CallbackPrepare结果ParseContentAs定制版.........")
|
|
|
|
+ fmt.Println(typekit.PrettyPrint(nres))
|
|
|
|
+}
|
|
|
|
+
|
|
func apiKeyObjAdjust(eventType string, workId string, obj *ent.ApiKey) {
|
|
func apiKeyObjAdjust(eventType string, workId string, obj *ent.ApiKey) {
|
|
if eventType != "fastgpt" {
|
|
if eventType != "fastgpt" {
|
|
return
|
|
return
|
|
@@ -370,9 +398,9 @@ func apiKeyObjAdjust(eventType string, workId string, obj *ent.ApiKey) {
|
|
var domainRegex = regexp.MustCompile(
|
|
var domainRegex = regexp.MustCompile(
|
|
// 多级域名(如 example.com)
|
|
// 多级域名(如 example.com)
|
|
`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}$` +
|
|
`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}$` +
|
|
- `|` +
|
|
|
|
- // 单级域名(如 localhost 或 mytest-svc)
|
|
|
|
- `^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`,
|
|
|
|
|
|
+ `|` +
|
|
|
|
+ // 单级域名(如 localhost 或 mytest-svc)
|
|
|
|
+ `^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`,
|
|
)
|
|
)
|
|
|
|
|
|
func IsValidURL(input *string, adjust bool) bool {
|
|
func IsValidURL(input *string, adjust bool) bool {
|