|
| 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 |
0 commit comments