-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: implement CertifyMissionRequest * feat: implement CertifyMissionResponse * chore: change local.properties keys * feat: implement OpenAiApiService * chore: rename OpenAi to OpenAI * feat: add openAIApiService into ServicePool * feat: update CertifyMissionRequest * feat: add custom Json instance * feat: implement CertifyMissionResponse toEntity() * chore: update HomeActions * chore: update PickMedia callback * chore: add new HomeDialogState * chore: update actions * feat: add MissionCertificationResultDialog * feat: add MissionRepository * feat: add ContextExtension * feat: add MissionCertificationType * feat: add MissionCertificationInfo * feat: implement CertifyMissionUseCase * feat: add new state for HomeUiState.SUCCESS * chore: rename * chore: update certifyMission * chore: implement certifyMission logic
- Loading branch information
1 parent
e3aba7b
commit 71980cd
Showing
21 changed files
with
337 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/java/univ/earthbreaker/namu/data/model/home/CertifyMissionRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package univ.earthbreaker.namu.data.model.home | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CertifyMissionRequest( | ||
val model: String = "gpt-4-vision-preview", | ||
val messages: List<Message>, | ||
@SerialName("max_tokens") val maxTokens: Int = 300, | ||
) { | ||
@Serializable | ||
data class Message( | ||
val role: String = "user", | ||
@SerialName("content") val contents: List<ImageContent>, | ||
) { | ||
@Serializable | ||
data class ImageContent( | ||
@SerialName("image_url") val imageUrl: ImageUrl, | ||
val prompt: String, | ||
@SerialName("type") val contentType: String = "image_url", | ||
) { | ||
@Serializable | ||
data class ImageUrl( | ||
val url: String, | ||
) | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
app/src/main/java/univ/earthbreaker/namu/data/model/home/CertifyMissionResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package univ.earthbreaker.namu.data.model.home | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import univ.earthbreaker.namu.domain.entity.mission.MissionCertificationInfo | ||
|
||
@Serializable | ||
data class CertifyMissionResponse( | ||
val id: String, | ||
@SerialName("object") val objectType: String = "chat.completion", | ||
val created: Int, | ||
val model: String, | ||
@SerialName("system_fingerprint") val systemFingerprint: String, | ||
val choices: List<Choice>, | ||
val usage: Usage, | ||
) { | ||
@Serializable | ||
data class Choice( | ||
val index: Int, | ||
val message: Message, | ||
@SerialName("finish_reason") val finishReason: String, | ||
val logprobs: Content?, | ||
) { | ||
@Serializable | ||
data class Message( | ||
val role: String, | ||
val content: String?, | ||
) | ||
|
||
@Serializable | ||
data class Content( | ||
val token: String, | ||
val logprob: Int, | ||
val bytes: List<Int>?, | ||
val top_logprobs: List<LogProb>, | ||
) { | ||
@Serializable | ||
data class LogProb( | ||
val token: String, | ||
val logprob: Int, | ||
val bytes: List<Int>?, | ||
) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class Usage( | ||
@SerialName("completion_tokens") val completionTokens: Int, | ||
@SerialName("prompt_tokens") val promptTokens: Int, | ||
@SerialName("total_tokens") val totalTokens: Int, | ||
) | ||
|
||
fun toEntity(): MissionCertificationInfo = | ||
MissionCertificationInfo(isSuccessful = this.choices.first().message.content.toBoolean()) | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/univ/earthbreaker/namu/data/repository/mission/MissionRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package univ.earthbreaker.namu.data.repository.mission | ||
|
||
import univ.earthbreaker.namu.data.ServicePool | ||
import univ.earthbreaker.namu.data.model.home.CertifyMissionRequest | ||
import univ.earthbreaker.namu.data.model.home.CertifyMissionRequest.Message | ||
import univ.earthbreaker.namu.data.model.home.CertifyMissionRequest.Message.ImageContent | ||
import univ.earthbreaker.namu.data.model.home.CertifyMissionRequest.Message.ImageContent.ImageUrl | ||
import univ.earthbreaker.namu.domain.entity.mission.MissionCertificationInfo | ||
import univ.earthbreaker.namu.domain.repository.MissionRepository | ||
|
||
class MissionRepositoryImpl : MissionRepository { | ||
override suspend fun certifyMission( | ||
prompt: String, | ||
imageBase64: String, | ||
): MissionCertificationInfo = | ||
ServicePool.openAIApiService.certifyMission( | ||
certifyMissionRequest = CertifyMissionRequest( | ||
messages = listOf( | ||
Message( | ||
contents = listOf( | ||
ImageContent( | ||
imageUrl = ImageUrl( | ||
url = imageBase64, | ||
), | ||
prompt = prompt, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
).toEntity() | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/univ/earthbreaker/namu/data/service/OpenAIApiService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package univ.earthbreaker.namu.data.service | ||
|
||
import retrofit2.http.Body | ||
import retrofit2.http.Header | ||
import retrofit2.http.POST | ||
import univ.earthbreaker.namu.BuildConfig | ||
import univ.earthbreaker.namu.data.model.home.CertifyMissionRequest | ||
import univ.earthbreaker.namu.data.model.home.CertifyMissionResponse | ||
|
||
interface OpenAIApiService { | ||
@POST("chat/completions") | ||
suspend fun certifyMission( | ||
@Header("Authorization") token: String = "Bearer ${BuildConfig.OPENAI_API_KEY}", | ||
@Body certifyMissionRequest: CertifyMissionRequest, | ||
): CertifyMissionResponse | ||
} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/univ/earthbreaker/namu/domain/entity/mission/MissionCertificationInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package univ.earthbreaker.namu.domain.entity.mission | ||
|
||
data class MissionCertificationInfo(val isSuccessful: Boolean) |
2 changes: 1 addition & 1 deletion
2
...namu/domain/entity/mission/MissionItem.kt → ...namu/domain/entity/mission/MissionInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package univ.earthbreaker.namu.domain.entity.mission | ||
|
||
data class MissionItem( | ||
data class MissionInfo( | ||
val missionName: String, | ||
val beforeChallenge: Boolean, | ||
) |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/univ/earthbreaker/namu/domain/repository/MissionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package univ.earthbreaker.namu.domain.repository | ||
|
||
import univ.earthbreaker.namu.domain.entity.mission.MissionCertificationInfo | ||
|
||
interface MissionRepository { | ||
suspend fun certifyMission( | ||
prompt: String, | ||
imageBase64: String, | ||
): MissionCertificationInfo | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/univ/earthbreaker/namu/domain/usecase/mission/CertifyMissionUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package univ.earthbreaker.namu.domain.usecase.mission | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import univ.earthbreaker.namu.data.repository.mission.MissionRepositoryImpl | ||
import univ.earthbreaker.namu.domain.entity.mission.MissionCertificationInfo | ||
import univ.earthbreaker.namu.domain.repository.MissionRepository | ||
|
||
class CertifyMissionUseCase(private val missionRepository: MissionRepository = MissionRepositoryImpl()) { | ||
suspend operator fun invoke(imageBase64: String): Flow<MissionCertificationInfo> = | ||
flow { | ||
emit( | ||
missionRepository.certifyMission( | ||
imageBase64 = imageBase64, | ||
prompt = """you're a helpful assistant who classify image by true and false. | ||
if an image is related to practicing environmental conservation, | ||
answer true. else false. | ||
""".trimIndent(), | ||
), | ||
) | ||
}.flowOn( | ||
Dispatchers.IO, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.