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

Add documentation snippets to the repo #182

Merged
merged 47 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
6e2faa7
Initial sample code
rlazo Jun 11, 2024
4623ffb
Add empty readme
rlazo Jun 11, 2024
7a44044
Add chat samples
rlazo Jun 12, 2024
17ed102
Initial token count samples.
rlazo Jun 12, 2024
66050a2
Fix duplicated `startChat` declarations
rlazo Jun 12, 2024
90384df
Add java countToken samples
rlazo Jun 13, 2024
9ecc7e7
Add function calling files.
rlazo Jun 13, 2024
cfb1331
Use right package name for java snippets
rlazo Jun 13, 2024
d83cd0c
Add controlled generation files
rlazo Jun 13, 2024
f03f26c
Add safety settings files
rlazo Jun 13, 2024
0d4a534
Add model configuration snippets
rlazo Jun 13, 2024
8dda71f
Add system instructions samples
rlazo Jun 13, 2024
b9d0999
Fix format
rlazo Jun 13, 2024
3b8953e
Fix count tokens sample
rlazo Jun 13, 2024
2f4a694
Add dummy BuidConfig files
rlazo Jun 13, 2024
a902099
Add missing imports in kotlin sample files
rlazo Jun 13, 2024
500ba0f
Java fix imports
rlazo Jun 13, 2024
0959163
Missing fixes for java code
rlazo Jun 13, 2024
2b97243
Add sample images
rlazo Jun 13, 2024
d382b88
Update generativeai quickstart app to include sample code
rlazo Jun 13, 2024
a01c6c9
Fix formatting
rlazo Jun 13, 2024
7771776
Fix kotlin formatting
rlazo Jun 13, 2024
c29dd67
Add build config warning comment
rlazo Jun 13, 2024
6cf2258
Change copyright comment style for Kotlin
rlazo Jun 13, 2024
59b0f3d
Add actual content to README
rlazo Jun 13, 2024
10da9cd
Add DEVELOPING.md to the snippets directory
rlazo Jun 13, 2024
9aa0765
Improve comments around executor usage
rlazo Jun 13, 2024
19e338f
Merge branch 'main' into rl.sample.snippet.initial
rlazo Jun 13, 2024
5c88f27
Use right tags in count token multimodal
rlazo Jun 19, 2024
ad241df
Merge branch 'main' into rl.sample.snippet.initial
rlazo Jun 24, 2024
0694a99
Update samples/src/main/java/com/google/ai/client/generative/samples/…
rlazo Jun 24, 2024
5a5d0c8
Apply suggestions from code review
rlazo Jun 24, 2024
2061786
Apply suggestions from code review
rlazo Jun 24, 2024
ae8b3ec
Apply suggestions from code review
rlazo Jun 24, 2024
6a3a0c7
Update samples/src/main/java/com/google/ai/client/generative/samples/…
rlazo Jun 24, 2024
49125c2
Remove exclude blocks
rlazo Jun 24, 2024
89e7232
Fix bad end block tag
rlazo Jun 24, 2024
687bed9
Use underscore in safety_settings
rlazo Jun 26, 2024
979352d
Use singular, system instruction
rlazo Jun 26, 2024
75317e9
Add comment block for api key
rlazo Jun 26, 2024
58e0bd8
Fix image prompts in `text_generation.kt`
rlazo Jun 27, 2024
2bfe9e3
Address compilation issues
rlazo Jun 27, 2024
022ed03
Merge branch 'main' into rl.sample.snippet.initial
rlazo Jul 2, 2024
a03f151
Hide samples source code inside a build variant
rlazo Jul 9, 2024
4cd8686
Merge branch 'main' into rl.sample.snippet.initial
rlazo Jul 9, 2024
8c76dff
add code execution samples (#198)
davidmotson Jul 10, 2024
11da79a
Update model configuration name
rlazo Jul 11, 2024
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
16 changes: 16 additions & 0 deletions generativeai-android-sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ android {
}
}

buildTypes {
create("samples") {
initWith(getByName("debug"))
}
}

sourceSets.getByName("samples") {
java.setSrcDirs(listOf("src/main/java", "src/main/kotlin", "../../samples/src/main/java"))
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -59,6 +69,12 @@ dependencies {
implementation("androidx.activity:activity-compose:1.8.1")
implementation("androidx.navigation:navigation-compose:2.7.5")

// Required for one-shot operations (to use `ListenableFuture` from Guava Android)
implementation("com.google.guava:guava:31.0.1-android")

// Required for streaming operations (to use `Publisher` from Reactive Streams)
implementation("org.reactivestreams:reactive-streams:1.0.4")

implementation(platform("androidx.compose:compose-bom:2023.10.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions samples/DEVELOPING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Developing

The snippets in this directory are organized to simplify their use as documentation.

## Snippets requirements

All snippets must compile.

## Workflow

1. In Android Studio, import the `generativeai-android-sample` project
2. In the left-hand bar, using the "Android" prespective, you'll
notice that the within the `app` module, there are two packages:
- `com.google.ai.client.generative.samples` which contains the snippets
- `com.google.ai.sample` which contains the actual quickstart app
3. Make all necessary changes to the code in the
`com.google.ai.client.generative.samples` snippets
4. To compile the snippets, compile the `app` module itself.

**IMPORTANT:** Always add both the Kotlin and the Java versions of the
snippets at the same time to maintain parity.

### How does it work

Under the hood, the configuration of the app module, in
`generativeai-android-sample/build.gradle.kts`, has been modified to
include in the main `sourceSet` the code for the samples.
1 change: 1 addition & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GenerativeAI sample snippets for Android in Kotlin and Java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ai.client.generative.samples;

// This file was manually created for testing purposes only.
// For instructions on how to generate this file automatically, see the section
// "Set up your API Key" in https://ai.google.dev/gemini-api/docs/quickstart?lang=android
public class BuildConfig {
public static String apiKey = "invalidApiKey";
}
107 changes: 107 additions & 0 deletions samples/src/main/java/com/google/ai/client/generative/samples/chat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.ai.client.generative.samples

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.google.ai.client.generativeai.GenerativeModel
import com.google.ai.client.generativeai.type.content
import com.google.ai.sample.R

// Set up your API Key
// ====================
//
// To use the Gemini API, you'll need an API key. To learn more, see
// the "Set up your API Key section" in the [Gemini API
// quickstart](https://ai.google.dev/gemini-api/docs/quickstart?lang=android#set-up-api-key).

suspend fun chat() {
// [START chat]
rlazo marked this conversation as resolved.
Show resolved Hide resolved
val generativeModel =
GenerativeModel(
// Specify a Gemini model appropriate for your use case
modelName = "gemini-1.5-flash",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
rlazo marked this conversation as resolved.
Show resolved Hide resolved
apiKey = BuildConfig.apiKey)

val chat =
generativeModel.startChat(
history =
listOf(
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
content(role = "model") {
text("Great to meet you. What would you like to know?")
}))

val response = chat.sendMessage("How many paws are in my house?")
print(response.text)
rlazo marked this conversation as resolved.
Show resolved Hide resolved
// [END chat]
}

suspend fun chatStreaming() {
// [START chat_streaming]
// Use streaming with multi-turn conversations (like chat)
val generativeModel =
GenerativeModel(
// Specify a Gemini model appropriate for your use case
modelName = "gemini-1.5-flash",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = BuildConfig.apiKey)

val chat =
generativeModel.startChat(
history =
listOf(
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
content(role = "model") {
text("Great to meet you. What would you like to know?")
}))

chat.sendMessageStream("How many paws are in my house?").collect { chunk -> print(chunk.text) }
// [END chat_streaming]
}

suspend fun chatStreamingWithImages(context: Context) {
// [START chat_streaming_with_images]
// Use streaming with multi-turn conversations (like chat)
val generativeModel =
GenerativeModel(
// Specify a Gemini model appropriate for your use case
modelName = "gemini-1.5-flash",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = BuildConfig.apiKey)

val chat =
generativeModel.startChat(
history =
listOf(
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
content(role = "model") {
text("Great to meet you. What would you like to know?")
}))

val image: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.image)

val inputContent = content {
image(image)
text("This is a picture of them, what breed are they?")
}

chat.sendMessageStream(inputContent).collect { chunk -> print(chunk.text) }
// [END chat_streaming_with_images]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.ai.client.generative.samples

import com.google.ai.client.generativeai.GenerativeModel
import com.google.ai.client.generativeai.type.Tool

// Set up your API Key
// ====================
//
// To use the Gemini API, you'll need an API key. To learn more, see
// the "Set up your API Key section" in the [Gemini API
// quickstart](https://ai.google.dev/gemini-api/docs/quickstart?lang=android#set-up-api-key).

suspend fun codeExecutionBasic() {
// [START code_execution_basic]

val model = GenerativeModel(
// Specify a Gemini model appropriate for your use case
modelName = "gemini-1.5-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = BuildConfig.apiKey,
tools = listOf(Tool.CODE_EXECUTION)
)

val response = model.generateContent("What is the sum of the first 50 prime numbers?")

println(response.text)
// [END code_execution_basic]
}

suspend fun codeExecutionChat() {
// [START code_execution_chat]

val model = GenerativeModel(
// Specify a Gemini model appropriate for your use case
modelName = "gemini-1.5-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = BuildConfig.apiKey,
tools = listOf(Tool.CODE_EXECUTION)
)

val chat = model.startChat()

val response = chat.sendMessage("What is the sum of the first 50 prime numbers?")

println(response.text)
// [END code_execution_chat]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.ai.client.generative.samples

import com.google.ai.client.generativeai.GenerativeModel
import com.google.ai.client.generativeai.type.generationConfig

// Set up your API Key
// ====================
//
// To use the Gemini API, you'll need an API key. To learn more, see
// the "Set up your API Key section" in the [Gemini API
// quickstart](https://ai.google.dev/gemini-api/docs/quickstart?lang=android#set-up-api-key).


suspend fun configureModel() {
// [START configure_model_parameters]
val config = generationConfig {
temperature = 0.9f
topK = 16
topP = 0.1f
maxOutputTokens = 200
stopSequences = listOf("red")
}

val generativeModel =
GenerativeModel(
// Specify a Gemini model appropriate for your use case
modelName = "gemini-1.5-flash",
apiKey = BuildConfig.apiKey,
generationConfig = config)
// [END configure_model_parameters]

// Added to silence the compiler warning about unused variable.
print(generativeModel)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.ai.client.generative.samples

// Set up your API Key
// ====================
//
// To use the Gemini API, you'll need an API key. To learn more, see
// the "Set up your API Key section" in the [Gemini API
// quickstart](https://ai.google.dev/gemini-api/docs/quickstart?lang=android#set-up-api-key).

// TODO
Loading
Loading