mismatch.go 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package compapi
  2. import (
  3. "wechat-api/internal/types"
  4. "github.com/openai/openai-go"
  5. )
  6. type MismatchClient struct {
  7. StdClient
  8. }
  9. // Generate the JSON schema at initialization time
  10. var MismatchResponseSchema = GenerateSchema[MismatchResponse]()
  11. type MismatchResponse struct {
  12. UserIntent string `json:"user_intent" jsonschema_description:"用户意图"`
  13. SimilarReply []string `json:"similar_reply" jsonschema_description:"类似回复"`
  14. Keywords []string `json:"keywords" jsonschema_description:"关键词库"`
  15. Regular []string `json:"regular" jsonschema_description:"正则表达式"`
  16. }
  17. /*
  18. "response_format":{"type":"json_object"}}
  19. {"type":"json_schema",
  20. "json_schema":{
  21. "description":"从通话记录中提取表单","name":"keyword_schema","schema":{
  22. "$schema":"https://json-schema.org/draft/2020-12/schema","additionalProperties":false,"properties":{
  23. "keywords":{
  24. "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}}
  25. */
  26. func (me *MismatchClient) BuildRequest(req *types.CompApiReq) error {
  27. //先重构message
  28. newMessSlice := make([]types.StdCompMessage, 2)
  29. newMessSlice[0] = types.StdCompMessage{Role: "system", Content: ` # 任务介绍
  30. 公司在用程序巡检电话通话记录时,有些用户的回复内容没有被关键词库识别到,请根据以下通话记录,给出一些类似的回复,以及对关键词库的具体建议,非常感谢!
  31. # 输出要求
  32. 1. 用户意图:结合上下文,首先考虑可能的语音识别错误并纠错,然后分析未识别内容的根本意图,用户说这句话的背后真实目的是什么,结合上下文挖掘用户最深的意图,请不要停留在表面的意思。
  33. 2. 类似回复:首先应包含未识别的内容原文:` + req.Variables["missed"] + `。其次生成尽可能多的类似意图的回复。
  34. 2. 关键词库:从类似回复中抽取特征词,要求有两点:一是特征词应尽可能的准确,即要覆盖住此类问题,又要尽可能避免和其他语境相冲突。二是需要注重通用性,一些专有名词、人名、地名、产品名、公司名等需要排除在外。
  35. 3. 正则表达式:对关键词词进行解耦,用正则表达式进行表示,专注于主要内容,排除次要内容
  36. # 注意事项
  37. 1. 应贴近真实的外呼场景,用户的素质参差不齐,请避免使用任何浮夸的用词,避免使用高级词汇,避免使用任何的礼貌用语或敬语,适当的低素质些,请至少给出20条结果
  38. 2. 通话记录为录音转译,因此可能有错别字或音同字不同的情况(例如:借和接),请根据上下文分析后就成可能的错字错误
  39. 3. 正则关键词库和正则表达式中也应该考虑到音同字不同的情况,且避免使用匹配次数相关的语法如{0,2}`}
  40. newMessSlice[1] = types.StdCompMessage{Role: "user", Content: `
  41. # 通话记录` + req.Variables["chat_history"] + `
  42. # 可能识别有误的内容:` + req.Variables["missed"]}
  43. //oldModel := req.Model
  44. //req.Model = "deepseek-v3"
  45. //再构造ResponseFormat
  46. if req.Model == "deepseek-v3" || req.Model == "deepseek-chat" {
  47. newMessSlice[1].Content = newMessSlice[1].Content.(string) + `json:{
  48. "user_intent": str, #用户意图
  49. "similar_reply": list[str], #类似回复
  50. "keywords": list[str], #关键词库
  51. "regular": list[str], #正则表达式
  52. }`
  53. req.ResponseFormat = openai.ResponseFormatJSONObjectParam{Type: "json_object"}
  54. } else {
  55. schemaParam := openai.ResponseFormatJSONSchemaJSONSchemaParam{
  56. Name: "keyword_schema",
  57. Description: openai.String("从通话记录中提取表单"),
  58. Schema: MismatchResponseSchema,
  59. Strict: openai.Bool(true),
  60. }
  61. req.ResponseFormat = openai.ResponseFormatJSONSchemaParam{JSONSchema: schemaParam}
  62. }
  63. //req.Model = oldModel
  64. req.Messages = newMessSlice
  65. return nil
  66. }
  67. func (me *MismatchClient) CallbackPrepare(params any) ([]byte, error) {
  68. res := MismatchResponse{}
  69. err := NewChatResult(params).ParseContentAs(&res)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return WrapJSON(res, "data", true)
  74. }