This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_ai.py
More file actions
113 lines (87 loc) · 3.08 KB
/
test_ai.py
File metadata and controls
113 lines (87 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright 2026 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.
import pytest
from bigframes.bigquery import ai, ml
import bigframes.pandas as bpd
@pytest.fixture(scope="session")
def embedding_model(bq_connection, dataset_id):
model_name = f"{dataset_id}.embedding_model"
return ml.create_model(
model_name=model_name,
options={"endpoint": "gemini-embedding-001"},
connection_name=bq_connection,
)
@pytest.fixture(scope="session")
def text_model(bq_connection, dataset_id):
model_name = f"{dataset_id}.text_model"
return ml.create_model(
model_name=model_name,
options={"endpoint": "gemini-2.5-flash"},
connection_name=bq_connection,
)
def test_generate_embedding(embedding_model):
df = bpd.DataFrame(
{
"content": [
"What is BigQuery?",
"What is BQML?",
]
}
)
result = ai.generate_embedding(embedding_model, df)
assert len(result) == 2
assert "embedding" in result.columns
assert "statistics" in result.columns
assert "status" in result.columns
def test_generate_embedding_with_options(embedding_model):
df = bpd.DataFrame(
{
"content": [
"What is BigQuery?",
"What is BQML?",
]
}
)
result = ai.generate_embedding(
embedding_model, df, task_type="RETRIEVAL_DOCUMENT", output_dimensionality=256
)
assert len(result) == 2
embedding = result["embedding"].to_pandas()
assert len(embedding[0]) == 256
def test_generate_text(text_model):
df = bpd.DataFrame({"prompt": ["Dog", "Cat"]})
result = ai.generate_text(text_model, df)
assert len(result) == 2
assert "result" in result.columns
assert "statistics" in result.columns
assert "full_response" in result.columns
assert "status" in result.columns
def test_generate_text_with_options(text_model):
df = bpd.DataFrame({"prompt": ["Dog", "Cat"]})
result = ai.generate_text(text_model, df, max_output_tokens=1)
# It basically asserts that the results are still returned.
assert len(result) == 2
def test_generate_table(text_model):
df = bpd.DataFrame(
{"prompt": ["Generate a table of 2 programming languages and their creators."]}
)
result = ai.generate_table(
text_model,
df,
output_schema="language STRING, creator STRING",
)
assert "language" in result.columns
assert "creator" in result.columns
# The model may not always return the exact number of rows requested.
assert len(result) > 0