form.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package compapi
  2. import (
  3. "time"
  4. "wechat-api/internal/types"
  5. "github.com/openai/openai-go"
  6. )
  7. type FormClient struct {
  8. StdClient
  9. }
  10. // Generate the JSON schema at initialization time
  11. var FormResponseSchema = GenerateSchema[FormResponse]()
  12. type FormResponse struct {
  13. DataIndex string `json:"dataIndex" jsonschema_description:"表单 id"`
  14. Value []string `json:"value" jsonschema_description:" 值列表"`
  15. }
  16. type FormList struct {
  17. Values []FormResponse `json:"values" jsonschema_description:"表单列表"`
  18. }
  19. /*
  20. "response_format":{"type":"json_object"}}
  21. {"type":"json_schema",
  22. "json_schema":{
  23. "description":"从通话记录中提取表单","name":"keyword_schema","schema":{
  24. "$schema":"https://json-schema.org/draft/2020-12/schema","additionalProperties":false,"properties":{
  25. "keywords":{
  26. "description":"关键词库","items":{"type":"string"},"type":"array"},"regular":{"description":"正则表达式","items":{"type":"string"},"type":"array"},"similar_reply":{"description":"类似回复","items":{"type":"string"},"type":"array"},"swear_words":{"description":"脏话列表","items":{"type":"string"},"type":"array"},"user_intent":{"description":"用户意图","type":"string"},"user_mood":{"description":"用户情绪","type":"string"}},"required":["user_intent","similar_reply","keywords","regular","user_mood","swear_words"],"type":"object"},"strict":true}}
  27. */
  28. func (me *FormClient) BuildRequest(req *types.CompApiReq) error {
  29. nowTime := time.Now().Format("2006-01-02 15:04:05")
  30. //bytes, err := json.Marshal(req.Variables["form_data"])
  31. //if err != nil {
  32. // return err
  33. //}
  34. //form_data := string(bytes)
  35. //先重构message
  36. newMessSlice := make([]types.StdCompMessage, 2)
  37. newMessSlice[0] = types.StdCompMessage{Role: "system", Content: `# 任务
  38. 请帮助user从通话记录中提取表单值,并返回一个JSON格式的表单值。
  39. # 背景信息
  40. ` + nowTime + `
  41. # 返回值示例
  42. * 如表单类型为 input、autoComplete、textarea,返回示例:["表单值"]
  43. * 如表单类型为 radio、select,返回示例:["值1"]
  44. * 如表单类型为 checkbox,返回示例:["值1", "值2"]
  45. * 如表单类型为 cascader,返回示例:["一级值1", "二级值3"]
  46. * 如表单类型为 date,返回示例:["2025-01-01"]`}
  47. newMessSlice[1] = types.StdCompMessage{Role: "user", Content: `# 表单数据
  48. ` + req.Variables["form_data"] + `
  49. # 聊天记录
  50. ` + req.Variables["chat_history"]}
  51. //再构造ResponseFormat
  52. if !IsOpenaiModel(req.Model) {
  53. newMessSlice[1].Content = newMessSlice[1].Content.(string) + `
  54. # 请以下方的json结构输出
  55. [{
  56. "dataIndex": str, # 表单ID
  57. "value": list[str] # 表单值列表
  58. }]`
  59. req.ResponseFormat = openai.ResponseFormatJSONObjectParam{Type: "json_object"}
  60. } else {
  61. schemaParam := openai.ResponseFormatJSONSchemaJSONSchemaParam{
  62. Name: "keyword_schema",
  63. Description: openai.String("从通话记录中提取表单"),
  64. Schema: FormResponseSchema,
  65. Strict: openai.Bool(true),
  66. }
  67. req.ResponseFormat = openai.ResponseFormatJSONSchemaParam{JSONSchema: schemaParam}
  68. }
  69. //req.Model = oldModel
  70. req.Messages = newMessSlice
  71. return nil
  72. }