Skip to content

Commit 8512319

Browse files
committed
extend AI docu with generationConfig info
1 parent 226e313 commit 8512319

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

docs/recipes/ai.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Lucee 7 includes full support for AI integration, allowing you to interact with
2626

2727
- **Multipart Content Support**: Send images, PDFs, and other documents along with text prompts
2828
- **Multipart Response Support**: Receive generated images and other content types from AI (currently Gemini, more providers coming soon)
29+
- **Structured JSON Responses**: Enforce specific JSON response schemas with Gemini beta API
2930
- **Simplified Function Names**: Functions are available without the `Lucee` prefix (though the prefix is still supported as an alias for backward compatibility)
3031
- **Stable API**: The AI functionality is no longer experimental and is ready for production use
3132
- **Enhanced File Handling**: Automatic detection and handling of file paths in multipart requests
@@ -321,6 +322,75 @@ if (isArray(response)) {
321322
- **ChatGPT (OpenAI)**: 🔄 Coming soon
322323
- **Ollama**: 🔄 Coming soon
323324

325+
### Structured JSON Responses (Gemini Beta)
326+
327+
**New in Lucee 7** - With Gemini's beta API, you can enforce structured JSON responses by defining a `generationConfig` with a response schema. This ensures the AI returns data in a specific, predictable format.
328+
329+
#### Configuration
330+
331+
Add `generationConfig` to your Gemini endpoint configuration to specify the desired response structure:
332+
333+
```json
334+
"recipes": {
335+
"class": "lucee.runtime.ai.google.GeminiEngine",
336+
"custom": {
337+
"connectTimeout": "2000",
338+
"beta": true,
339+
"message": "Keep all answers concise and accurate",
340+
"model": "gemini-2.5-flash",
341+
"apikey": "${GEMINI_API_KEY}",
342+
"socketTimeout": "120000",
343+
"temperature": "0.7",
344+
"conversationSizeLimit": "10",
345+
346+
"generationConfig": {
347+
"responseMimeType": "application/json",
348+
"responseSchema": {
349+
"type": "ARRAY",
350+
"items": {
351+
"type": "OBJECT",
352+
"properties": {
353+
"recipeName": { "type": "STRING" },
354+
"calories": { "type": "INTEGER" }
355+
},
356+
"required": ["recipeName", "calories"]
357+
}
358+
}
359+
}
360+
}
361+
}
362+
```
363+
364+
**Configuration Options:**
365+
- **`responseMimeType`**: Set to `"application/json"` to enforce JSON responses
366+
- **`responseSchema`**: Define the expected JSON structure using a schema
367+
- **`generationConfig`**: All other Gemini generation config options are supported and passed directly to the API
368+
369+
#### Usage Example
370+
371+
```javascript
372+
aiSessionJson = createAISession(
373+
name: "recipes",
374+
limit: 100,
375+
temperature: 0.2,
376+
systemMessage: "Keep your answers as short as possible"
377+
);
378+
379+
response = inquiryAISession(aiSessionJson, "List 3 healthy breakfast ideas.");
380+
dump(response);
381+
```
382+
383+
**Response:**
384+
```json
385+
[
386+
{"recipeName":"Oatmeal with berries","calories":300},
387+
{"recipeName":"Greek yogurt with fruit","calories":250},
388+
{"recipeName":"Scrambled eggs with spinach","calories":350}
389+
]
390+
```
391+
392+
**Note**: This feature requires Gemini's beta API (`"beta": true` or URL set to `https://generativelanguage.googleapis.com/v1beta/`).
393+
324394
### Session Serialization
325395

326396
Save and restore conversation state across requests. See [AI Session Serialization](https://github.com/lucee/lucee-docs/blob/master/docs/recipes/ai-serialisation.md) for details.
@@ -466,6 +536,7 @@ If you're upgrading from Lucee 6.2 with the experimental AI features:
466536
3. **New features**: You can now use:
467537
- Multipart content (images, PDFs) in your AI queries
468538
- Multipart responses (generated images and files from AI)
539+
- Structured JSON responses with Gemini beta API
469540
- Session serialization for persistent conversations
470541
- RAG with Lucene integration
471542

@@ -651,6 +722,37 @@ answer = inquiryAISession(aiSession, augmentedQuery);
651722
dump(answer);
652723
```
653724

725+
### Structured Data Extraction
726+
```javascript
727+
// Configure Gemini with JSON schema for product data
728+
aiSessionJson = createAISession(
729+
name: "geminijson",
730+
systemMessage: "Extract product information accurately"
731+
);
732+
733+
// Extract structured data from unstructured text
734+
productText = "The new MacBook Pro 16-inch costs $2499 and is currently in stock. Popular tags include: laptop, apple, professional.";
735+
736+
productData = inquiryAISession(aiSessionJson,
737+
"Extract the product information from this text: #productText#"
738+
);
739+
740+
// Response is guaranteed to match the schema defined in config
741+
dump(productData);
742+
// Returns: {"productName":"MacBook Pro 16-inch","price":2499,"inStock":true,"tags":["laptop","apple","professional"]}
743+
744+
// Direct database insertion is now easy
745+
queryExecute(
746+
"INSERT INTO products (name, price, in_stock, tags) VALUES (?, ?, ?, ?)",
747+
[
748+
productData.productName,
749+
productData.price,
750+
productData.inStock,
751+
serializeJSON(productData.tags)
752+
]
753+
);
754+
```
755+
654756
## Related Documentation
655757

656758
- **[AI Augmentation with Lucene](ai-augmentation.md)** - Implement RAG to enhance AI with your own data

0 commit comments

Comments
 (0)