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/chat.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Chat Completion — pluggable chat/completion provider for the Graphile LLM plugin
3
+ *
4
+ * Provides a provider-based architecture for LLM chat completions.
5
+ * Currently supports Ollama via @agentic-kit/ollama.
6
+ *
7
+ * Used by the RAG plugin to generate answers from retrieved context.
8
+ *
9
+ * Resolution order mirrors the embedder:
10
+ * 1. The `llm_module` api_modules configuration (per-database)
11
+ * 2. The preset's `defaultChatCompleter` option (fallback for dev/testing)
12
+ * 3. Environment variables (CHAT_PROVIDER, CHAT_MODEL, CHAT_BASE_URL)
13
+ */
14
+ import type { ChatConfig, ChatFunction, LlmModuleData } from './types';
15
+ /**
16
+ * Build a chat completion function from a config object.
17
+ *
18
+ * @returns A ChatFunction, or null if the provider is not recognized
19
+ */
20
+ export declare function buildChatCompleter(config: ChatConfig): ChatFunction | null;
21
+ /**
22
+ * Build a chat completer from an `llm_module` api_modules row.
23
+ *
24
+ * @param data - The llm_module data from services_public.api_modules
25
+ * @returns A ChatFunction, or null if the chat provider is not configured
26
+ */
27
+ export declare function buildChatCompleterFromModule(data: LlmModuleData): ChatFunction | null;
28
+ /**
29
+ * Resolve a chat completer from environment variables.
30
+ * This is a fallback for development when no llm_module or defaultChatCompleter is configured.
31
+ *
32
+ * Environment variables:
33
+ * CHAT_PROVIDER - Provider name ('ollama')
34
+ * CHAT_MODEL - Model identifier (e.g. 'llama3')
35
+ * CHAT_BASE_URL - Provider base URL
36
+ */
37
+ export declare function buildChatCompleterFromEnv(): ChatFunction | null;
package/chat.js ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ /**
3
+ * Chat Completion — pluggable chat/completion provider for the Graphile LLM plugin
4
+ *
5
+ * Provides a provider-based architecture for LLM chat completions.
6
+ * Currently supports Ollama via @agentic-kit/ollama.
7
+ *
8
+ * Used by the RAG plugin to generate answers from retrieved context.
9
+ *
10
+ * Resolution order mirrors the embedder:
11
+ * 1. The `llm_module` api_modules configuration (per-database)
12
+ * 2. The preset's `defaultChatCompleter` option (fallback for dev/testing)
13
+ * 3. Environment variables (CHAT_PROVIDER, CHAT_MODEL, CHAT_BASE_URL)
14
+ */
15
+ var __importDefault = (this && this.__importDefault) || function (mod) {
16
+ return (mod && mod.__esModule) ? mod : { "default": mod };
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.buildChatCompleter = buildChatCompleter;
20
+ exports.buildChatCompleterFromModule = buildChatCompleterFromModule;
21
+ exports.buildChatCompleterFromEnv = buildChatCompleterFromEnv;
22
+ const ollama_1 = __importDefault(require("@agentic-kit/ollama"));
23
+ // ─── Built-in Providers ─────────────────────────────────────────────────────
24
+ /**
25
+ * Create an Ollama-based chat completion function.
26
+ *
27
+ * Uses OllamaClient.generate() with a messages array, which internally
28
+ * routes to the /api/chat endpoint.
29
+ */
30
+ function createOllamaChatCompleter(baseUrl = 'http://localhost:11434', model = 'llama3') {
31
+ const client = new ollama_1.default(baseUrl);
32
+ return async (messages, options) => {
33
+ // Build the input for OllamaClient.generate() in chat mode
34
+ const input = {
35
+ model,
36
+ messages: messages.filter((m) => m.role !== 'system'),
37
+ };
38
+ // Extract system message if present
39
+ const systemMsg = messages.find((m) => m.role === 'system');
40
+ if (systemMsg) {
41
+ input.system = systemMsg.content;
42
+ }
43
+ if (options?.temperature !== undefined) {
44
+ input.temperature = options.temperature;
45
+ }
46
+ const startTime = Date.now();
47
+ const response = await client.generate(input);
48
+ const latencyMs = Date.now() - startTime;
49
+ // Token count logging (metering deferred to billing system)
50
+ console.log(`[graphile-llm] Chat completion: model=${model}, latency=${latencyMs}ms, ` +
51
+ `messages=${messages.length}`);
52
+ return response;
53
+ };
54
+ }
55
+ // ─── Chat Completer Construction ────────────────────────────────────────────
56
+ /**
57
+ * Build a chat completion function from a config object.
58
+ *
59
+ * @returns A ChatFunction, or null if the provider is not recognized
60
+ */
61
+ function buildChatCompleter(config) {
62
+ switch (config.provider) {
63
+ case 'ollama':
64
+ return createOllamaChatCompleter(config.baseUrl, config.model);
65
+ // Future: 'openai', 'anthropic', 'custom'
66
+ default:
67
+ return null;
68
+ }
69
+ }
70
+ // ─── Resolution from LLM Module ─────────────────────────────────────────────
71
+ /**
72
+ * Build a chat completer from an `llm_module` api_modules row.
73
+ *
74
+ * @param data - The llm_module data from services_public.api_modules
75
+ * @returns A ChatFunction, or null if the chat provider is not configured
76
+ */
77
+ function buildChatCompleterFromModule(data) {
78
+ if (!data.chat_provider)
79
+ return null;
80
+ return buildChatCompleter({
81
+ provider: data.chat_provider,
82
+ model: data.chat_model,
83
+ baseUrl: data.chat_base_url,
84
+ apiKey: data.api_key_ref,
85
+ });
86
+ }
87
+ /**
88
+ * Resolve a chat completer from environment variables.
89
+ * This is a fallback for development when no llm_module or defaultChatCompleter is configured.
90
+ *
91
+ * Environment variables:
92
+ * CHAT_PROVIDER - Provider name ('ollama')
93
+ * CHAT_MODEL - Model identifier (e.g. 'llama3')
94
+ * CHAT_BASE_URL - Provider base URL
95
+ */
96
+ function buildChatCompleterFromEnv() {
97
+ const provider = process.env.CHAT_PROVIDER;
98
+ if (!provider)
99
+ return null;
100
+ return buildChatCompleter({
101
+ provider,
102
+ model: process.env.CHAT_MODEL,
103
+ baseUrl: process.env.CHAT_BASE_URL,
104
+ });
105
+ }
package/embedder.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Embedder — pluggable text-to-vector embedding for the Graphile LLM plugin
3
+ *
4
+ * Provides a provider-based architecture for converting text into vector
5
+ * embeddings. Currently supports Ollama via @agentic-kit/ollama.
6
+ *
7
+ * The embedder is resolved at request time from:
8
+ * 1. The `llm_module` api_modules configuration (per-database)
9
+ * 2. The preset's `defaultEmbedder` option (fallback for dev/testing)
10
+ * 3. Environment variables (EMBEDDER_PROVIDER, EMBEDDER_MODEL, EMBEDDER_BASE_URL)
11
+ */
12
+ import type { EmbedderConfig, EmbedderFunction, LlmModuleData } from './types';
13
+ /**
14
+ * Build an embedder function from a config object.
15
+ *
16
+ * @returns An EmbedderFunction, or null if the provider is not recognized
17
+ */
18
+ export declare function buildEmbedder(config: EmbedderConfig): EmbedderFunction | null;
19
+ /**
20
+ * Build an embedder from an `llm_module` api_modules row.
21
+ *
22
+ * @param data - The llm_module data from services_public.api_modules
23
+ * @returns An EmbedderFunction, or null if the provider is not supported
24
+ */
25
+ export declare function buildEmbedderFromModule(data: LlmModuleData): EmbedderFunction | null;
26
+ /**
27
+ * Resolve an embedder from environment variables.
28
+ * This is a fallback for development when no llm_module or defaultEmbedder is configured.
29
+ *
30
+ * Environment variables:
31
+ * EMBEDDER_PROVIDER - Provider name ('ollama')
32
+ * EMBEDDER_MODEL - Model identifier
33
+ * EMBEDDER_BASE_URL - Provider base URL
34
+ */
35
+ export declare function buildEmbedderFromEnv(): EmbedderFunction | null;
package/embedder.js ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ /**
3
+ * Embedder — pluggable text-to-vector embedding for the Graphile LLM plugin
4
+ *
5
+ * Provides a provider-based architecture for converting text into vector
6
+ * embeddings. Currently supports Ollama via @agentic-kit/ollama.
7
+ *
8
+ * The embedder is resolved at request time from:
9
+ * 1. The `llm_module` api_modules configuration (per-database)
10
+ * 2. The preset's `defaultEmbedder` option (fallback for dev/testing)
11
+ * 3. Environment variables (EMBEDDER_PROVIDER, EMBEDDER_MODEL, EMBEDDER_BASE_URL)
12
+ */
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.buildEmbedder = buildEmbedder;
18
+ exports.buildEmbedderFromModule = buildEmbedderFromModule;
19
+ exports.buildEmbedderFromEnv = buildEmbedderFromEnv;
20
+ const ollama_1 = __importDefault(require("@agentic-kit/ollama"));
21
+ // ─── Built-in Providers ─────────────────────────────────────────────────────
22
+ /**
23
+ * Create an Ollama-based embedder function.
24
+ */
25
+ function createOllamaEmbedder(baseUrl = 'http://localhost:11434', model = 'nomic-embed-text') {
26
+ const client = new ollama_1.default(baseUrl);
27
+ return async (text) => {
28
+ return client.generateEmbedding(text, model);
29
+ };
30
+ }
31
+ // ─── Embedder Construction ──────────────────────────────────────────────────
32
+ /**
33
+ * Build an embedder function from a config object.
34
+ *
35
+ * @returns An EmbedderFunction, or null if the provider is not recognized
36
+ */
37
+ function buildEmbedder(config) {
38
+ switch (config.provider) {
39
+ case 'ollama':
40
+ return createOllamaEmbedder(config.baseUrl, config.model);
41
+ // Future: 'openai', 'anthropic', 'custom'
42
+ default:
43
+ return null;
44
+ }
45
+ }
46
+ // ─── Resolution from LLM Module ─────────────────────────────────────────────
47
+ /**
48
+ * Build an embedder from an `llm_module` api_modules row.
49
+ *
50
+ * @param data - The llm_module data from services_public.api_modules
51
+ * @returns An EmbedderFunction, or null if the provider is not supported
52
+ */
53
+ function buildEmbedderFromModule(data) {
54
+ return buildEmbedder({
55
+ provider: data.embedding_provider,
56
+ model: data.embedding_model,
57
+ baseUrl: data.embedding_base_url,
58
+ apiKey: data.api_key_ref,
59
+ });
60
+ }
61
+ /**
62
+ * Resolve an embedder from environment variables.
63
+ * This is a fallback for development when no llm_module or defaultEmbedder is configured.
64
+ *
65
+ * Environment variables:
66
+ * EMBEDDER_PROVIDER - Provider name ('ollama')
67
+ * EMBEDDER_MODEL - Model identifier
68
+ * EMBEDDER_BASE_URL - Provider base URL
69
+ */
70
+ function buildEmbedderFromEnv() {
71
+ const provider = process.env.EMBEDDER_PROVIDER;
72
+ if (!provider)
73
+ return null;
74
+ return buildEmbedder({
75
+ provider,
76
+ model: process.env.EMBEDDER_MODEL,
77
+ baseUrl: process.env.EMBEDDER_BASE_URL,
78
+ });
79
+ }
@@ -0,0 +1 @@
1
+ export {};