form.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package compapi
  2. import (
  3. "time"
  4. "wechat-api/internal/types"
  5. )
  6. type FormClient struct {
  7. MismatchClient
  8. }
  9. func (me *FormClient) ResponseFormatSetting(req *types.CompApiReq) ResponseFormatConfig {
  10. nowTime := time.Now().Format("2006-01-02 15:04:05")
  11. weekday := time.Now().Weekday().String()
  12. //Message重构的配置
  13. me.ResformatConfig.SysmesArgs = []any{nowTime, weekday}
  14. me.ResformatConfig.SysmesTmpl = `# 任务
  15. 请帮助user从聊天记录中提取表单值,并返回一个JSON格式的表单值。
  16. # 背景信息
  17. 当前时间是:%s
  18. 当前星期是:%s
  19. # 返回值示例
  20. * 如表单类型为 input、autoComplete、textarea,返回示例:["表单值"]
  21. * 如表单类型为 radio、select,返回示例:["值1"]
  22. * 如表单类型为 checkbox,返回示例:["值1", "值2"]
  23. * 如表单类型为 cascader,返回示例:["一级值1", "二级值3"]
  24. * 如表单类型为 date,返回示例:["2025-01-01"]
  25. * 如没有找到某个表单相关的值,请不要返回该表单`
  26. me.ResformatConfig.UsermesArgs = []any{req.Variables["form_data"], req.Variables["chat_history"]}
  27. me.ResformatConfig.UsermesTmpl = `# 待提取表单
  28. %s
  29. # 聊天记录
  30. %s`
  31. //ResponseFormat设置的配置
  32. me.ResformatConfig.ResformatDesc = "从通话记录中提取表单"
  33. me.ResformatConfig.ResformatTxt = `[{
  34. "dataIndex": str, # 表单ID
  35. "value": list[str] # 表单值列表
  36. }]`
  37. type formStruct struct {
  38. DataIndex string `json:"dataIndex" jsonschema_description:"表单ID"`
  39. Value []string `json:"value" jsonschema_description:"值列表"`
  40. }
  41. me.ResformatConfig.ResformatStruct = struct {
  42. Values []formStruct `json:"values" jsonschema_description:"表单列表"`
  43. }{}
  44. me.ResformatConfig.HaveSet = true
  45. return me.ResformatConfig
  46. }
  47. func (me *FormClient) BuildRequest(req *types.CompApiReq) error {
  48. //设置相关个性化ResponseFormat配置信息
  49. if !me.ResformatConfig.HaveSet {
  50. me.ResponseFormatSetting(req)
  51. }
  52. return me.MismatchClient.BuildRequest(req)
  53. }
  54. // 向Client.getClientActFace工厂方法注册
  55. func init() {
  56. builder := func(c *Client) (clientActionFace, error) {
  57. return &FormClient{MismatchClient{StdClient: StdClient{Client: c}}}, nil
  58. }
  59. err := RegisterClient("form", builder)
  60. if err != nil {
  61. //panic(fmt.Sprintf("Failed to register client type 'mismatch': %v", err))
  62. }
  63. }