change code

This commit is contained in:
2026-01-10 02:52:48 +09:00
parent 5e66ac91f8
commit 44147d0742
5 changed files with 49 additions and 19 deletions

View File

@@ -0,0 +1,6 @@
package com.example.curation_train_app
data class AiReply(
val text: String,
val emotion: EmotionType
)

View File

@@ -0,0 +1,24 @@
package com.example.curation_train_app
import org.json.JSONObject
object AiReplyParser {
fun parse(json: String): AiReply {
return try {
val obj = JSONObject(json)
// GPT から返ってくるのは「text」だけ
val text = obj.optString("text", "(解析エラー)")
// emotion は入っていないため、常に NORMAL を返す
val emotion = EmotionType.NORMAL
AiReply(text, emotion)
} catch (e: Exception) {
AiReply("(JSON解析エラー)", EmotionType.NORMAL)
}
}
}

View File

@@ -25,15 +25,19 @@ class CharacterReplyManager {
?: return "キャラが見つかりません。" ?: return "キャラが見つかりません。"
return responder.respond(info, type) return responder.respond(info, type)
} }
fun replyWithAI(characterId: String, info: String, type: InfoType): String {
val character = CharacterProfiles.getProfile(characterId)
?: return "(エラー:キャラが不明です)"
fun replyWithAI(characterId: String, info: String, type: InfoType): AiReply {
val character = CharacterProfiles.getProfile(characterId)
?: return AiReply("(エラー:キャラが不明です)", EmotionType.NORMAL)
val prompt = PromptBuilder.buildPrompt(character, info, type) val prompt = PromptBuilder.buildPrompt(character, info, type)
val ai = AiClient(API_KEY) val ai = AiClient(apiKey = API_KEY)
return ai.requestCharacterReply(prompt) val raw = ai.requestCharacterReply(prompt)
// JSON パース
return AiReplyParser.parse(raw)
} }
} }

View File

@@ -43,6 +43,7 @@ class MainActivity : ComponentActivity() {
val inputText = findViewById<EditText>(R.id.inputText) val inputText = findViewById<EditText>(R.id.inputText)
val inputCharacter = findViewById<EditText>(R.id.inputCharacter) val inputCharacter = findViewById<EditText>(R.id.inputCharacter)
val textResult = findViewById<TextView>(R.id.textTestResult) val textResult = findViewById<TextView>(R.id.textTestResult)
val commentView = findViewById<TextView>(R.id.textCharacterComment)
btn.setOnClickListener { btn.setOnClickListener {
@@ -51,23 +52,13 @@ class MainActivity : ComponentActivity() {
val infoType = InfoClassifier.classify(text) val infoType = InfoClassifier.classify(text)
Thread { Thread {
val reply = CharacterReplyManager().replyWithAI(charId, text, infoType) val result = CharacterReplyManager().replyWithAI(charId, text, infoType)
runOnUiThread { runOnUiThread {
val commentView = findViewById<TextView>(R.id.textCharacterComment) commentView.text = result.text
updateCharacterExpression(charId, result.emotion)
val cleanText =
if (reply.trim().startsWith("{") || reply.trim().startsWith("[")) {
AiClient("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").extractText(reply)
} else reply
commentView.text = cleanText
// 表情切り替え
val infoType = InfoClassifier.classify(text)
val emotion = infoTypeToEmotion(infoType)
updateCharacterExpression(charId,emotion)
} }
}.start() }.start()
} }

View File

@@ -14,6 +14,11 @@ object PromptBuilder {
return """ return """
あなたは以下のキャラクターになりきって返答してください。 あなたは以下のキャラクターになりきって返答してください。
あなたの返答は次の JSON 形式にしてください:
{"text": "返す文章", "emotion": "happy / angry / think / worry / surprised / sleepy / confused / normal"}
【キャラ設定】 【キャラ設定】
名前:${character.name} 名前:${character.name}
話し方:${character.speakingStyle} 話し方:${character.speakingStyle}