@@ -337,6 +337,100 @@ def generate_double(
337337 return series_list [0 ]._apply_nary_op (operator , series_list [1 :])
338338
339339
340+ @log_adapter .method_logger (custom_base_name = "bigquery_ai" )
341+ def if_ (
342+ prompt : PROMPT_TYPE ,
343+ * ,
344+ connection_id : str | None = None ,
345+ ) -> series .Series :
346+ """
347+ Evaluates the prompt to True or False. Compared to `ai.generate_bool()`, this function
348+ provides optimization such that not all rows are evaluated with the LLM.
349+
350+ **Examples:**
351+ >>> import bigframes.pandas as bpd
352+ >>> import bigframes.bigquery as bbq
353+ >>> bpd.options.display.progress_bar = None
354+ >>> us_state = bpd.Series(["Massachusetts", "Illinois", "Hawaii"])
355+ >>> bbq.ai.if_((us_state, " has a city called Springfield"))
356+ 0 True
357+ 1 True
358+ 2 False
359+ dtype: boolean
360+
361+ >>> us_state[bbq.ai.if_((us_state, " has a city called Springfield"))]
362+ 0 Massachusetts
363+ 1 Illinois
364+ dtype: string
365+
366+ Args:
367+ prompt (Series | List[str|Series] | Tuple[str|Series, ...]):
368+ A mixture of Series and string literals that specifies the prompt to send to the model. The Series can be BigFrames Series
369+ or pandas Series.
370+ connection_id (str, optional):
371+ Specifies the connection to use to communicate with the model. For example, `myproject.us.myconnection`.
372+ If not provided, the connection from the current session will be used.
373+
374+ Returns:
375+ bigframes.series.Series: A new series of bools.
376+ """
377+
378+ prompt_context , series_list = _separate_context_and_series (prompt )
379+ assert len (series_list ) > 0
380+
381+ operator = ai_ops .AIIf (
382+ prompt_context = tuple (prompt_context ),
383+ connection_id = _resolve_connection_id (series_list [0 ], connection_id ),
384+ )
385+
386+ return series_list [0 ]._apply_nary_op (operator , series_list [1 :])
387+
388+
389+ @log_adapter .method_logger (custom_base_name = "bigquery_ai" )
390+ def score (
391+ prompt : PROMPT_TYPE ,
392+ * ,
393+ connection_id : str | None = None ,
394+ ) -> series .Series :
395+ """
396+ Computes a score based on rubrics described in natural language. It will return a double value.
397+ There is no fixed range for the score returned. To get high quality results, provide a scoring
398+ rubric with examples in the prompt.
399+
400+ **Examples:**
401+ >>> import bigframes.pandas as bpd
402+ >>> import bigframes.bigquery as bbq
403+ >>> bpd.options.display.progress_bar = None
404+ >>> animal = bpd.Series(["Tiger", "Rabbit", "Blue Whale"])
405+ >>> bbq.ai.score(("Rank the relative weights of ", animal, " on the scale from 1 to 3")) # doctest: +SKIP
406+ 0 2.0
407+ 1 1.0
408+ 2 3.0
409+ dtype: Float64
410+
411+ Args:
412+ prompt (Series | List[str|Series] | Tuple[str|Series, ...]):
413+ A mixture of Series and string literals that specifies the prompt to send to the model. The Series can be BigFrames Series
414+ or pandas Series.
415+ connection_id (str, optional):
416+ Specifies the connection to use to communicate with the model. For example, `myproject.us.myconnection`.
417+ If not provided, the connection from the current session will be used.
418+
419+ Returns:
420+ bigframes.series.Series: A new series of double (float) values.
421+ """
422+
423+ prompt_context , series_list = _separate_context_and_series (prompt )
424+ assert len (series_list ) > 0
425+
426+ operator = ai_ops .AIScore (
427+ prompt_context = tuple (prompt_context ),
428+ connection_id = _resolve_connection_id (series_list [0 ], connection_id ),
429+ )
430+
431+ return series_list [0 ]._apply_nary_op (operator , series_list [1 :])
432+
433+
340434def _separate_context_and_series (
341435 prompt : PROMPT_TYPE ,
342436) -> Tuple [List [str | None ], List [series .Series ]]:
0 commit comments