123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package compapi
- import (
- "errors"
- "fmt"
- "wechat-api/ent"
- "wechat-api/internal/types"
- "github.com/openai/openai-go"
- )
- type MismatchClient struct {
- StdClient
- }
- // Generate the JSON schema at initialization time
- var MismatchResponseSchema = GenerateSchema[MismatchResponse]()
- type MismatchResponse struct {
- UserIntent string `json:"user_intent" jsonschema_description:"用户意图"`
- SimilarReply []string `json:"similar_reply" jsonschema_description:"类似回复"`
- Keywords []string `json:"keywords" jsonschema_description:"关键词库"`
- Regular []string `json:"regular" jsonschema_description:"正则表达式"`
- }
- /*
- "response_format":{"type":"json_object"}}
- {"type":"json_schema",
- "json_schema":{
- "description":"从通话记录中提取表单","name":"keyword_schema","schema":{
- "$schema":"https://json-schema.org/draft/2020-12/schema","additionalProperties":false,"properties":{
- "keywords":{
- "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}}
- */
- func (me *MismatchClient) BuildRequest(req *types.CompApiReq) error {
- //先重构message
- newMessSlice := make([]types.StdCompMessage, 2)
- newMessSlice[0] = types.StdCompMessage{Role: "system", Content: ` # 任务介绍
- 公司在用程序巡检电话通话记录时,有些用户的回复内容没有被关键词库识别到,请根据以下通话记录,给出一些类似的回复,以及对关键词库的具体建议,非常感谢!
- # 输出要求
- 1. 用户意图:结合上下文,首先考虑可能的语音识别错误并纠错,然后分析未识别内容的根本意图,用户说这句话的背后真实目的是什么,结合上下文挖掘用户最深的意图,请不要停留在表面的意思。
- 2. 类似回复:首先应包含未识别的内容原文:` + req.Variables["missed"] + `。其次生成尽可能多的类似意图的回复。
- 2. 关键词库:从类似回复中抽取特征词,要求有两点:一是特征词应尽可能的准确,即要覆盖住此类问题,又要尽可能避免和其他语境相冲突。二是需要注重通用性,一些专有名词、人名、地名、产品名、公司名等需要排除在外。
- 3. 正则表达式:对关键词词进行解耦,用正则表达式进行表示,专注于主要内容,排除次要内容
- # 注意事项
- 1. 应贴近真实的外呼场景,用户的素质参差不齐,请避免使用任何浮夸的用词,避免使用高级词汇,避免使用任何的礼貌用语或敬语,适当的低素质些,请至少给出20条结果
- 2. 通话记录为录音转译,因此可能有错别字或音同字不同的情况(例如:借和接),请根据上下文分析后就成可能的错字错误
- 3. 正则关键词库和正则表达式中也应该考虑到音同字不同的情况,且避免使用匹配次数相关的语法如{0,2}`}
- newMessSlice[1] = types.StdCompMessage{Role: "user", Content: `
- # 通话记录` + req.Variables["chat_history"] + `
- # 可能识别有误的内容:` + req.Variables["missed"]}
- //再构造ResponseFormat
- if !IsOpenaiModel(req.Model) {
- newMessSlice[1].Content = newMessSlice[1].Content.(string) + `{
- "user_intent": str, #用户意图
- "similar_reply": list[str], #类似回复
- "keywords": list[str], #关键词库
- "regular": list[str], #正则表达式
- }`
- req.ResponseFormat = openai.ResponseFormatJSONObjectParam{Type: "json_object"}
- } else {
- schemaParam := openai.ResponseFormatJSONSchemaJSONSchemaParam{
- Name: "keyword_schema",
- Description: openai.String("从通话记录中提取表单"),
- Schema: MismatchResponseSchema,
- Strict: openai.Bool(true),
- }
- req.ResponseFormat = openai.ResponseFormatJSONSchemaParam{JSONSchema: schemaParam}
- }
- //req.Model = oldModel
- req.Messages = newMessSlice
- return nil
- }
- func (me *MismatchClient) CallbackPrepare(params any) ([]byte, error) {
- taskData, ok := params.(*ent.CompapiAsynctask)
- if !ok {
- return nil, errors.New("invalid callback taskdata")
- }
- type OutResult struct {
- InternalID string `json:"internal_id"`
- ExternalID string `json:"external_id"`
- ChatID string `json:"chat_id"`
- EventType string `json:"event_type"`
- Content string `json:"content"`
- }
- res := OutResult{}
- res.InternalID = fmt.Sprintf("%d", taskData.ID)
- res.ExternalID = taskData.ResponseChatItemID
- res.EventType = taskData.EventType
- res.ChatID = taskData.ChatID
- var err error
- res.Content, err = NewChatResult(taskData.ResponseRaw).GetContentJsonStr()
- if err != nil {
- return nil, err
- }
- return WrapJSON(res, "", false)
- }
|