Skip to content

Commit 7c0c9ad

Browse files
authored
SNOW-2405395: Add ai_translate (#3969)
1 parent b2696ad commit 7c0c9ad

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- Added support for `Session.client_telemetry`.
1010
- Added support for `Session.udf_profiler`.
11+
- Added support for `functions.ai_translate`.
1112

1213
#### Improvements
1314

docs/source/snowpark/functions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Functions
3333
ai_similarity
3434
ai_summarize_agg
3535
ai_transcribe
36+
ai_translate
3637
any_value
3738
all_user_names
3839
approx_count_distinct

src/snowflake/snowpark/functions.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13892,3 +13892,65 @@ def ai_sentiment(
1389213892
return _call_function(
1389313893
sql_func_name, text_col, cat_col, _ast=ast, _emit_ast=_emit_ast
1389413894
)
13895+
13896+
13897+
@publicapi
13898+
def ai_translate(
13899+
text: ColumnOrLiteralStr,
13900+
source_language: ColumnOrLiteralStr,
13901+
target_language: ColumnOrLiteralStr,
13902+
_emit_ast: bool = True,
13903+
) -> Column:
13904+
"""
13905+
Translates the given input text from one supported language to another.
13906+
13907+
Args:
13908+
text: A string or Column containing the text to be translated.
13909+
source_language: A string or Column specifying the language code for the source language.
13910+
Specify an empty string ``''`` to automatically detect the source language.
13911+
target_language: A string or Column specifying the language code for the target language.
13912+
13913+
Returns:
13914+
A string containing a translation of the original text into the target language.
13915+
13916+
See details in `AI_TRANSLATE <https://docs.snowflake.com/en/sql-reference/functions/ai_translate>`_.
13917+
13918+
Examples::
13919+
13920+
>>> # Translate literal text from English to German
13921+
>>> df = session.range(1).select(
13922+
... ai_translate('Hello world', 'en', 'de').alias('translation')
13923+
... )
13924+
>>> df.collect()[0][0].lower()
13925+
'hallo welt'
13926+
13927+
>>> # Auto-detect source language and translate to English
13928+
>>> df = session.range(1).select(
13929+
... ai_translate('Hola mundo', '', 'en').alias('translation')
13930+
... )
13931+
>>> df.collect()[0][0].lower()
13932+
'hi world'
13933+
"""
13934+
sql_func_name = "ai_translate"
13935+
13936+
# Build AST
13937+
ast = (
13938+
build_function_expr(sql_func_name, [text, source_language, target_language])
13939+
if _emit_ast
13940+
else None
13941+
)
13942+
13943+
# Convert arguments to columns
13944+
text_col = _to_col_if_lit(text, sql_func_name)
13945+
source_lang_col = _to_col_if_lit(source_language, sql_func_name)
13946+
target_lang_col = _to_col_if_lit(target_language, sql_func_name)
13947+
13948+
# Call the function
13949+
return _call_function(
13950+
sql_func_name,
13951+
text_col,
13952+
source_lang_col,
13953+
target_lang_col,
13954+
_ast=ast,
13955+
_emit_ast=_emit_ast,
13956+
)

0 commit comments

Comments
 (0)