Commit fb5e23a9 by Ford

添加了用户画像的创建和查询相关世界的用户画像

parent 44ca2d74
......@@ -208,7 +208,23 @@
"adaptability": "应变能力",
"communicationSkills": "沟通能力",
"socialEtiquette": "社交礼仪"
}
},
"npc_role_detail_translations": {
"roleName": "角色名称",
"introduction": "简介",
"consumptionExpectations": "消费心理预期",
"psychologicalTraits": "心理特征",
"appearance": "外观",
"personality": "性格",
"decisionMakingStyle": "决策风格",
"riskPreference": "风险偏好",
"attitudeTowardsUsers": "对用户的态度",
"educationLevel": "教育水平",
"professionalBackground": "职业背景",
"purpose": "目的性",
"knownInformation": "已知信息"
}
......
......@@ -62,9 +62,13 @@ type AppConfig struct {
JwtSecret string `json:"jwt_secret"`
MiGuAuthId string `json:"miGuAuthId"`
// 增加 BusinessManagerAssessmentConfigTranslations 字段
// 增加 业务经理考核系统世界配置信息 BusinessManagerAssessmentConfigTranslations 字段
BusinessManagerAssessmentConfigTranslations map[string]string `json:"business_manager_assessment_config_translations"` //英文转中文
ReverseTranslations map[string]string // 反向映射,中文转英文,不需要从JSON解码
BusinessManagerReverseTranslations map[string]string // 反向映射,中文转英文,不需要从JSON解码
// 用户画像角色详情信息 NpcRoleDetail
NpcRoleDetailTranslations map[string]string `json:"npc_role_detail_translations"` //英文对照中文
NpcRoleDetailReverseTranslations map[string]string // 反向映射,中文转英文,不需要从JSON解码
SoulUrl string `json:"soul_url"`
SoulAuth string `json:"soul_auth"`
......@@ -120,6 +124,15 @@ func GetModelPathsByName(name string) (string, string, bool) {
return "", "", false
}
// 用于反转映射的辅助函数
func ReverseMap(m map[string]string) map[string]string {
reversed := make(map[string]string)
for k, v := range m {
reversed[v] = k
}
return reversed
}
func InitConfig() *AppConfig {
fmt.Println(" 读取配置文件... ")
file, err := os.Open("./config.json")
......@@ -149,12 +162,17 @@ func InitConfig() *AppConfig {
}
// 创建世界配置信息json反向映射字典
reverseTranslations := make(map[string]string)
for k, v := range conf.BusinessManagerAssessmentConfigTranslations {
reverseTranslations[v] = k
}
//reverseTranslations := make(map[string]string)
//for k, v := range conf.BusinessManagerAssessmentConfigTranslations {
// reverseTranslations[v] = k
//}
reverseTranslations := ReverseMap(conf.BusinessManagerAssessmentConfigTranslations)
// 将反向映射存储为全局变量,或添加到配置结构中
conf.ReverseTranslations = reverseTranslations // 假设你在AppConfig中添加了这个字段
conf.BusinessManagerReverseTranslations = reverseTranslations // 假设你在AppConfig中添加了这个字段
//将用户画像的角色详情信息反向中文转英文映射
NpcRoleDetailReverseMap := ReverseMap(conf.NpcRoleDetailTranslations)
conf.NpcRoleDetailReverseTranslations = NpcRoleDetailReverseMap
Conf = &conf
return &conf
......
......@@ -67,52 +67,6 @@ func init() {
}
}*/
// translateKeys 是递归函数,用于转换JSON对象的键从中文到英文
// 中文到英文
func TranslateKeysChineseToEnglish(data interface{}) interface{} {
ReverseTranslations := config.Conf.ReverseTranslations
switch obj := data.(type) {
case map[string]interface{}:
newMap := make(map[string]interface{})
for k, v := range obj {
newKey := k
if val, ok := ReverseTranslations[k]; ok {
newKey = val
}
newMap[newKey] = TranslateKeysChineseToEnglish(v)
}
return newMap
case []interface{}:
for i, v := range obj {
obj[i] = TranslateKeysChineseToEnglish(v)
}
}
return data
}
// translateKeys 是递归函数,用于转换JSON对象的键
// 英文转中文
func TranslateKeysEnglishToChinese(data interface{}) interface{} {
BusinessManagerAssessmentTranslations := config.Conf.BusinessManagerAssessmentConfigTranslations
switch obj := data.(type) {
case map[string]interface{}:
newMap := make(map[string]interface{})
for k, v := range obj {
newKey := k
if val, ok := BusinessManagerAssessmentTranslations[k]; ok {
newKey = val
}
newMap[newKey] = TranslateKeysEnglishToChinese(v)
}
return newMap
case []interface{}:
for i, v := range obj {
obj[i] = TranslateKeysEnglishToChinese(v)
}
}
return data
}
//查询用户与那些世界进行过聊天会话
func QueryUserToWorldConversation(ctx *gin.Context) {
/*
......@@ -988,7 +942,8 @@ func MiGuCreateBusinessManagerAssessmentSystem1(ctx *gin.Context) {
}
// 转换键名
translatedData := TranslateKeysEnglishToChinese(data)
BusinessManagerAssessmentTranslations := config.Conf.BusinessManagerAssessmentConfigTranslations
translatedData := utils.TranslateKeysEnglishToChinese(BusinessManagerAssessmentTranslations, data)
translatedJSON, err := json.MarshalIndent(translatedData, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
......@@ -1169,7 +1124,8 @@ func MiGuCreateBusinessManagerAssessmentSystem(ctx *gin.Context) {
return
}
// 转换键名
translatedData := TranslateKeysEnglishToChinese(data)
BusinessManagerAssessmentTranslations := config.Conf.BusinessManagerAssessmentConfigTranslations
translatedData := utils.TranslateKeysEnglishToChinese(BusinessManagerAssessmentTranslations, data)
translatedJSON, err := json.MarshalIndent(translatedData, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
......@@ -1372,7 +1328,8 @@ func EditBusinessManagerAssessmentSystem(ctx *gin.Context) {
return
}
/* var panel models.AssessmentCustomizationPanel
/*
var panel models.AssessmentCustomizationPanel
if err := ctx.BindJSON(&panel); err != nil {
ctx.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
......@@ -1700,7 +1657,9 @@ func MiGuQueryBusinessManagerWorldInfo(ctx *gin.Context) {
return
}
// 转换键名
translatedData := TranslateKeysChineseToEnglish(data)
// 中文转英文
ReverseTranslations := config.Conf.BusinessManagerReverseTranslations
translatedData := utils.TranslateKeysChineseToEnglish(ReverseTranslations, data)
translatedJSON, err := json.MarshalIndent(translatedData, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
......
......@@ -3,12 +3,15 @@ package main
import (
"WorldEpcho/src/config"
"WorldEpcho/src/datasource"
"WorldEpcho/src/https_auth"
"WorldEpcho/src/models"
"WorldEpcho/src/routers"
"WorldEpcho/src/service"
"WorldEpcho/src/utils"
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
......@@ -22,10 +25,10 @@ func main() {
config.RandInit()
datasource.InitMysql()
// 同步结构体与数据库表
/* err := datasource.Engine.Sync2(new(models.WorldDeductionResult))
err := datasource.Engine.Sync2(new(models.NpcRole))
if err != nil {
log.Fatal("创建表失败:", err)
}*/
fmt.Printf("创建表失败:", err)
}
//生成一个9位的邀请码
//println("生成一个9位的邀请码: ", utils.GenerateInviteCode(1))
......@@ -99,7 +102,7 @@ func main() {
//service.ExampleSend()
/* router.GET("/https_test", func(c *gin.Context) {
router.GET("/https_test", func(c *gin.Context) {
fmt.Println(c.Request.Host)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
......@@ -108,11 +111,11 @@ func main() {
})
router.Use(https_auth.HttpsHandler()) //https对应的中间件
path := "https_auth/CA/" //证书的路径
router.RunTLS(":8099", path+"admin.mindepoch.com.pem", path+"admin.mindepoch.com.key") //开启HTTPS服务*/
router.RunTLS(":8099", path+"admin.mindepoch.com.pem", path+"admin.mindepoch.com.key") //开启HTTPS服务
//初始化redis配置
//utils.InitRedisStore()
//Run("里面不指定端口号默认为8080")
router.Run(":8099")
//router.Run(":8099")
}
package models
import (
"WorldEpcho/src/datasource"
"fmt"
"github.com/go-xorm/xorm"
"strconv"
)
// NPCRole 表示一个 NPC 角色的全部信息
type NpcRole struct {
ID int64 `xorm:"'id' pk autoincr" json:"id"` //NPC角色ID
Name string `xorm:"'name'" json:"name"` // 名称
Details string `xorm:"'details' TEXT" json:"details"` //详情 JSON string
Gender string `xorm:"'gender'" json:"gender"` //性别
Tags []string `xorm:"'tags' json" json:"tags"` //世界标签集
RelatedWorlds []int64 `xorm:"'related_worlds' json" json:"related_worlds"` //相关世界
UpdateTime int64 `xorm:"updated" json:"update_time"` //更新时间
CreateTime int64 `xorm:"created" json:"create_time"` //构造时间
ServiceProviderId string `xorm:"-" json:"authId,omitempty"` // ServiceProviderId 在数据库中忽略
}
// 增加一个新的 NPC 角色
func CreateNpcRole(session *xorm.Session, npcRole *NpcRole) (*NpcRole, error) {
_, err := session.Insert(npcRole)
if err != nil {
return nil, fmt.Errorf("插入NPC角色失败: %v", err)
}
return npcRole, nil
}
// FindNpcRolesByWorldID 查询包含指定related_worlds ID的NPC角色数组
func FindNpcRolesByWorldID(session *xorm.Session, worldID int64) ([]NpcRole, error) {
var npcRoles []NpcRole
// 使用LIKE操作符来搜索包含特定world ID的文本字段
worldIDStr := strconv.FormatInt(worldID, 10) // 将参与者ID转换为字符串
query := fmt.Sprintf("[%s]", worldIDStr)
err := session.Where("JSON_CONTAINS(related_worlds, ?)", query).Find(&npcRoles)
if err != nil {
return nil, fmt.Errorf("根据相关世界ID查询画像失败: %v", err)
}
return npcRoles, nil
}
// 根据 ID 删除 NPC 角色
func DeleteNpcRole(id int) error {
npcRole := new(NpcRole)
_, err := datasource.Engine.ID(id).Delete(npcRole)
if err != nil {
return fmt.Errorf("failed to delete npc role: %v", err)
}
return nil
}
// 更新 NPC 角色信息
func UpdateNpcRole(npcRole *NpcRole) error {
_, err := datasource.Engine.ID(npcRole.ID).Update(npcRole)
if err != nil {
return fmt.Errorf("failed to update npc role: %v", err)
}
return nil
}
// 根据 ID 查询单个 NPC 角色
func GetNpcRoleByID(id int) (*NpcRole, error) {
npcRole := new(NpcRole)
has, err := datasource.Engine.ID(id).Get(npcRole)
if err != nil {
return nil, fmt.Errorf("failed to get npc role: %v", err)
}
if !has {
return nil, fmt.Errorf("npc role not found")
}
return npcRole, nil
}
// 查询所有 NPC 角色
func GetAllNpcRoles() ([]NpcRole, error) {
var npcRoles []NpcRole
err := datasource.Engine.Find(&npcRoles)
if err != nil {
return nil, fmt.Errorf("failed to get all npc roles: %v", err)
}
return npcRoles, nil
}
......@@ -61,8 +61,12 @@ func InitRouter() *gin.Engine {
router.POST("/MiGuQueryBusinessManagerWorldInfo", controllers.MiGuQueryBusinessManagerWorldInfo)
//语音识别
router.POST("/MiGuSpeechConvertor", service.MiGuTranscribeAudio)
// 创建用户画像
router.POST("/MiGuCreateNpcUserPortrait", controllers.CreateNpcUserPortrait)
// 查询用户画像
router.POST("/MiGuQueryNpcRoleInfo", controllers.QueryNpcRoleInfo)
//======================================================================//
// ====================================================================== //
//语音识别路由
router.POST("/speechConvertor", service.TranscribeAudio)
//AI接口绘制漫画头像
......
......@@ -45,7 +45,7 @@ type WorldReplyMsg struct {
// 转发AI接口意识流响应给前端
type WorldSoulReplyMsg struct {
Code int `json:"code"` // 5 ==>
Code int `json:"code"`
WObj map[string]interface{} `json:"WObj"`
ISLIU string `json:"ISLIU"`
WorldName string `json:"WorldName"`
......
......@@ -1008,3 +1008,49 @@ func SendMessage_HUAWEICloud(templateParas map[string]string, phones, smsTemplat
return nil
}
// translateKeys 是递归函数,用于转换JSON对象的键从中文到英文
// 中文到英文
func TranslateKeysChineseToEnglish(translateKeysMap map[string]string, data interface{}) interface{} {
//ReverseTranslations := config.Conf.BusinessManagerReverseTranslations
switch obj := data.(type) {
case map[string]interface{}:
newMap := make(map[string]interface{})
for k, v := range obj {
newKey := k
if val, ok := translateKeysMap[k]; ok {
newKey = val
}
newMap[newKey] = TranslateKeysChineseToEnglish(translateKeysMap, v)
}
return newMap
case []interface{}:
for i, v := range obj {
obj[i] = TranslateKeysChineseToEnglish(translateKeysMap, v)
}
}
return data
}
// translateKeys 是递归函数,用于转换JSON对象的键
// 英文转中文
func TranslateKeysEnglishToChinese(translateKeysMap map[string]string, data interface{}) interface{} {
//BusinessManagerAssessmentTranslations := config.Conf.BusinessManagerAssessmentConfigTranslations
switch obj := data.(type) {
case map[string]interface{}:
newMap := make(map[string]interface{})
for k, v := range obj {
newKey := k
if val, ok := translateKeysMap[k]; ok {
newKey = val
}
newMap[newKey] = TranslateKeysEnglishToChinese(translateKeysMap, v)
}
return newMap
case []interface{}:
for i, v := range obj {
obj[i] = TranslateKeysEnglishToChinese(translateKeysMap, v)
}
}
return data
}
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment