|
35 | 35 | ] |
36 | 36 |
|
37 | 37 |
|
| 38 | +@log_adapter.method_logger(custom_base_name="bigquery_ai") |
| 39 | +def generate( |
| 40 | + prompt: PROMPT_TYPE, |
| 41 | + *, |
| 42 | + connection_id: str | None = None, |
| 43 | + endpoint: str | None = None, |
| 44 | + request_type: Literal["dedicated", "shared", "unspecified"] = "unspecified", |
| 45 | + model_params: Mapping[Any, Any] | None = None, |
| 46 | + # TODO(b/446974666) Add output_schema parameter |
| 47 | +) -> series.Series: |
| 48 | + """ |
| 49 | + Returns the AI analysis based on the prompt, which can be any combination of text and unstructured data. |
| 50 | +
|
| 51 | + **Examples:** |
| 52 | +
|
| 53 | + >>> import bigframes.pandas as bpd |
| 54 | + >>> import bigframes.bigquery as bbq |
| 55 | + >>> bpd.options.display.progress_bar = None |
| 56 | + >>> df = bpd.DataFrame({ |
| 57 | + ... "col_1": ["apple", "bear", "pear"], |
| 58 | + ... "col_2": ["fruit", "animal", "animal"] |
| 59 | + ... }) |
| 60 | + >>> bbq.ai.generate_bool((df["col_1"], " is a ", df["col_2"])) |
| 61 | + 0 {'result': True, 'full_response': '{"candidate... |
| 62 | + 1 {'result': True, 'full_response': '{"candidate... |
| 63 | + 2 {'result': False, 'full_response': '{"candidat... |
| 64 | + dtype: struct<result: bool, full_response: extension<dbjson<JSONArrowType>>, status: string>[pyarrow] |
| 65 | +
|
| 66 | + >>> bbq.ai.generate_bool((df["col_1"], " is a ", df["col_2"])).struct.field("result") |
| 67 | + 0 True |
| 68 | + 1 True |
| 69 | + 2 False |
| 70 | + Name: result, dtype: boolean |
| 71 | +
|
| 72 | + Args: |
| 73 | + prompt (Series | List[str|Series] | Tuple[str|Series, ...]): |
| 74 | + A mixture of Series and string literals that specifies the prompt to send to the model. The Series can be BigFrames Series |
| 75 | + or pandas Series. |
| 76 | + connection_id (str, optional): |
| 77 | + Specifies the connection to use to communicate with the model. For example, `myproject.us.myconnection`. |
| 78 | + If not provided, the connection from the current session will be used. |
| 79 | + endpoint (str, optional): |
| 80 | + Specifies the Vertex AI endpoint to use for the model. For example `"gemini-2.5-flash"`. You can specify any |
| 81 | + generally available or preview Gemini model. If you specify the model name, BigQuery ML automatically identifies and |
| 82 | + uses the full endpoint of the model. If you don't specify an ENDPOINT value, BigQuery ML selects a recent stable |
| 83 | + version of Gemini to use. |
| 84 | + request_type (Literal["dedicated", "shared", "unspecified"]): |
| 85 | + Specifies the type of inference request to send to the Gemini model. The request type determines what quota the request uses. |
| 86 | + * "dedicated": function only uses Provisioned Throughput quota. The function returns the error Provisioned throughput is not |
| 87 | + purchased or is not active if Provisioned Throughput quota isn't available. |
| 88 | + * "shared": the function only uses dynamic shared quota (DSQ), even if you have purchased Provisioned Throughput quota. |
| 89 | + * "unspecified": If you haven't purchased Provisioned Throughput quota, the function uses DSQ quota. |
| 90 | + If you have purchased Provisioned Throughput quota, the function uses the Provisioned Throughput quota first. |
| 91 | + If requests exceed the Provisioned Throughput quota, the overflow traffic uses DSQ quota. |
| 92 | + model_params (Mapping[Any, Any]): |
| 93 | + Provides additional parameters to the model. The MODEL_PARAMS value must conform to the generateContent request body format. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + bigframes.series.Series: A new struct Series with the result data. The struct contains these fields: |
| 97 | + * "result": a BOOL value containing the model's response to the prompt. The result is None if the request fails or is filtered by responsible AI. |
| 98 | + * "full_response": a JSON value containing the response from the projects.locations.endpoints.generateContent call to the model. |
| 99 | + The generated text is in the text element. |
| 100 | + * "status": a STRING value that contains the API response status for the corresponding row. This value is empty if the operation was successful. |
| 101 | + """ |
| 102 | + |
| 103 | + prompt_context, series_list = _separate_context_and_series(prompt) |
| 104 | + assert len(series_list) > 0 |
| 105 | + |
| 106 | + operator = ai_ops.AIGenerate( |
| 107 | + prompt_context=tuple(prompt_context), |
| 108 | + connection_id=_resolve_connection_id(series_list[0], connection_id), |
| 109 | + endpoint=endpoint, |
| 110 | + request_type=request_type, |
| 111 | + model_params=json.dumps(model_params) if model_params else None, |
| 112 | + ) |
| 113 | + |
| 114 | + return series_list[0]._apply_nary_op(operator, series_list[1:]) |
| 115 | + |
| 116 | + |
38 | 117 | @log_adapter.method_logger(custom_base_name="bigquery_ai") |
39 | 118 | def generate_bool( |
40 | 119 | prompt: PROMPT_TYPE, |
|
0 commit comments