This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreDid you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now
Data Engineers working with Microsoft Fabric often need to manage environment-specific configurations, including the modification of Lakehouse names, file paths, or schema names for development, testing, and production environments. In this scenario, you would want to avoid hard coding this information. This is where variable libraries in Fabric can help data engineers manage their environment configuration when working with Fabric User data function.
Benefits of using variable libraries:
NOTE: Always use the latest `fabric-user-data-functions` library.
Manage_environment_configuration_in_nbsp_Fabric_User_data_functions_nbsp_with_va
To retrieve variable values stored in a library item within your function, reference the library using the alias assigned during setup. This method enhances code flexibility and security by enabling dynamic access to configuration values or secrets without the need for hardcoding.
Implements a Fabric User Data Function that sends a chat request to Azure OpenAI using configuration values from Fabric Variable Library and secrets from Azure Key Vault.
Code snippet
# Select 'Manage connections' and add a connection to a Variable Library# Replace the alias "<My Variable Library Alias>" with your connection alias.
from openai import AzureOpenAI
from azure.keyvault.secrets import SecretClient
udf = fn.UserDataFunctions()
@udf.generic_connection(argName="keyVaultClient", audienceType="KeyVault")
@udf.connection(argName="varLib", alias="<My Variable Library Alias>")
@udf.function()
def chat_request(prompt: str, keyVaultClient: fn.FabricItem, varLib: fn.FabricVariablesClient) -> str:
'''
Description: Sends a chat completion request to an Azure OpenAI model using configuration values
retrieved from a Fabric Variable Library and Azure Key Vault.
Pre-requisites:
* Create an Azure OpenAI endpoint in Azure Portal
* Create an Azure Key Vault and store your Azure OpenAI API key as a secret
* Grant your Fabric User Data Functions item owner's identity access to read secrets (Access policies or RBAC). Guidance: https://learn.microsoft.com/en-us/fabric/data-factory/azure-key-vault-reference-overview
* Create a Variable Library in Fabric and add variables for:
- KEY_VAULT_URL: Your Azure Key Vault URL (e.g., "https://your-keyvault.vault.azure.net/")
- API_KEY_SECRET_NAME: Name of the secret in Key Vault containing the API key
- ENDPOINT: Your Azure OpenAI endpoint URL
- MODEL: Your deployed model name
* Add the openai and azure-keyvault-secrets libraries to your function dependencies
* Ensure fabric-user-data-functions library is using the latest version
Args:
prompt (str): The user input or query to be processed by the model.
varLib (fn.FabricVariablesClient): A client instance to access stored variables in Variable Library
for Key Vault URL, secret name, endpoint, and model name.
Returns:
str: The generated response from the Azure OpenAI model.
''' # Retrieve configuration from Variable Library
variables = varLib.getVariables()
key_vault_url = variables["KEY_VAULT_URL"]
api_key_secret_name = variables["API_KEY_SECRET_NAME"]
endpoint = variables["ENDPOINT"]
model_name = variables["MODEL"]
# Obtain a credential from the generic Key Vault connection (Fabric-managed identity)
credential = keyVaultClient.get_access_token()
secret_client = SecretClient(vault_url=key_vault_url, credential=credential)
key = secret_client.get_secret(api_key_secret_name).value
api_version = "2024-12-01-preview"
client = AzureOpenAI(
api_version=api_version,
azure_endpoint=endpoint,
api_key=key,
)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": prompt
}
],
max_completion_tokens=13107,
temperature=1.0,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
model=model_name
)
return (response.choices[0].message.content)
This code does the following:
Variable libraries in User data functions ensure consistency by letting you define values once and reuse them. They improve security through referencing sensitive information securely, not directly in code. You can update configurations without redeploying and integrate variables into deployment pipelines for seamless CI/CD workflows with Fabric. Checkout these sample functions.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.