AI Search Module User Manual

Comprehensive guide for installation, configuration, usage, analytics, and troubleshooting (Drupal 10+).

1. Introduction

The AI Search module integrates Drupal's Search API results with OpenAI-powered contextual responses and optional summaries. It enhances traditional keyword search by providing concise, AI-generated answers derived strictly from indexed site content.

The module is designed to be safe and deterministic: AI prompts instruct the model not to hallucinate or introduce information not found in the provided context.

2. Key Features

3. Requirements

ComponentMinimumNotes
Drupal Core10.0+Tested with Drupal 10.x.
PHP8.1+Consistent with Drupal 10 core requirements.
Search APILatest stableProvide an index; database backend acceptable.
Search API BackendDatabase OR Solr/ElasticDatabase backend sufficient for small & mid sites.
OpenAI AccountValid API keyGenerated at OpenAI.
HTTP ClientGuzzle (core)Used via Drupal's service container.
Tip: Start with the database backend. Scale to Solr/Elastic only if advanced relevance tuning or large volumes are required.

4. Installation

4.1 Download / Place Module

Place the module directory ai-search inside web/modules/custom.

4.2 Enable Module

drush en airagsearch -y

Or via the Drupal UI: Extend > Search > AI Search.

4.3 Create or Select Search API Index

  1. Navigate to /admin/config/search/search-api.
  2. Create a new index (e.g., Site Content).
  3. Select "Database" server or existing search server.
  4. Add fields: title, body, and optional taxonomy or metadata.
  5. Index the content (queue or immediate).

4.4 Rebuild Index (If Needed)

drush search-api:reindex <index_id>
# Example:
# drush search-api:reindex site_content_index

5. Configuration (Admin UI)

Path: /admin/config/search/airagsearch

The settings form is organized into logical sections (all collapsed by default):

5.1 Core Setup

SettingDescriptionNotes
OpenAI API KeyYour secret key (begins sk-).Mandatory. Missing key logs error & disables AI features.
Search API IndexIndex used for content retrieval.Must exist and be populated.
ChatGPT ModelSelect from GPT-4o, GPT-4 Turbo, GPT-3.5 etc.Recommended: gpt-4o-mini for balance.

5.2 Search Behavior

SettingDescriptionNotes
Results Per PagePagination size for search listing.Default: 10. Range: 1-100.

5.3 AI Context Settings

Important: These settings directly impact OpenAI token usage and costs.

SettingDescriptionNotes
AI Response Context CountNumber of search results used for AI responses.Default: 3. Range: 1-50. More results = better context but higher costs.
Summary Context CountNumber of results for AI summaries.Default: 20. Range: 5-50. Used on summary page.
Content Length Per ResultCharacter limit per search result sent to AI.Default: 1200 chars. Range: 200-5000. Each char ≈ 0.25 tokens.
Token Cost Warning: Increasing context count or length significantly raises OpenAI costs. Example: 10 results × 2000 chars = ~5000 tokens per query.

5.4 Analytics & Logging

SettingDescriptionNotes
Max Analytics RecordsRetention cap for search logs.Default: 1000. Range: 100-100000. Older records pruned automatically.

5.5 Advanced AI Parameters

SettingDescriptionNotes
TemperatureControls creativity (0–2).Lower = deterministic answers; default: 0.7.
Max Response TokensUpper bound for answer length.Default: 500 tokens (~2000 chars). Range: 50-4000.
Important: If either the API key or index is unset, AI enhancement and summaries will not run. Clear error logs appear under Reports > Recent log messages.

6. Performing Searches

6.1 Search Page

URL: /airagsearch. Users enter keywords; module stores the last query in session for summary generation.

6.2 Relevance

The controller attempts to sort by search_api_relevance if the field is available. Ensure your index back-end supports relevance scoring.

6.3 Context Documents

Top results (currently first 3 for answers) are converted into concise context objects (title, body snippet, URL) passed to OpenAI for answer synthesis.

7. AI Responses & Summaries

7.1 Answer Generation

The module constructs a prompt instructing the model to reply only using provided context. If insufficient, it returns a fallback message.

7.2 Context Building

Context sent to OpenAI is configurable via AI Context Settings:

Each result contributes: Title + URL + Content (up to character limit). Total tokens = (Results × Characters) ÷ 4 approximately.

7.3 Summary Page

URL: /airagsearch/summary (available after a successful search). Uses configurable number of results (default: 20) for comprehensive summaries.

7.4 Markdown Processing

AI output is converted to HTML via the module's MarkdownProcessor service.

7.5 Token & Cost Management

Cost Example: 10 results × 2000 chars = ~5000 input tokens. With GPT-4o-mini at $0.15/1M tokens = ~$0.0075 per query.
Tip: Log entries capture model usage (prompt/completion token counts) when available for auditing.

8. Analytics Dashboard

Path: /admin/reports/airagsearch-analytics. Displays logged queries, counts, and whether a query returned results.

8.1 Data Retention

Older records are pruned automatically once the configured cap is exceeded.

8.2 Privacy Considerations

Avoid logging personally identifiable information (PII) as part of queries. The module does not store user IDs by default for analytics.

9. Security & Privacy

Do not index unpublished or confidential node types if their content should never reach external services.

10. Performance Considerations

11. Troubleshooting

11.1 Missing API Key

Error log: Missing OpenAI API key: set it at /admin/config/search/airagsearch.... Add key under settings.

11.2 Missing Search API Index

Response: { error: 'Search API index is not configured...' }. Select index in settings and re-run search.

11.3 No Results Returned

Log entry indicates empty result; AI answer replaced by fallback message.

11.4 Invalid Key Format

Settings validation flags suspicious keys not starting with sk-.

11.5 Timeouts

OpenAI requests default to 30–40s timeout. Reduce response size (tokens) or check network if frequently exceeded.

11.6 Token Errors

Adjust Max Tokens downward; ensure model supports requested length.

11.7 Drush Cache Clear

drush cr

12. Uninstallation

  1. Disable module: drush pm:uninstall airagsearch -y
  2. Remove directory: rm -rf web/modules/custom/airagsearch
  3. Optionally delete related analytics tables (if created) and configuration in the database.
Uninstalling removes configuration entities; analytics data may persist if stored in custom tables—drop manually if required.

13. FAQ

Q: Can I use another AI provider?

A: Not in core module version; you can swap the OpenAIClient service with a custom implementation via service overrides.

Q: How do I change the number of context documents?

A: Modify the controller logic limiting $ai_context_docs (currently first 3 results).

Q: Does the module cache AI responses?

A: No built-in caching. Add a custom layer (e.g., key-value store keyed by normalized query string).

Q: Are user roles considered?

A: Access checks rely on Search API index entity visibility. Ensure indexed data respects permissions.

14. Change Log (Overview)