Skip to content

Commit 54c93e9

Browse files
committed
d
1 parent 385d069 commit 54c93e9

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/snowflake/snowpark/functions.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13871,3 +13871,65 @@ def ai_sentiment(
1387113871
return _call_function(
1387213872
sql_func_name, text_col, cat_col, _ast=ast, _emit_ast=_emit_ast
1387313873
)
13874+
13875+
13876+
@publicapi
13877+
def ai_translate(
13878+
text: ColumnOrLiteralStr,
13879+
source_language: ColumnOrLiteralStr,
13880+
target_language: ColumnOrLiteralStr,
13881+
_emit_ast: bool = True,
13882+
) -> Column:
13883+
"""
13884+
Translates the given input text from one supported language to another.
13885+
13886+
Args:
13887+
text: A string or Column containing the text to be translated.
13888+
source_language: A string or Column specifying the language code for the source language.
13889+
Specify an empty string ``''`` to automatically detect the source language.
13890+
target_language: A string or Column specifying the language code for the target language.
13891+
13892+
Returns:
13893+
A string containing a translation of the original text into the target language.
13894+
13895+
See details in `AI_TRANSLATE <https://docs.snowflake.com/en/sql-reference/functions/ai_translate>`_.
13896+
13897+
Examples::
13898+
13899+
>>> # Translate literal text from English to German
13900+
>>> df = session.range(1).select(
13901+
... ai_translate('Hello world', 'en', 'de').alias('translation')
13902+
... )
13903+
>>> df.collect()[0][0].lower()
13904+
'hallo welt'
13905+
13906+
>>> # Auto-detect source language and translate to English
13907+
>>> df = session.range(1).select(
13908+
... ai_translate('Hola mundo', '', 'en').alias('translation')
13909+
... )
13910+
>>> df.collect()[0][0].lower()
13911+
'hi world'
13912+
"""
13913+
sql_func_name = "ai_translate"
13914+
13915+
# Build AST
13916+
ast = (
13917+
build_function_expr(sql_func_name, [text, source_language, target_language])
13918+
if _emit_ast
13919+
else None
13920+
)
13921+
13922+
# Convert arguments to columns
13923+
text_col = _to_col_if_lit(text, sql_func_name)
13924+
source_lang_col = _to_col_if_lit(source_language, sql_func_name)
13925+
target_lang_col = _to_col_if_lit(target_language, sql_func_name)
13926+
13927+
# Call the function
13928+
return _call_function(
13929+
sql_func_name,
13930+
text_col,
13931+
source_lang_col,
13932+
target_lang_col,
13933+
_ast=ast,
13934+
_emit_ast=_emit_ast,
13935+
)

0 commit comments

Comments
 (0)