This video shows the steps to use PaLM APIs in your Android App’s Java code.
It refers to the steps mentioned at https://developers.generativeai.google/tutorials/text_java_quickstart
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.googlepalmapi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.ai.generativelanguage.v1beta3.GenerateTextRequest;
import com.google.ai.generativelanguage.v1beta3.GenerateTextResponse;
import com.google.ai.generativelanguage.v1beta3.TextCompletion;
import com.google.ai.generativelanguage.v1beta3.TextPrompt;
import com.google.ai.generativelanguage.v1beta3.TextServiceClient;
import com.google.ai.generativelanguage.v1beta3.TextServiceSettings;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import java.io.IOException;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private TextServiceClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textViewOutput);
HashMap<String, String> headers = new HashMap<>();
headers.put("x-goog-api-key", "AIzaSyCYT4ITJn2WtVHAFhJgoOAeQfzMRHsUEWE");
TransportChannelProvider provider = InstantiatingGrpcChannelProvider.newBuilder()
.setHeaderProvider(FixedHeaderProvider.create(headers))
.build();
TextServiceSettings settings = null;
try {
settings = TextServiceSettings.newBuilder()
.setTransportChannelProvider(provider)
.setCredentialsProvider(FixedCredentialsProvider.create(null))
.build();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
client = TextServiceClient.create(settings);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void buttonPaLMAPI(View view){
TextPrompt prompt = TextPrompt.newBuilder()
.setText("Some facts about Jupiter")
.build();
GenerateTextRequest request = GenerateTextRequest.newBuilder()
.setModel("models/text-bison-001") // Required, which model to use to generate the result
.setPrompt(prompt) // Required
.setTemperature(0.5f) // Optional, controls the randomness of the output
.setCandidateCount(1) // Optional, the number of generated texts to return
.build();
GenerateTextResponse response = client.generateText(request);
TextCompletion returnedText = response.getCandidatesList().get(0);
textView.setText(returnedText.getOutput());
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GooglePaLMAPI"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
plugins {
id("com.android.application")
}
android {
namespace = "com.programmerworld.googlepalmapi"
compileSdk = 34
defaultConfig {
applicationId = "com.programmerworld.googlepalmapi"
minSdk = 34
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
packaging{
resources.excludes.add("META-INF/INDEX.LIST")
resources.excludes.add("META-INF/DEPENDENCIES")
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("com.google.cloud:gapic-google-cloud-ai-generativelanguage-v1beta3-java:0.0.0-SNAPSHOT")
implementation("io.grpc:grpc-okhttp:1.53.0")
}
pluginManagement {
repositories {
google()
mavenCentral()
mavenLocal()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
mavenLocal()
}
}
rootProject.name = "Google PaLM API"
include(":app")
Installation to mavenLocal steps in Windows command Prompt:
C:\Work\GooglePaLMAPI>set JAVA_HOME=C:\Program Files\Java\jdk-13.0.2
C:\Work\GooglePaLMAPI>echo $JAVA_HOME$
$JAVA_HOME$
C:\Work\GooglePaLMAPI>echo %JAVA_HOME%
C:\Program Files\Java\jdk-13.0.2
C:\Work\GooglePaLMAPI>cd google-cloud-ai-generativelanguage-v1beta3-java
C:\Work\GooglePaLMAPI\google-cloud-ai-generativelanguage-v1beta3-java>gradlew publishToMavenLocal
> Task :gapic-google-cloud-ai-generativelanguage-v1beta3-java:compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
BUILD SUCCESSFUL in 9s
17 actionable tasks: 17 executed
C:\Work\GooglePaLMAPI\google-cloud-ai-generativelanguage-v1beta3-java>