|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def test_gemini_text_generator_model(): |
| 17 | + # Determine project id, in this case prefer the one set in the environment |
| 18 | + # variable GOOGLE_CLOUD_PROJECT (if any) |
| 19 | + import os |
| 20 | + |
| 21 | + PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "bigframes-dev") |
| 22 | + REGION = "us" |
| 23 | + CONN_NAME = "bigframes-default-connection" |
| 24 | + |
| 25 | + # [START bigquery_dataframes_gemini_model] |
| 26 | + from bigframes.ml.llm import GeminiTextGenerator |
| 27 | + import bigframes.pandas as bpd |
| 28 | + |
| 29 | + # Create the Gemini LLM model |
| 30 | + session = bpd.get_global_session() |
| 31 | + connection = f"{PROJECT_ID}.{REGION}.{CONN_NAME}" |
| 32 | + model = GeminiTextGenerator(session=session, connection_name=connection) |
| 33 | + |
| 34 | + df_api = bpd.read_csv("gs://cloud-samples-data/vertex-ai/bigframe/df.csv") |
| 35 | + |
| 36 | + # Prepare the prompts and send them to the LLM model for prediction |
| 37 | + df_prompt_prefix = "Generate Pandas sample code for DataFrame." |
| 38 | + df_prompt = df_prompt_prefix + df_api["API"] |
| 39 | + |
| 40 | + # Predict using the model |
| 41 | + df_pred = model.predict(df_prompt.to_frame(), max_output_tokens=1024) |
| 42 | + # [END bigquery_dataframes_gemini_model] |
| 43 | + assert df_pred["ml_generate_text_llm_result"] is not None |
| 44 | + assert df_pred["ml_generate_text_llm_result"].iloc[0] is not None |
0 commit comments