Skip to Content

AI_FILL

Overview

AI_FILL automatically fills missing or incomplete data by analyzing patterns from a provided example range. It is designed for completing datasets where there is a predictable relationship between columns, automating data entry and ensuring consistency. The function leverages large language models from Mistral AI (documentation), which support structured output and advanced pattern recognition. Learn more about available models here.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel, enter it as a formula in a cell, specifying your example and fill ranges, and any optional arguments as needed:

=AI_FILL(example_range, fill_range, [temperature], [model], [max_tokens])
  • example_range (2D list, required): The range containing complete data that serves as the basis for detecting patterns and relationships.
  • fill_range (2D list, required): The range with incomplete data that will be filled based on the detected patterns from the example_range.
  • temperature (float, optional, default=0.0): Controls the randomness/creativity of the response (0.0 to 2.0). Lower values are more deterministic.
  • model (string, optional, default=“mistral-small-latest”): The specific AI model ID to use (must support JSON mode, e.g., mistral-small-latest).
  • max_tokens (int, optional, default=1500): Maximum number of tokens for the generated content.

The function returns a 2D list with the missing data filled in. If an error occurs, it returns a 2D list with an error message (e.g., [["Error: ..."]]).

Examples

Example 1: Completing Product Catalog Information

Fill in missing product specifications based on similar products.

Sample Input Example Range:

Product IDCategoryPriceWeight (kg)
PRD-001Laptop12991.8
PRD-002Laptop9992.1
PRD-003Tablet4990.7

Sample Input Fill Range:

Product IDCategoryPriceWeight (kg)
PRD-004Laptop
PRD-005Tablet
PRD-0067991.2
=AI_FILL(A1:D4, A5:D7)

Expected output:

Product IDCategoryPriceWeight (kg)
PRD-004Laptop10991.9
PRD-005Tablet5490.8
PRD-006Laptop7991.2

Example 2: Filling Employee Information

Complete missing employee department and location information based on job titles.

Sample Input Example Range:

Employee IDJob TitleDepartmentLocation
EMP-001Sales ManagerSalesNew York
EMP-002Marketing SpecialistMarketingChicago
EMP-003Sales RepresentativeSalesLos Angeles
EMP-004Software DeveloperEngineeringSan Francisco

Sample Input Fill Range:

Employee IDJob TitleDepartmentLocation
EMP-005Sales Director
EMP-006UX Designer
EMP-007Marketing Director
EMP-008Senior Developer
=AI_FILL(A1:D5, A6:D9)

Expected output:

Employee IDJob TitleDepartmentLocation
EMP-005Sales DirectorSalesNew York
EMP-006UX DesignerEngineeringSan Francisco
EMP-007Marketing DirectorMarketingChicago
EMP-008Senior DeveloperEngineeringSan Francisco

Example 3: Completing Financial Forecasts

Fill in missing quarterly projections based on existing data and trends.

Sample Input Example Range:

MetricQ1 2024Q2 2024Q3 2024Q4 2024
Revenue250000280000310000350000
Expenses180000195000215000235000

Sample Input Fill Range:

MetricQ1 2024Q2 2024Q3 2024Q4 2024
Profit
Headcount3235
=AI_FILL(A1:E3, A4:E6)

Expected output:

MetricQ1 2024Q2 2024Q3 2024Q4 2024
Profit700008500095000115000
Headcount32353842

Python Code

import requests import json def ai_fill(example_range, fill_range, temperature=0.0, model="mistral-small-latest", max_tokens=1500, api_key=None, api_url="https://api.mistral.ai/v1/chat/completions"): """ Fills missing or incomplete data in a target range by learning patterns from an example range using a large language model. Args: example_range: 2D list containing complete data as examples for the AI to learn from. fill_range: 2D list containing data with missing values to be filled. temperature: Optional float. Controls response creativity (0.0-2.0). Default is 0.0. model: Optional string. ID of the model to use. Default is "mistral-small-latest". max_tokens: Optional int. Maximum tokens for response generation. Default is 1500. api_key: Optional string. API key for authentication. api_url: Optional string. OpenAI-compatible URL. Default is "https://api.mistral.ai/v1/chat/completions". Returns: 2D list with missing data filled in, or a 2D list with an error message. This example function is provided as-is without any representation of accuracy. """ # Validate temperature if not isinstance(temperature, (float, int)) or not (0 <= float(temperature) <= 2): return [["Error: temperature must be a float between 0 and 2 (inclusive)"]] # Validate max_tokens if not isinstance(max_tokens, int) or not (5 <= max_tokens <= 5000): return [["Error: max_tokens must be an integer between 5 and 5000 (inclusive)"]] # Validate inputs if not isinstance(example_range, list) or not example_range: return [["Error: Example range is empty or invalid."]] if not isinstance(fill_range, list) or not fill_range: return [["Error: Fill range is empty or invalid."]] # Demo fallback logic (same as ai_ask) if api_key is None or api_url is None: if "idToken" in globals(): api_key = globals()["idToken"] api_url = "https://llm.boardflare.com" else: return [["Error: Login on the Functions tab for limited demo usage, or sign up for a free Mistral AI account at https://console.mistral.ai/ and add your own api_key."]] # Convert example_range and fill_range to JSON strings for the prompt example_json = json.dumps(example_range) fill_json = json.dumps(fill_range) # Construct a specific prompt for filling data fill_prompt = ( "Fill in the missing values in the target data based on patterns in the example data.\n" f"Example data (complete): {example_json}\n\n" f"Target data (with missing values): {fill_json}\n\n" "Study the patterns in the example data and complete the target data by filling in missing values. Preserve all existing values in the target data." ) fill_prompt += ("\n\nReturn ONLY a JSON object with a key 'items' whose value is a JSON array of arrays (2D array) with the completed target data. " "Do not include any explanatory text, just the JSON object. " "For example: {\"items\": [[\"row1col1\", \"row1col2\"], [\"row2col1\", \"row2col2\"]]}") # Prepare the API request payload payload = { "messages": [{"role": "user", "content": fill_prompt}], "temperature": temperature, "model": model, "max_tokens": max_tokens, "response_format": {"type": "json_object"} } headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", "Accept": "application/json" } try: response = requests.post(api_url, headers=headers, json=payload) if response.status_code == 429: return [["You have hit the rate limit for the API. Please try again later."]] response.raise_for_status() response_data = response.json() content = response_data["choices"][0]["message"]["content"] try: filled_data = json.loads(content) if isinstance(filled_data, dict) and "items" in filled_data: filled_data = filled_data["items"] elif isinstance(filled_data, dict): if "data" in filled_data: filled_data = filled_data["data"] elif "filled_data" in filled_data: filled_data = filled_data["filled_data"] elif "result" in filled_data: filled_data = filled_data["result"] if isinstance(filled_data, list) and all(isinstance(row, list) for row in filled_data): if (len(filled_data) == len(fill_range) and all(len(row) == len(fill_range[i]) for i, row in enumerate(filled_data))): return filled_data else: return [["Error: AI response dimensions don't match the fill range."]] else: return [["Error: Unable to parse response. Expected a 2D array."]] except (json.JSONDecodeError, ValueError): return [["Error: Unable to fill data. The AI response wasn't in the expected format."]] except requests.exceptions.RequestException as e: return [["Error: API request failed.", str(e)]]

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on