From 85d7fc442d6d55f5d90b05915166a620dc79f48d Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Tue, 28 May 2024 23:23:00 +0800 Subject: [PATCH] Beta133 (#132) * beta1 * beta2 * beta3 * beta4 * beta5 * beta6 * beta7 * beta8 * beta9 * beta10 * beta11 * beta12 * beta13 * beta14 * beta15 * beta16 * beta16 * beta19 * beta20 * beta21 * beta22 * beta23 * beta24 * beta25 * beta27 * beta28 * beta29 * beta30 * beta31 * beta33 * beta34 * beta35 * beta36 * beta37 * beta38 * beta39 * beta40 * beta41 * beta42 * beta43 * beta44 * beta45 * beta45 * beta46 * beat48 * beta49 * beta50 * beta51 * beta52 * beta53 * beta54 * beta55 * beta57 * beta58 * beta59 * beta61 * beta62 * beta63 * beta63 * beta64 * beta65 * beta66 * beta67 * beta70 * beta71 * beta72 * beta72 * beta74 * beta75 * beta76 * beta77 * beta78 * beta79 * beta80 * beta81 * beta82 * beta83 * beta85 * beta86 * beta87 * beta88 * beta89 * beta90 * beta91 * beta92 * beta93 * beta94 * beta94 * beta96 * beta97 * beta98 * beta99 * beta100 * beta101 * beta102 * beta104 * beta105 * beta106 * beta107 * beta108 * beta109 * beta110 * beta111 * beta112 * beta113 * beta115 * beta116 * beta117 * beta118 * beta119 * beta120 * beta121 * beta122 * beta123 * beta124 * beta125 * beta126 * beta127 * beta128 * beta129 * beta130 * beta131 * beta132 * beta1333 --- applogic/yuanqi.go | 5 +- config/config.go | 142 ++++++++++++++++++++++++------------ structs/struct.go | 19 +++-- template/config_template.go | 10 ++- 4 files changed, 118 insertions(+), 58 deletions(-) diff --git a/applogic/yuanqi.go b/applogic/yuanqi.go index d9b04d5..6479149 100644 --- a/applogic/yuanqi.go +++ b/applogic/yuanqi.go @@ -192,7 +192,8 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) { } apiURL := config.GetYuanqiApiPath(promptstr) - token := config.GetYuanqiToken(promptstr) + + assistantID, token := config.GetYuanqiConf(promptstr) fmtf.Printf("YuanQi上下文history:%v\n", history) // 构建请求到yuanqi API的请求体 @@ -226,7 +227,7 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) { // 创建请求数据结构体 requestBody = structs.RequestDataYuanQi{ - AssistantID: config.GetYuanqiAssistantID(promptstr), + AssistantID: assistantID, UserID: useridstr, Stream: config.GetuseSse(promptstr), ChatType: config.GetYuanqiChatType(promptstr), diff --git a/config/config.go b/config/config.go index dff4434..ef2a01c 100644 --- a/config/config.go +++ b/config/config.go @@ -2881,74 +2881,122 @@ func getTemperatureHunyuanInternal(options ...string) float64 { return value } -// 获取助手ID -func GetYuanqiAssistantID(options ...string) string { - mu.Lock() - defer mu.Unlock() - return getYuanqiAssistantIDInternal(options...) -} +// // 获取助手ID +// func GetYuanqiAssistantID(options ...string) string { +// mu.Lock() +// defer mu.Unlock() +// return getYuanqiAssistantIDInternal(options...) +// } -func getYuanqiAssistantIDInternal(options ...string) string { - if len(options) == 0 || options[0] == "" { - if instance != nil { - return instance.Settings.YuanqiAssistantID - } - return "" // 默认值或错误处理 - } +// func getYuanqiAssistantIDInternal(options ...string) string { +// if len(options) == 0 || options[0] == "" { +// if instance != nil { +// return instance.Settings.YuanqiAssistantID +// } +// return "" // 默认值或错误处理 +// } - basename := options[0] - assistantIDInterface, err := prompt.GetSettingFromFilename(basename, "YuanqiAssistantID") - if err != nil { - log.Println("Error retrieving YuanqiAssistantID:", err) - return getYuanqiAssistantIDInternal() // 递归调用内部函数,不传递任何参数 - } +// basename := options[0] +// assistantIDInterface, err := prompt.GetSettingFromFilename(basename, "YuanqiAssistantID") +// if err != nil { +// log.Println("Error retrieving YuanqiAssistantID:", err) +// return getYuanqiAssistantIDInternal() // 递归调用内部函数,不传递任何参数 +// } - assistantID, ok := assistantIDInterface.(string) - if !ok { // 检查类型断言是否失败 - log.Println("Type assertion failed for YuanqiAssistantID, fetching default") - return getYuanqiAssistantIDInternal() // 递归调用内部函数,不传递任何参数 - } +// assistantID, ok := assistantIDInterface.(string) +// if !ok { // 检查类型断言是否失败 +// log.Println("Type assertion failed for YuanqiAssistantID, fetching default") +// return getYuanqiAssistantIDInternal() // 递归调用内部函数,不传递任何参数 +// } - if assistantID == "" { - return getYuanqiAssistantIDInternal() // 递归调用内部函数,不传递任何参数 - } +// if assistantID == "" { +// return getYuanqiAssistantIDInternal() // 递归调用内部函数,不传递任何参数 +// } - return assistantID -} +// return assistantID +// } + +// // 获取Token +// func GetYuanqiToken(options ...string) string { +// mu.Lock() +// defer mu.Unlock() +// return getYuanqiTokenInternal(options...) +// } + +// func getYuanqiTokenInternal(options ...string) string { +// if len(options) == 0 || options[0] == "" { +// if instance != nil { +// return instance.Settings.YuanqiToken +// } +// return "" // 默认值或错误处理 +// } + +// basename := options[0] +// YuanqiTokenInterface, err := prompt.GetSettingFromFilename(basename, "YuanqiToken") +// if err != nil { +// log.Println("Error retrieving YuanqiToken:", err) +// return getYuanqiTokenInternal() // 递归调用内部函数,不传递任何参数 +// } + +// YuanqiToken, ok := YuanqiTokenInterface.(string) +// if !ok { // 检查类型断言是否失败 +// log.Println("Type assertion failed for YuanqiToken, fetching default") +// return getYuanqiTokenInternal() // 递归调用内部函数,不传递任何参数 +// } -// 获取Token -func GetYuanqiToken(options ...string) string { +// if YuanqiToken == "" { +// return getYuanqiTokenInternal() // 递归调用内部函数,不传递任何参数 +// } + +// return YuanqiToken +// } + +// GetYuanqiConf return conf.YuanqiAssistantID, conf.YuanqiToken +func GetYuanqiConf(options ...string) (string, string) { mu.Lock() defer mu.Unlock() - return getYuanqiTokenInternal(options...) + return getYuanqiConfInternal(options...) } -func getYuanqiTokenInternal(options ...string) string { - if len(options) == 0 || options[0] == "" { - if instance != nil { - return instance.Settings.YuanqiToken +// getYuanqiConfInternal 内部递归函数,处理配置获取逻辑 +func getYuanqiConfInternal(options ...string) (string, string) { + // 从instance中读取配置数组 + if instance != nil && len(instance.Settings.Yuanqiconfs) > 0 { + if len(options) == 0 || options[0] == "" { + // 从instance全局变量中随机选择一个配置 + index := rand.Intn(len(instance.Settings.Yuanqiconfs)) + conf := instance.Settings.Yuanqiconfs[index] + return conf.YuanqiAssistantID, conf.YuanqiToken } - return "" // 默认值或错误处理 + } else { + log.Println("No configurations available in instance or default behavior") + return "", "" // 默认值或错误处理 } + // 使用prompt包从指定的文件名中获取配置 basename := options[0] - YuanqiTokenInterface, err := prompt.GetSettingFromFilename(basename, "YuanqiToken") + confInterface, err := prompt.GetSettingFromFilename(basename, "Yuanqiconfs") if err != nil { - log.Println("Error retrieving YuanqiToken:", err) - return getYuanqiTokenInternal() // 递归调用内部函数,不传递任何参数 + log.Printf("Error retrieving settings from file: %s, error: %v", basename, err) + return getYuanqiConfInternal() // 递归调用内部函数,不传递任何参数 } - YuanqiToken, ok := YuanqiTokenInterface.(string) - if !ok { // 检查类型断言是否失败 - log.Println("Type assertion failed for YuanqiToken, fetching default") - return getYuanqiTokenInternal() // 递归调用内部函数,不传递任何参数 + // 断言获取的interface{}为[]YuanqiConf + confs, ok := confInterface.([]structs.YuanqiConf) + if !ok { + log.Println("Type assertion failed for YuanqiConfs, attempting default behavior") + return getYuanqiConfInternal() // 递归调用内部函数,尝试默认配置 } - if YuanqiToken == "" { - return getYuanqiTokenInternal() // 递归调用内部函数,不传递任何参数 + if len(confs) == 0 { + log.Println("No configurations found in file:", basename) + return getYuanqiConfInternal() // 递归调用内部函数,尝试默认配置 } - return YuanqiToken + // 随机选择一个配置返回 + index := rand.Intn(len(confs)) + conf := confs[index] + return conf.YuanqiAssistantID, conf.YuanqiToken } // 获取助手版本 diff --git a/structs/struct.go b/structs/struct.go index 856598c..c88dff9 100644 --- a/structs/struct.go +++ b/structs/struct.go @@ -395,13 +395,12 @@ type Settings struct { GlmToolChoice string `yaml:"glmToolChoice"` // 工具选择策略 GlmUserID string `yaml:"glmUserID"` // 用户ID - YuanqiApiPath string `yaml:"yuanqiApiPath"` // 元器api地址 - YuanqiAssistantID string `yaml:"yuanqiAssistantID"` // 助手ID - YuanqiVersion float64 `yaml:"yuanqiVersion"` // 助手版本, 仅对内部开放 - YuanqiUserID string `yaml:"yuanqiUserID"` // 用户ID,调用者业务侧的用户ID,会影响智能体的数据统计,建议按实际情况填写 - YuanqiStream bool `yaml:"yuanqiStream"` // 是否启用流式返回,默认为false - YuanqiChatType string `yaml:"yuanqiChatType"` // 聊天类型,默认为published,preview时使用草稿态智能体,仅对内部开放 - YuanqiToken string `yaml:"yuanqiToken"` // Token + YuanqiApiPath string `yaml:"yuanqiApiPath"` // 元器api地址 + Yuanqiconfs []YuanqiConf `yaml:"yuanqiConfs"` // 元器api配置 支持多个 + YuanqiVersion float64 `yaml:"yuanqiVersion"` // 助手版本, 仅对内部开放 + YuanqiUserID string `yaml:"yuanqiUserID"` // 用户ID,调用者业务侧的用户ID,会影响智能体的数据统计,建议按实际情况填写 + YuanqiStream bool `yaml:"yuanqiStream"` // 是否启用流式返回,默认为false + YuanqiChatType string `yaml:"yuanqiChatType"` // 聊天类型,默认为published,preview时使用草稿态智能体,仅对内部开放 WSServerToken string `yaml:"wsServerToken"` WSPath string `yaml:"wsPath"` @@ -424,6 +423,12 @@ type Settings struct { PromptCoverA []string `yaml:"promptCoverA"` //暂时用不上 待实现 } +type YuanqiConf struct { + YuanqiAssistantID string `yaml:"yuanqiAssistantID"` // 助手ID + YuanqiToken string `yaml:"yuanqiToken"` // Token + YuanqiName string `yaml:"yuanqiName"` // 名字 +} + type MetaEvent struct { PostType string `json:"post_type"` MetaEventType string `json:"meta_event_type"` diff --git a/template/config_template.go b/template/config_template.go index 5d0be0e..35d1a88 100644 --- a/template/config_template.go +++ b/template/config_template.go @@ -187,9 +187,15 @@ settings: # Yuanqi 助手配置文件,确保按业务需求配置。 yuanqiApiPath: "https://open.hunyuan.tencent.com/openapi/v1/agent/chat/completions" - yuanqiAssistantID: "" # 助手ID,唯一标识您的助手实例 - yuanqiToken: "" # 元器Token yuanqiChatType: "published" # 聊天类型,默认为published,支持preview模式下使用草稿态智能体 + yuanqiConfs: + - yuanqiAssistantID: "123" + yuanqiToken: "123" + yuanqiName: "123" + - yuanqiAssistantID: "123" + uanqiToken: "123" + yuanqiName: "123" + ` const Logo = `