У меня есть два метода с дублирующимся кодом. Мне нужно, чтобы ты оптимизировал их и решил проблему с дублирующимся кодом.

```kotlin
    fun generatePersonalVillagerData(villager: Villager, prompt: String) {

        val jsonInputString = """
        {
            "contents": [
                {
                    "parts": [
                        {
                            "text": "$prompt"
                        }
                    ]
                }
            ],
            "safetySettings": [
                {
                    "category": "7",
                    "threshold": "4"
                },
            ]
        }
        """.trimIndent()

        val body = jsonInputString.toRequestBody("application/json".toMediaTypeOrNull())
        val request = Request.Builder()
            .url(url)
            .post(body)
            .build()

        client.newCall(request).enqueue(object : Callback {

            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                if (response.isSuccessful) {
                    try {
                        val responseData = plugin.gson.fromJson(response.body!!.string(), ResponseData::class.java)
                        val responseText = responseData?.candidates?.get(0)?.content?.parts?.get(0)?.text ?: return
                        val responseJson = findJson(responseText) ?: return

                        val cleanedJsonResponse = cleanQuestJson(responseJson)
                        plugin.server.scheduler.runTask(plugin) { _ ->
                            plugin.server.pluginManager.callEvent(
                                PersonalVillagerDataGenerateEvent(
                                    villager,
                                    plugin.gson.fromJson(
                                        cleanedJsonResponse,
                                        VillagerManager.PersonalVillagerData::class.java
                                    )
                                )
                            )
                        }
                        return
                        

                    } catch (ignored: Exception) {
                        return
                    }
                } else {
                    plugin.logger.info("Request failed: ${response.code}, ${response.message}")
                    plugin.logger.info("Request: $request")
                }
            }

        })

    }
    
    fun generateQuestData(villager: Villager, quest: VillagerQuest.Builder, prompt: String) {

        val jsonInputString = """
        {
            "contents": [
                {
                    "parts": [
                        {
                            "text": "$prompt"
                        }
                    ]
                }
            ],
            "safetySettings": [
                {
                    "category": "7",
                    "threshold": "4"
                },
            ]
        }
        """.trimIndent()

        val body = jsonInputString.toRequestBody("application/json".toMediaTypeOrNull())
        val request = Request.Builder()
            .url(url)
            .post(body)
            .build()

        client.newCall(request).enqueue(object : Callback {

            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                if (response.isSuccessful) {
                    try {
                        val responseData = plugin.gson.fromJson(response.body!!.string(), ResponseData::class.java)
                        val responseText = responseData?.candidates?.get(0)?.content?.parts?.get(0)?.text ?: return
                        val questJson = findJson(responseText) ?: return
                        val cleanedQuestJson = cleanQuestJson(questJson)
                        quest.setQuestInfo(plugin.gson.fromJson(cleanedQuestJson, VillagerQuest.QuestInfo::class.java))
                        plugin.server.scheduler.runTask(plugin) { _ ->
                            plugin.server.pluginManager.callEvent(QuestSuccessfulGenerateEvent(villager, quest.build()))
                        }
                    } catch (ignored: Exception) {
                        return
                    }
                } else {
                    plugin.logger.info("Request failed: ${response.code}, ${response.message}")
                    plugin.logger.info("Request: $request")
                }
            }

        })

    }
```