graphile-llm 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +193 -0
  3. package/__tests__/graphile-llm.test.d.ts +1 -0
  4. package/__tests__/graphile-llm.test.js +721 -0
  5. package/chat.d.ts +37 -0
  6. package/chat.js +105 -0
  7. package/embedder.d.ts +35 -0
  8. package/embedder.js +79 -0
  9. package/esm/__tests__/graphile-llm.test.d.ts +1 -0
  10. package/esm/__tests__/graphile-llm.test.js +683 -0
  11. package/esm/chat.d.ts +37 -0
  12. package/esm/chat.js +97 -0
  13. package/esm/embedder.d.ts +35 -0
  14. package/esm/embedder.js +71 -0
  15. package/esm/index.d.ts +39 -0
  16. package/esm/index.js +42 -0
  17. package/esm/plugins/llm-module-plugin.d.ts +38 -0
  18. package/esm/plugins/llm-module-plugin.js +82 -0
  19. package/esm/plugins/rag-plugin.d.ts +36 -0
  20. package/esm/plugins/rag-plugin.js +341 -0
  21. package/esm/plugins/text-mutation-plugin.d.ts +44 -0
  22. package/esm/plugins/text-mutation-plugin.js +191 -0
  23. package/esm/plugins/text-search-plugin.d.ts +41 -0
  24. package/esm/plugins/text-search-plugin.js +163 -0
  25. package/esm/preset.d.ts +55 -0
  26. package/esm/preset.js +74 -0
  27. package/esm/types.d.ts +173 -0
  28. package/esm/types.js +6 -0
  29. package/index.d.ts +39 -0
  30. package/index.js +56 -0
  31. package/package.json +76 -0
  32. package/plugins/llm-module-plugin.d.ts +38 -0
  33. package/plugins/llm-module-plugin.js +85 -0
  34. package/plugins/rag-plugin.d.ts +36 -0
  35. package/plugins/rag-plugin.js +344 -0
  36. package/plugins/text-mutation-plugin.d.ts +44 -0
  37. package/plugins/text-mutation-plugin.js +194 -0
  38. package/plugins/text-search-plugin.d.ts +41 -0
  39. package/plugins/text-search-plugin.js +166 -0
  40. package/preset.d.ts +55 -0
  41. package/preset.js +77 -0
  42. package/types.d.ts +173 -0
  43. package/types.js +7 -0
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive <developers@constructive.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # graphile-llm
2
+
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml">
9
+ <img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" />
10
+ </a>
11
+ <a href="https://github.com/constructive-io/constructive/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
12
+ <a href="https://www.npmjs.com/package/graphile-llm"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=graphile%2Fgraphile-llm%2Fpackage.json"/></a>
13
+ </p>
14
+
15
+ LLM integration plugin for PostGraphile v5 — server-side text-to-vector embedding, resolve-time vector injection, and RAG (Retrieval-Augmented Generation) for pgvector columns using `@agentic-kit/ollama`.
16
+
17
+ ## Table of contents
18
+
19
+ - [Installation](#installation)
20
+ - [Usage](#usage)
21
+ - [Features](#features)
22
+ - [Plugins](#plugins)
23
+ - [Configuration](#configuration)
24
+ - [RAG queries](#rag-queries)
25
+ - [License](#license)
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ npm install graphile-llm
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ```typescript
36
+ import { GraphileLlmPreset } from 'graphile-llm';
37
+
38
+ const preset = {
39
+ extends: [
40
+ GraphileLlmPreset({
41
+ defaultEmbedder: {
42
+ provider: 'ollama',
43
+ model: 'nomic-embed-text',
44
+ baseUrl: 'http://localhost:11434',
45
+ },
46
+ }),
47
+ ],
48
+ };
49
+ ```
50
+
51
+ The preset bundles all plugins listed below. You can also import each plugin individually (`createLlmModulePlugin`, `createLlmTextSearchPlugin`, `createLlmTextMutationPlugin`, `createLlmRagPlugin`) if you prefer a-la-carte.
52
+
53
+ ## Features
54
+
55
+ - **Text-based vector search** — adds `text: String` field to `VectorNearbyInput`; clients pass natural language instead of raw float vectors
56
+ - **Text mutation fields** — adds `{column}Text: String` companion fields on create/update inputs for vector columns
57
+ - **RAG queries** — adds `ragQuery` and `embedText` root query fields; detects `@hasChunks` smart tags for chunk-aware retrieval
58
+ - **Pluggable providers** — provider-based architecture for both embedding and chat completion (Ollama via `@agentic-kit/ollama`, extensible to OpenAI, etc.)
59
+ - **Per-database configuration** — reads `llm_module` from `services_public.api_modules` for per-API provider config
60
+ - **Toggleable** — each capability (`enableTextSearch`, `enableTextMutations`, `enableRag`) can be independently enabled or disabled
61
+ - **Plugin-conditional** — fields only appear in the schema when the plugin is loaded
62
+
63
+ ## Plugins
64
+
65
+ | Plugin | Description | Toggle |
66
+ |--------|-------------|--------|
67
+ | `LlmModulePlugin` | Resolves embedder and chat completer from config; stores on build context | Always included |
68
+ | `LlmTextSearchPlugin` | Adds `text: String` to `VectorNearbyInput` with resolve-time embedding | `enableTextSearch` (default: `true`) |
69
+ | `LlmTextMutationPlugin` | Adds `{column}Text` companion fields on mutation inputs | `enableTextMutations` (default: `true`) |
70
+ | `LlmRagPlugin` | Adds `ragQuery` and `embedText` root query fields | `enableRag` (default: `false`) |
71
+
72
+ ## Configuration
73
+
74
+ ```typescript
75
+ GraphileLlmPreset({
76
+ // Embedding provider (required for text fields and RAG)
77
+ defaultEmbedder: {
78
+ provider: 'ollama',
79
+ model: 'nomic-embed-text',
80
+ baseUrl: 'http://localhost:11434',
81
+ },
82
+
83
+ // Chat completion provider (required for RAG)
84
+ defaultChatCompleter: {
85
+ provider: 'ollama',
86
+ model: 'llama3',
87
+ baseUrl: 'http://localhost:11434',
88
+ },
89
+
90
+ // Toggle individual capabilities
91
+ enableTextSearch: true, // text field on VectorNearbyInput
92
+ enableTextMutations: true, // *Text companion fields on mutations
93
+ enableRag: false, // ragQuery + embedText root fields
94
+
95
+ // RAG defaults (overridable per-query)
96
+ ragDefaults: {
97
+ contextLimit: 10,
98
+ maxTokens: 4000,
99
+ minSimilarity: 0.3,
100
+ },
101
+ })
102
+ ```
103
+
104
+ Providers can also be configured via environment variables (`EMBEDDER_PROVIDER`, `EMBEDDER_MODEL`, `EMBEDDER_BASE_URL`, `CHAT_PROVIDER`, `CHAT_MODEL`, `CHAT_BASE_URL`).
105
+
106
+ ## RAG queries
107
+
108
+ When `enableRag: true` and tables have `@hasChunks` smart tags, the plugin adds:
109
+
110
+ ```graphql
111
+ # Full RAG: embed prompt, search chunks, assemble context, call chat LLM
112
+ query {
113
+ ragQuery(
114
+ prompt: "What is machine learning?"
115
+ contextLimit: 5
116
+ minSimilarity: 0.3
117
+ ) {
118
+ answer
119
+ sources { content similarity tableName parentId }
120
+ tokensUsed
121
+ }
122
+ }
123
+
124
+ # Standalone embedding
125
+ query {
126
+ embedText(text: "machine learning concepts") {
127
+ vector
128
+ dimensions
129
+ }
130
+ }
131
+ ```
132
+
133
+ ## License
134
+
135
+ MIT
136
+
137
+ ---
138
+
139
+ ## Education and Tutorials
140
+
141
+ 1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
142
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
143
+
144
+ 2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
145
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
146
+
147
+ 3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
148
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
149
+
150
+ 4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
151
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
152
+
153
+ 5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
154
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
155
+
156
+ 6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
157
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
158
+
159
+ 7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
160
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
161
+
162
+ ## Related Constructive Tooling
163
+
164
+ ### 📦 Package Management
165
+
166
+ * [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
167
+
168
+ ### 🧪 Testing
169
+
170
+ * [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
171
+ * [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
172
+ * [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
173
+ * [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
174
+ * [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
175
+
176
+ ### 🧠 Parsing & AST
177
+
178
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
179
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
180
+ * [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
181
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
182
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
183
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
184
+
185
+ ## Credits
186
+
187
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
188
+
189
+ ## Disclaimer
190
+
191
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
192
+
193
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
@@ -0,0 +1 @@
1
+ export {};