Skip to content

AI functions parameter map - #109232

Merged
george-larionov merged 15 commits into
masterfrom
ai-functions-default-credentials-params-map
Jul 8, 2026
Merged

george-larionov merged 15 commits into
masterfrom
ai-functions-default-credentials-params-map

Conversation

@george-larionov

@george-larionov george-larionov commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Description

Reworks how the experimental AI functions (aiGenerate, aiEmbed, aiClassify, aiExtract, aiTranslate) receive credentials and tunables.

Previously the named collection was a mandatory first positional argument, and each tunable (system_prompt, temperature, dimensions, translate instructions) was a separate positional argument. This made a casual call verbose and forced every query to name the collection.

Now:

  • The per-row text is the first argument; the named collection is no longer positional.

  • Credentials are resolved from a credentials key in an optional trailing Map(String, String) argument, or — when the map omits it — from a default-credentials setting:

    • ai_function_text_default_credentials for the text functions (aiGenerate/aiClassify/aiExtract/aiTranslate),
    • ai_function_embedding_default_credentials for aiEmbed.

    The split exists because a chat-completions endpoint/model differs from an embeddings one, and the named collection's endpoint is used verbatim.

  • The same map carries the tunables: model (overrides the collection), max_tokens, temperature, system_prompt (aiGenerate), instructions (aiTranslate), dimensions (aiEmbed). Map values are strings; unknown keys and duplicate keys are rejected; integer values are overflow-checked and capped at Int64 max.

SET ai_function_text_default_credentials = 'my_nc';

-- casual: use the default
SELECT aiGenerate(body) FROM articles;

-- override per call
SELECT aiGenerate(body, map('temperature', '0.2', 'credentials', 'my_other_nc')) FROM articles;

Internally, each function declares its parameters (name, kind, default-or-required, whether it inherits from the named collection) via a small spec, and a single resolver in FunctionBaseAI does the parsing, validation and defaulting.

Note: an explicitly empty value is now honored (map('system_prompt', '') sends an empty system prompt); the default applies only when the key is absent.

This is a breaking change to the AI function signatures, but they are experimental (guarded by allow_experimental_ai_functions) and were introduced only in 26.4/26.6.

Changelog category (leave one):

  • Experimental Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

AI functions no longer take the named collection as the first positional argument. Credentials come from the ai_function_text_default_credentials / ai_function_embedding_default_credentials settings or from a credentials key in an optional trailing Map(String, String) argument, which also carries tunables (model, max_tokens, temperature, system_prompt, instructions, dimensions).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.641 (included in 26.7 and later)
  • Backported to: 26.6.2.137, 26.5.6.99

@george-larionov
george-larionov force-pushed the ai-functions-default-credentials-params-map branch from c8f24f3 to e9f1c4c Compare July 2, 2026 21:27
@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [995571d]

Summary: ✅


AI Review

Summary

This PR rewires the AI functions around a trailing Map(String, String) parameter and new default-credentials settings. The central resolver refactor is mostly coherent, but there is still one security-sensitive regression test missing on the new settings-based credential path, and the previously raised empty-model validation regression remains unresolved in the current code.

Findings
  • ⚠️ Majors
    • [src/Functions/FunctionBaseAI.cpp:268-276] [dismissed by author -- https://github.com/AI functions parameter map #109232#discussion_r3516888010] Empty resolved model values still bypass local validation. A named collection with model = '' (or map('model', '')) now reaches the zero-row fast path and only fails later, provider-side, on non-empty input, which regresses the previous deterministic BAD_ARGUMENTS validation for malformed configuration.
Tests
  • ⚠️ [tests/queries/0_stateless/04142_ai_functions_named_collection_access.sh:35] The new ai_function_text_default_credentials / ai_function_embedding_default_credentials path is not covered by the ACL test. resolveAIParams now accepts the collection name indirectly from a session setting, but 04142 still only exercises explicit map('credentials', ...) calls. Please add no-grant / grant cases for bare aiGenerate and aiEmbed with the defaults set, so a regression that skips NAMED_COLLECTION checks on the new path cannot merge silently.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Add an access-control regression test for the default-credentials settings path.
    • Either restore non-empty model validation, or document and test empty model as an intentional supported contract.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Jul 2, 2026
@george-larionov
george-larionov marked this pull request as ready for review July 3, 2026 00:59
Comment thread src/Functions/FunctionBaseAI.cpp Resolved
Comment thread src/Core/SettingsChangesHistory.cpp Outdated
addSettingsChanges(settings_changes_history, "26.7",
{
{"s3_validate_etag_on_read", false, true, "New setting to detect concurrent in-place overwrites of S3/GCS objects during a read by validating the GET response ETag against the listed one. previous_value=false so `compatibility` with versions before 26.7 restores the pre-existing behavior (no validation)."},
{"ai_function_text_default_credentials", "", "", "New setting: default named collection for the text AI functions (aiGenerate, aiClassify, aiExtract, aiTranslate) when the call omits credentials."},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just "New setting" will do the trick ;-)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? All the others have some description

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SettingsChangesHistory.cpp is only used by compatibility, so the reason field is most useful when it explains a behavior flip or why previous_value differs from the normal docs default. For a brand-new setting with previous_value == new_value == "", the full semantics already live in Settings.cpp, so repeating the whole setting description here mostly duplicates the reference docs. That was the reasoning behind the suggestion to keep it at New setting.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see I am outnumbered 😅

Comment thread src/Functions/aiExtract.cpp Resolved
Comment thread src/Functions/aiEmbed.cpp Resolved
std::string_view name;
AIParamKind kind;
/// `std::nullopt` => required: the resolver throws if the key is absent everywhere.
std::optional<Field> default_value;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I asked elsewhere why std::nullopt is passed instead of an empty string. I think I now know why.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, also trying to future proof the code so that it is possible later to distinguish an absent value from an empty value. The empty value (empty string) could be passed on to the endpoint which can then decide the value (f.e. the model) to use based on various info like the calling function.

Comment thread src/Functions/FunctionBaseAI.h Resolved
@george-larionov
george-larionov force-pushed the ai-functions-default-credentials-params-map branch from 1a52d61 to 8ead71a Compare July 6, 2026 18:21
@mintlify

mintlify Bot commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Jul 6, 2026, 10:25 PM

Manerone pushed a commit to Manerone/ClickHouse that referenced this pull request Jul 7, 2026
… migration

The `/docs` tree is mid-migration from Docusaurus to Mintlify. Per
`docs/AGENTS.md`, contributors should edit only the legacy `/docs/en`
files (which build the current Docusaurus site); the Mintlify files are
not to be edited yet. The AI review bot (`ci/jobs/copilot_review_job.py`)
follows `.claude/skills/review/SKILL.md`, so without this note it asks
contributors to also update the served `.mdx` reference pages, which is
against the migration policy.

Add the `docs/AGENTS.md` guidance verbatim to the skill's documentation
review section, plus an explicit instruction not to flag a PR for editing
only `/docs/en` and not the Mintlify files.

Related: ClickHouse#109232

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Functions/aiEmbed.cpp Resolved
george-larionov and others added 12 commits July 7, 2026 20:40
Move the named collection out of the positional arguments. Credentials now
come from a `credentials` key in an optional trailing `Map(String, String)`
argument, or from `ai_function_text_default_credentials` (chat functions) /
`ai_function_embedding_default_credentials` (aiEmbed) when the map omits it.
The map also carries tunables (`model`, `max_tokens`, `temperature`,
`system_prompt`, `instructions`, `dimensions`); each function declares its
params, defaults, and required-ness via a small spec resolved centrally.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Reject duplicate keys in the parameter map instead of silently keeping the
  last value, matching the unknown-key rejection.
- AIParams::getString/getFloat/getUInt throw a DB::Exception on a missing key
  rather than std::map::at's std::out_of_range.
- Drop the unnecessary ColumnMap.h include.
- Tests: negative / out-of-range max_tokens and duplicate map key.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Parse UInt map values (max_tokens, dimensions) with overflow checking so a
value above UInt64 max is rejected instead of silently wrapping (e.g.
"18446744073709551616" -> 0). Add a test for the wrap boundary.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Expand each function's `params` documentation with per-key meaning and defaults.
- Clarify the `AIParamKind` and parameter-parsing comments.
- Parse parameters via exhaustive `switch` over `AIParamKind` so a new kind is a
  compile error rather than a silent fallthrough; guard `getResolvedAIParam`
  with `chassert`.
- Add a test rejecting a parameter map passed in the prompt position.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Style check: give the small AI-parameter `std::vector`/`std::map` config
  containers `STYLE_CHECK_ALLOW_STD_CONTAINERS`-marked aliases instead of raw
  std containers.
- clang-tidy (cppcoreguidelines-init-variables): initialize `value` in the
  UInt parameter parser.
- Fast test: update 04142_ai_functions_named_collection_access to the new
  signature (credentials via the parameter map); reference unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.90% 85.90% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 317/332 (95.48%) · Uncovered code

Full report · Diff report

@george-larionov
george-larionov added this pull request to the merge queue Jul 8, 2026
Merged via the queue into master with commit 35325f6 Jul 8, 2026
175 checks passed
@george-larionov
george-larionov deleted the ai-functions-default-credentials-params-map branch July 8, 2026 03:50
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 8, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 30, 2026
alexey-milovidov added a commit that referenced this pull request Jul 31, 2026
Resolve conflicts by adapting #110619 (make `model` a required positional
argument for `aiEmbed`) onto the 26.5 AI-function API, where the named
collection is the first positional argument and the parameter-map API of
#109232 does not exist. The 26.5 signature becomes
`aiEmbed(collection, text, model[, dimensions])`; a named collection that
defines `model` is rejected for `aiEmbed` rather than silently ignored.
The params-map-specific parts of the original change (Settings.cpp,
QueryFuzzer.cpp, FunctionBaseAI parameter-map handling) do not apply to
26.5 and are dropped; tests and docs are adapted accordingly.
tests/integration/test_ai_functions/test.py and
04492_ai_functions_default_credentials.sql do not exist in 26.5 and stay
deleted.
george-larionov added a commit that referenced this pull request Jul 31, 2026
26.6 carries #106881 (credentials from `ai_function_credentials`), which master
reverted and re-landed as #109232, so that setting is replaced by the two
default-credentials settings. Keep 26.6's retry handling and converge `aiEmbed`
to `aiEmbed(text, model[, params])` so the #110619 backport is not undone.

Related: #112520 (comment)

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
robot-clickhouse-ci-2 added a commit that referenced this pull request Jul 31, 2026
Cherry pick #109232 to 26.5: AI functions parameter map
robot-clickhouse-ci-2 added a commit that referenced this pull request Jul 31, 2026
Cherry pick #109232 to 26.6: AI functions parameter map
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 31, 2026
george-larionov added a commit that referenced this pull request Jul 31, 2026
26.6 gained #107927 (retriable provider error classification) after this branch
was cut, so keep its retry code and layer the parameter map on top instead of
reverting it. Drop `resolveConfig`/`resolveTemperature`, superseded by
`resolveAIParams`, and take the integration tests from master so the new retry
tests use the parameter map and `aiEmbed`'s positional model.

Related: #112710

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
george-larionov added a commit that referenced this pull request Jul 31, 2026
Backport #109232 to 26.5: AI functions parameter map
george-larionov added a commit that referenced this pull request Jul 31, 2026
Backport #109232 to 26.6: AI functions parameter map
george-larionov added a commit that referenced this pull request Aug 1, 2026
#110619's source changes are already on 26.5: they were pulled in when
#109232 was backported. The only net change is adding the
`test_ai_functions` integration test, which 26.5 lacked.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
… migration

The `/docs` tree is mid-migration from Docusaurus to Mintlify. Per
`docs/AGENTS.md`, contributors should edit only the legacy `/docs/en`
files (which build the current Docusaurus site); the Mintlify files are
not to be edited yet. The AI review bot (`ci/jobs/copilot_review_job.py`)
follows `.claude/skills/review/SKILL.md`, so without this note it asks
contributors to also update the served `.mdx` reference pages, which is
against the migration policy.

Add the `docs/AGENTS.md` guidance verbatim to the skill's documentation
review section, plus an explicit instruction not to flag a PR for editing
only `/docs/en` and not the Mintlify files.

Related: ClickHouse#109232

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ault-credentials-params-map

AI functions parameter map

This branch was successfully deployed

1 active deployment
staging - docs — 995571d3 Deployed Jul 7, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-experimental Experimental Feature pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants