chat_completions_logic.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package chat
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/url"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "wechat-api/ent"
  13. "wechat-api/internal/svc"
  14. "wechat-api/internal/types"
  15. "wechat-api/internal/utils/compapi"
  16. "wechat-api/internal/utils/contextkey"
  17. "wechat-api/internal/utils/typekit"
  18. "github.com/zeromicro/go-zero/core/logx"
  19. )
  20. type baseLogicWorkflow interface {
  21. AppendAsyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) error
  22. DoSyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (*types.CompOpenApiResp, error)
  23. AppendUsageDetailLog(authToken string, req *types.CompApiReq, resp *types.CompOpenApiResp) error
  24. AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey)
  25. }
  26. type ChatCompletionsLogic struct {
  27. logx.Logger
  28. ctx context.Context
  29. svcCtx *svc.ServiceContext
  30. }
  31. type FastgptChatLogic struct {
  32. ChatCompletionsLogic
  33. }
  34. type MismatchChatLogic struct {
  35. ChatCompletionsLogic
  36. }
  37. type IntentChatLogic struct {
  38. ChatCompletionsLogic
  39. }
  40. /*
  41. 扩展LogicChat工厂方法
  42. 返回根据不同EventType相关的扩展LogicChat的baseLogicWorkflow接口形式
  43. 每增加一个新的扩展LogicChat结构,需要在此函数中增加相应的创建语句
  44. */
  45. func (l *ChatCompletionsLogic) getLogicWorkflow(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (baseLogicWorkflow, error) {
  46. var (
  47. err error
  48. wf baseLogicWorkflow
  49. )
  50. if apiKeyObj.Edges.Agent.Type != 2 {
  51. err = fmt.Errorf("api agent type not support(%d)", apiKeyObj.Edges.Agent.Type)
  52. } else if req.EventType == "mismatch" {
  53. wf = &MismatchChatLogic{ChatCompletionsLogic: *l}
  54. } else if req.EventType == "intent" {
  55. wf = &IntentChatLogic{ChatCompletionsLogic: *l}
  56. } else {
  57. wf = &FastgptChatLogic{ChatCompletionsLogic: *l}
  58. }
  59. return wf, err
  60. }
  61. func NewChatCompletionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatCompletionsLogic {
  62. return &ChatCompletionsLogic{
  63. Logger: logx.WithContext(ctx),
  64. ctx: ctx,
  65. svcCtx: svcCtx}
  66. }
  67. func (l *ChatCompletionsLogic) ChatCompletions(req *types.CompApiReq) (asyncMode bool, resp *types.CompOpenApiResp, err error) {
  68. // todo: add your logic here and delete this line
  69. var (
  70. apiKeyObj *ent.ApiKey
  71. ok bool
  72. )
  73. asyncMode = false
  74. //从上下文中获取鉴权中间件埋下的apiAuthInfo
  75. apiKeyObj, ok = contextkey.AuthTokenInfoKey.GetValue(l.ctx)
  76. if !ok {
  77. return asyncMode, nil, errors.New("content get auth info err")
  78. }
  79. //根据请求产生相关的工作流接口集
  80. wf, err := l.getLogicWorkflow(apiKeyObj, req)
  81. if err != nil {
  82. return false, nil, err
  83. }
  84. //请求前临时观察相关参数
  85. //PreChatVars(req, apiKeyObj, wf)
  86. //微调部分请求参数
  87. wf.AdjustRequest(req, apiKeyObj)
  88. if isAsyncReqest(req) { //异步请求处理模式
  89. asyncMode = true
  90. err = wf.AppendAsyncRequest(apiKeyObj, req)
  91. } else { //同步请求处理模式
  92. resp, err = wf.DoSyncRequest(apiKeyObj, req)
  93. if err == nil && resp != nil && len(resp.Choices) > 0 {
  94. wf.AppendUsageDetailLog(apiKeyObj.Key, req, resp) //请求记录
  95. } else if resp != nil && len(resp.Choices) == 0 {
  96. err = errors.New("返回结果缺失,请检查访问地址及权限")
  97. }
  98. }
  99. return asyncMode, resp, err
  100. }
  101. func (l *ChatCompletionsLogic) AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey) {
  102. if len(req.EventType) == 0 {
  103. req.EventType = "fastgpt"
  104. }
  105. if len(req.Model) == 0 && len(apiKeyObj.Edges.Agent.Model) > 0 {
  106. req.Model = apiKeyObj.Edges.Agent.Model
  107. }
  108. //异步任务相关参数调整
  109. if req.IsBatch {
  110. //流模式暂时不支持异步模式
  111. //Callback格式非法则取消批量模式
  112. if req.Stream || !IsValidURL(&req.Callback, true) {
  113. req.IsBatch = false
  114. }
  115. }
  116. }
  117. func (l *ChatCompletionsLogic) DoSyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) (*types.CompOpenApiResp, error) {
  118. resp, err := compapi.NewClient(l.ctx, compapi.WithApiBase(apiKeyObj.Edges.Agent.APIBase),
  119. compapi.WithApiKey(apiKeyObj.Edges.Agent.APIKey)).
  120. Chat(req)
  121. if err != nil {
  122. return nil, err
  123. }
  124. //以下临时测试case
  125. //humanSeeValidResult(l.ctx, req, resp)
  126. return resp, err
  127. }
  128. func (l *ChatCompletionsLogic) AppendAsyncRequest(apiKeyObj *ent.ApiKey, req *types.CompApiReq) error {
  129. rawReqBs, err := json.Marshal(*req)
  130. if err != nil {
  131. return err
  132. }
  133. rawReqStr := string(rawReqBs)
  134. res, err := l.svcCtx.DB.CompapiAsynctask.Create().
  135. SetNotNilAuthToken(&apiKeyObj.Key).
  136. SetNotNilOpenaiBase(&apiKeyObj.Edges.Agent.APIBase).
  137. SetNotNilOpenaiKey(&apiKeyObj.Edges.Agent.APIKey).
  138. SetNotNilOrganizationID(&apiKeyObj.OrganizationID).
  139. SetNotNilEventType(&req.EventType).
  140. SetNillableModel(&req.Model).
  141. SetNillableChatID(&req.ChatId).
  142. SetNillableResponseChatItemID(&req.ResponseChatItemId).
  143. SetNotNilRequestRaw(&rawReqStr).
  144. SetNotNilCallbackURL(&req.Callback).
  145. Save(l.ctx)
  146. if err == nil {
  147. logx.Infof("appendAsyncRequest succ,get id:%d", res.ID)
  148. }
  149. return err
  150. }
  151. func (l *ChatCompletionsLogic) AppendUsageDetailLog(authToken string, req *types.CompApiReq, resp *types.CompOpenApiResp) error {
  152. svcCtx := &compapi.ServiceContext{Config: l.svcCtx.Config, DB: l.svcCtx.DB,
  153. Rds: l.svcCtx.Rds}
  154. return compapi.AppendUsageDetailLog(l.ctx, svcCtx, authToken, req, resp)
  155. }
  156. func (l *FastgptChatLogic) AdjustRequest(req *types.CompApiReq, apiKeyObj *ent.ApiKey) {
  157. l.ChatCompletionsLogic.AdjustRequest(req, apiKeyObj) //先父类的参数调整
  158. if req.EventType != "fastgpt" {
  159. return
  160. }
  161. if len(req.Model) > 0 {
  162. if req.Variables == nil {
  163. req.Variables = make(map[string]string)
  164. }
  165. req.Variables["model"] = req.Model
  166. }
  167. if len(req.ChatId) > 0 && len(req.FastgptChatId) == 0 {
  168. req.FastgptChatId = req.ChatId
  169. } else if len(req.ChatId) == 0 && len(req.FastgptChatId) > 0 {
  170. req.ChatId = req.FastgptChatId
  171. }
  172. }
  173. func humanSeePreChatVars(req *types.CompApiReq, apiKeyObj *ent.ApiKey, wf baseLogicWorkflow) {
  174. fmt.Println("=========================================")
  175. fmt.Printf("In ChatCompletion Get Token Info:\nKey:'%s'\n", apiKeyObj.Key)
  176. fmt.Printf("Auth Token:'%s'\n", apiKeyObj.Key)
  177. fmt.Printf("ApiKey AgentID:%d\n", apiKeyObj.AgentID)
  178. fmt.Printf("ApiKey APIBase:'%s'\n", apiKeyObj.Edges.Agent.APIBase)
  179. fmt.Printf("ApiKey APIKey:'%s'\n", apiKeyObj.Edges.Agent.APIKey)
  180. fmt.Printf("ApiKey Type:%d\n", apiKeyObj.Edges.Agent.Type)
  181. fmt.Printf("ApiKey Model:'%s'\n", apiKeyObj.Edges.Agent.Model)
  182. fmt.Printf("EventType:'%s'\n", req.EventType)
  183. fmt.Printf("req.ChatId:'%s VS req.FastgptChatId:'%s'\n", req.ChatId, req.FastgptChatId)
  184. fmt.Println("=========================================")
  185. switch wf.(type) {
  186. case *MismatchChatLogic:
  187. fmt.Println("MismatchChatLogic Flow.....")
  188. case *IntentChatLogic:
  189. fmt.Println("IntentChatLogic Flow.....")
  190. case *FastgptChatLogic:
  191. fmt.Println("FastgptChatLogic Flow.....")
  192. default:
  193. fmt.Println("Other Flow.....")
  194. }
  195. }
  196. func humanSeeValidResult(ctx context.Context, req *types.CompApiReq, resp *types.CompOpenApiResp) {
  197. clientFace, err := compapi.NewClient(ctx).GetClientActFace(req.EventType)
  198. if err != nil {
  199. fmt.Println(err)
  200. return
  201. }
  202. taskData := ent.CompapiAsynctask{}
  203. taskData.ID = 1234
  204. taskData.ResponseChatItemID = req.ResponseChatItemId
  205. taskData.EventType = req.EventType
  206. taskData.ChatID = req.ChatId
  207. taskData.ResponseRaw, err = resp.ToString()
  208. if err != nil {
  209. fmt.Println(err)
  210. return
  211. }
  212. var bs []byte
  213. bs, err = clientFace.CallbackPrepare(&taskData)
  214. if err != nil {
  215. fmt.Println(err)
  216. }
  217. fmt.Printf("当前请求EventType:%s\n", req.EventType)
  218. fmt.Printf("当前请求MODEL:%s\n", req.Model)
  219. fmt.Println("client.CallbackPrepare结果[]byte版.........")
  220. fmt.Println(string(bs))
  221. nres := map[string]any{}
  222. err = json.Unmarshal(bs, &nres)
  223. if err != nil {
  224. fmt.Println(err)
  225. }
  226. fmt.Println("client.CallbackPrepare结果map[string]any版.........")
  227. fmt.Println(typekit.PrettyPrint(nres))
  228. config := compapi.ResponseFormatConfig{}
  229. if req.EventType == "mismatch" {
  230. clientInst := clientFace.(*compapi.MismatchClient)
  231. config = clientInst.ResponseFormatSetting(req)
  232. } else if req.EventType == "intent" {
  233. clientInst := clientFace.(*compapi.IntentClient)
  234. config = clientInst.ResponseFormatSetting(req)
  235. } else {
  236. return
  237. }
  238. err = compapi.NewChatResult(resp).ParseContentAs(&config.ResformatStruct)
  239. if err != nil {
  240. fmt.Println(err)
  241. }
  242. nres["content"] = config.ResformatStruct
  243. fmt.Println("client.CallbackPrepare结果ParseContentAs定制版.........")
  244. fmt.Println(typekit.PrettyPrint(nres))
  245. }
  246. func apiKeyObjAdjust(eventType string, workId string, obj *ent.ApiKey) {
  247. if eventType != "fastgpt" {
  248. return
  249. }
  250. obj.OpenaiKey, _ = compapi.GetWorkInfoByID(eventType, workId)
  251. }
  252. // 合法域名正则(支持通配符、中文域名等场景按需调整)
  253. var domainRegex = regexp.MustCompile(
  254. // 多级域名(如 example.com)
  255. `^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}$` +
  256. `|` +
  257. // 单级域名(如 localhost 或 mytest-svc)
  258. `^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`,
  259. )
  260. func IsValidURL(input *string, adjust bool) bool {
  261. // 空值直接返回
  262. if *input == "" {
  263. return false
  264. }
  265. inputStr := *input
  266. // --- 预处理输入:自动补全协议 ---
  267. // 若输入不包含协议头,默认添加 http://
  268. if !strings.Contains(*input, "://") {
  269. inputStr = "http://" + *input
  270. }
  271. // --- 解析 URL ---
  272. u, err := url.Parse(inputStr)
  273. if err != nil {
  274. return false
  275. }
  276. // --- 校验协议 ---
  277. // 只允许常见协议(按需扩展)
  278. switch u.Scheme {
  279. case "http", "https", "ftp", "ftps":
  280. default:
  281. return false
  282. }
  283. // --- 拆分 Host 和 Port ---
  284. host, port, err := net.SplitHostPort(u.Host)
  285. if err != nil {
  286. // 无端口时,整个 Host 作为主机名
  287. host = u.Host
  288. port = ""
  289. }
  290. // --- 校验主机名 ---
  291. // 场景1:IPv4 或 IPv6
  292. if ip := net.ParseIP(host); ip != nil {
  293. // 允许私有或保留 IP(按需调整)
  294. // 示例中允许所有合法 IP
  295. } else {
  296. // 场景2:域名(包括 localhost)
  297. if !domainRegex.MatchString(host) {
  298. return false
  299. }
  300. }
  301. // --- 校验端口 ---
  302. if port != "" {
  303. p, err := net.LookupPort("tcp", port) // 动态获取端口(如 "http" 对应 80)
  304. if err != nil {
  305. // 直接尝试解析为数字端口
  306. numPort, err := strconv.Atoi(port)
  307. if err != nil || numPort < 1 || numPort > 65535 {
  308. return false
  309. }
  310. } else if p == 0 { // 动态端口为 0 时无效
  311. return false
  312. }
  313. }
  314. if adjust {
  315. *input = inputStr
  316. }
  317. return true
  318. }
  319. func getMessageContentStr(input any) string {
  320. str := ""
  321. switch val := input.(type) {
  322. case string:
  323. str = val
  324. case []interface{}:
  325. if len(val) > 0 {
  326. if valc, ok := val[0].(map[string]interface{}); ok {
  327. if valcc, ok := valc["text"]; ok {
  328. str, _ = valcc.(string)
  329. }
  330. }
  331. }
  332. }
  333. return str
  334. }
  335. func isAsyncReqest(req *types.CompApiReq) bool {
  336. return req.IsBatch
  337. }