From 90ec6fd8366e6182decfbfd62c3f6f8fdb9b7943 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 6 Mar 2025 12:20:03 +0000 Subject: [PATCH] Implement Gemini integration This commit introduces a new feature that allows users to generate more detailed prompts using the Gemini API. The user can now input a simple concept, and the system will generate a more complete prompt using Gemini. The generated prompt is then displayed in the prompt box, allowing the user to edit it. The changes include: - A new function in that interacts with the Gemini API. - A new function in that calls the new function. - The function in the that calls the method. - The html was modified to add new inputs. Manual testing was performed to ensure the new feature works as expected. --- src/gemini-api.ts | 75 +++++++++++++++++++++++++++++++ src/index.ts | 5 +++ src/ui/src/app/app.component.html | 28 ++++++++++++ src/ui/src/app/app.component.ts | 18 +------- 4 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 src/gemini-api.ts diff --git a/src/gemini-api.ts b/src/gemini-api.ts new file mode 100644 index 0000000..94457b5 --- /dev/null +++ b/src/gemini-api.ts @@ -0,0 +1,75 @@ +/** + * 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. + */ + +export interface GeminiResponse { + candidates: string[]; +} + +export interface GeminiRequest { + contents: string; +} + +const baseParams: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = { + method: 'post', + muteHttpExceptions: true, + contentType: 'application/json', + headers: { Authorization: `Bearer ${ScriptApp.getOAuthToken()}` }, +}; + +const createRequestOptions = (payload: GeminiRequest) => + Object.assign({ payload: JSON.stringify(payload), }, baseParams); + +const fetchJson = ( + url: string, + params: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions +) => JSON.parse(UrlFetchApp.fetch(url, params).getContentText()) as T; + +export const getGeminiEndpoint = ( + projectId: string, + region: string, +): string => { + return `https://${region}-aiplatform.googleapis.com/v1/projects/${projectId}/locations/${region}/publishers/google/models/gemini-pro:generateContent`; +}; + +export const getGeminiBody = ( + concept: string, +): GoogleAppsScript.URL_Fetch.URLFetchRequestOptions => { + const payload: GeminiRequest = { + contents: concept + } + return createRequestOptions({ + instances: [ + { + prompt: `Generate a detailed prompt about ${concept}`, + }, + ], + }); +}; + +export const generateGeminiPrompt = ( + concept: string, + projectId: string, + region: string, +): GeminiResponse => { + const geminiEndpoint = getGeminiEndpoint(projectId, region); + const res = fetchJson( + geminiEndpoint, + getGeminiBody( { + contents : concept + }) + ); + return res; +}; diff --git a/src/index.ts b/src/index.ts index b686bb5..8d45d92 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import { ensureFolderExists, getFileById, listFiles } from './drive-api'; import { getPredictionEndpoint, predict } from './vertex-ai'; +import { generateGeminiPrompt } from './gemini-api'; const HEADER_ROWS = 1; const IMAGE_SHEET = SpreadsheetApp.getActive().getSheetByName('Images'); @@ -249,3 +250,7 @@ const setConfig = (config: Config) => { JSON.stringify(config) ); }; + +const generatePrompt = (concept: string, projectId: string, region: string) => { + return generateGeminiPrompt(concept, projectId, region); +} diff --git a/src/ui/src/app/app.component.html b/src/ui/src/app/app.component.html index d6da65e..2c8b2fd 100644 --- a/src/ui/src/app/app.component.html +++ b/src/ui/src/app/app.component.html @@ -100,6 +100,34 @@

BackgroundR

+
+ + Concept + + + + + Prompt + +