lambiengcode / compose-chatgpt-kotlin-android-chatbot

Compose ChatGPT Kotlin - Android Chatbot using Kotlin + Jetpack Compose + Coroutine + MVVM + Retrofit2 + OpenAI's GPT-3 API. Allow stream response from ChatGPT API.
https://youtube.com/shorts/esLh-n3Ao5U
MIT License
226 stars 33 forks source link

Feature request: Replace \n with a linefeed in the chat answers #14

Open TimRivoli opened 1 year ago

TimRivoli commented 1 year ago

The chat answers have \n instead of line feeds. That could be a simple string replacement and improve the appearance of the answers.

YoungJeansKR commented 1 year ago

The chat answers have \n instead of line feeds. That could be a simple string replacement and improve the appearance of the answers.

Have you solved it? I am also suffering from the same symptoms.

sollarp commented 1 year ago

@TimRivoli I can have a look but can you be more specific. Where is this happening?

YoungJeansKR commented 1 year ago

@TimRivoli I can have a look but can you be more specific. Where is this happening?

KakaoTalk_20230930_170121567

As shown in the picture above, when gpt answers a question, it does not recognize \n as a line feed and outputs it as is. Do you know a solution?

sollarp commented 1 year ago

@TimRivoli I can have a look but can you be more specific. Where is this happening?

KakaoTalk_20230930_170121567

As shown in the picture above, when gpt answers a question, it does not recognize \n as a line feed and outputs it as is. Do you know a solution?

Hi, Unfortunately I can't test this code since my API key is some reason does not work in this app. I have tested my key in other apps and works ok with gpt-3.5 but not gpt-4 anyways. Can I ask you to test this with your API key and let me know If it's working. So I could do a proper pull request.

in OpenAIRepositoryImpl change the code at line 83 and 94 and paste this code.

private fun lookupDataFromResponse(jsonString: String): String {
val regex = """"text"\s*:\s*"([^"]+)"""".toRegex()
val matchResult = regex.find(jsonString)

if (matchResult != null && matchResult.groupValues.size > 1) {
    val extractedText = matchResult.groupValues[1]
    return extractedText.replace("\\n\\n", " ").replace("\\n", " ")
}

return " "
}
YoungJeansKR commented 1 year ago

@TimRivoli I can have a look but can you be more specific. Where is this happening?

KakaoTalk_20230930_170121567 As shown in the picture above, when gpt answers a question, it does not recognize \n as a line feed and outputs it as is. Do you know a solution?

Hi, Unfortunately I can't test this code since my API key is some reason does not work in this app. I have tested my key in other apps and works ok with gpt-3.5 but not gpt-4 anyways. Can I ask you to test this with your API key and let me know If it's working. So I could do a proper pull request.

in OpenAIRepositoryImpl change the code at line 83 and 94 and paste this code.

private fun lookupDataFromResponse(jsonString: String): String {
val regex = """"text"\s*:\s*"([^"]+)"""".toRegex()
val matchResult = regex.find(jsonString)

if (matchResult != null && matchResult.groupValues.size > 1) {
    val extractedText = matchResult.groupValues[1]
    return extractedText.replace("\\n\\n", " ").replace("\\n", " ")
}

return " "
}

As a result of testing, it works very well. \n is no longer printed and the replace function works fine. Thank you very much:)