executeDifyCompletionMessages 执行 DIFY Completion Messages
(c *gin.Context, workflowRequest *WorkflowRunRequest, agent *model.Agent, channel *model.Channel, modelName string)
| 1000 | |
| 1001 | // executeDifyCompletionMessages 执行 DIFY Completion Messages |
| 1002 | func executeDifyCompletionMessages(c *gin.Context, workflowRequest *WorkflowRunRequest, agent *model.Agent, channel *model.Channel, modelName string) (*custom.WorkflowResponseData, error) { |
| 1003 | // 获取元数据 |
| 1004 | meta := GetByContext(c) |
| 1005 | meta.APIType = model.GetApiType(channel.Type) |
| 1006 | meta.OriginModelName = modelName |
| 1007 | meta.ChannelId = int(channel.ChannelID) |
| 1008 | if channel.BaseURL != nil { |
| 1009 | meta.BaseURL = *channel.BaseURL |
| 1010 | } |
| 1011 | meta.APIKey = channel.Key |
| 1012 | |
| 1013 | // 应用模型映射 |
| 1014 | mappedModel, _ := getMappedModelName(modelName, meta.ModelMapping) |
| 1015 | meta.ActualModelName = mappedModel |
| 1016 | |
| 1017 | logger.SysLogf("DIFY补全消息执行 - 模型映射,OriginModel: %s, ActualModel: %s", |
| 1018 | meta.OriginModelName, meta.ActualModelName) |
| 1019 | |
| 1020 | // 构建 completion request |
| 1021 | workflowID := extractWorkflowID(agent.Model, agent.CustomConfig) |
| 1022 | if workflowID == "" { |
| 1023 | return nil, fmt.Errorf("无法提取工作流ID") |
| 1024 | } |
| 1025 | |
| 1026 | // 构建请求体,符合 DIFY /completion-messages API 规范 |
| 1027 | var inputs map[string]interface{} |
| 1028 | |
| 1029 | // 检查参数中是否包含 query,如果没有则默认添加 |
| 1030 | queryExists := false |
| 1031 | for key := range workflowRequest.Parameters { |
| 1032 | if key == "query" { |
| 1033 | queryExists = true |
| 1034 | break |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | if !queryExists { |
| 1039 | // 如果没有query参数,从参数中提取第一个值作为query,或者使用默认值 |
| 1040 | if len(workflowRequest.Parameters) > 0 { |
| 1041 | for _, value := range workflowRequest.Parameters { |
| 1042 | if strValue, ok := value.(string); ok && strValue != "" { |
| 1043 | workflowRequest.Parameters["query"] = strValue |
| 1044 | queryExists = true |
| 1045 | break |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | if !queryExists { |
| 1051 | workflowRequest.Parameters["query"] = "请处理请求" |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | inputs = workflowRequest.Parameters |
| 1056 | |
| 1057 | // 创建 completion request 对象 |
| 1058 | completionRequest := map[string]interface{}{ |
| 1059 | "inputs": inputs, |
no test coverage detected