Call Palm Api Securely Firebase Extensions
Firebase
Sep 12, 2023
Introduction
AI integrations have become very popular recently. With most applications now looking to integrate this new technology, developers are in search of ways to install this type of technology in a safe and secure way.
The Call PaLM API Securely extension provides an authenticated endpoint, allowing developers to integrate and call the PaLM API securely from client applications. In total, three endpoints are provided for developers:
- GetModels: Returns a list of the available PaLM models.
- GetModel: Returns information about the specified model.
- Post: Sends a request to the PaLM API and returns the AI’s response.
Google PaLM API
Google’s PaLM API is an additional API layer that connects to Google’s new PaLM Large Language Model (LLM). An LLM, or Large Language Model, is a type of artificial intelligence program designed to understand, generate, and respond to human language in a way that is coherent and contextually relevant. Using the available APIs, you can select the type of PaLM model to generate a response, and then post requests to prompt a response from the AI.
Getting Started with the Call PaLM API Securely extension
Before you can access a secure solution to PaLM APIs, ensure you have a Firebase project set up. If it’s your first time, here’s how to get started:
Once your project is prepared, you can integrate the palm-secure-backend extension into your application.
Extension Installation
Installing the “Call PaLM API Securely” extension can be done via the Firebase Console or using the Firebase CLI.
Option 1: Firebase Console
Find your way to the Firebase Extensions catalog and locate the “Call PaLM API Securely” extension. Click “Install in Firebase Console” and complete the installation process by configuring your extension’s parameters.
Option 2: Firebase CLI
For those who prefer using command-line tools, the Firebase CLI offers a straightforward installation command:
firebaseext:installgooglecloud/palm-secure-backend --project=your_project_id
Configure Parameters
To optimize the “Call PaLM API Securely” Firebase extension for your specific needs, it’s essential to understand and set its configuration parameters appropriately.
Here’s a breakdown of each parameter:
- Cloud Functions Location: Choose where to deploy the Cloud Functions for the extension. For instance, if the majority of your customers reside in the central US, select us-central1.Refer to the location selection guide.
- API Key: Your PaLMAPI key, which will be stored using GoogleSecret Manager. You can access your key usingMakersuite.
- Enforce App Check: Choose Yes or No. If selected,App Check will be included to add extra security to your created endpoints. You must implement App Check client side if this feature is enabled.
- Custom Function Hook Url (Optional): An HTTPS callable function that will be called with the request and response body of the PaLM API call, as well as user context.
How it works
After installation and configuration, the extension will have created three endpoints which you can now call from a client application. Here’s what you need to do to get started:
Before calling the function, a user must be logged into the app via Firebase Authentication.
Once authenticated, you can call the named function of your choice with one of the client software development kits (sdks).
- Authenticate through a client application: The created endpoints use special functions called onCall functions.
- Call the PaLM API: The extension will then use the query, passing on the request to the PaLM API.
- Generate response: PaLM will then process and return a response to the extension.
- Update Firestore: The extension will return the resulting data directly as an API response.
Steps to connect to the Call PaLM API Securely Extension
Before you begin, ensure that the Firebase SDK is initialized and you can access Firebase Cloud Authentication and Functions.
const firebaseConfig = {
// Your Firebase configuration keys
};
firebase.initializeApp(firebaseConfig);
const functions = getFunctions(app);
const auth = getAuth(app);
As an onCall
function is required, we should log in before running a search.
We can do this by signing in as an anonymous user:
// Ensure a user has been authenticated
signInAnonymously(authRef).then(() => {
console.log('Signed in anonymously');
});
Using a chosen mode, method and the user’s input text. You can post and receive an AI generated response. A breakdown of the available model options can be found here.
import { httpsCallable } from 'firebase/functions';
// Send a message using the onCall function
async function sendMessage(event) {
// Get the text from the input
const inputText = document.getElementById('input-message');
// Get the onCall function
const post = httpsCallable(functions, 'ext-palm-secure-backend-post');
const response = await post({
model: 'text-bison-001', // Chosen model
method: 'generateText', // Chosen method
prompt: {
text: inputText.value, // Query for the AI from the input text.
},
});
// Handle the response
}
The response from the api can include a list of responses, in this scenario - we would like to extract the first response from the list of candidates
. Before updating the UI with the response.
// Extract the first candidate response output
{
const output = `🤖: ${response.data.candidates[0].output}`;
}
// Update UI
console.log(output);
Accessing available models
You can also use the getModels endpoint to provide a list of available models. The options provided can be used for the model
parameter for a post
request.
// Set the onCall function
const getModels = httpsCallable(functions, 'ext-palm-secure-backend-getModels');
// Call the getModels API
const result = await getModels();
// Iterate through the resulting models
result.data.models.forEach((model) => {
// Add a new item using model.name to the select element
});
Loading a specified model
The getModel endpoint will provide information about a specified model.
// Set the onCall function
const getModel = httpsCallable(functions, 'ext-palm-secure-backend-getModel');
// Call the getModel API, update the name for the selected model
const result = await getModel({ name: 'chat-bison-001' });
// Update UI
console.log(result.data);
Conclusion
By installing the Secure PaLM API extension, you can access secure endpoints that allow you to safely add AI capabilities to your client application. Flexibility in choosing a model allows users to quickly and safely engage with Goggles PaLM API.
Through the provided code snippets and explanations, we’ve explored how to find the models available, discover in-depth details about each available model, and how to post and retrieve a response from the PaLM AI. These steps form a cohesive pipeline that leverages Firebase’s Authentication, Functions and Firestore to quickly create an effective client based AI component.