Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TBD: Gemini & Open AI trial #87

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ dependencies {
// About
implementation(libs.about.lib.core)
implementation(libs.about.lib.compose.ui)

// AI
implementation(libs.gemini)
implementation(libs.openai)
}

kapt {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.odaridavid.weatherapp.data.ai

import com.github.odaridavid.weatherapp.BuildConfig
import com.google.ai.client.generativeai.GenerativeModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject

class OutfitRecommendationRecommenderGemini @Inject constructor() {

private val outfitsModel by lazy {
GenerativeModel(
modelName = "gemini-1.0-pro",
apiKey = BuildConfig.VERTEX_AI_API_KEY
)
}

suspend fun generateOutfitRecommendation(temperature: String): Flow<String?> {
// TODO implement for language changes
val prompt = "What would be a nice outfit for $temperature weather?"
return flowOf(outfitsModel.generateContent(prompt).text)
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.odaridavid.weatherapp.data.ai


import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.ChatRole
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.github.odaridavid.weatherapp.BuildConfig
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject

class OutfitRecommendationRecommenderOpenAI @Inject constructor() {

private val openAi by lazy {
OpenAI(token = BuildConfig.OPEN_AI_KEY)
}

suspend fun generateOutfitRecommendation(temperature: String): Flow<String?> {
// TODO implement for language changes
val prompt = "What would be a nice outfit for $temperature weather?"
val chatCompletionRequest = ChatCompletionRequest(
model = ModelId("gpt-3.5-turbo"),
messages = listOf(
ChatMessage(
role = ChatRole.Assistant,
content = prompt
)
)
)
val completion = openAi.chatCompletion(chatCompletionRequest)

val response = completion.choices.first().message.content
return flowOf(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ fun HomeScreen(
if (state.errorMessageId != null) {
ErrorScreen(state.errorMessageId, onTryAgainClicked)
} else {
state.recommendation?.let { recommendation ->
MediumBody(
text = recommendation,
modifier = Modifier
.fillMaxWidth()
.padding(WeatherAppTheme.dimens.medium),
textAlign = TextAlign.Center
)
}
state.weather?.current?.let { currentWeather ->
CurrentWeatherWidget(currentWeather = currentWeather)
} ?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.odaridavid.weatherapp.api.SettingsRepository
import com.github.odaridavid.weatherapp.api.WeatherRepository
import com.github.odaridavid.weatherapp.data.ai.OutfitRecommendationRecommenderOpenAI
import com.github.odaridavid.weatherapp.model.DefaultLocation
import com.github.odaridavid.weatherapp.model.Result
import com.github.odaridavid.weatherapp.model.SupportedLanguage
Expand All @@ -14,14 +15,16 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(
private val weatherRepository: WeatherRepository,
private val settingsRepository: SettingsRepository
private val settingsRepository: SettingsRepository,
private val outfitRecommendationRecommenderOpenAI: OutfitRecommendationRecommenderOpenAI,
) : ViewModel() {

private val _state = MutableStateFlow(HomeScreenViewState(isLoading = true))
Expand Down Expand Up @@ -70,6 +73,19 @@ class HomeViewModel @Inject constructor(
when (result) {
is Result.Success -> {
val weatherData = result.data
viewModelScope.launch {
// TODO Do this someplace else?
val temperature = weatherData.current?.temperature.toString()
outfitRecommendationRecommenderOpenAI
.generateOutfitRecommendation(temperature = temperature)
.catch { e ->
e.printStackTrace()
}
.collect { recommendation ->
setState { copy(recommendation = recommendation) }
}

}
setState {
copy(
weather = weatherData,
Expand Down Expand Up @@ -104,5 +120,6 @@ data class HomeScreenViewState(
val language: SupportedLanguage = SupportedLanguage.ENGLISH,
val weather: Weather? = null,
val isLoading: Boolean = false,
val recommendation: String? = null,
@StringRes val errorMessageId: Int? = null
)
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ androidx-test-runner = "1.5.2"
androidx-test-rules = "1.5.0"
core-ktx = "1.5.0"
coroutines = "1.8.0"
gemini = "0.2.2"
openai = "3.7.0"

[libraries]
activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity-compose" }
Expand Down Expand Up @@ -89,6 +91,8 @@ about-lib-compose-ui = { module = "com.mikepenz:aboutlibraries-compose-m3", vers
android-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" }
android-test-rules = { module = "androidx.test:rules", version.ref = "androidx-test-rules" }
test-core-ktx = { group = "androidx.test", name = "core-ktx", version.ref = "core-ktx" }
gemini = { module = "com.google.ai.client.generativeai:generativeai", version.ref = "gemini" }
openai = { module = "com.aallam.openai:openai-client", version.ref = "openai" }

[plugins]
com-android-application = { id = "com.android.application", version.ref = "android-gradle-plugin" }
Expand Down