Skip to content

Commit 6b62cb6

Browse files
authored
feat(bigframes): Add ai_generate functions to the dataframe bq accessor (#17302)
internal issue: b/517233441
1 parent 54fd04b commit 6b62cb6

2 files changed

Lines changed: 373 additions & 16 deletions

File tree

packages/bigframes/bigframes/extensions/core/dataframe_accessor.py

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,36 @@
1515
from __future__ import annotations
1616

1717
import abc
18-
from typing import TYPE_CHECKING, Generic, Iterable, TypeVar
18+
from typing import (
19+
TYPE_CHECKING,
20+
Any,
21+
Generic,
22+
Iterable,
23+
List,
24+
Literal,
25+
Mapping,
26+
Tuple,
27+
TypeVar,
28+
Union,
29+
)
1930

2031
if TYPE_CHECKING:
32+
import pandas as pd
33+
2134
import bigframes.dataframe
35+
import bigframes.series
2236
import bigframes.session
2337

38+
PROMPT_TYPE = Union[
39+
str,
40+
bigframes.series.Series,
41+
pd.Series,
42+
List[Union[str, bigframes.series.Series, pd.Series]],
43+
Tuple[Union[str, bigframes.series.Series, pd.Series], ...],
44+
]
45+
else:
46+
PROMPT_TYPE = Any
47+
2448
T = TypeVar("T")
2549
S = TypeVar("S")
2650

@@ -84,6 +108,112 @@ def forecast(
84108
)
85109
return self._to_dataframe(result)
86110

111+
def generate(
112+
self,
113+
prompt: PROMPT_TYPE,
114+
*,
115+
connection_id: str | None = None,
116+
endpoint: str | None = None,
117+
request_type: Literal["dedicated", "shared", "unspecified"] | None = None,
118+
model_params: Mapping[Any, Any] | None = None,
119+
output_schema: Mapping[str, str] | None = None,
120+
) -> S:
121+
"""
122+
Returns the AI analysis based on the prompt, which can be any combination of text and unstructured data.
123+
124+
This is an accessor for :func:`bigframes.bigquery.ai.generate`. See that
125+
function's documentation for detailed parameter descriptions and examples.
126+
"""
127+
import bigframes.bigquery.ai
128+
129+
result = bigframes.bigquery.ai.generate(
130+
prompt,
131+
connection_id=connection_id,
132+
endpoint=endpoint,
133+
request_type=request_type,
134+
model_params=model_params,
135+
output_schema=output_schema,
136+
)
137+
return self._to_series(result)
138+
139+
def generate_bool(
140+
self,
141+
prompt: PROMPT_TYPE,
142+
*,
143+
connection_id: str | None = None,
144+
endpoint: str | None = None,
145+
request_type: Literal["dedicated", "shared", "unspecified"] | None = None,
146+
model_params: Mapping[Any, Any] | None = None,
147+
) -> S:
148+
"""
149+
Returns the AI analysis based on the prompt, which can be any combination of text and unstructured data.
150+
151+
This is an accessor for :func:`bigframes.bigquery.ai.generate_bool`. See that
152+
function's documentation for detailed parameter descriptions and examples.
153+
"""
154+
import bigframes.bigquery.ai
155+
156+
result = bigframes.bigquery.ai.generate_bool(
157+
prompt,
158+
connection_id=connection_id,
159+
endpoint=endpoint,
160+
request_type=request_type,
161+
model_params=model_params,
162+
)
163+
return self._to_series(result)
164+
165+
def generate_int(
166+
self,
167+
prompt: PROMPT_TYPE,
168+
*,
169+
connection_id: str | None = None,
170+
endpoint: str | None = None,
171+
request_type: Literal["dedicated", "shared", "unspecified"] | None = None,
172+
model_params: Mapping[Any, Any] | None = None,
173+
) -> S:
174+
"""
175+
Returns the AI analysis based on the prompt, which can be any combination of text and unstructured data.
176+
177+
This is an accessor for :func:`bigframes.bigquery.ai.generate_int`. See that
178+
function's documentation for detailed parameter descriptions and examples.
179+
"""
180+
import bigframes.bigquery.ai
181+
182+
result = bigframes.bigquery.ai.generate_int(
183+
prompt,
184+
connection_id=connection_id,
185+
endpoint=endpoint,
186+
request_type=request_type,
187+
model_params=model_params,
188+
)
189+
return self._to_series(result)
190+
191+
def generate_double(
192+
self,
193+
prompt: PROMPT_TYPE,
194+
*,
195+
connection_id: str | None = None,
196+
endpoint: str | None = None,
197+
request_type: Literal["dedicated", "shared", "unspecified"] | None = None,
198+
model_params: Mapping[Any, Any] | None = None,
199+
) -> S:
200+
"""
201+
Returns the AI analysis based on the prompt, which can be any combination of text and unstructured data.
202+
203+
This is an accessor for :func:`bigframes.bigquery.ai.generate_double`. See that
204+
function's documentation for detailed parameter descriptions and examples.
205+
"""
206+
import bigframes.bigquery.ai
207+
208+
result = bigframes.bigquery.ai.generate_double(
209+
prompt,
210+
connection_id=connection_id,
211+
endpoint=endpoint,
212+
request_type=request_type,
213+
model_params=model_params,
214+
)
215+
return self._to_series(result)
216+
87217

88218
class BigQueryDataFrameAccessor(AbstractBigQueryDataFrameAccessor[T, S]):
89219
"""

0 commit comments

Comments
 (0)