|
| 1 | +# AI Completion Adapters |
| 2 | + |
| 3 | +Used for AI-powered text completion. |
| 4 | + |
| 5 | +[CompletionAdapter source class](https://github.com/devforth/adminforth/blob/917d897c866975a4aee29273377f2c07cb6ddf81/adminforth/types/adapters/CompletionAdapter.ts#L16) |
| 6 | + |
| 7 | +Feel free to fork and implement other models, including Anthropic, Google Gemini, or any other AI model that supports text completion. |
| 8 | + |
| 9 | +## Common `complete()` signature |
| 10 | + |
| 11 | +All completion adapters implement the same `complete()` method signature from the shared `CompletionAdapter` interface. |
| 12 | + |
| 13 | +```ts |
| 14 | +complete({ |
| 15 | + content: string, |
| 16 | + maxTokens: number, |
| 17 | + outputSchema?: any, |
| 18 | + reasoningEffort?: 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh', |
| 19 | + tools?: CompletionTool[], |
| 20 | + onChunk?: ( |
| 21 | + chunk: string, |
| 22 | + event?: { |
| 23 | + type: 'output' | 'reasoning'; |
| 24 | + delta: string; |
| 25 | + text: string; |
| 26 | + source?: 'summary' | 'text'; |
| 27 | + }, |
| 28 | + ) => void | Promise<void>, |
| 29 | +}) |
| 30 | +``` |
| 31 | + |
| 32 | +`reasoningEffort` is optional and defaults to `low` in the adapter implementation. |
| 33 | + |
| 34 | +## Using `json_schema` |
| 35 | + |
| 36 | +All completion adapters accept structured output through the shared `outputSchema` field in the request object. The example below uses the OpenAI adapter, but the same request shape is common for all completion adapters. |
| 37 | + |
| 38 | +```ts |
| 39 | +const adapter = new CompletionAdapterOpenAIResponses({ |
| 40 | + openAiApiKey: process.env.OPENAI_API_KEY as string, |
| 41 | + model: 'gpt-5-mini', |
| 42 | +}); |
| 43 | + |
| 44 | +const prompt = 'What is the capital of France? return json'; |
| 45 | + |
| 46 | +adapter.complete({ |
| 47 | + content: prompt, |
| 48 | + maxTokens: 200, |
| 49 | + outputSchema: { |
| 50 | + name: 'capital_response', |
| 51 | + schema: { |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + capital: { type: 'string' }, |
| 55 | + }, |
| 56 | + required: ['capital'], |
| 57 | + }, |
| 58 | + }, |
| 59 | +}).then((resp) => { |
| 60 | + console.log(resp); |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +## Using streaming output and reasoning events |
| 66 | + |
| 67 | +All completion adapters accept `onChunk` inside the request object. The example below uses the OpenAI adapter, but the callback signature is common for all completion adapters. |
| 68 | + |
| 69 | +Streaming reasoning events depend on the selected model and provider response. |
| 70 | + |
| 71 | +```ts |
| 72 | +const adapter = new CompletionAdapterOpenAIResponses({ |
| 73 | + openAiApiKey: process.env.OPENAI_API_KEY as string, |
| 74 | + model: 'gpt-5-mini', |
| 75 | +}); |
| 76 | + |
| 77 | +await adapter.complete({ |
| 78 | + content: 'Think step by step and write a short answer about how ABS brakes work', |
| 79 | + maxTokens: 300, |
| 80 | + reasoningEffort: 'medium', |
| 81 | + onChunk: async (chunk, event) => { |
| 82 | + if (!event) return; |
| 83 | + |
| 84 | + if (event.type === 'reasoning') { |
| 85 | + console.log('Reasoning chunk:', event.delta); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + console.log('Output chunk:', event.delta); |
| 90 | + }, |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +Then output will be like: |
| 95 | + |
| 96 | +```ts |
| 97 | +{ content: '{"capital":"Paris"}', finishReason: 'stop' } |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +## OpenAI Responses Completion Adapter |
| 102 | + |
| 103 | +```bash |
| 104 | +pnpm i @adminforth/completion-adapter-openai-responses |
| 105 | +``` |
| 106 | + |
| 107 | +Integrates AdminForth with OpenAI's Responses API to provide AI-powered completion and conversational features. |
| 108 | + |
| 109 | +The older `@adminforth/completion-adapter-open-ai-chat-gpt` package is deprecated. Use this package instead. |
| 110 | + |
| 111 | +```ts |
| 112 | +import CompletionAdapterOpenAIResponses from '@adminforth/completion-adapter-openai-responses'; |
| 113 | + |
| 114 | +new CompletionAdapterOpenAIResponses({ |
| 115 | + openAiApiKey: process.env.OPENAI_API_KEY as string, |
| 116 | + model: 'gpt-5.2', |
| 117 | + extraRequestBodyParameters: { |
| 118 | + temperature: 0.7, |
| 119 | + reasoning: { |
| 120 | + effort: 'medium', |
| 121 | + }, |
| 122 | + }, |
| 123 | +}), |
| 124 | +``` |
| 125 | + |
| 126 | +You can specify any GPT model you need. The default is `gpt-5-nano`. |
| 127 | + |
| 128 | +This adapter uses the OpenAI `responses` API. |
| 129 | + |
| 130 | +## Google Gemini Completion Adapter |
| 131 | + |
| 132 | +```bash |
| 133 | +pnpm i @adminforth/completion-adapter-google-gemini |
| 134 | +``` |
| 135 | + |
| 136 | +Integrates AdminForth with Google Gemini models to provide AI-powered completion and conversational features. |
| 137 | + |
| 138 | +```ts |
| 139 | +import CompletionAdapterGoogleGemini from '@adminforth/completion-adapter-google-gemini'; |
| 140 | + |
| 141 | +new CompletionAdapterGoogleGemini({ |
| 142 | + geminiApiKey: process.env.GEMINI_API_KEY as string, |
| 143 | + model: 'gemini-3-pro-preview', |
| 144 | + extraRequestBodyParameters: { |
| 145 | + temperature: 0.7, |
| 146 | + }, |
| 147 | +}), |
| 148 | +``` |
| 149 | + |
| 150 | +You can specify any Gemini model you need. The default is `gemini-3-flash-preview`. |
| 151 | + |
| 152 | +## Adding extra request body params |
| 153 | + |
| 154 | +There might be cases where you want to add extra body params to the request sent to the AI provider. For those cases you can use `extraRequestBodyParameters`: |
| 155 | + |
| 156 | +```ts |
| 157 | +import CompletionAdapterGoogleGemini from '@adminforth/completion-adapter-google-gemini'; |
| 158 | + |
| 159 | +new CompletionAdapterGoogleGemini({ |
| 160 | + geminiApiKey: process.env.GEMINI_API_KEY as string, |
| 161 | + model: 'gemini-3-pro-preview', |
| 162 | + extraRequestBodyParameters: { |
| 163 | + temperature: 0.7, |
| 164 | + responseMimeType: 'application/json', |
| 165 | + }, |
| 166 | +}), |
| 167 | +``` |
0 commit comments