|
| 1 | +# Copyright (c) 2025, Salesforce, Inc. |
| 2 | +# SPDX-License-Identifier: Apache-2 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""LLM completion functions for Data Cloud Custom Code. |
| 17 | +""" |
| 18 | + |
| 19 | +from typing import Union |
| 20 | + |
| 21 | +from pyspark.sql import Column |
| 22 | +from pyspark.sql.functions import call_function, lit |
| 23 | + |
| 24 | +# Default values for llm_complete function |
| 25 | +# TODO: Validate these defaults |
| 26 | +_DEFAULT_MODEL_ID = "sfdc_ai__DefaultGPT4Omni" |
| 27 | +_DEFAULT_MAX_TOKENS = 200 |
| 28 | +_LLM_GATEWAY_UDF_NAME = "llm_gateway_generate" |
| 29 | + |
| 30 | + |
| 31 | +def llm_complete( |
| 32 | + prompt_col: Union[Column, str], |
| 33 | + *, |
| 34 | + model_id: str = _DEFAULT_MODEL_ID, |
| 35 | + max_tokens: int = _DEFAULT_MAX_TOKENS, |
| 36 | +) -> Column: |
| 37 | + """Returns the AI-generated text response as a string column. |
| 38 | +
|
| 39 | + Args: |
| 40 | + prompt_col: Column or column name containing the prompt text. |
| 41 | + The prompt should be a string value (max 32KB recommended). |
| 42 | + Use string functions like concat_ws(), format_string(), etc. |
| 43 | + to construct complex prompts from multiple columns. |
| 44 | + model_id: Defaults to "sfdc_ai__DefaultGPT4Omni". |
| 45 | + Available models depend on your org's configuration. |
| 46 | + max_tokens: Maximum tokens in the response. Defaults to 200. |
| 47 | + Higher values allow longer responses but increase latency and cost. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + Column of StringType with AI-generated response. |
| 51 | + Returns null if the input prompt is null. |
| 52 | +
|
| 53 | + Raises: |
| 54 | + TypeError: If prompt_col is not a Column or string. |
| 55 | + ValueError: If max_tokens is not positive. |
| 56 | + """ |
| 57 | + # Input validation |
| 58 | + if not isinstance(prompt_col, (Column, str)): |
| 59 | + raise TypeError( |
| 60 | + f"prompt_col must be a Column or str, got {type(prompt_col).__name__}" |
| 61 | + ) |
| 62 | + |
| 63 | + if not isinstance(max_tokens, int) or max_tokens <= 0: |
| 64 | + raise ValueError(f"max_tokens must be a positive integer, got {max_tokens}") |
| 65 | + |
| 66 | + # Convert string column name to Column |
| 67 | + if isinstance(prompt_col, str): |
| 68 | + from pyspark.sql.functions import col |
| 69 | + |
| 70 | + prompt_col = col(prompt_col) |
| 71 | + |
| 72 | + from pyspark.sql.functions import named_struct |
| 73 | + |
| 74 | + template = "{prompt}" |
| 75 | + values_struct = named_struct(lit("prompt"), prompt_col) |
| 76 | + |
| 77 | + return call_function( |
| 78 | + _LLM_GATEWAY_UDF_NAME, |
| 79 | + lit(template), |
| 80 | + values_struct, |
| 81 | + lit(model_id), |
| 82 | + lit(max_tokens), |
| 83 | + ) |
0 commit comments