change code
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package com.example.curation_train_app
|
||||
|
||||
data class AiReply(
|
||||
val text: String,
|
||||
val emotion: EmotionType
|
||||
)
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,15 +25,19 @@ class CharacterReplyManager {
|
||||
?: return "キャラが見つかりません。"
|
||||
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 ai = AiClient(API_KEY)
|
||||
return ai.requestCharacterReply(prompt)
|
||||
val ai = AiClient(apiKey = API_KEY)
|
||||
val raw = ai.requestCharacterReply(prompt)
|
||||
|
||||
// JSON パース
|
||||
return AiReplyParser.parse(raw)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ class MainActivity : ComponentActivity() {
|
||||
val inputText = findViewById<EditText>(R.id.inputText)
|
||||
val inputCharacter = findViewById<EditText>(R.id.inputCharacter)
|
||||
val textResult = findViewById<TextView>(R.id.textTestResult)
|
||||
val commentView = findViewById<TextView>(R.id.textCharacterComment)
|
||||
|
||||
|
||||
btn.setOnClickListener {
|
||||
@@ -51,23 +52,13 @@ class MainActivity : ComponentActivity() {
|
||||
val infoType = InfoClassifier.classify(text)
|
||||
|
||||
Thread {
|
||||
val reply = CharacterReplyManager().replyWithAI(charId, text, infoType)
|
||||
val result = CharacterReplyManager().replyWithAI(charId, text, infoType)
|
||||
|
||||
runOnUiThread {
|
||||
val commentView = findViewById<TextView>(R.id.textCharacterComment)
|
||||
|
||||
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)
|
||||
commentView.text = result.text
|
||||
updateCharacterExpression(charId, result.emotion)
|
||||
}
|
||||
|
||||
}.start()
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,11 @@ object PromptBuilder {
|
||||
return """
|
||||
あなたは以下のキャラクターになりきって返答してください。
|
||||
|
||||
あなたの返答は次の JSON 形式にしてください:
|
||||
{"text": "返す文章", "emotion": "happy / angry / think / worry / surprised / sleepy / confused / normal"}
|
||||
|
||||
|
||||
|
||||
【キャラ設定】
|
||||
名前:${character.name}
|
||||
話し方:${character.speakingStyle}
|
||||
|
||||
Reference in New Issue
Block a user