8000 Gemini API Key and README by lethargicpanda · Pull Request #80 · android/socialite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Gemini API Key and README #80

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

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Jetpack APIs used:
- [Room](https://developer.android.com/jetpack/androidx/releases/room) Persisting messages in a SQLite database
- [Window](https://developer.android.com/jetpack/androidx/releases/window) Detecting foldable device states

The app also integrates the Gemini API that powers chatbot capabilities:

- [Google AI Client SDK](https://developer.android.com/ai/google-ai-client-sdk) Generative AI with Gemini 1.5 Flash


> 🚧 **Work-in-Progress:** This sample is still in the early stage of development, and we're excited to add more features over time.

## App Overview
Expand Down Expand Up @@ -41,6 +46,12 @@ Here are the screens that make up SociaLite:
2. Open the whole project in Android Studio.
3. Sync & Run `app` configuration

### (optional) Add Gemini API:
4. Create a Gemini API key in [Google AI studio](https://aistudio.google.com/app/apikey),
5. Add your Gemini API key as `API_KEY` to your [`local.properties`] file,
6. Sync & Run `app` configuration,
7. In the app, go to Settings and tap the "AI Chatbot" button.

## Reporting Issues

You can report an [issue with a sample](https://github.com/android/socialite/issues) using
Expand Down
5 changes: 5 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ plugins {
alias(libs.plugins.hilt)
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.ksp)
alias(libs.plugins.secrets)
}

kotlin {
jvmToolchain(17)
}

secrets {
defaultPropertiesFileName = "secret.defaults.properties"
}

android {
namespace = "com.google.android.samples.socialite"
compileSdk = 34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.datastore.preferences.preferencesDataStore
import com.google.ai.client.generativeai.GenerativeModel
import com.google.ai.client.generativeai.type.Content
import com.google.ai.client.generativeai.type.content
import com.google.android.samples.socialite.BuildConfig
import com.google.android.samples.socialite.R
import com.google.android.samples.socialite.data.ChatDao
import com.google.android.samples.socialite.data.ContactDao
Expand Down Expand Up @@ -57,7 +58,6 @@ class ChatRepository @Inject internal constructor(
private val coroutineScope: CoroutineScope,
@ApplicationContext private val appContext: Context,
) {
private val geminiApiKey = "YOUR_API_KEY"
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
private val enableChatbotKey = booleanPreferencesKey("enable_chatbot")
val isBotEnabled = appContext.dataStore.data.map {
Expand Down Expand Up @@ -96,8 +96,8 @@ class ChatRepository @Inject internal constructor(
// Create a generative AI Model to interact with the Gemini API.
val generativeModel = GenerativeModel(
modelName = "gemini-1.5-pro-latest",
// Set your Gemini API
apiKey = geminiApiKey,
// Set your Gemini API in as an `API_KEY` variable in your local.properties file
apiKey = BuildConfig.API_KEY,
// Set a system instruction to set the behavior of the model.
systemInstruction = content {
text("Please respond to this chat conversation like a friendly ${detail.firstContact.replyModel}.")
Expand All @@ -113,13 +113,15 @@ class ChatRepository @Inject internal constructor(
)

// Send a message prompt to the model to generate a response
var generateContentResult = try {
chat.sendMessage(text)
var response = try {
chat.sendMessage(text).text?.trim() ?: "..."
} catch (e: Exception) {
e.printStackTrace()
null
appContext.getString(
R.string.gemini_error,
e.message ?: appContext.getString(R.string.unknown_error),
)
}
val response = generateContentResult?.text ?: "GenAI failed :(".trim()

// Save the generated response to the database
saveMessageAndNotify(chatId, response, detail.firstContact.id, null, null, detail, PushReason.IncomingMessage)
Expand Down Expand Up @@ -252,7 +254,7 @@ class ChatRepository @Inject internal constructor(
}

fun toggleChatbotSetting() {
if (geminiApiKey == "YOUR_API_KEY") {
if (BuildConfig.API_KEY == "DUMMY_API_KEY") {
Toast.makeText(
appContext,
appContext.getString(R.string.set_api_key_toast),
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
<string name="ai_chatbot_setting">AI Chatbot</string>
<string name="ai_chatbot_setting_enabled">enabled</string>
<string name="ai_chatbot_setting_disabled">disabled</string>
<string name="set_api_key_toast">Set your Gemini API key</string>
<string name="set_api_key_toast">Please set your API key in local.properties</string>
<string name="gemini_error">Gemini error (%1$s)</string>
<string name="unknown_error">Unknown error</string>

</resources>
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
secrets = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secrets" }
3 changes: 3 additions & 0 deletions secret.defaults.properties
4320
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file provides default values for the secrets gradle plugin.
# Actual values should be defined in local.properties.
API_KEY="DUMMY_API_KEY"
Loading
0