Skip to content

feat: generate DQL from natural language (bring-your-own-key AI)#416

Open
shaunpatterson wants to merge 4 commits into
dgraph-io:mainfrom
shaunpatterson:sp/nl2dql
Open

feat: generate DQL from natural language (bring-your-own-key AI)#416
shaunpatterson wants to merge 4 commits into
dgraph-io:mainfrom
shaunpatterson:sp/nl2dql

Conversation

@shaunpatterson

Copy link
Copy Markdown
Contributor

What

Adds an AI button to the editor toolbar: describe the query in plain language, get DQL back, review/edit it in the modal, insert into the editor. Comparable tools are shipping this (NebulaGraph Studio's AI assistant, etc.).

Privacy / key handling

Strictly bring-your-own-key: the user pastes their own API key, it's stored in localStorage only (per provider), and requests go directly browser → model API — nothing passes through the Ratel server and no key ever appears in a URL.

Providers

A small provider registry (buildRequest / parseResponse / parseError per provider; prompt, schema context and DQL extraction shared):

Provider Models Notes
Anthropic Claude Haiku 4.5, Sonnet 4.6 messages API with the explicit anthropic-dangerous-direct-browser-access opt-in
OpenAI GPT-5 mini, GPT-5.1 chat completions, Bearer auth
Google Gemini 2.5 Flash, Pro generateContent, key in the x-goog-api-key header (not the query string)

Schema-aware

On generate, the current schema {} is summarized compactly (predicates with types/indexes/@reverse, type definitions, dgraph.* filtered) and sent as context, so generated queries use real predicates. The system prompt constrains output to a single fenced DQL block; extraction falls back to raw text; the result is editable before inserting.

Testing

22 unit tests: request shapes for all three providers, response parsing, settings persistence/sanitization/migration, end-to-end mocked generation, error surfacing, missing-key/unknown-provider rejection. npm run build passes.

🤖 Generated with Claude Code

shaunpatterson and others added 2 commits June 12, 2026 10:39
Adds an 'AI' button to the editor toolbar that opens a modal:
describe the query in plain language, get DQL back, review/edit it,
insert into the editor.

- Bring-your-own-key: Anthropic API key entered by the user, stored
  in localStorage only; requests go directly browser -> model API
  (anthropic-dangerous-direct-browser-access) so nothing routes
  through the Ratel server. Model selectable (Haiku default, Sonnet).
- The current schema (schema {}) is summarized compactly (predicates
  with types/indexes, type definitions, dgraph.* filtered out) and
  sent as context, so generated queries use real predicates.
- The system prompt constrains output to a single fenced DQL block;
  extraction falls back to raw text. Generated DQL is editable before
  inserting.
- lib/nl2dql.js is pure and fully unit-tested (12 tests: settings
  persistence/sanitization, schema summarization, prompt content,
  extraction, API call shape, key/error handling).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Generalizes the NL->DQL feature behind a provider registry:

- Anthropic (Claude Haiku 4.5 / Sonnet 4.6), OpenAI (GPT-5 mini /
  GPT-5.1, chat completions with bearer auth) and Google Gemini
  (2.5 Flash / Pro, generateContent with the key in the
  x-goog-api-key header so it never appears in URLs).
- Provider selector in the modal; API keys and model choice are
  stored per provider in localStorage. Legacy single-provider
  settings migrate into the anthropic slot automatically.
- Each provider defines buildRequest/parseResponse/parseError; the
  generation flow, prompt and DQL extraction are shared.
- 22 unit tests: request shapes for all three providers, response
  parsing, settings migration/sanitization, end-to-end mocks,
  error surfacing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@shaunpatterson shaunpatterson requested a review from a team as a code owner June 12, 2026 17:38
shaunpatterson and others added 2 commits June 12, 2026 13:47
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@shaunpatterson

Copy link
Copy Markdown
Contributor Author
image

@shaunpatterson

Copy link
Copy Markdown
Contributor Author
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant